Using Native Tokens and Fee in Unit Tests for CCIP Local Units
When testing decentralized applications (dApps) on The Ethereum Testnet or Mainnet, it’s essential to have a reliable and efficient way to handle transaction fees. In this article, we’ll explore how to use native tokens like ETH as the fee token in unit tests for the Cross-CChain Interoperability Protocol (CCIP).
Setting up unit tests for CCIP with LINK token
When using LINK as the fee token on The Ethereum Testnet or Mainnet, we can leverage the native tokens’ support for CCIP to simplify our testing process. Here’s a step-by-step guide:
- Set up a test wallet: Create a new test wallet on The Ethereum Testnet (e.g., Alchemy Testnet) and set it as the default wallet.
- Install necessary dependencies: Install
truffle
for contract development,web3.js
for interacting with the blockchain, andccip-tester
for testing CCIP functionality.
- Define test suite: Create a new test file (e.g.,
eth.test.js
) and importccip-tester
. Define a simple test case that uses LINK as the fee token:
const { CCIP } = require('./ccip');
const { AlchemyTestnet } = require('alchemy-testnet');
describe('EthFee', () => {
it('should pay ETH for every transaction', async () => {
const contract = new CCIP();
const txId = await contract.createTransaction({
from: '0x...', // sender address
to: '0x...',
value: 100,
gasPrice: '1e8',
});
const result = await txId.wait(10);
expect(result.status).toBe(OK);
expect(result.txhash).not.toBeUndefined();
expect(result.value).toBeLessThan(txId.gasPrice * 10); //ETH
});
});
- Run the test: Compile and run the contract on The Ethereum Testnet (e.g., Alchemy Testnet) using
truffle compile
andtruffle run
.
- Verify results
: Observe that your test passes without paying any ETH for every transaction.
Using native tokens as fee in unit tests
Now, let’s modify our test suite to use ETH as the fee token:
- Replace LINK with ETH: Update the
link
variable in the test file to point to an ETH account on The Ethereum Testnet (e.g., Alchemy Testnet).
- Modify the test case: Update the test case to use ETH as the fee token:
const { CCIP } = require('./ccip');
const { AlchemyTestnet } = require('alchemy-testnet');
describe('EthFee', () => {
it('should pay ETH for every transaction', async () => {
const contract = new CCIP();
const txId = await contract.createTransaction({
from: '0x...', // sender address
to: '0x...',
value: 100,
gasPrice: '1e8',
fee: ‘0.01’,
});
const result = await txId.wait(10);
expect(result.status).toBe(OK);
expect(result.txhash).not.toBeUndefined();
expect(result.value).toBeLessThan(txId.gasPrice * 100); //ETH
});
});
- Run the test: Compile and run the contract on The Ethereum Testnet (e.g., Alchemy Testnet) using
truffle compile
andtruffle run
.
- Verify results: Observe that your test passes without paying any ETH for every transaction.
Conclusion
By leveraging native tokens like ETH as fee tokens, which can simplify our unit tests for CCIP on The Ethereum Testnet or Mainnet. This approach not only saves time but also provides a more efficient way to handle transaction fees, making it easier to develop and test decentralized applications.