Last active
February 23, 2024 15:40
-
-
Save ptsayli/f25717f2f2db85be2433e2b25963799e to your computer and use it in GitHub Desktop.
Send USDC on Polygon chain to DAI on Arbitrum chain
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { Path, Socket } from "@socket.tech/socket-v2-sdk"; | |
import { ethers } from "ethers"; | |
const PRIVATE_KEY = '<Paste your Private key here>' | |
const wallet = new ethers.Wallet(PRIVATE_KEY) | |
const socket = new Socket({ | |
apiKey: "72a5b4b0-e727-48be-8aa1-5da9d62fe635", | |
}); | |
const fromAmount = '2' | |
const rpcURL = 'https://polygon-mainnet.g.alchemy.com/v2/kQ36fgi3Kg_pdtsBv8wjaX3tuw1m-YcC' | |
const provider = new ethers.providers.JsonRpcProvider(rpcURL) | |
async function test() { | |
const tokenList = await socket.getTokenList({ | |
fromChainId: 137, | |
toChainId: 42161, | |
}); | |
const fromUSDCPoly = | |
tokenList.from.tokenByAddress('0x2791Bca1f2de4661ED88A30C99A7a9449Aa84174'); | |
const toDAIArb = tokenList.to.tokenByAddress('0xda10009cbd5d07dd0cecc66161fc93d7c9000da1'); | |
const path = new Path({ fromToken: fromUSDCPoly, toToken: toDAIArb }); | |
const amount = ethers.utils.parseUnits(fromAmount, fromUSDCPoly.decimals).toString(); | |
const address = await wallet.getAddress() | |
const quote = await socket.getBestQuote({ | |
path, | |
amount, // 1 ETH | |
address, | |
}); | |
if (!quote) { | |
throw new Error("No quote found"); | |
} | |
const execute = await socket.start(quote) | |
let next = await execute.next(); | |
while (!next.done && next.value) { | |
const tx = next.value; | |
const approvalTxData = await tx.getApproveTransaction() | |
if (approvalTxData) { | |
const approvalTx = await wallet.connect(provider).sendTransaction(approvalTxData); | |
console.log(`Approving: ${approvalTx.hash}`); | |
await approvalTx.wait(); | |
} | |
const sendTxData = await tx.getSendTransaction(); | |
const sendTx = await wallet.connect(provider).sendTransaction(sendTxData); | |
console.log(`Sending: ${sendTx.hash}`); | |
await sendTx.wait(); | |
next = await execute.next(sendTx.hash); | |
} | |
} | |
test() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment