-
Hello, The unstake function is: function unstakeTokens(address _token, uint256 _amount) public {
uint256 balance = stakingBalance[_token][msg.sender];
require(balance > 0, "Staking balance cannot be zero.");
require(stakingBalance[_token][msg.sender] <= _amount, "Do not have this amount of token staked.");
IERC20(_token).transferFrom(address(this), msg.sender, _amount);
stakingBalance[_token][msg.sender] = stakingBalance[_token][msg.sender] - _amount;
if (stakingBalance[_token][msg.sender] == 0) {
uniqueTokenStaked[msg.sender] = uniqueTokenStaked[msg.sender] - 1;
removeStaker(msg.sender);
}
} Testing: # amount_staked defined in conftest.py as 1 ether
def test_unstake_tokens(amount_staked):
# Arrange
if network.show_active() not in LOCAL_BLOCKCHAIN_ENVIRONMENTS:
pytest.skip("Only for local testing!")
account = get_account()
token_farm, dapp_token = test_stake_tokens(amount_staked)
# Act
tx_unstake = token_farm.unstakeTokens(dapp_token.address, amount_staked, {"from": account})
tx_unstake.wait(1)
# Assert
assert token_farm.stakingBalance(dapp_token.address, account.address) == 0
assert token_farm.stakers(0) != account.address
def test_stake_tokens(amount_staked):
# Arrange
if network.show_active() not in LOCAL_BLOCKCHAIN_ENVIRONMENTS:
pytest.skip("Only for local testing!")
account = get_account()
token_farm, dapp_token = deploy_token_farm_and_dapp_token()
# Act
# Approve to the token farm to stake our tokens
tx_approve = dapp_token.approve(token_farm.address, amount_staked, {"from": account})
tx_approve.wait(1)
# Stake our token
tx_stake = token_farm.stakeTokens(dapp_token.address, amount_staked, {"from": account})
tx_stake.wait(1)
# Assert
# 1) current staking balance must equal to amount staked
assert token_farm.stakingBalance(dapp_token.address, account.address) == amount_staked
# 2) test, if only one token has been staked
assert token_farm.uniqueTokenStaked(account.address) == 1
# 3) the first staker is us
assert token_farm.stakers(0) == account.address
# return token farm and dapp token to be able to use it in further testing
return token_farm, dapp_token Thank you very much in advance. |
Beta Was this translation helpful? Give feedback.
Answered by
haraslub
Dec 7, 2021
Replies: 1 comment 1 reply
-
Solved, the following line: |
Beta Was this translation helpful? Give feedback.
1 reply
Answer selected by
haraslub
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Solved, the following line:
IERC20(_token).transferFrom(address(this), msg.sender, _amount);
replaced by:
IERC20(_token).transfer(msg.sender, _amount);