Use of native tokens as expense in unit tests for local CCIP units
When you test decentralized applications (DAPP) on Ethereum Testnet or Mainnet, it is essential to have a reliable and effective way to manage transaction costs. In this article, we will explore how to use native tokens as ETH as a fees in unit tests for the cross -interoperability protocol (CCIP).
Configuration of unit tests for CCIP with the link token
When you use the link as a fresh token on Ethereum Testnet or Mainnet, we can take advantage of the management of native tokens to simplify our test process. Here is a step by step guide:
- Configure a test portfolio : Create a new test portfolio on Ethereum Testnet (for example, alchemy testnet) and define it as default portfolio.
- Install the necessary dependencies : Install
Truffes' for the development of contracts,
Web3.jsto interact with the blockchain and
cipc-testerto test the CCIP functionality.
- Define a series of tests
: Create a new test file (for example,Eth.tet.js
) and Import 'CCIP-TESTER
. Define a simple test case that uses the link as a fresh token:
`Javascript
Const {ccip} = require ('./ cipc');
Const {alchemytetnet} = require ('alchemy-testnet');
Describe ('ethfee', () => {
it ('should pay ETH for each transaction', async () => {
Const contract = New CCIP ();
Const txid = wait for contract.
De: '0x ...', // address of the sender
to: '0x ...',
Value: 100,
Gasprice: '1e8',
});
Const result = wait txid.wait (10);
wait (result.status) .Tobe ('ok');
Wait (Result.txhash) .not.Tobeundefined ();
Wait (Result.Value) .Toblesshan (txid.gasprice * 10); // eth
});
});
'
- Run the test: Compile and execute the contract on Ethereum TESTNET (for example, alchemy testnet) using compile and run
truffles.
- Check the results : Observe that your test passes without paying ETH for each transaction.
Use of native tokens as expenses in unit tests
Now, modify our testing of tests to use ETH as a fresh token:
- Replace the link with ETH : update the variableLink
in the test file to point an ETH account on the Ethereum Testhemnet (for example, ALCHEMY TESTNET).
- Change the test case : update the test case to use ETH as a fees:
Javascript
Const {ccip} = require ('./ cipc');
Const {alchemytetnet} = require ('alchemy-testnet');
Describe ('ethfee', () => {
it ('should pay ETH for each transaction', async () => {
Const contract = New CCIP ();
Const txid = wait for contract.
De: '0x ...', // address of the sender
to: '0x ...',
Value: 100,
Gasprice: '1e8',
Fresh: "0.01",
});
Const result = wait txid.wait (10);
wait (result.status) .Tobe ('ok');
Wait (Result.txhash) .not.Tobeundefined ();
wait (result.value) .Toblesshan (txid.gasprice * 100); // eth
});
});
'
- Run the test: Compile and execute the contract on Ethereum TESTNET (for example, alchemy testnet) using compile and run` truffle truffle.
- Check the results : Observe that your test passes without paying ETH for each transaction.
Conclusion
By taking advantage of native tokens like ETH as a fees, we can simplify our unit tests for CCIP on Ethereum Testnet or Mainnet. This approach not only saves time, but also provides a more effective way to manage transaction costs, which facilitates the development and test of decentralized applications.
Leave a Reply