Visualize ERC20 token transfers in Truffle Tests
$ npm install truffle-token-test-utils
const tokenTransfers = require("truffle-token-test-utils");
contract("ContractName", (accounts) => {
tokenTransfers.setWeb3(web3); // web3 auto injected by truffle in tests
...
const tx = await someContract.someFunction();
await tokenTransfers.print(tx);
}
const { web3 } = require("@openzeppelin/test-environment");
const tokenTransfers = require("truffle-token-test-utils");
tokenTransfers.setWeb3(web3);
...
const tx = await someContract.someFunction();
await tokenTransfers.print(tx);
1. Substitute address with custom names
An object for custom addressToName
mapping can be passed along with print()
to display names instead of ETH addresses.
Without it the default 'SENDER'
and 'RECEIVER'
are displayed for the transaction sender and receiver contract address repectively. They both can also be overwritten with the passed object.
Eg:
await tokenTransfers.print(tx, {
"0xAb5801a7D398351b8bE11C439e05C5B3259aeC9B": "Vitalik"
});
Output:
2. Get overall accounts balance change
Pass boolean true
as third argument for print()
to display overall token balance change as shown
await tokenTransfers.print(tx, {}, true);
Output: