I am getting this error when I am transfering some tokens to the decentralized bank Here is the code
Test.js:
await decentralbank.stakeTokens(tokens("100"), {
from: customer,
});
DecentralBank.sol:
function stakeTokens(uint256 _amount) public {
tether.transferFrom(msg.sender, address(this), _amount);
// update staking balance
stakingBalance[msg.sender] = stakingBalance[msg.sender] + _amount;
// add user to stakers array *only* if they haven't staked already
if (!hasStaked[msg.sender]) {
stakers.push(msg.sender);
}
// update stakng status
isStaking[msg.sender] = true;
hasStaked[msg.sender] = true;
}
Tether.sol:
function transferFrom(
address _from,
address _to,
uint256 _value
) public returns (bool success) {
require(_value <= balanceOf[_from]);
require(_value <= allowance[_from][msg.sender]);
balanceOf[_from] -= _value;
balanceOf[_to] += _value;
allowance[_from][msg.sender] -= _value;
emit Transfer(_from, _to, _value);
return true;
}
By looking at that i guess you forgot to approve the token transfer before calling the stakeTokens.
await tether.approve(decentralbank.address, tokens("100"), {
from: costumer,
});
await decentralbank.stakeTokens(tokens("100"), {
from: customer,
});
Also, one security note, you should call the transferFrom before all the state changes in the contract in order to avoid re-entrancy attacks, or you can just use this library.