0

I have a function that transfers the amount from owner to user. I'm getting success in the event emitted message and my test is failing every time.

Here is my code.

function investInMPay(uint256 _amount) public validateInvestor(_amount) {
        Investors[msg.sender] = investor(block.timestamp, _amount);
        transferTokenToInvestor(msg.sender, _amount);
    }

function transferTokenToInvestor(address _to, uint256 _value) public returns (bool success) {
       require(balanceOf[owner] >= _value);
        _transfer(owner, _to, _value);
        return true; 
    }

function _transfer(address _from, address _to, uint256 _value) internal {
        require(_to != address(0));
        balanceOf[_from] -= _value;
        balanceOf[_to] += _value;
        emit Transfer(_from, _to, _value, balanceOf[_to]);
    }

Here is a test that I have written.

it('test token transfer', async () => {
                invest = await Invest.new();
                await invest.investInMPay(ether(1), { from: user })
                let result = await token.balanceOf(user)
                assert.equal(result.toString(),tokens(1).toString());
            })

While running the test I'm getting following error.

Events emitted during test:
---------------------------

    Token.Transfer(
      from: <indexed> 0xf3a429e4FAFf561f5AD75A3F944Bc14432b225ee (type: address),
      to: <indexed> 0x273638fa06e2A6870566D1fbe5F3D37402A95655 (type: address),
      _value: 1000000000000000000 (type: uint256),
      balance: 1000000000000000000 (type: uint256)
    )
<----ERROR---->
    success
          test token transfer:
    
          AssertionError: expected '0' to equal '1000000000000000000'
          + expected - actual
    
          -0
          +1000000000000000000
6
  • 1
    what is tokens? Oct 22, 2022 at 18:42
  • tokens is my function which transfers ether to wei. @JavierMarchetti
    – 0xSavan
    Oct 25, 2022 at 7:03
  • This line seems a bit off to me: { let result = await token.balanceOf(user) } What is the [token.] in this case (Seems like you are sending ETH, but getting balance of some ERC20 token, or something like that)
    – Sky
    Oct 25, 2022 at 7:42
  • Actually, I have a function that transfers ERC20 tokens in exchange for ETH. So transferTokenToInvestor functions transfer the ERC20 token. And I want to check that transfer balance to the user balance. @Sky
    – 0xSavan
    Oct 25, 2022 at 8:26
  • What does your _transfer function look like? Can you update your question with the _transfer function? Oct 25, 2022 at 12:55

1 Answer 1

1
+50
       await invest.investInMPay(ether(1), { from: user })
       let result = await token.balanceOf(user)

I think this is token.balanceOf not returning updated state. In "invest" contract you already have balanceOf mapping so if you get the result directly from the mapping it should work. if you define the balanceOf inside invest contact

function balanceOf(address _owner) public view override returns (uint256 balance){
        // since name of the func is balanceOf, I named the mapping as tokenBalances
        return tokenBalances[_owner];
    }

so get the result:

   let result = await invest.balanceOf(user)      

It is not clear what token is in your testing function. probably token is another contract you keep the state, but that state is not getting updated and when you write invest.balanceOf(user) you are getting 0 as the default value. In solidity mapping, if the key does not exist, since you probably defined mapping mapping(address=>uint) balanceOf, you are getting a default value of 0 for uint

1
  • You guessed it, right man. It works. Thank you.
    – 0xSavan
    Oct 29, 2022 at 6:40

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.