Example of Token Cross-Chain (JS)
EVM to ENDLESS
`First===>: Estimated gas`
//Gas Estimation Method
const getGasPrice = async (chain: ChainData) => {
const provider = getProvider(chain.rpc_url)
const feeData = await provider.getFeeData()
return feeData.gasPrice?.toString() || '0'
}
const contract = await getContract(tokenAddress, IBridgePoolAbi, toChain.rpc_url)
const maxFeePerGasWei = await getGasPrice(toChain) //Get gas through 1559
const arg1 = {
msg_header: _msgHead,
msg_body: `0x${msg_body}`,
}
const amount = await contract.getLpFeeAndFinalAmount(
combainChain(chain).toString(),
sourceToken,
BigInt(toChainNumber(bridgeData.amount, toToken.decimals)).toString(),
) //Get the payment amount by calling the contract
useTokenStore.getState().updateReceiveAmount([...amount][1])
const gaslimit = await contract.processMsg.estimateGas(arg1, [], {
from: '0x0000000000000000000000000000000000000001',
}) //Get gasLimit
const estimatedGasCost = BigNumber(maxFeePerGasWei)
.times(gaslimit.toString())
.div(10 ** toChain.token_decimal)
.toFixed()
`Second===>: Cross-chain`
//Cross-chain method
const bridgeTo = async () => {
const gasFee = useBridgeStore.getState().gasFee
let addr_bridge_token = toChain.addr_bridge_token
const chain_type = getChainType(toChain.id)
const chain_id = toChain.id.split('-')[1]
const chain = new Uint8Array(9)
chain.set(numberToU8(chain_type), 0)
chain.set(numberToU64(Number(chain_id)), 1)
const config = new EndlessConfig({
network: EndlessNetwork,
})
const endless = new Endless(config)
let feeAuto = BigInt(BigNumber(toChainNumber(gasFee, fromChain.token_decimal)).toFixed() || 0)
const functionArguments = [
AccountAddress.fromBs58String(fromToken.address),
chain,
stringToBytes32(addr_bridge_token),
stringToBytes32(receiveAddress),
BigInt(toChainNumber(bridgeData.amount, fromToken.decimals)),
feeAuto,
]
const transferData: EndlessSignAndSubmitTransactionInput = {
payload: {
function: `${getBytes32Address(fromChain.id, fromChain.addr_bridge_token)}::execute::bridge_proposal`,
functionArguments: functionArguments,
typeArguments: [],
},
}
const transactionRes = await useEndlessStore.getState()?.jssdk?.signAndSubmitTransaction(transferData)
useTokenStore.getState().updateProgress(1)
if (transactionRes?.status === UserResponseStatus.APPROVED) {
const result = await endless.waitForTransaction({ transactionHash: transactionRes.args.hash })
if (result.success) {
return {
txhash: transactionRes.args.hash,
bridgeData: {},
}
} else {
throw transactionRes
}
} else {
throw transactionRes
}
}2、ENDLESS to EVM
Last updated