Skip to main content
Version: V1

Execute Trade

Introduction#

The SDK is designed to make things simpler and accessible. It however cannot execute trades or send transactions. With its robust functionality, the SDK calculates data required to interact with the DMM. Please note that the SDK cannot execute trades or send transactions on your behalf. Rather, it offers utility classes and functions which make it easy to calculate the data required to safely interact with DMM. You will have to construct the transaction yourself, just use the SDK to get the inputs needed.

The example below focuses exclusively on sending a transaction to the DMM router

Sending a Transaction to the Router#

Trade 1 WETH for DAI:

import {
ChainId,
Token,
WETH,
Fetcher,
Trade,
Route,
TokenAmount,
TradeType,
} from '@dynamic-amm/sdk';
// DMM Factory Address if using Ethereum Mainnet
const DMMFactoryAddress = '0x833e4083B7ae46CeA85695c4f7ed25CDAd8886dE';
const DAI = new Token(
ChainId.MAINNET,
'0x6B175474E89094C44Da98b954EedeAC495271d0F',
18,
);
const pool = await Fetcher.fetchPairData(
DAI,
WETH[DAI.chainId],
DMMFactoryAddress,
);
const route = new Route([pool], WETH[DAI.chainId]);
const amountIn = '1000000000000000000'; // 1 WETH
const trade = new Trade(
route,
new TokenAmount(WETH[DAI.chainId], amountIn),
TradeType.EXACT_INPUT,
);

Now, we have constructed the trade entity, lets now put in other parameters to send the transaction:

import {Percent} from '@dynamic-amm/sdk';
const slippageTolerance = new Percent('30', '10000'); // 30 bps, or 0.30%
const amountOutMin = trade.minimumAmountOut(slippageTolerance).raw; // needs to be converted to e.g. hex
const path = [WETH[DAI.chainId].address, DAI.address];
const to = ''; // should be a checksummed recipient address
const value = trade.inputAmount.raw; // // needs to be converted to e.g. hex
  • minimumAmountOut is the minimum amount of DAI you receive before the trade reverts.
  • to is the recipient's address.
  • value is the amount of ETH that must be included as the msg.value in the transaction.