/**
* This script is used to trade USDC on Arbitrum for USDT on Optimism
* It demonstrates how to build a transaction locally and send it to the network,
* while utilizing the Mach SDK to get the quote and encode the order data
*/
import {
createWalletClients,
encodeOrderData,
marketMakeOrder,
getChainFromName,
contracts,
dollarToTokenValue,
getQuote,
} from "@tristeroresearch/mach-sdk";
import { Hex } from "viem";
import { tokens, chains } from "@tristeroresearch/mach-sdk/constants";
try {
// Define the source and destination assets
const srcChain = chains.arbitrum;
const srcAsset = tokens.arb.USDC;
const dstAsset = tokens.op.USDT;
// Convert dollar amount to token value (0.005 USD)
const amt = await dollarToTokenValue(0.005, srcAsset);
// Create wallet clients with private key (for server-side usage)
// For client-side, you would connect to the user's wallet
const { publicClient, walletClient, account } = createWalletClients(srcChain, process.env.PRIVATE_KEY! as Hex);
// Get the Mach contract address for the source chain
const contractAddress = contracts.arb.order_book;
// Get a quote for the swap
const quote = await getQuote(srcAsset, dstAsset, amt);
// Encode order data for the contract call
const data = encodeOrderData(quote);
// Estimate gas for the transaction
const gas = await publicClient.estimateGas({
to: contractAddress,
data,
account: account,
});
// Get chain configuration for the transaction
const chain = getChainFromName(srcChain);
// Sign the transaction
const signedHash = await walletClient.signTransaction({
to: contractAddress,
chain,
data,
value: 0n,
nonce: await publicClient.getTransactionCount({
address: account.address,
}),
account: account,
type: "eip1559",
maxFeePerGas: BigInt("20000000"),
maxPriorityFeePerGas: BigInt("1000000"),
gas,
});
// Send the signed transaction
const hash = await publicClient.sendRawTransaction({
serializedTransaction: signedHash,
});
// Wait for transaction confirmation
const receipt = await publicClient.waitForTransactionReceipt({ hash });
// Call the market maker to complete the order
const response = await marketMakeOrder(srcChain, receipt);
console.log(response);
} catch (error) {
console.error("Error making transaction:", error);
}