This repository has been archived by the owner on Oct 8, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathexampleRunner.ts
140 lines (126 loc) · 4.14 KB
/
exampleRunner.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
import axios from "axios";
import { Server, Socket } from "../src";
import * as ethers from "ethers";
import { Path } from "../src/path";
// import { BridgeName } from "../src/client/models/BridgeDetails";
import { ChainId } from "../src/client/models/ChainId";
import { SocketTx } from "../src/socketTx";
// import { Bridge } from "@socket.tech/ll-core";
const API_KEY = "72a5b4b0-e727-48be-8aa1-5da9d62fe635"; // Testing key
const wallet = process.env.PRIVATE_KEY
? new ethers.Wallet(process.env.PRIVATE_KEY)
: ethers.Wallet.createRandom();
// Polygon ethers fee data is broken
async function getPolygonFeeData() {
const gas: {
standard: {
maxPriorityFee: number;
maxFee: number;
};
} = (await axios.get("https://gasstation-mainnet.matic.network/v2")).data;
return {
maxPriorityFeePerGas: ethers.utils.parseUnits(
Math.ceil(gas.standard.maxPriorityFee).toString(),
"gwei"
),
maxFeePerGas: ethers.utils.parseUnits(Math.ceil(gas.standard.maxFee).toString(), "gwei"),
};
}
const chainProviders: { [index: number]: ethers.providers.JsonRpcProvider } = {
100: new ethers.providers.JsonRpcProvider("https://gnosis-mainnet.public.blastapi.io"),
137: new ethers.providers.JsonRpcProvider("https://polygon-rpc.com"),
56: new ethers.providers.JsonRpcProvider("https://bsc-dataseed4.binance.org"),
};
export async function runRoute({
fromAmount,
fromChainId,
toChainId,
fromTokenAddress,
toTokenAddress,
// bridge,
multiTx = false,
feeTakerAddress,
feePercent,
bridgeWithGas = false,
}: {
fromAmount: string;
fromChainId: ChainId;
toChainId: ChainId;
fromTokenAddress: string;
toTokenAddress: string;
// bridge?: BridgeName;
multiTx?: boolean;
feeTakerAddress?: string;
feePercent?: string;
bridgeWithGas?: boolean
}) {
const socket = new Socket({
apiKey: API_KEY,
defaultQuotePreferences: {
singleTxOnly: !multiTx,
},
});
const userAddress = await wallet.getAddress();
const tokenList = await socket.getTokenList({
fromChainId: fromChainId,
toChainId: toChainId,
});
const fromToken = tokenList.from.tokenByAddress(fromTokenAddress);
const toToken = tokenList.to.tokenByAddress(toTokenAddress);
const path = new Path({ fromToken, toToken });
if (!fromToken.decimals) {
throw new Error("danger! from token has no decimals!");
}
const amount = ethers.utils.parseUnits(fromAmount, fromToken.decimals).toString();
// const prefs = bridge ? { includeBridges: [bridge] } : undefined;
const quote = await socket.getBestQuote(
{
path: path,
amount,
address: userAddress,
},
{
feePercent: feePercent,
feeTakerAddress: feeTakerAddress,
bridgeWithGas,
singleTxOnly: true,
// @ts-ignore
excludeBridges: ['synapse', 'across']
}
);
if (!quote) {
throw new Error("no quote available");
}
console.log('quote', quote);
const execute = await Server.getSingleTx({requestBody: {route: quote?.route, refuel: quote?.refuel}});
console.log('execute', execute);
// const execute = await socket.start(quote);
// await executeRoute(execute);
}
export async function executeRoute(execute: AsyncGenerator<SocketTx, void, string>) {
let next = await execute.next();
while (!next.done && next.value) {
const tx = next.value;
console.log(`Executing step ${tx.userTxIndex} "${tx.userTxType}" on chain ${tx.chainId}`);
const provider = chainProviders[tx.chainId];
const approvalTxData = await tx.getApproveTransaction();
if (approvalTxData) {
const feeData = tx.chainId === 137 ? await getPolygonFeeData() : {};
const approvalTx = await wallet.connect(provider).sendTransaction({
...approvalTxData,
...feeData,
});
console.log(`Approving: ${approvalTx.hash}`);
await approvalTx.wait();
}
const sendTxData = await tx.getSendTransaction();
const feeData = tx.chainId === 137 ? await getPolygonFeeData() : {};
const sendTx = await wallet.connect(provider).sendTransaction({
...sendTxData,
...feeData,
});
console.log(`Sending: ${sendTx.hash}`);
await sendTx.wait();
next = await execute.next(sendTx.hash);
}
}