Reputation: 905
I have followed this Hardhat tutorial and trying to test a demo smart contract on RSK regtest blockchain.
Here is the hardhat.config.js
setup I am using:
require('@nomiclabs/hardhat-waffle');
module.exports = {
solidity: '0.7.3',
defaultNetwork: 'rskregtest',
networks: {
hardhat: {},
rskregtest: {
url: 'http://localhost:4444',
},
};
This is the test I am trying to perform:
const { expect } = require('chai');
describe('Transactions', () => {
it('Should transfer tokens between accounts', async () => {
const signers = await ethers.getSigners();
const [owner, addr1] = signers;
const Token = await ethers.getContractFactory('Token');
const hardhatToken = await Token.deploy();
console.log('Before transction');
console.log('Owner balance: ', await hardhatToken.balanceOf(owner.address));
console.log('Addr1 balance: ', await hardhatToken.balanceOf(addr1.address));
// Transfer 50 tokens from owner to addr1
await hardhatToken.connect(owner).transfer(addr1.address, 50);
console.log('After transaction');
console.log('Owner balance: ', await hardhatToken.balanceOf(owner.address));
console.log('Addr1 balance: ', await hardhatToken.balanceOf(addr1.address));
expect(await hardhatToken.balanceOf(addr1.address)).to.equal(50);
});
});
I expect this test to pass, with the "owner balance" to be 999950
(total supply 1000000
minus 50
) and the "address1 balance" to be 50
.
However the test fails with the console output:
Before transction
Owner balance: BigNumber { value: "1000000" }
Addr1 balance: BigNumber { value: "0" }
After transction
Owner balance: BigNumber { value: "1000000" }
Addr1 balance: BigNumber { value: "0" }
Looks like the transfer transaction is being rejected and the tokens have not been transferred.
What am I doing wrong? How can I transfer RSK regtest deployed tokens from one account to another using Ethers.js and Hardhat?
Upvotes: 7
Views: 360
Reputation: 2196
Your transaction is eventually being processed (not rejected). However, in your code you start querying the balances before waiting for the transaction to finalize.
First you need to send a transaction request and then wait()
for the response. After receiving the response you can end up querying the balances.
const txResponse = await hardhatToken
.connect(owner)
.transfer(addr1.address, 50);
await txResponse.wait();
console.log('After transaction');
console.log('Owner balance: ', await hardhatToken.balanceOf(owner.address));
Ref: TransactionResponse.wait()
in ethers.js docs
Upvotes: 5