Skip to content

Usage

Get the tokens and chains supported by the SDK

  • The SDK defines constants for some of the tokens and all of the chains supported by the Mach platform.
  • They are provided for convenience
import { tokens, chains } from "@tristeroresearch/mach-sdk/constants";

Get a quote from the API

  • The Mach API provides up to date quotes that provide the best price for a given trade.
  • The SDK provides a helper function that takes two assets, an amount to trade, and a private key, then makes an API request to get a quote.
  • Note that the private key stays on the client side and is NOT passed to the API.
import { getQuote, dollarToTokenValue } from '@tristeroresearch/mach-sdk';
import { tokens } from '@tristeroresearch/mach-sdk';
//Set up the chains and assets

const srcAsset = tokens.arb.USDC;
const dstAsset = tokens.op.USDT;

//Convert the dollar amount that you'd like to trade to the token amount
const amt = await dollarToTokenValue(100, srcAsset);

//Get the quote
const quote = await getQuote(srcAsset, dstAsset, amt);

//Print the quote
console.log(quote);

Submit an order

  • The SDK provides a helper function that takes a quote and a private key, then makes an API request to make an order.
  • Note that the private key stays on the client side and is NOT passed to the API.
  • The SDK also provides an all-in-one function that gets a quote, makes an order, and requests for the API to market make the order.
  • Also, the SDK provides helper functions
import {
  encodeOrderData,
  signTransaction,
  getQuote,
  createWalletClients,
  dollarToTokenValue,
  marketMakeOrder,
} from '@tristeroresearch/mach-sdk';
import { chains, tokens } from '@tristeroresearch/mach-sdk';
//Set up the wallet clients
const key = process.env.PRIVATE_KEY as `0x${string}`;
const srcChain = chains.arbitrum;

const { publicClient } = await createWalletClients(srcChain, key);

const amt = await dollarToTokenValue(0.1, tokens.arb.USDC);
//Get the quote
const quote = await getQuote(tokens.arb.USDC, tokens.op.USDT, amt);

//Encode the order data
const data = encodeOrderData(quote);

//Sign the transaction
const signedHash = await signTransaction(data, srcChain, key);

//Send the transaction
const transaction = { serializedTransaction: signedHash };
const hash = await publicClient.sendRawTransaction(transaction);

//Wait for the transaction to be mined
const receipt = await publicClient.waitForTransactionReceipt({ hash });

//Make the order
const response = await marketMakeOrder(srcChain, receipt);

//Print the response
console.log(response);

Easy Interfaces that Omit Parameters

  • You can use the easyQuote, easyOrder, and makeOrder functions to get quotes and make orders without handling the transaction on the blockchain level.
import { getQuote, dollarToTokenValue, submitOrder, marketMakeOrder } from '@tristeroresearch/mach-sdk';
import { tokens, chains } from '@tristeroresearch/mach-sdk/constants';

const amount = await dollarToTokenValue(0.01, tokens.arb.USDC);
const quote = await getQuote(tokens.arb.USDC, tokens.op.USDT, amount);
const receipt = await submitOrder(quote);
const response = await marketMakeOrder(chains.arb, receipt);

console.log(response);

Single function to get a quote, make an order, and market make an order

  • The SDK provides a single function, order, that gets a quote, makes an order, and market makes an order.
  • This function takes a quote and a private key, then makes an API request to get a quote, makes an order, and market makes an order.
import { order, dollarToTokenValue } from '@tristeroresearch/mach-sdk';
import { tokens } from '@tristeroresearch/mach-sdk/constants';

const amount = await dollarToTokenValue(0.01, tokens.arb.USDC);
console.log(await order(tokens.arb.USDC, tokens.op.USDT, amount));

Remember, scripts that omit passing a private key to the functions are using the private key from the environment variable PRIVATE_KEY.

Setting Gas Fee Override

You may find at times that the Mach SDK's default gas fee recommendations are not optimal for your transaction. If you want to override the default gas fee recommendations provided by the SDK, you can manually set the gas fee, priority fee, and gas limit. This is useful if you want to adjust network fees for faster transactions or customize your transaction costs.

Below is an example:

import { config } from '@tristeroresearch/mach-sdk';

const sdk = await config;
sdk.setGasRecommendationOverride(true);

// Set gas fee higher than the current base fee (e.g., current base fee: 588802000 wei)
sdk.setGasFee(BigInt('700000000')); // 700 million wei (0.7 Gwei)

// Set priority fee to determine transaction inclusion speed
sdk.setPriorityFee(BigInt('10000000')); // 10 million wei (0.01 Gwei)

// Optionally, you can set a custom gas limit to optimize cost
sdk.setGasLimit(BigInt('500000'));

Be sure that the values you set correspond to the current network conditions and your transaction requirements.

Back to top