I’m doing the DAPP university create a token tutorial as a way to learn solidity,etc.
Things look good – except that the transfer function doesn’t see to actually do anything.
When the contract deploys account[0] has 5001 tokens in it.When i run a simply test of the transfer function moving 1000 from account[0] to account[1] the function returns successful.
But the assert fail because the amount isn’t actually change in either account[0] or accounts[1].
I did use truffle debug – and when I step through it it does work fine and I can see the account values change.When I run my test Js file though – it looks like the values refuse to change. Also when I do it by hand in the console as shown below. I’m a bit baffled. perhaps I’m not defining the hash correctly?
Code:
mapping(address => uint256) public balanceOf; ... function transfer(address receiver, uint256 numTokens) public returns (bool) { require(numTokens <= balanceOf[msg.sender]); balanceOf[msg.sender] = balanceOf[msg.sender] - numTokens; balanceOf[receiver] = balanceOf[receiver] + numTokens; emit Transfer(msg.sender, receiver, numTokens); return true; }
javascript test
Contract: xfer 1) The Big Bang (aka Constuct) > No events were emitted 0 passing (487ms) 1 failing 1) Contract: xfer The Big Bang (aka Constuct): AssertionError: expected 5001 to equal 4001 + expected - actual -5001 +4001
truffle console test:
PS D:\morte\token> truffle console truffle(development)> let token = await Token.deployed(); undefined truffle(development)> token.totalSupply().then(function(s) {total = s;}); undefined truffle(development)> total.toNumber(); 5001 truffle(development)> token.balanceOf(accounts[0]).then(function(t) {acct0bal = t;}); undefined truffle(development)> acct0bal.toNumber(); 5001 truffle(development)> token.balanceOf(accounts[1]).then(function(t) {acct1bal = t;}); undefined truffle(development)> acct1bal.toNumber(); 0 truffle(development)> token.transfer.call(accounts[1],1000,{from: accounts[0]}).then(function(suc){success=suc}); undefined truffle(development)> success true truffle(development)> token.balanceOf(accounts[0]).then(function(t) {acct0bal = t;}); undefined truffle(development)> acct0bal.toNumber(); 5001 truffle(development)> token.balanceOf(accounts[1]).then(function(t) {acct1bal = t;}); undefined truffle(development)> acct1bal.toNumber(); 0
truffle transaction Debug:
olidity built-ins: msg: { data: hex'a9059cbb0000000000000000000000007ef620e82589275cdb4a45821ecfdb883e8739d400000000000000000000000000000000000000000000000000000000000003e8', sig: 0xa9059cbb, sender: 0xb1A934bed6cF1B0093DB1290CB0F9f4eEaD05785, value: 0 } tx: { origin: 0xb1A934bed6cF1B0093DB1290CB0F9f4eEaD05785, gasprice: 20000000000 } block: { coinbase: 0x0000000000000000000000000000000000000000, difficulty: 0, gaslimit: 6721975, number: 6, timestamp: 1625597037 } this: 0x46bd6d8Eb8033e2edaA33AfaD522A70347778617 (KairosToken) now: 1625597037 Contract variables: name: '' symbol: '' totalSupply: 0 fullPotential: 0 lifeExpectancy: 0 potentialSupply: 0 balanceOf: Map(2) { 0xb1A934bed6cF1B0093DB1290CB0F9f4eEaD05785 => 3001, 0x7EF620E82589275CDB4A45821ecfDB883E8739d4 => 2000 } minted: Map(0) {} potential: Map(0) {} Local variables: _to: 0x7EF620E82589275CDB4A45821ecfDB883E8739d4 _value: 1000
Answer
I believe it’s because of token.transfer.call(accounts[1],1000,{from: accounts[0]})
.
Have a look at the end of this page at Simulate behaviour of non-view functions https://blockheroes.dev/when-to-use-call-solidity-functions-truffle/. This explains everything.
Basically, your transfer
function changes the state variables. But because you use call() when executing it, it only simulates the result (which is ‘true’ in your case), but doesn’t actually push the changes to the blockchain. Try using send() instead.
Attribution
Source : Link , Question Author : WhenDingo , Answer Author : Ioana Roceanu