> For the complete documentation index, see [llms.txt](https://docs.carbondefi.xyz/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.carbondefi.xyz/contracts-and-functions/carboncontroller-functions/write-functions/trade/tradebytargetamount.md).

# tradeByTargetAmount()

{% hint style="info" %}
The function parameters must include the list of orders (tradeActions) to interact with.&#x20;

**Using the SDK is highly recommended.** See [Performing Trades](/developer-guides/carbon-defi-sdk/interacting-with-the-sdk/performing-trades.md) for details.&#x20;
{% endhint %}

### Function **tradeByTargetAmount()**

This function will trade to receive the exact amount of indicated **target** tokens for as few **source** tokens as possible.&#x20;

{% code title="CarbonController.sol" overflow="wrap" lineNumbers="true" %}

```solidity
    function tradeByTargetAmount(
        Token sourceToken,
        Token targetToken,
        TradeAction[] calldata tradeActions,
        uint256 deadline,
        uint128 maxInput
    ) external payable nonReentrant whenNotPaused onlyProxyDelegate returns (uint128)
```

{% endcode %}

### **Function Arguments**

<table><thead><tr><th width="196">Name</th><th width="147.33333333333331">Type</th><th>Description</th></tr></thead><tbody><tr><td>sourceToken</td><td>Token</td><td>The source token address</td></tr><tr><td>targetToken</td><td>Token</td><td>The target token address</td></tr><tr><td>tradeActions</td><td>list</td><td>This is a list of TradeAction objects, each of which  contain the <strong>strategyId</strong> and <strong>amount</strong> of target tokens to <strong>receive</strong> from the strategy. </td></tr><tr><td>deadline</td><td>uint256</td><td>Unix timestamp after which the transaction will revert</td></tr><tr><td>maxInput</td><td>uint128</td><td>The maximum amount of source tokens you are willing to spend (use) for the transaction to result in the requested targetToken amount and not revert</td></tr></tbody></table>

{% hint style="warning" %}
tradeAction `amount` need to follow the relevant token decimal. \
tradeByTargetAmount, follows the targetToken decimals
{% endhint %}

### Example

{% code overflow="wrap" %}

```solidity
tradeByTargetAmount(
0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2, //source token
0x1F573D6Fb3F13d689FF844B4cE37794d79a7FF1C, //target token
[{59, 1600000000000000000}, {72, 3140000000000000000}], //array of tradeActions [{strategyId, amount},{strategyId, amount}...]
1675680190, //deadline
5320000000000000000 //maxInput
)
```

{% endcode %}

{% hint style="success" %}
While all required data is available on chain, it is recommended to use the [Carbon DeFi SDK](/developer-guides/carbon-defi-sdk.md).
{% endhint %}

{% hint style="info" %}
To trade directly with the ETH token, use the contract address: *0xEeeeeEeeeEeEeeEeEeEeeEEEeeeeEeeeeeeeEEeE*&#x20;
{% endhint %}


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## 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, and the optional `goal` query parameter:

```
GET https://docs.carbondefi.xyz/contracts-and-functions/carboncontroller-functions/write-functions/trade/tradebytargetamount.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

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.
