This repository has been archived by the owner on Jun 28, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 12
/
index.ts
109 lines (97 loc) · 3.3 KB
/
index.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
import Lifi, {
ChainId,
CoinKey,
ConfigUpdate,
Execution,
ExecutionSettings,
findDefaultToken,
Route
} from "@lifi/sdk";
import { providers, Signer, Wallet } from 'ethers';
const mnemonic = process.env.MNEMONIC || "";
async function demo() {
// setup wallet
if (!process.env.MNEMONIC) {
console.warn(
'Please specify a MNEMONIC phrase in your environment variables: `export MNEMONIC="..."`'
);
return;
}
console.log(">> Setup Wallet");
const provider = new providers.JsonRpcProvider(
"https://polygon-rpc.com/",
137
);
const wallet = Wallet.fromMnemonic(mnemonic).connect(provider);
// get Route
console.log(">> Request route");
const routeRequest = {
fromChainId: ChainId.POL, // Polygon
fromAmount: "1000000", // 1 USDT
fromTokenAddress: findDefaultToken(CoinKey.USDT, ChainId.POL).address,
toChainId: ChainId.DAI, // xDai
toTokenAddress: findDefaultToken(CoinKey.USDT, ChainId.DAI).address,
options: {
slippage: 0.03, // = 3%
allowSwitchChain: false, // execute all transaction on starting chain
exchanges: {
allow: [], // only find direct transfers
},
},
};
// STEP 1: Initialize the API
// ☝️ This configuration is totally optional! ------------------------------------
const optionalConfigs: ConfigUpdate = {
apiUrl: 'https://li.quest', // DEFAULT production endpoint
rpcs: { // You can provide custom RPCs
137: ['https://polygon-rpc.com/']
},
multicallAddresses: { // You can provide custom addresses for multicall
137: '0x02817C1e3543c2d908a590F5dB6bc97f933dB4BD'
},
defaultExecutionSettings: { // You can provide default execution settings @see {ExecutionSettings}
updateCallback: (route: Route):void => {
console.log('>> Route updated', route);
},
switchChainHook: (requiredChainId: number):Promise<Signer | undefined> => {
console.log('>> Switching to chain', requiredChainId);
return Promise.resolve(wallet);
},
infiniteApproval: false, // DEFAULT false
}
}
// ---------------------------------------------------------------------------
const api = new Lifi(optionalConfigs);
// STEP 2: Request a route
const routeResponse = await api.getRoutes(routeRequest);
const route = routeResponse.routes[0];
console.log(">> Got Route");
console.log(route);
// STEP 3: Execute the route
console.log(">> Start Execution");
// These are optonal settings for execution ------------------------------------
const settings: ExecutionSettings = {
updateCallback: (updatedRoute) => {
let lastExecution: Execution | undefined = undefined;
for (const step of updatedRoute.steps) {
if (step.execution) {
lastExecution = step.execution;
}
}
console.log(lastExecution);
},
switchChainHook: async (requiredChainId: number) => {
console.log(">>Switching Chains");
const provider = new providers.JsonRpcProvider(
"https://rpc.xdaichain.com/",
requiredChainId
);
const wallet = Wallet.fromMnemonic(mnemonic).connect(provider);
return wallet;
},
};
// ---------------------------------------------------------------------------
await api.executeRoute(wallet, route, settings);
console.log(">> Done");
}
demo();