本文へスキップ
このページは英語からの機械翻訳を使用しており、誤りや不明瞭な表現が含まれている可能性があります。最も正確な情報については、オリジナルの英語版をご覧ください。頻繁な更新のため、一部のコンテンツはオリジナルの英語になっている可能性があります。Crowdinでの取り組みに参加して、このページの翻訳改善にご協力ください。 (Crowdin translation page, Contributing guide)

Gasless

Import the ethers and @kaiachain/ethers-ext/v6 packages to add gasless features on ethers.js

The ERC20 token address (TEST token on Kairos testnet).

Declare senderAddr: The wallet address of the sender performing the swap and senderPriv: The sender’s private key for signing transactions.

Creating a JSON-RPC provider and an instance of Wallet with the Kaia Kairos testnet

A minimal ABI for the ERC20 token, including functions to query decimals, symbol, allowance, and balance.

A fixed amount of 0.01 KAIA (in wei) that the sender wants to receive from the swap to fund a subsequent application transaction.

Show token at address:

Creates an instance of an ERC20 token contract to interact with its functions.

Queries the token’s symbol (e.g., “TEST” for the TEST token).

Queries the token’s decimal places (e.g., 18 for most ERC20 tokens like ETH or TEST).

Queries the sender’s balance of the ERC20 token.

Display the sender’s initial balances.

Retrieves an instance of the GaslessSwapRouter contract, which facilitates gasless token swaps on the Kaia blockchain.

Retrieves the blockchain address of the GaslessSwapRouter contract.

Checks whether the specified ERC20 token is supported by the GaslessSwapRouter for gasless swaps.

Retrieves the commission rate charged by the GaslessSwapRouter for facilitating gasless swaps.

Show the GaslessSwapRouter address, true if the token is supported and commission rate.

Checks how many tokens the GaslessSwapRouter is allowed to spend on behalf of the sender.

Determines if an approval transaction is needed by checking if the allowance is zero.

Initializes an empty array to store transactions that will be sent later.

Checks if an approval transaction is needed based on the approveRequired flag. Generates an ERC20 approve transaction to allow the GaslessSwapRouter to spend the sender’s tokens.

Retrieves the current gas price from the blockchain and converts it to a JavaScript number.

Calculates the total KAIA amount (in wei) that the sender must repay to the block miner for funding the gas costs of the transactions.

Calculates the minimum amount of KAIA (in wei) that the swap must produce to cover the repayment, commission, and the desired application transaction fee.

Calculates the amount of ERC20 tokens to swap to receive at least minAmountOut KAIA, accounting for slippage.

Checks if the sender’s ERC20 token balance is insufficient to cover the amount of tokens required for the swap.

Generates the swap transaction to exchange a specified amount of ERC20 tokens for at least a minimum amount of KAIA, incorporating gasless repayment logic. Adds the swap transaction to the txs array for batch execution.

Sends all transactions in the txs array using the sender’s wallet. Iterates over the sent transactions to log their details.

Waits for the transaction to be mined and retrieves its transaction receipt.

Listing the block's transactions related to the sender

Final balance of the sender

Gasless.js

