I am currently trying to test a smart contract using the truffle suit ( with 'truffle test'). My contract is of a simple ERC20 token which includes the standard Transfer event:
event Transfer(address indexed from, address indexed to, uint256 value);
Below is my code which attempts to subscribe to all "Transfer" events only coming from account[0] to account[1]. Even though the 2 parameters are declared as "indexed", the filter does not seem to work and I get all Transfer events no matter the sender/receiver. I tried modifying the code in various ways but nothing seems to work so far
const EnergyToken = artifacts.require("EnergyToken");
const assert = require('assert');
const truffleAssert = require('truffle-assertions');
const { performance } = require('perf_hooks');
contract("Energy Heavy Load Test", async accounts => {
const numberOfTransactions = 10;
let startTime = 0;
let finishTime = 0;
let subscription;
let contract;
let i = 0;
console.log(web3.version);
// Function for rounding numbers
function round(value, precision) {
var multiplier = Math.pow(10, precision || 0);
return Math.round(value * multiplier) / multiplier;
}
// executed before each test
before(async () => {
contract = await EnergyToken.at("0x5BbD383bD43aC3896B86207eFe88cf0628ad06F0");
});
// Test mining time of 2500 transaction calls on the EnergyToken smart contract's transfer() method
it("send " + numberOfTransactions + " transactions of 1 EnergyToken from account 0 to account 1", (done) => {
// contract.Transfer(function(error, event) {
// i++;
// console.log(event)
// if(i == 1)
// startTime = performance.now();
// if (i == numberOfTransactions) {
// finishTime = performance.now();
// console.log("Transactions confirmed!" + " It took " + round((finishTime - startTime)/1000,2) + " seconds to confirm " + numberOfTransactions + " transactions");
// . done();
// }
// });
// contract.getPastEvents('Transfer', {
// filter: { from: accounts[0], to: accounts[1] },
// fromBlock: 0,
// toBlock: 'latest'
// }, function(error, result) {
// if(error) {
// console.log(error, 'error');
// } else {
// console.log(result, 'result');
// }
// });
for (let j = 0; j < numberOfTransactions; j++) {
contract.transfer(accounts[0], 1, { from:accounts[1] });
}
console.log("Finished sending " + numberOfTransactions + " transactions from account 1!!" + accounts[1] + " to " + accounts[0]);
});
it("Calculate average gas used by the transfer() function", async () => {
let result = await contract.transfer(accounts[1], 1);
let hash = await result.tx;
let tx = await web3.eth.getTransactionReceipt(hash);
let gasUsed = await tx.gasUsed;
console.log(" Gas used for a 'Transfer' transaction: " + gasUsed);
});
it("Calculate average gas used by the approve() function", async () => {
let result = await contract.approve(accounts[1], 1000);
let hash = await result.tx;
let tx = await web3.eth.getTransactionReceipt(hash);
let gasUsed = await tx.gasUsed;
console.log(" Gas used for an 'Approve' transaction: " + gasUsed);
});
it("Calculate average gas used by the transferFrom() function", async () => {
await contract.approve(accounts[1], 100, { from: accounts[0] });
let result = await contract.transferFrom(accounts[0], accounts[1], 100, { from: accounts[1] });
let hash = await result.tx;
let tx = await web3.eth.getTransactionReceipt(hash);
let gasUsed = await tx.gasUsed;
console.log(" Gas used for an 'Approve' transaction: " + gasUsed);
});
});
Note: My version of web3 is 1.2.6
contract.events.Transfer
instead ofcontract.Transfer
. The fact that your code runs and even fetches events, leads me to the logical conclusion that your web3 version is 0.x. In order to verify this, please runconsole.log(web3.version)
in your program. And after verifying this, please refer to the official documentation of that specific version. Side note: if you're on Truffle 4.x, then the web3 used inside your Truffle tests IS 0.x.