UNPKG

@broxus/js-bridge-essentials

Version:

Bridge JavaScript Essentials library

210 lines (209 loc) 10.5 kB
import { getFullContractState, resolveTvmAddress } from '@broxus/js-core'; import { debug } from '@broxus/js-utils'; import { alienProxyV8Contract, mergePoolV5Contract, mergeRouterContract } from '../../models/alien-proxy/contracts'; import { TokenRootAlienEvm } from '../../models/token-root-alien-evm'; import { TokenRootAlienSolana } from '../../models/token-root-alien-solana'; export class AlienProxyV8Utils { static async getConfiguration(connection, proxyAddress, cachedState) { const state = cachedState ?? await getFullContractState(connection, proxyAddress); return alienProxyV8Contract(connection, proxyAddress) .methods.getConfiguration({ answerId: 0 }) .call({ cachedState: state, responsible: true }); } /** * Returns Native configuration address that is used in transfers between TVM and EVM for TVM-based tokens. * @param {ProviderRpcClient} connection * @param {Address | string} proxyAddress * @param {FullContractState} [cachedState] - An optional cached state of the proxy contract to optimize the call. */ static async getTvmEvmConfiguration(connection, proxyAddress, cachedState) { const result = await AlienProxyV8Utils.getConfiguration(connection, proxyAddress, cachedState); return result.value0.everscaleConfiguration; } /** * Returns Alien configuration address that is used in transfers between Solana and TVM for Solana-based tokens. * @param {ProviderRpcClient} connection * @param {Address | string} proxyAddress * @param {FullContractState} [cachedState] - An optional cached state of the proxy contract to optimize the call. */ static async getSolTvmConfiguration(connection, proxyAddress, cachedState) { const result = await AlienProxyV8Utils.getConfiguration(connection, proxyAddress, cachedState); return result.value1.solanaConfiguration; } /** * Returns Native configuration address that is used in transfers between TVM and Solana for TVM-based tokens. * @param {ProviderRpcClient} connection * @param {Address | string} proxyAddress * @param {FullContractState} [cachedState] - An optional cached state of the proxy contract to optimize the call. */ static async getTvmSolConfiguration(connection, proxyAddress, cachedState) { const result = await AlienProxyV8Utils.getConfiguration(connection, proxyAddress, cachedState); return result.value1.everscaleConfiguration; } /** * Derive EVM alien token address in TVM by the given proxy address and EVM token params * @param {ProviderRpcClient} connection * @param {Address | string} proxyAddress * @param {DeriveEvmAlienTokenRootV8AbiParams} params * @param {FullContractState} [cachedState] - An optional cached state of the proxy contract to optimize the call. */ static async deriveEvmAlienTokenRoot(connection, proxyAddress, params, cachedState) { const state = cachedState ?? await getFullContractState(connection, proxyAddress); const result = await alienProxyV8Contract(connection, proxyAddress) .methods.deriveEVMAlienTokenRoot({ answerId: 0, chainId: params.chainId, decimals: params.decimals, name: params.name, symbol: params.symbol, token: params.token, }) .call({ cachedState: state, responsible: true }); return result.value0; } /** * Derive Solana alien token address in TVM by the given proxy address and Solana token params * @param {ProviderRpcClient} connection * @param {Address | string} proxyAddress * @param {DeriveSolanaAlienTokenRootV8AbiParams} params * @param {FullContractState} [cachedState] - An optional cached state of the proxy contract to optimize the call. */ static async deriveSolanaAlienTokenRoot(connection, proxyAddress, params, cachedState) { const state = cachedState ?? await getFullContractState(connection, proxyAddress); const result = await alienProxyV8Contract(connection, proxyAddress) .methods.deriveSolanaAlienTokenRoot({ answerId: 0, decimals: params.decimals, name: params.name, symbol: params.symbol, token: params.token, }) .call({ cachedState: state, responsible: true }); return result.value0; } /** * Get derive merge router address by the given TVM-like token and proxy addresses * @param {ProviderRpcClient} connection * @param {Address | string} proxyAddress * @param {Address | string} tokenAddress * @param {FullContractState} [cachedState] - An optional cached state of the proxy contract to optimize the call. */ static async deriveMergeRouter(connection, proxyAddress, tokenAddress, cachedState) { const state = cachedState ?? await getFullContractState(connection, proxyAddress); const result = await alienProxyV8Contract(connection, proxyAddress) .methods.deriveMergeRouter({ answerId: 0, token: resolveTvmAddress(tokenAddress), }) .call({ cachedState: state, responsible: true }); return result.router; } /** * Get merge pool address by the given merge router address * @param {ProviderRpcClient} connection * @param {Address | string} mergeRouterAddress * @param {FullContractState} [cachedState] - An optional cached state of the proxy contract to optimize the call. */ static async getMergePoolAddress(connection, mergeRouterAddress, cachedState) { const state = cachedState ?? await getFullContractState(connection, mergeRouterAddress); const result = await mergeRouterContract(connection, mergeRouterAddress) .methods.getPool({ answerId: 0 }) .call({ cachedState: state, responsible: true }); return result.value0; } static async getMergePoolTokens(connection, mergePoolAddress, cachedState) { const state = cachedState ?? await getFullContractState(connection, mergePoolAddress); const result = await mergePoolV5Contract(connection, mergePoolAddress) .methods.getTokens({ answerId: 0 }) .call({ cachedState: state, responsible: true }); return { canon: result._canon, tokens: result._tokens.map(([addr, settings]) => [addr, settings]), }; } static async getEvmTokenMergeDetails(connection, proxyAddress, tokenAddress, chainId, cachedState) { try { const state = cachedState ?? await getFullContractState(connection, proxyAddress); const mergeRouterAddress = await AlienProxyV8Utils.deriveMergeRouter(connection, proxyAddress, tokenAddress, state); const mergePoolAddress = await AlienProxyV8Utils.getMergePoolAddress(connection, mergeRouterAddress); const mergedTokens = await AlienProxyV8Utils.getMergePoolTokens(connection, mergePoolAddress); const enabled = mergedTokens.tokens.filter(([, settings]) => settings.enabled); const promises = enabled.map(async ([address]) => { try { const meta = await TokenRootAlienEvm.Utils.meta(connection, address); return { baseChainId: meta.baseChainId, evmTokenAddress: meta.evmTokenAddress, isMerged: meta.baseChainId.toString() === chainId, }; } catch (e) { return { isMerged: false }; } }); const results = await Promise.all(promises); const merged = results.find(e => e.isMerged); if (!merged?.baseChainId) { return undefined; } return { baseChainId: merged.baseChainId, canonicalAddress: mergedTokens.canon, evmTokenAddress: merged.evmTokenAddress, mergePoolAddress, mergeRouterAddress, tvmTokenAddress: resolveTvmAddress(tokenAddress), }; } catch (e) { debug(e); return undefined; } } static async getSolTokenMergeDetails(connection, proxyAddress, tokenAddress, cachedState) { try { const state = cachedState ?? await getFullContractState(connection, proxyAddress); const mergeRouterAddress = await AlienProxyV8Utils.deriveMergeRouter(connection, proxyAddress, tokenAddress, state); const mergePoolAddress = await AlienProxyV8Utils.getMergePoolAddress(connection, mergeRouterAddress); const mergedTokens = await AlienProxyV8Utils.getMergePoolTokens(connection, mergePoolAddress); const enabled = mergedTokens.tokens.filter(([, settings]) => settings.enabled); const promises = enabled.map(async ([address]) => { try { const meta = await TokenRootAlienSolana.Utils.meta(connection, address); return { isMerged: true, solTokenAddress: meta.solTokenAddress, }; } catch (e) { return { isMerged: false }; } }); const results = await Promise.all(promises); const merged = results.find(e => e.isMerged); if (!merged) { return undefined; } return { canonicalAddress: mergedTokens.canon, mergePoolAddress, mergeRouterAddress, solTokenAddress: merged.solTokenAddress, tvmTokenAddress: resolveTvmAddress(tokenAddress), }; } catch (e) { debug(e); return undefined; } } static decodeEvent(connection, proxyAddress, args) { return alienProxyV8Contract(connection, proxyAddress).decodeEvent(args); } static decodeTransaction(connection, proxyAddress, args) { return alienProxyV8Contract(connection, proxyAddress).decodeTransaction(args); } static decodeTransactionEvents(connection, proxyAddress, transaction) { return alienProxyV8Contract(connection, proxyAddress).decodeTransactionEvents({ transaction }); } }