In this guide, we will provide an example token swap through OKX DEX, using ETH from the Ethereum chain to purchase USDT. This process includes:
Import the necessary Node.js libraries and set your environment variables as well as define helper functions and assembly parameters Node.js Environment Settings.
Next, define the parameters for the swap you want to perform.
const swapParams = {
chainId,
fromTokenAddress,
toTokenAddress,
amount: '1000000000000000',
slippage: '0.03',
userWalletAddress,
referrerAddress: '', // Optional
feePercent: '', // Optional
};
Define helper functions that will be used to interact with the DEX swap API.
const getSwapData = async () => {
const apiRequestUrl = getRequestUrl(apiBaseUrl, '/swap', swapParams);
return fetch(apiRequestUrl, {
method: 'get',
headers: headersParams,
})
.then((res) => res.json())
.then((res) => {
return res;
});
};
Transaction parameters are assembled to initiate transactions.
const { data } = await getSwapData();
const transaction = data[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: transaction.data,
gasPrice: transaction.gasPrice,
gas: transaction.gas,
to: transaction.to,
value: transaction.value,
nonce,
};
Obtain the specific quantity. If allowanceAmount !== '0'
, please check step 4. If allowanceAmount === '0'
, please check step 5.
const { data: allowanceData } = await getAllowanceData();
const allowanceAmount = allowanceData?.[0]?.allowanceAmount;
if (allowanceAmount !== '0') {
const { rawTransaction } = await web3.eth.accounts.signTransaction(
signTransactionParams,
privateKey
);
const chainTxInfo = await web3.eth.sendSignedTransaction(rawTransaction);
console.log('chainTxInfo', chainTxInfo);
}
allowanceAmount === '0'
, it indicates that approval has not been granted,
and approval operations need to be performed at this time.Next, define the parameters for the transaction approval you want to perform.
const getApproveTransactionParams = {
chainId,
tokenContractAddress: fromTokenAddress,
userWalletAddress,
approveAmount: '...',
};
Define helper functions that will be used to interact with the DEX transaction approval API.
const approveTransaction = async () => {
const apiRequestUrl = getRequestUrl(
apiBaseUrl,
'/approve-transaction',
getApproveTransactionParams
);
return fetch(apiRequestUrl, {
method: 'get',
headers: headersParams,
})
.then((res) => res.json())
.then((res) => {
return res;
});
};
Get tx information after obtaining approval.
const { data } = await approveTransaction();
Update tx to approved tx.
signTransactionParams = {
...signTransactionParams,
...{ data: data[0].data },
};
Obtain signature tx information and send transaction.
if (allowanceAmount === '0') {
const { rawTransaction } = await web3.eth.accounts.signTransaction(
signTransactionParams,
privateKey
);
const chainTxInfo = await web3.eth.sendSignedTransaction(rawTransaction);
console.log('chainTxInfo', chainTxInfo);
}