Carbon DeFi Dev
  • Carbon DeFi
    • Introducing Carbon DeFi
      • What Makes Carbon DeFi Unique?
      • Fees & Payments
      • Resources
    • Security & Audits
  • Contracts & Functions
    • Contracts
      • CarbonController
      • CarbonVortex
      • CarbonVortexBridge
      • Deployments
        • Mainnet Contracts
        • Testnet Contracts
    • CarbonController Functions
      • Write Functions
        • Transaction Prerequisites
          • approve() / allowance()
        • Trade
          • tradeBySourceAmount()
          • tradeByTargetAmount()
        • Strategy Management
          • createStrategy()
          • updateStrategy
          • deleteStrategy()
      • Read Functions
        • controllerType()
        • tradingFeePPM()
        • pairTradingFeePPM()
        • pair()
        • pairs()
        • strategy()
        • strategiesByPair()
        • strategiesByPairCount()
        • calculateTradeSourceAmount()
        • calculateTradeTargetAmount()
      • Data & Events
        • TradingFeePPMUpdated()
        • PairCreated()
        • StrategyCreated()
        • StrategyDeleted()
        • StrategyUpdated()
        • TokensTraded()
        • FeesWithdrawn()
    • CarbonVortex Functions
      • Write Functions
        • trade()
        • execute()
      • Read Functions
        • amountAvailableForTrading()
        • availableTokens()
        • expectedTradeInput()
        • expectedTradeReturn()
        • finalTargetToken()
        • minTargetTokenSaleAmount()
        • minTokenSaleAmount()
        • pairDisabled()
        • priceDecayHalfLife()
        • rewardsPPM()
        • targetToken()
        • targetTokenPriceDecayHalfLife()
        • targetTokenSaleAmount()
        • tokenPrice()
        • totalCollected()
        • tradingEnabled()
    • CarbonVortexBridge Functions
      • Write Functions
        • Bridge()
  • Rest API
    • Carbon DeFi Public REST API
  • Developer Guides
    • Carbon DeFi SDK
      • Getting started
      • Interacting with the SDK
        • Managing Strategies
        • Performing Trades
        • Getting User and Pair Data
        • Collecting Liquidity Data
      • SDK Functions
        • constructor
        • startDataSync
        • getCachedPairs
        • hasLiquidityByPair
        • getLiquidityByPair
        • getUserStrategies
        • getTradeData
        • composeTradeByTargetTransaction
        • composeTradeBySourceTransaction
        • createBuySellStrategy
        • updateStrategy
        • deleteStrategy
        • getMinRateByPair
        • getMaxRateByPair
        • getRateLiquidityDepthByPair
    • The Carbon DeFi versus Uniswap V3 Invariant Functions
    • Carbon DeFi Subgraph
      • Accessing the Subgraph
      • Creating Subgraph Queries
      • Subgraph Query in Python
      • Sample Subgraph Queries
    • Using CarbonVortex
      • CarbonVortex Dynamics
Powered by GitBook
On this page
  • Function tradeByTargetAmount()
  • Function Arguments
  • Example
  1. Contracts & Functions
  2. CarbonController Functions
  3. Write Functions
  4. Trade

tradeByTargetAmount()

PrevioustradeBySourceAmount()NextStrategy Management

Last updated 7 months ago

The function parameters must include the list of orders (tradeActions) to interact with.

Using the SDK is highly recommended. See for details.

Function tradeByTargetAmount()

This function will trade to receive the exact amount of indicated target tokens for as few source tokens as possible.

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

Function Arguments

Name
Type
Description

sourceToken

Token

The source token address

targetToken

Token

The target token address

tradeActions

list

This is a list of TradeAction objects, each of which contain the strategyId and amount of target tokens to receive from the strategy.

deadline

uint256

Unix timestamp after which the transaction will revert

maxInput

uint128

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

tradeAction amount need to follow the relevant token decimal. tradeByTargetAmount, follows the targetToken decimals

Example

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

To trade directly with the ETH token, use the contract address: 0xEeeeeEeeeEeEeeEeEeEeeEEEeeeeEeeeeeeeEEeE

While all required data is available on chain, it is recommended to use the .

Performing Trades
Carbon DeFi SDK