-
Notifications
You must be signed in to change notification settings - Fork 1
/
enso.ts
103 lines (90 loc) · 2.65 KB
/
enso.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
import axios, { AxiosError } from "axios";
import { BigNumber } from "ethers";
import { ETH, SAFE_WALLET } from "./constants";
const AUTH_HEADER = {
headers: {
Authorization: "Bearer 1e02632d-6feb-4a75-a157-documentation",
},
};
/*
TODO:
1. pass inside the params into one call instead of constantly rewriting the query
*/
export async function getRoute(toToken: string, amountIn: BigNumber) {
const url = "XXX";
try {
const response = (
await axios.get(
`${url}?chainId=1&fromAddress=${SAFE_WALLET}&amountIn=${amountIn.toString()}&tokenIn=${ETH}&tokenOut=${toToken}`,
AUTH_HEADER
)
).data;
console.log("Single response:", response);
return response;
} catch (e: any) {
if (e instanceof AxiosError) throw `Route failed: ${e.response?.data}`;
else throw e;
}
}
export async function getRouteBundle(toTokens: string[], amountIn: BigNumber) {
const url = "XXX";
const amountSplit = amountIn.div(toTokens.length).toString();
const data = toTokens.map((token) => ({
protocol: "enso",
action: "route",
args: {
tokenIn: ETH,
tokenOut: token,
amountIn: amountSplit,
},
}));
try {
const response = (
await axios.post(
`${url}?chainId=1&fromAddress=${SAFE_WALLET}`,
data,
AUTH_HEADER
)
).data;
console.log("Batch response: ", response);
return response;
} catch (e) {
if (e instanceof AxiosError)
throw `Bundle failed: ${JSON.stringify(e.response?.data)}`;
else throw e;
}
}
export async function getProjects() {
const url = "XXX";
let projects: any = [];
const standards = (await axios.get(url, AUTH_HEADER)).data;
standards.forEach((standard: any) => {
if (standard.protocol) projects.push(standard.protocol.slug);
if (standard.forks)
projects.push(...standard.forks.map((fork: any) => fork.slug));
});
return projects;
}
export async function getPools(project: string) {
const url = "XXX";
let pools = [["Pool Name | APY | TVL | Pool Address"]];
const response = (await axios.get(`${url}?protocol=${project}`, AUTH_HEADER))
.data;
// limit to 50 pools due to telegram message limit. You can still console.log the full list here before array if you want more details
for (let i = 0; i < (response.length < 50 ? response.length : 50); i++) {
pools.push([
response[i].subtitle,
response[i].apy,
response[i].tvl,
response[i].poolAddress,
]);
}
return pools;
}
export async function getPoolApy(poolAddress: string) {
const url = "XXX";
const apy = (
await axios.get(`${url}?tokenAddress=${poolAddress}`, AUTH_HEADER)
).data[0].apy;
return apy;
}