-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathshortcuts.ts
More file actions
217 lines (208 loc) · 10.2 KB
/
shortcuts.ts
File metadata and controls
217 lines (208 loc) · 10.2 KB
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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
import { ChainIds } from '@ensofinance/shortcuts-builder/types';
import { BigNumberish } from '@ethersproject/bignumber';
import { StaticJsonRpcProvider } from '@ethersproject/providers';
import { Beets_Sts_Deposit_Shortcut } from '../shortcuts/beets/sts_deposit';
import { Beets_Sts_Redeem_Shortcut } from '../shortcuts/beets/sts_redeem';
import { Dahlia_StS_Deposit_Shortcut } from '../shortcuts/dahlia/sts_deposit';
import { Dahlia_StS_Redeem_Shortcut } from '../shortcuts/dahlia/sts_redeem';
import { Dahlia_SUSDC_Deposit_Shortcut } from '../shortcuts/dahlia/susdc_deposit';
import { Dahlia_SUSDC_Redeem_Shortcut } from '../shortcuts/dahlia/susdc_redeem';
import { Dahlia_WstkscETH_ScETH_Deposit_Shortcut } from '../shortcuts/dahlia/wstksceth_sceth_deposit';
import { Dahlia_WstkscETH_ScETH_Redeem_Shortcut } from '../shortcuts/dahlia/wstksceth_sceth_redeem';
import { Dahlia_WstkscUSD_ScUSD_Deposit_Shortcut } from '../shortcuts/dahlia/wstkscusd_scusd_deposit';
import { Dahlia_WstkscUSD_ScUSD_Redeem_Shortcut } from '../shortcuts/dahlia/wstkscusd_scusd_redeem';
import { Dahlia_WstkscUSD_USDC_Deposit_Shortcut } from '../shortcuts/dahlia/wstkscusd_usdc_deposit';
import { Dahlia_WstkscUSD_USDC_Redeem_Shortcut } from '../shortcuts/dahlia/wstkscusd_usdc_redeem';
import { Origin_Wos_Deposit_Shortcut } from '../shortcuts/origin/wos_deposit';
import { Origin_Wos_Redeem_Shortcut } from '../shortcuts/origin/wos_redeem';
import { Pendle_LptWstkscETH_Deposit_Shortcut } from '../shortcuts/pendle/lpt_wstkscETH_deposit';
import { Pendle_LptWstkscETH_Redeem_Shortcut } from '../shortcuts/pendle/lpt_wstkscETH_redeem';
import { Pendle_LptWstkscUSD_Deposit_Shortcut } from '../shortcuts/pendle/lpt_wstkscUSD_deposit';
import { Pendle_LptWstkscUSD_Redeem_Shortcut } from '../shortcuts/pendle/lpt_wstkscUSD_redeem';
import { Rings_Wstkscusd_Deposit_Shortcut } from '../shortcuts/rings/wstkscusd_deposit';
import { Rings_Wstkscusd_Redeem_Shortcut } from '../shortcuts/rings/wstkscusd_redeem';
import { Silo_BUSDCe_Deposit_Shortcut } from '../shortcuts/silo/busdce_deposit';
import { Silo_BUSDCe_Redeem_Shortcut } from '../shortcuts/silo/busdce_redeem';
import { Silo_Ws_Deposit_Shortcut } from '../shortcuts/silo/ws_deposit';
import { Silo_Ws_Redeem_Shortcut } from '../shortcuts/silo/ws_redeem';
import { Stability_CstSSL_Deposit_Shortcut } from '../shortcuts/stability/cstssl-deposit';
import { Stability_CstSSL_Redeem_Shortcut } from '../shortcuts/stability/cstssl-redeem';
import { Stability_Cwossal_Deposit_Shortcut } from '../shortcuts/stability/cwossal-deposit';
import { Stability_Cwossal_Redeem_Shortcut } from '../shortcuts/stability/cwossal-redeem';
import { StableJack_PtScUsd_Deposit_Shortcut } from '../shortcuts/stablejack/PT-scUSD_deposit';
import { StableJack_PtScUsd_Redeem_Shortcut } from '../shortcuts/stablejack/PT-scUSD_redeem';
import { StableJack_PtSts_Deposit_Shortcut } from '../shortcuts/stablejack/PT-stS_deposit';
import { StableJack_PtSts_Redeem_Shortcut } from '../shortcuts/stablejack/PT-stS_redeem';
import { StableJack_PtWos_Deposit_Shortcut } from '../shortcuts/stablejack/PT-wOS_deposit';
import { StableJack_PtWos_Redeem_Shortcut } from '../shortcuts/stablejack/PT-wOS_redeem';
import { StableJack_YtScUsd_Deposit_Shortcut } from '../shortcuts/stablejack/YT-scUSD_deposit';
import { StableJack_YtScUsd_Redeem_Shortcut } from '../shortcuts/stablejack/YT-scUSD_redeem';
import { StableJack_YtSts_Deposit_Shortcut } from '../shortcuts/stablejack/YT-stS_deposit';
import { StableJack_YtSts_Redeem_Shortcut } from '../shortcuts/stablejack/YT-stS_redeem';
import { StableJack_YtWos_Deposit_Shortcut } from '../shortcuts/stablejack/YT-wOS_deposit';
import { StableJack_YtWos_Redeem_Shortcut } from '../shortcuts/stablejack/YT-wOS_redeem';
import { BuiltShortcut, Shortcut } from '../types';
import { buildVerificationHash } from './utils';
// TODO: this structure can be generated by iterating `supportedShortcuts`, building each shortcut,
// and mapping them to the `shortcuts` object (using name from `<Shortcut>.name`).
export const shortcuts: Record<string, Record<string, Shortcut>> = {
beets: {
'sts-deposit': new Beets_Sts_Deposit_Shortcut(),
'sts-redeem': new Beets_Sts_Redeem_Shortcut(),
},
dahlia: {
'susdc-deposit': new Dahlia_SUSDC_Deposit_Shortcut(),
'susdc-redeem': new Dahlia_SUSDC_Redeem_Shortcut(),
'sts-deposit': new Dahlia_StS_Deposit_Shortcut(),
'sts-redeem': new Dahlia_StS_Redeem_Shortcut(),
'wstksceth-sceth-deposit': new Dahlia_WstkscETH_ScETH_Deposit_Shortcut(),
'wstksceth-sceth-redeem': new Dahlia_WstkscETH_ScETH_Redeem_Shortcut(),
'wstkscusd-scusd-deposit': new Dahlia_WstkscUSD_ScUSD_Deposit_Shortcut(),
'wstkscusd-scusd-redeem': new Dahlia_WstkscUSD_ScUSD_Redeem_Shortcut(),
'wstkscusd-usdc-deposit': new Dahlia_WstkscUSD_USDC_Deposit_Shortcut(),
'wstkscusd-usdc-redeem': new Dahlia_WstkscUSD_USDC_Redeem_Shortcut(),
},
origin: {
'wos-deposit': new Origin_Wos_Deposit_Shortcut(),
'wos-redeem': new Origin_Wos_Redeem_Shortcut(),
},
silo: {
'ws-deposit': new Silo_Ws_Deposit_Shortcut(),
'ws-redeem': new Silo_Ws_Redeem_Shortcut(),
'busdce-deposit': new Silo_BUSDCe_Deposit_Shortcut(),
'busdce-redeem': new Silo_BUSDCe_Redeem_Shortcut(),
},
stablejack: {
'pt-scusd-deposit': new StableJack_PtScUsd_Deposit_Shortcut(),
'pt-scusd-redeem': new StableJack_PtScUsd_Redeem_Shortcut(),
'pt-sts-deposit': new StableJack_PtSts_Deposit_Shortcut(),
'pt-sts-redeem': new StableJack_PtSts_Redeem_Shortcut(),
'pt-wos-deposit': new StableJack_PtWos_Deposit_Shortcut(),
'pt-wos-redeem': new StableJack_PtWos_Redeem_Shortcut(),
'yt-scusd-deposit': new StableJack_YtScUsd_Deposit_Shortcut(),
'yt-scusd-redeem': new StableJack_YtScUsd_Redeem_Shortcut(),
'yt-sts-deposit': new StableJack_YtSts_Deposit_Shortcut(),
'yt-sts-redeem': new StableJack_YtSts_Redeem_Shortcut(),
'yt-wos-deposit': new StableJack_YtWos_Deposit_Shortcut(),
'yt-wos-redeem': new StableJack_YtWos_Redeem_Shortcut(),
},
rings: {
wstkscusd_deposit: new Rings_Wstkscusd_Deposit_Shortcut(),
wstkscusd_redeem: new Rings_Wstkscusd_Redeem_Shortcut(),
},
stability: {
'cstssl-deposit': new Stability_CstSSL_Deposit_Shortcut(),
'cstssl-redeem': new Stability_CstSSL_Redeem_Shortcut(),
'cwossal-deposit': new Stability_Cwossal_Deposit_Shortcut(),
'cwossal-redeem': new Stability_Cwossal_Redeem_Shortcut(),
},
pendle: {
'lpt-wstksceth-deposit': new Pendle_LptWstkscETH_Deposit_Shortcut(),
'lpt-wstksceth-redeem': new Pendle_LptWstkscETH_Redeem_Shortcut(),
'lpt-wstkscusd-deposit': new Pendle_LptWstkscUSD_Deposit_Shortcut(),
'lpt-wstkscusd-redeem': new Pendle_LptWstkscUSD_Redeem_Shortcut(),
},
};
export function getAllMarkets(): string[] {
return Object.entries(shortcuts).flatMap(([protocol, markets]) =>
Object.keys(markets).map((market) => `${protocol}-${market}`),
);
}
export const supportedShortcuts = [
Dahlia_SUSDC_Deposit_Shortcut,
Dahlia_SUSDC_Redeem_Shortcut,
Dahlia_StS_Deposit_Shortcut,
Dahlia_StS_Redeem_Shortcut,
Dahlia_WstkscETH_ScETH_Deposit_Shortcut,
Dahlia_WstkscETH_ScETH_Redeem_Shortcut,
Dahlia_WstkscUSD_ScUSD_Deposit_Shortcut,
Dahlia_WstkscUSD_ScUSD_Redeem_Shortcut,
Dahlia_WstkscUSD_USDC_Deposit_Shortcut,
Dahlia_WstkscUSD_USDC_Redeem_Shortcut,
Silo_Ws_Deposit_Shortcut,
Silo_Ws_Redeem_Shortcut,
Silo_BUSDCe_Deposit_Shortcut,
Silo_BUSDCe_Redeem_Shortcut,
StableJack_PtScUsd_Deposit_Shortcut,
StableJack_PtScUsd_Redeem_Shortcut,
StableJack_PtSts_Deposit_Shortcut,
StableJack_PtSts_Redeem_Shortcut,
StableJack_PtWos_Deposit_Shortcut,
StableJack_PtWos_Redeem_Shortcut,
StableJack_YtScUsd_Deposit_Shortcut,
StableJack_YtScUsd_Redeem_Shortcut,
StableJack_YtSts_Deposit_Shortcut,
StableJack_YtSts_Redeem_Shortcut,
StableJack_YtWos_Deposit_Shortcut,
StableJack_YtWos_Redeem_Shortcut,
Origin_Wos_Deposit_Shortcut,
Origin_Wos_Redeem_Shortcut,
Rings_Wstkscusd_Deposit_Shortcut,
Rings_Wstkscusd_Redeem_Shortcut,
Beets_Sts_Deposit_Shortcut,
Beets_Sts_Redeem_Shortcut,
Stability_CstSSL_Deposit_Shortcut,
Stability_CstSSL_Redeem_Shortcut,
Stability_Cwossal_Deposit_Shortcut,
Stability_Cwossal_Redeem_Shortcut,
Pendle_LptWstkscETH_Deposit_Shortcut,
Pendle_LptWstkscETH_Redeem_Shortcut,
Pendle_LptWstkscUSD_Deposit_Shortcut,
Pendle_LptWstkscUSD_Redeem_Shortcut,
];
export async function buildShortcutsHashMap(
chainId: number,
provider: StaticJsonRpcProvider,
): Promise<Record<string, Shortcut>> {
const shortcutsArray = [];
for (const protocol in shortcuts) {
for (const market in shortcuts[protocol]) {
const shortcut = shortcuts[protocol][market];
if (shortcut.inputs[chainId]) shortcutsArray.push(shortcuts[protocol][market]);
}
}
const hashArray = await Promise.all(
shortcutsArray.map(async (shortcut) => {
const { script, metadata } = await shortcut.build(chainId, provider);
return buildVerificationHash(metadata.tokensOut![0], script);
}),
);
const shortcutsHashMap: Record<string, Shortcut> = {};
for (const i in shortcutsArray) {
shortcutsHashMap[hashArray[i]] = shortcutsArray[i];
}
return shortcutsHashMap;
}
export async function buildShortcut(
chainId: ChainIds,
provider: StaticJsonRpcProvider,
shortcut: Shortcut,
requiresFunding: boolean,
amountsIn?: BigNumberish[],
): Promise<BuiltShortcut> {
let builtShortcut: BuiltShortcut;
try {
builtShortcut = await shortcut.build(chainId, provider);
} catch (error) {
throw new Error(`Unexpected error building shortcut: ${shortcut.name}. Reason: ${error}`);
}
const { script, metadata } = builtShortcut;
// Validate metadata
const { tokensIn, tokensOut } = metadata;
if (!tokensIn) {
throw new Error(
`Invalid 'metadata.tokensIn' in shortcut: ${shortcut.name}. "tokensIn" array must have one item at least`,
);
}
if (!tokensOut) {
throw new Error(
`Invalid 'metadata.tokensIn' in shortcut: ${shortcut.name}. "tokensOut" array must have one item at least`,
);
}
if (requiresFunding && (amountsIn as BigNumberish[]).length != tokensIn.length) {
throw new Error(
`Invalid 'metadata.amountsIn' and 'metadata.tokensIn' in shortcut: ${shortcut.name}. Length mismatch`,
);
}
return { script, metadata };
}