UNPKG

@catalabs/catalyst-sdk

Version:
235 lines 9.96 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.EvmVaultsModuleV5 = void 0; const ethers_1 = require("ethers"); const ethers_v5_1 = require("ethers-v5"); const contracts_v5_1 = require("../contracts-v5"); const router_1 = require("../router"); const utils_1 = require("../utils"); class EvmVaultsModuleV5 { 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_v5_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 = deployedVaultFilter .topics?.[0]; if (result) { for (const log of result.logs) { try { const description = catalystVaultFactory.interface.parseLog(log); if (description) { const { topic, args } = description; if (topic === deployedVaultFilterTopic) { 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_v5_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_v5_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_v5_1.Router__factory.connect(this.sdk.addresses.router, this.sdk.signer); let gasLimit = options?.gasLimit ? ethers_v5_1.BigNumber.from(options.gasLimit) : undefined; if (!gasLimit) { const estimation = await contract.estimateGas['execute(bytes,bytes[])'](instructions.commands, instructions.inputs, { value: gasAmount, }); const defaultEstimation = ethers_v5_1.BigNumber.from(600_000); if (estimation.gt(defaultEstimation)) { gasLimit = estimation; } else { gasLimit = defaultEstimation; } } 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_v5_1.Router__factory.connect(this.sdk.addresses.router, this.sdk.signer); let gasLimit = options?.gasLimit ? ethers_v5_1.BigNumber.from(options.gasLimit) : undefined; if (!gasLimit) { try { const estimation = await contract.estimateGas['execute(bytes,bytes[])'](instructions.commands, instructions.inputs, { value: gasAmount, }); const defaultEstimation = ethers_v5_1.BigNumber.from(600_000); if (estimation.gt(defaultEstimation)) { gasLimit = estimation; } else { gasLimit = defaultEstimation; } } catch (error) { throw new Error('Gas estimation failed. Source chain is experiencing high traffic.'); } } return gasLimit.toBigInt(); } 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_v5_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) { const result = await contracts_v5_1.Vault__factory.connect(vault, this.sdk.provider).calcSendAsset(from, amount); return BigInt(result.toString()); } async calcReceiveAsset(vault, to, units) { const result = await contracts_v5_1.Vault__factory.connect(vault, this.sdk.provider).calcReceiveAsset(to, units); return BigInt(result.toString()); } async depositMixed(vault, tokenAmounts, minOut) { if (!this.sdk.signer) { throw new Error('depositMixed requires a signer, try calling connectSigner first'); } const contract = contracts_v5_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_v5_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_v5_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 result = await contracts_v5_1.Vault__factory.connect(vault, this.sdk.provider).getUnitCapacity(); return BigInt(result.toString()); } } exports.EvmVaultsModuleV5 = EvmVaultsModuleV5; //# sourceMappingURL=evm-vaults.module-v5.js.map