Withdraw from a farm

Withdrawing Rewards and staked tokens from a farm contract

Withdraw

To withdraw from a TokensFarm contract, you must call withdraw on the TokensFarm smart contract.

In order to withdraw, you must first GET the Stake ID. go to "Get Stake Ids and Stake Details" bellow

function withdraw(
        uint256 _amount,
        uint256 stakeId
    )

Get Stake Ids and Stake Details

To get staking info, pending amounts, and stake IDs from a TokensFarm contract, you must call getUserStakesAndPendingAmounts on the TokensFarm smart contract.

function getUserStakesAndPendingAmounts(address user)
external view returns (uint256[] memory, uint256[] memory, uint256[] memory)

returns:

if the user has staked only once, then for in order to withdraw this particular stake, insert into the Withdraw Function the stake ID parameter: stakeID = deposits[0], where deposits is the array that is returned from the getUserStakesAndPendingAmounts function. If a user staked multiple times, then you must call the Withdraw function multiple times - one for each stake with the following: stakeId = deposit[0..n]

Example script for withdrawing

const hre = require("hardhat");

async function main(){
    const tokensFarmContract = await hre.ethers.getContractAt(
        tokensFarmArtifact.abi,
        "the contracts address here"
    );
    let userAddress = "user address that wish to withdraw";
    let stakeId;
    let {deposits, amounts, depositeTimes } = await tokensFarmContract.getUserStakesAndPendingAmounts(userAddress);
    stakeId = deposits[0]; //withdraw only the user first deposit
    
    //Call withdraw function
    await tokensFarmContract.withdraw(userAddress, stakeId);
}

Last updated