@catalabs/catalyst-sdk
Version:
Catalyst AMM SDK
215 lines • 9.22 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.EvmVaultsModule = void 0;
const ethers_1 = require("ethers");
const contracts_1 = require("../contracts");
const router_1 = require("../router");
const utils_1 = require("../utils");
class EvmVaultsModule {
sdk;
constructor(sdk) {
this.sdk = sdk;
}
async deployVault({ vaultType, assets, initialBalances, weights, amplification, vaultFee, name, symbol, chainInterface, }) {
if (!this.sdk.signer) {
throw new Error('createVault requires a signer, try calling connectSigner first');
}
const registryAddress = this.sdk.registryModule.registryAddress();
const [vaultFactory, vaultTemplate] = await Promise.all([
this.sdk.registryModule.getCatalystVaultFactory(registryAddress),
this.sdk.registryModule.getCatalystVaultTemplate(registryAddress, vaultType),
]);
const catalystVaultFactory = contracts_1.VaultFactory__factory.connect(vaultFactory, this.sdk.signer);
const tx = await catalystVaultFactory.deployVault(vaultTemplate, assets, initialBalances, weights, amplification, vaultFee, name, symbol, chainInterface);
async function resolveVaultAddress() {
const result = await tx.wait();
const deployedVaultFilter = catalystVaultFactory.filters.VaultDeployed();
const deployedVaultFilterTopic = await deployedVaultFilter.getTopicFilter();
if (result) {
for (const log of result.logs) {
try {
const description = catalystVaultFactory.interface.parseLog(log);
if (description) {
const { topic, args } = description;
if (deployedVaultFilterTopic.some((item) => item === topic)) {
return args.vaultAddress;
}
}
}
catch { }
}
}
throw new Error(`Unable to resolve vault address from hash: ${tx.hash}`);
}
return {
hash: tx.hash,
wait: resolveVaultAddress,
};
}
async setConnection(vault, toVault, channelId, isActive) {
if (!this.sdk.signer) {
throw new Error('setConnection requires a signer, try calling connectSigner first');
}
const vaultContract = contracts_1.Vault__factory.connect(vault, this.sdk.signer);
const toVaultFormatted = router_1.RouterArguments.addressEncodeHelper(toVault);
const tx = await vaultContract.setConnection(channelId, toVaultFormatted, isActive);
return {
hash: tx.hash,
wait: async () => {
await tx.wait();
},
};
}
async finishSetup(vault) {
if (!this.sdk.signer) {
throw new Error('setConnection requires a signer, try calling connectSigner first');
}
const vaultContract = contracts_1.Vault__factory.connect(vault, this.sdk.signer);
const tx = await vaultContract.finishSetup();
return {
hash: tx.hash,
wait: async () => {
await tx.wait();
},
};
}
async sendAssetWithRouter(instructions, gasAmount, options) {
if (!this.sdk.signer) {
throw new Error('sendAssetWithRouter requires a signer, try calling connectSigner first');
}
const contract = contracts_1.Router__factory.connect(this.sdk.addresses.router, this.sdk.signer);
let gasLimit = options?.gasLimit;
if (!gasLimit) {
const estimation = await contract['execute(bytes,bytes[])'].estimateGas(instructions.commands, instructions.inputs, {
value: gasAmount,
});
gasLimit = estimation > utils_1.MIN_GAS_LIMIT ? estimation : utils_1.MIN_GAS_LIMIT;
}
const tx = await contract['execute(bytes,bytes[])'](instructions.commands, instructions.inputs, {
value: gasAmount,
gasLimit,
gasPrice: options?.gasPrice,
maxFeePerGas: options?.maxFeePerGas,
maxPriorityFeePerGas: options?.maxPriorityFeePerGas,
});
return {
hash: tx.hash,
wait: async () => {
await tx.wait();
},
};
}
async estimateGasForExecuteInstructions(instructions, gasAmount, options) {
if (!this.sdk.signer) {
throw new Error('estimateGasForExecuteInstructions requires a signer, try calling connectSigner first');
}
const contract = contracts_1.Router__factory.connect(this.sdk.addresses.router, this.sdk.signer);
let gasLimit = options?.gasLimit;
if (!gasLimit) {
try {
const estimation = await contract['execute(bytes,bytes[])'].estimateGas(instructions.commands, instructions.inputs, {
value: gasAmount,
});
gasLimit = estimation > utils_1.MIN_GAS_LIMIT ? estimation : utils_1.MIN_GAS_LIMIT;
}
catch (error) {
throw new Error('Gas estimation failed. Source chain is experiencing high traffic.');
}
}
return gasLimit;
}
async sendAssetWithRouterFromQuote(quote) {
const isNativeTransaction = quote.fromAsset === utils_1.GAS_TOKEN_IDENTIFIER;
return this.sendAssetWithRouter(quote.executionInstructions, isNativeTransaction ? BigInt(quote.amount) : BigInt('0'));
}
async sendAsset({ vault, channelId, toVault, toAccount, fromAsset, toAssetIndex, amount, minOut, fallbackUser, callData, }) {
if (!this.sdk.signer) {
throw new Error('sendAsset requires a signer, try calling connectSigner first');
}
const contract = contracts_1.Vault__factory.connect((0, utils_1.bytes32ToAddress)(vault), this.sdk.signer);
const tx = await contract.sendAsset({
chainIdentifier: channelId,
toVault,
toAccount,
incentive: {
maxGasDelivery: '0',
maxGasAck: '0',
refundGasTo: ethers_1.ethers.ZeroAddress,
priceOfDeliveryGas: '0',
priceOfAckGas: '0',
targetDelta: '0',
},
}, fromAsset, toAssetIndex, amount, minOut, fallbackUser, 0n, callData ?? '');
return {
hash: tx.hash,
wait: async () => {
await tx.wait();
},
};
}
async sendAssetFromQuote({ vault, fromAsset, amount, toAssetIndex, toVault, minOut, channelId, }, toAccount, fallbackUser, callData) {
return this.sendAsset({
vault,
toAccount: (0, utils_1.addressToBytes32)(toAccount),
fromAsset,
amount: BigInt(amount),
fallbackUser,
toAssetIndex,
toVault,
minOut: BigInt(minOut),
channelId,
callData,
});
}
async calcSendAsset(vault, from, amount) {
return contracts_1.Vault__factory.connect(vault, this.sdk.provider).calcSendAsset(from, amount);
}
async calcReceiveAsset(vault, to, units) {
return contracts_1.Vault__factory.connect(vault, this.sdk.provider).calcReceiveAsset(to, units);
}
async depositMixed(vault, tokenAmounts, minOut) {
if (!this.sdk.signer) {
throw new Error('depositMixed requires a signer, try calling connectSigner first');
}
const contract = contracts_1.Vault__factory.connect(vault, this.sdk.signer);
const tx = await contract.depositMixed(tokenAmounts, minOut);
return {
hash: tx.hash,
wait: async () => {
await tx.wait();
},
};
}
async withdrawAll(vault, poolTokens, minOut) {
if (!this.sdk.signer) {
throw new Error('withdrawAll requires a signer, try calling connectSigner first');
}
const contract = contracts_1.Vault__factory.connect(vault, this.sdk.signer);
const tx = await contract.withdrawAll(poolTokens, minOut);
return {
hash: tx.hash,
wait: async () => {
await tx.wait();
},
};
}
async withdrawMixed(vault, poolTokens, withdrawRatio, minOut) {
if (!this.sdk.signer) {
throw new Error('withdrawMixed requires a signer, try calling connectSigner first');
}
const contract = contracts_1.Vault__factory.connect(vault, this.sdk.signer);
const tx = await contract.withdrawMixed(poolTokens, withdrawRatio, minOut);
return {
hash: tx.hash,
wait: async () => {
await tx.wait();
},
};
}
async getUnitCapacity(vault) {
const contract = contracts_1.Vault__factory.connect(vault, this.sdk.provider);
return contract.getUnitCapacity();
}
}
exports.EvmVaultsModule = EvmVaultsModule;
//# sourceMappingURL=evm-vaults.module.js.map