# Withdraw from a farm

### Withdraw

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

{% hint style="info" %}
In order to withdraw, you must first `GET` the Stake ID. go to "Get Stake Ids and Stake Details" bellow
{% endhint %}

```solidity
function withdraw(
        uint256 _amount,
        uint256 stakeId
    )
```

| Parameters | Type      | Description                                          |
| ---------- | --------- | ---------------------------------------------------- |
| \_amount   | `uint256` | the amount the user is wish to withdraw to the farm. |
| stakeId    | `uint256` | the id of the staker                                 |

### 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.

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

| Parameters | Type      | Description                         |
| ---------- | --------- | ----------------------------------- |
| user       | `address` | the user address to get the data on |

returns:

<table><thead><tr><th width="242">Parameters</th><th width="234.9992523364486">Type</th><th>Description</th></tr></thead><tbody><tr><td>deposits</td><td><code>uint256[]</code></td><td>An array of stake Ids for each user deposit.</td></tr><tr><td>pendingAmount</td><td><code>uint256[]</code></td><td>An array of the amount deposited in each stake</td></tr><tr><td>depositeTime</td><td><code>uint256[]</code></td><td>An array of the time each staking occurred</td></tr></tbody></table>

{% hint style="info" %}
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]`&#x20;
{% endhint %}

#### Example script for withdrawing

```javascript
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);
}
```


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://docs.tokensfarm.com/for-developers/integrate-tokensfarm/withdraw-from-a-farm.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
