0

I have these to functions:


function stake(address _token, uint256 amount) external payable {
    address sender = msg.sender;
    address receiver = address(this);
    IERC20 token = IERC20(_token);

    if (_token == weth) {
        // ETH deposit
        require(msg.value > 0, "No ETH sent");
        stakedAmountPerToken[_token] = stakedAmountPerToken[_token].add(amount);
        userData[sender].userBalance[_token] = userData[sender].userBalance[_token].add(amount);
        emit Staked(_token, sender, amount);
        if (!isUser[sender]) {
            users.push(sender);
            isUser[sender] = true;
        }
    } else {
        uint256 allowance = token.allowance(sender, receiver);
        require(allowance >= amount, "Insufficient token allowance");

        token.transferFrom(sender, receiver, amount);
        userData[sender].userBalance[_token] = userData[sender].userBalance[_token].add(amount);
        if (!isUser[sender]) {
            users.push(sender);
            isUser[sender] = true;
        }
        emit Staked(_token, sender, amount);
    }
}

function withdraw(address token, uint256 amount) external payable {
    address payable sender = payable(msg.sender);
    require(isUser[sender], "User has no staked balance");
    require(userData[sender].userBalance[token] >= amount, "Insufficient balance for withdrawal");

    if (token == weth) {
        // Convert WETH to ETH
        IWETH(weth).withdraw(amount);
        sender.transfer(amount);
    } else {
        // Token withdrawal
        require(IERC20(token).transfer(sender, amount), "Token transfer failed");
    }

    userData[sender].userBalance[token] = userData[sender].userBalance[token].sub(amount);

    if (userData[sender].userBalance[token] == 0) {
        isUser[sender] = false;
    }

    emit Withdrawed(token, sender, amount);
}

The way i interact with the IWETH in the stake works but when i call the withdraw if the requisites from the reverts are filled i got this error:

Error Message: Called function does not exist in the contract

Can i have a hint from someone over this?

EDIT

The deposit function works

But the withdraw doesn't

2
  • 1
    You ll need to share some more data like the input parameters and the rest of the code. Are you sure you are passing the correct parameters? Also withdraw isnt suppose to accept eth so it shouldnt be market as payable.
    – Nal Luksic
    Jun 15, 2023 at 11:16
  • The params are okey i'm using tenderly to simulate. The payabçe was a suggestion from chatGPT. Jun 15, 2023 at 18:42

1 Answer 1

0
function withdraw(address token, uint256 amount)

Please make sure the token is a contract address, if the token is an external account address, the call will fail.

1
  • The problem was lack of fallback functions. Jun 16, 2023 at 9:20

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.