const ethers = require("ethers");
const { Wallet, gasless } = require("@kaiachain/ethers-ext/v6");
// Replace with ERC20 token address to be spent
const tokenAddr = "0xcB00BA2cAb67A3771f9ca1Fa48FDa8881B457750"; // Kairos:TEST token
// Replace with your wallet address and private key
const senderAddr = "0xa0Ee7A142d267C1f36714E4a8F75612F20a79720";
const senderPriv = "0x2a871d0798f97d79848a013d4936a73bf4cc922c825d33c1cf7073dff6d409c6";
const provider = new ethers.JsonRpcProvider("https://public-en-kairos.node.kaia.io");
const wallet = new Wallet(senderPriv, provider);
const ERC20_ABI = [
"function decimals() view returns (uint8)",
"function symbol() view returns (string)",
"function allowance(address owner, address spender) view returns (uint256)",
"function balanceOf(address owner) view returns (uint256)"
];
// senderAddr wants to swap the ERC20 token for at least 0.01 KAIA so she can execute the AppTx.
async function main() {
const appTxFee = ethers.parseEther("0.01").toString();
// Query the environment
console.log(`Using token at address: ${tokenAddr}`);
const token = new ethers.Contract(tokenAddr, ERC20_ABI, provider);
const tokenSymbol = await token.symbol();
const tokenDecimals = await token.decimals();
const tokenBalance = await token.balanceOf(senderAddr);
console.log(`\nInitial balance of the sender ${senderAddr}`);
console.log(`- ${ethers.formatEther(await provider.getBalance(senderAddr))} KAIA`);
console.log(`- ${ethers.formatUnits(tokenBalance, tokenDecimals)} ${tokenSymbol}`);
const router = await gasless.getGaslessSwapRouter(provider);
const routerAddr = await router.getAddress();
const isTokenSupported = await router.isTokenSupported(tokenAddr);
const commissionRate = Number(await router.commissionRate());
console.log(`\nGaslessSwapRouter address: ${routerAddr}`);
console.log(`- The token is supported: ${isTokenSupported}`);
console.log(`- Commission rate: ${commissionRate} bps`);
// If sender hasn't approved, include ApproveTx first.
const allowance = await token.allowance(senderAddr, routerAddr);
const approveRequired = (allowance == 0n);
const txs = [];
if (approveRequired) {
console.log("\nAdding ApproveTx because allowance is 0");
const approveTx = await gasless.getApproveTx(
provider,
senderAddr,
tokenAddr,
routerAddr,
gasPrice,
);
txs.push(approveTx);
} else {
console.log("\nNo ApproveTx needed");
}
// - amountRepay (KAIA) is the cost of LendTx, ApproveTx, and SwapTx. The block miner shall fund it first,
// then the sender has to repay from the swap output.
// - minAmountOut (KAIA) is the required amount of the swap output. It must be enough to cover the amountRepay
// and pay the commission, still leaving appTxFee.
// - amountIn (token) is the amount of the token to be swapped to produce minAmountOut plus slippage.
console.log("\nCalculating the amount of the token to be swapped...");
const gasPrice = Number((await provider.getFeeData()).gasPrice);
console.log(`- gasPrice: ${ethers.formatUnits(gasPrice, "gwei")} gkei`);
const amountRepay = gasless.getAmountRepay(approveRequired, gasPrice);
console.log(`- amountRepay: ${ethers.formatEther(amountRepay)} KAIA`);
const minAmountOut = gasless.getMinAmountOut(amountRepay, appTxFee, commissionRate);
console.log(`- minAmountOut: ${ethers.formatEther(minAmountOut)} KAIA`);
const slippageBps = 50 // 0.5%
const amountIn = await gasless.getAmountIn(router, tokenAddr, minAmountOut, slippageBps);
console.log(`- amountIn: ${ethers.formatUnits(amountIn, tokenDecimals)} ${tokenSymbol}`);
if (tokenBalance < amountIn) {
console.log(`\nInsufficient balance of the token: ${ethers.formatUnits(tokenBalance, tokenDecimals)} ${tokenSymbol}`);
console.log(`- Please transfer more ${tokenSymbol} to the sender ${senderAddr}`);
return;
}
const swapTx = await gasless.getSwapTx(
provider,
senderAddr,
tokenAddr,
routerAddr,
amountIn,
minAmountOut,
amountRepay,
gasPrice,
approveRequired,
);
txs.push(swapTx);
console.log("\nSending transactions...");
const sentTxs = await wallet.sendTransactions(txs);
for (const tx of sentTxs) {
console.log(`- Tx sent: (nonce: ${tx.nonce}) ${tx.hash}`);
}
console.log("\nWaiting for transactions to be mined...");
let blockNum = 0;
for (const sentTx of sentTxs) {
const receipt = await sentTx.wait();
console.log(`- Tx mined at block ${receipt.blockNumber}`);
blockNum = receipt.blockNumber;
}
console.log("\nListing the block's transactions related to the sender...");
const block = await provider.getBlock(blockNum, true);
const names = {
[senderAddr.toLowerCase()]: "sender",
[tokenAddr.toLowerCase()]: "token",
[routerAddr.toLowerCase()]: "router",
}
for (const txhash of block.transactions) {
const tx = await provider.getTransaction(txhash);
const fromName = names[tx.from.toLowerCase()] || tx.from;
const toName = names[tx.to.toLowerCase()] || tx.to;
if (fromName != tx.from || toName != tx.to) {
console.log(`- Tx ${tx.hash}: ${fromName} => ${toName}`);
}
}
console.log(`\nFinal balance of the sender ${senderAddr}`);
console.log(`- ${ethers.formatEther(await provider.getBalance(senderAddr))} KAIA`);
console.log(`- ${ethers.formatUnits(await token.balanceOf(senderAddr), tokenDecimals)} ${tokenSymbol}`);
}
main().catch(console.error);

output

❯ node Gasless.js
Using token at address: 0xcB00BA2cAb67A3771f9ca1Fa48FDa8881B457750
Initial balance of the sender 0xa0Ee7A142d267C1f36714E4a8F75612F20a79720
- 49.9969537425 KAIA
- 3.0 TEST
GaslessSwapRouter address: 0x4b41783732810b731569E4d944F59372F411BEa2
- The token is supported: true
- Commission rate: 0 bps
- 3.0 TEST
GaslessSwapRouter address: 0x4b41783732810b731569E4d944F59372F411BEa2
- The token is supported: true
- Commission rate: 0 bps
GaslessSwapRouter address: 0x4b41783732810b731569E4d944F59372F411BEa2
- The token is supported: true
- Commission rate: 0 bps
- The token is supported: true
- Commission rate: 0 bps
- Commission rate: 0 bps
Adding ApproveTx because allowance is 0
Calculating the amount of the token to be swapped...
- gasPrice: 27.5 gkei
- amountRepay: 0.0170775 KAIA
- minAmountOut: 0.0270775 KAIA
- amountIn: 0.027300931296609197 TEST
Sending transactions...
- Tx sent: (nonce: 5) 0x1d2bd9e5ae11653b8cddb297d040074b189fe456ae986197c4c2bbf74a51e6ef
- Tx sent: (nonce: 6) 0x96dd81962d7ae14baf31bd0a8afb900be1a6c9c8d30ad854011a4120fb0efbaf
Waiting for transactions to be mined...
- Tx mined at block 192123894
- Tx mined at block 192123894
Listing the block's transactions related to the sender...
- Tx 0x50769b14c7814f4e6b169f63595c1c19d9c46ce1c626c14b6b3c5610f691f58a: 0xB74Ff9DEa397fE9E231df545eb53fE2ADF776cb2 => sender
- Tx 0x1d2bd9e5ae11653b8cddb297d040074b189fe456ae986197c4c2bbf74a51e6ef: sender => token
- Tx 0x96dd81962d7ae14baf31bd0a8afb900be1a6c9c8d30ad854011a4120fb0efbaf: sender => router
Final balance of the sender 0xa0Ee7A142d267C1f36714E4a8F75612F20a79720
- 50.016275725 KAIA
- 2.972699068703390803 TEST

ページを改善してください。