@broxus/js-bridge-essentials
Version:
Bridge JavaScript Essentials library
278 lines (277 loc) • 13.8 kB
JavaScript
import { areAddressesEqual, DEFAULT_NATIVE_CURRENCY_DECIMALS, getFullContractState, resolveTvmAddress, toInt, } from '@broxus/js-core';
import { TokenRootAlienEvm, TokenRootAlienSolana } from '../../models';
import { alienProxyV9Contract, mergePoolV6Contract, mergeRouterContract } from '../../models/alien-proxy/contracts';
export class AlienProxyV9Utils {
static async deployTokenFee(provider, proxyAddress, params, args) {
return alienProxyV9Contract(provider, proxyAddress)
.methods.deployTokenFee({
_remainingGasTo: resolveTvmAddress(params.remainingGasTo),
_token: resolveTvmAddress(params.tokenAddress),
})
.sendDelayed({
amount: toInt(6.6, DEFAULT_NATIVE_CURRENCY_DECIMALS),
bounce: true,
from: resolveTvmAddress(params.remainingGasTo),
...args,
});
}
static async getExpectedTokenFeeAddress(connection, proxyAddress, tokenAddress, cachedState) {
const state = cachedState ?? await getFullContractState(connection, proxyAddress);
const result = await alienProxyV9Contract(connection, proxyAddress)
.methods.getExpectedTokenFeeAddress({
_token: resolveTvmAddress(tokenAddress),
answerId: 0,
})
.call({ cachedState: state, responsible: true });
return result.value0;
}
static async getTvmDefaultFee(connection, proxyAddress, cachedState) {
const state = cachedState ?? await getFullContractState(connection, proxyAddress);
return alienProxyV9Contract(connection, proxyAddress)
.methods.getTvmDefaultFee({ answerId: 0 })
.call({ cachedState: state, responsible: true });
}
static async getTvmFees(connection, proxyAddress, cachedState) {
const state = cachedState ?? await getFullContractState(connection, proxyAddress);
const result = await alienProxyV9Contract(connection, proxyAddress)
.methods.getTvmFees({ answerId: 0 })
.call({ cachedState: state, responsible: true });
return new Map(result.value0.flat(0).map(([addr, value]) => [addr.toString(), value]));
}
static async getTvmTokenFee(connection, proxyAddress, tokenAddress, cachedState) {
const state = cachedState ?? await getFullContractState(connection, proxyAddress);
return alienProxyV9Contract(connection, proxyAddress)
.methods.getTvmTokenFee({
_token: resolveTvmAddress(tokenAddress),
answerId: 0,
})
.call({ cachedState: state, responsible: true });
}
static async getConfiguration(connection, proxyAddress, cachedState) {
const state = cachedState ?? await getFullContractState(connection, proxyAddress);
return alienProxyV9Contract(connection, proxyAddress)
.methods.getConfiguration({ answerId: 0 })
.call({ cachedState: state, responsible: true });
}
static async getTvmEvmConfiguration(connection, proxyAddress, cachedState) {
const result = await AlienProxyV9Utils.getConfiguration(connection, proxyAddress, cachedState);
return result.value0.everscaleConfiguration;
}
static async getSolTvmConfiguration(connection, proxyAddress, cachedState) {
const result = await AlienProxyV9Utils.getConfiguration(connection, proxyAddress, cachedState);
return result.value1.solanaConfiguration;
}
static async getTvmSolConfiguration(connection, proxyAddress, cachedState) {
const result = await AlienProxyV9Utils.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 {DeriveEvmAlienTokenRootV9AbiParams} params
* @param {FullContractState} [cachedState] - optional. Full contract state of the alien proxy contract
*/
static async deriveEvmAlienTokenRoot(connection, proxyAddress, params, cachedState) {
const state = cachedState ?? await getFullContractState(connection, proxyAddress);
const result = await alienProxyV9Contract(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 TVM alien token address in TVM by the given proxy address and EVM token params
* @param {ProviderRpcClient} connection
* @param {Address | string} proxyAddress
* @param {DeriveTvmAlienTokenRootV9AbiParams} params
* @param {FullContractState} [cachedState] - optional. Full contract state of the alien proxy contract
*/
static async deriveTvmAlienTokenRoot(connection, proxyAddress, params, cachedState) {
const state = cachedState ?? await getFullContractState(connection, proxyAddress);
const result = await alienProxyV9Contract(connection, proxyAddress)
.methods.deriveTVMAlienTokenRoot({
answerId: 0,
chainId: params.chainId,
decimals: params.decimals,
name: params.name,
native_proxy_wallet: params.native_proxy_wallet,
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 {DeriveSolanaAlienTokenRootV9AbiParams} params
* @param {FullContractState} [cachedState] - optional. Full contract state of the alien proxy contract
*/
static async deriveSolanaAlienTokenRoot(connection, proxyAddress, params, cachedState) {
const state = cachedState ?? await getFullContractState(connection, proxyAddress);
const result = await alienProxyV9Contract(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] - optional. Full contract state of the alien proxy contract
*/
static async deriveMergeRouter(connection, proxyAddress, tokenAddress, cachedState) {
const state = cachedState ?? await getFullContractState(connection, proxyAddress);
const result = await alienProxyV9Contract(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] - optional. Full contract state of the merge router contract
*/
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 mergePoolV6Contract(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 AlienProxyV9Utils.deriveMergeRouter(connection, proxyAddress, tokenAddress, state);
const mergePoolAddress = await AlienProxyV9Utils.getMergePoolAddress(connection, mergeRouterAddress);
const mergedTokens = await AlienProxyV9Utils.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) {
return undefined;
}
}
static async getTvmTokenMergeDetails(connection, proxyAddress, tokenAddress, cachedState) {
try {
const state = cachedState ?? await getFullContractState(connection, proxyAddress);
const mergeRouterAddress = await AlienProxyV9Utils.deriveMergeRouter(connection, proxyAddress, tokenAddress, state);
const mergePoolAddress = await AlienProxyV9Utils.getMergePoolAddress(connection, mergeRouterAddress);
const mergedTokens = await AlienProxyV9Utils.getMergePoolTokens(connection, mergePoolAddress);
const merged = mergedTokens.tokens.find(([address, settings]) => areAddressesEqual(address, tokenAddress) && settings.enabled);
if (!merged) {
return undefined;
}
return merged
? {
canonicalAddress: mergedTokens.canon,
mergePoolAddress,
mergeRouterAddress,
tvmTokenAddress: resolveTvmAddress(tokenAddress),
}
: undefined;
}
catch (e) {
return undefined;
}
}
static async getSolTokenMergeDetails(connection, proxyAddress, tokenAddress, cachedState) {
try {
const state = cachedState ?? await getFullContractState(connection, proxyAddress);
const mergeRouterAddress = await AlienProxyV9Utils.deriveMergeRouter(connection, proxyAddress, tokenAddress, state);
const mergePoolAddress = await AlienProxyV9Utils.getMergePoolAddress(connection, mergeRouterAddress);
const mergedTokens = await AlienProxyV9Utils.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) {
return undefined;
}
}
static decodeEvent(connection, proxyAddress, args) {
return alienProxyV9Contract(connection, proxyAddress).decodeEvent(args);
}
static decodeTransaction(connection, proxyAddress, args) {
return alienProxyV9Contract(connection, proxyAddress).decodeTransaction(args);
}
static decodeTransactionEvents(connection, proxyAddress, transaction) {
return alienProxyV9Contract(connection, proxyAddress).decodeTransactionEvents({ transaction });
}
}