在本指南中,我们将通过欧易 DEX 提供的一个示例来展示如何进行跨链兑换,使用 Ethereum 链上的 USDT 兑换 Arbitrum 链上的 USDC。这个过程包括:
导入必要的 Node.js 库并设置你的环境变量以及定义辅助函数和组装参数 Node.js 环境设置
Demo 为 JavaScript 语言
连接到以太坊节点
:您需要确保您已经连接到一个可用的以太坊节点。您可以使用 web3.js 或其他以太坊开发库来连接到节点。在代码中,您需要指定节点的 HTTP 或 WebSocket 端点。获取代币合约实例
:使用代币的合约地址和 ABI,您需要创建一个代币合约的实例。您可以使用 web3.js 的 web3.eth.Contract 方法来实现这一点,将合约地址和 ABI 作为参数传递给合约实例。查询授权额度
:通过调用合约实例的 allowance 函数来查询授权额度。该函数需要两个参数:拥有者的地址和被授权者的地址。您可以通过在调用时提供这两个地址来查询授权额度。const { Web3 } = require('web3');
// Connect to an Ethereum node
const web3 = new Web3('https://xxxxx');
// token address and ABI
const tokenAddress = '0xxxxxxxxx';
// user address
const ownerAddress = '0xxxxxxxx';
// ETH dex token approval address
const spenderAddress = '0x40aa958dd87fc8305b97f2ba922cddca374bcd7f';
const tokenABI = [
{
"constant": true,
"inputs": [
{
"name": "_owner",
"type": "address"
},
{
"name": "_spender",
"type": "address"
}
],
"name": "allowance",
"outputs": [
{
"name": "",
"type": "uint256"
}
],
"payable": false,
"stateMutability": "view",
"type": "function"
}
];
// Create token contract instance
const tokenContract = new web3.eth.Contract(tokenABI, tokenAddress);
// Query token approve allowance function
async function getAllowance(ownerAddress, spenderAddress) {
try {
const allowance = await tokenContract.methods.allowance(ownerAddress, spenderAddress).call();
console.log(`Allowance for ${ownerAddress} to ${spenderAddress}: ${allowance}`);
} catch (error) {
console.error('Failed to query allowance:', error);
}
}
getAllowance(ownerAddress, spenderAddress).then(r => console.log(r));
const { data: allowanceData } = await getAllowanceData();
const allowanceAmount = allowanceData?.[0]?.allowanceAmount;
接下来,定义你要执行授权交易的参数。
const getApproveTransactionParams = {
chainId: fromChainId,
tokenContractAddress: fromTokenAddress,
userWalletAddress,
approveAmount: fromTokenAmount,
};
定义一个辅助函数,用于与 DEX API 进行交互。
const approveTransaction = async () => {
const { apiRequestUrl, path } = getAggregatorRequestUrl(
'/approve-transaction',
getApproveTransactionParams
);
return fetch(apiRequestUrl, {
method: 'get',
headers: headersParams,
})
.then((res) => res.json())
.then((res) => {
return res;
});
};
if (parseFloat(allowanceAmount) < parseFloat(fromTokenAmount)) {
const { data } = await approveTransaction(allowanceAmount);
let allowanceParams = {
...{ data: data[0].data }, // You can modify the data content you want according to the web3 official website
};
const { rawTransaction } = await web3.eth.accounts.signTransaction(
allowanceParams,
privateKey
);
await web3.eth.sendSignedTransaction(rawTransaction);
}
接下来,定义参数,并通过 fromChainId 拿到对应的 toChainId 列表。
const toChainListParams = {
chainId: fromChainId,
};
定义一个辅助函数,用于与 DEX API 进行交互。
const getSupportedChain = async () => {
const { apiRequestUrl, path } = getCrossChainBaseUrl(
'/supported/chain',
toChainListParams
);
return fetch(apiRequestUrl, {
method: 'get',
headers: headersParams,
})
.then((res) => res.json())
.then((res) => {
return res;
});
};
const { data: supportedChainList } = await getSupportedChain();
const selectChainItem = supportedChainList.find((item) => {
return item.chainName === 'Arbitrum';
});
toChainId = selectChainItem?.chainId;
接下来,定义参数,并通过 toChainId 拿到可交易的币种列表。
const toChainTokenListParams = {
chainId: toChainId,
};
定义一个辅助函数,用于与 DEX API 进行交互。
const getToChainTokenList = async () => {
const { apiRequestUrl, path } = getAggregatorRequestUrl(
'/all-tokens'',
toChainTokenListParams
);
return fetch(apiRequestUrl, {
method: 'get',
headers: headersParams,
})
.then((res) => res.json())
.then((res) => {
return res;
});
};
const { data: toChainTokenList } = await getToChainTokenList();
const selectToChainToken = toChainTokenList.find((item) => {
return item.tokenSymbol === 'USDC';
});
toTokenAddress = selectToChainToken?.tokenContractAddress;
接下来,定义参数,用来拿到询价的基础信息和路径列表信息。
const quoteParams = {
fromChainId,
toChainId,
fromTokenAddress,
toTokenAddress,
amount: fromTokenAmount,
slippage,
};
定义一个辅助函数,用于与 DEX API 进行交互。
const getQuote = async () => {
const { apiRequestUrl, path } = getCrossChainBaseUrl('/quote', quoteParams);
return fetch(apiRequestUrl, {
method: 'get',
headers: headersParams,
})
.then((res) => res.json())
.then((res) => {
return res;
});
};
const { data: quoteData } = await getQuote();
bridgeId = quoteData[0]?.routerList[0]?.router?.bridgeId;
接下来,定义参数,并获取跨链兑换的 tx 信息。
const swapParams = {
fromChainId: fromChainId,
toChainId: toChainId,
fromTokenAddress,
toTokenAddress,
amount: fromTokenAmount,
slippage,
userWalletAddress,
bridgeId,
};
定义一个辅助函数,用于与 DEX API 进行交互。
const getSwapData = async () => {
const { apiRequestUrl, path } = getCrossChainBaseUrl('/build-tx', swapParams);
return fetch(apiRequestUrl, {
method: 'get',
headers: headersParams,
})
.then((res) => res.json())
.then((res) => {
return res;
});
};
const { data: swapData } = await getSwapData();
const swapDataTxInfo = swapData[0].tx;
const nonce = await web3.eth.getTransactionCount(userWalletAddress, 'latest');
// You can obtain the latest nonce and process the hexadecimal numbers starting with 0x according to your needs
let signTransactionParams = {
data: swapDataTxInfo.data,
gasPrice: swapDataTxInfo.gasPrice,
to: swapDataTxInfo.to,
value: swapDataTxInfo.value,
nonce,
};
const { rawTransaction } = await web3.eth.accounts.signTransaction(
signTransactionParams,
privateKey
);
const chainTxInfo = await web3.eth.sendSignedTransaction(rawTransaction);
transactionTx = chainTxInfo;
接下来,定义参数,主要为源链 Hash 地址信息。
const getCheckStatusParams = {
hash: transactionTx,
};
定义一个辅助函数,用于与 DEX API 进行交互。
const checkTransactionStatus = async () => {
const { apiRequestUrl, path } = getCrossChainBaseUrl(
'/status',
getCheckStatusParams
);
return fetch(apiRequestUrl, {
method: 'get',
headers: headersParams,
})
.then((res) => res.json())
.then((res) => {
return res;
});
};
const { data: statusInfo } = await checkTransactionStatus();
console.log(statusInfo?.data[0]?.detailStatus);