-
Notifications
You must be signed in to change notification settings - Fork 52
/
Copy pathmanual.ts
277 lines (241 loc) · 7.52 KB
/
manual.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
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
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
import {
AttestedTransferReceipt,
Chain,
ChainAddress,
ChainContext,
CompletedTransferReceipt,
Network,
RedeemedTransferReceipt,
Signer,
TokenId,
TransferState,
Wormhole,
WormholeMessageId,
amount,
isAttested,
isRedeemed,
isSourceFinalized,
isSourceInitiated,
routes,
signSendWait,
} from "@wormhole-foundation/sdk-connect";
import "@wormhole-foundation/sdk-definitions-ntt";
import { NttRoute } from "./types.js";
type Op = NttRoute.Options;
type Tp = routes.TransferParams<Op>;
type Vr = routes.ValidationResult<Op>;
type Vp = NttRoute.ValidatedParams;
type QR = routes.QuoteResult<Op, Vp>;
type Q = routes.Quote<Op, Vp>;
type R = NttRoute.TransferReceipt;
export function nttManualRoute(config: NttRoute.Config) {
class NttRouteImpl<N extends Network> extends NttManualRoute<N> {
static override config = config;
}
return NttRouteImpl;
}
export class NttManualRoute<N extends Network>
extends routes.FinalizableRoute<N, Op, Vp, R>
implements routes.StaticRouteMethods<typeof NttManualRoute>
{
override NATIVE_GAS_DROPOFF_SUPPORTED: boolean = false;
override IS_AUTOMATIC: boolean = false;
// @ts-ignore
// Since we set the config on the static class, access it with this param
// the NttManualRoute.config will always be empty
readonly staticConfig = this.constructor.config;
static config: NttRoute.Config = { tokens: {} };
static meta = { name: "ManualNtt" };
static supportedNetworks(): Network[] {
return NttRoute.resolveSupportedNetworks(this.config);
}
static supportedChains(network: Network): Chain[] {
return NttRoute.resolveSupportedChains(this.config, network);
}
static async supportedSourceTokens(
fromChain: ChainContext<Network>
): Promise<TokenId[]> {
return NttRoute.resolveSourceTokens(this.config, fromChain);
}
static async supportedDestinationTokens<N extends Network>(
sourceToken: TokenId,
fromChain: ChainContext<N>,
toChain: ChainContext<N>
): Promise<TokenId[]> {
return NttRoute.resolveDestinationTokens(
this.config,
sourceToken,
fromChain,
toChain
);
}
static isProtocolSupported<N extends Network>(
chain: ChainContext<N>
): boolean {
return chain.supportsProtocol("Ntt");
}
getDefaultOptions(): Op {
return NttRoute.ManualOptions;
}
async validate(params: Tp): Promise<Vr> {
const options = params.options ?? this.getDefaultOptions();
const amt = amount.parse(params.amount, this.request.source.decimals);
const gasDropoff = amount.units(
amount.parse(
options.gasDropoff ?? "0.0",
this.request.toChain.config.nativeTokenDecimals
)
);
const validatedParams: Vp = {
amount: params.amount,
normalizedParams: {
amount: amt,
sourceContracts: NttRoute.resolveNttContracts(
this.staticConfig,
this.request.source.id
),
destinationContracts: NttRoute.resolveNttContracts(
this.staticConfig,
this.request.destination.id
),
options: {
queue: false,
automatic: false,
gasDropoff,
},
},
options,
};
return { valid: true, params: validatedParams };
}
async quote(params: Vp): Promise<QR> {
return {
success: true,
params,
sourceToken: {
token: this.request.source.id,
amount: params.normalizedParams.amount,
},
destinationToken: {
token: this.request.destination.id,
amount: amount.parse(params.amount, this.request.destination.decimals),
},
};
}
async initiate(signer: Signer, quote: Q, to: ChainAddress): Promise<R> {
const { params } = quote;
const { fromChain } = this.request;
const sender = Wormhole.parseAddress(signer.chain(), signer.address());
const ntt = await fromChain.getProtocol("Ntt", {
ntt: params.normalizedParams.sourceContracts,
});
const initXfer = ntt.transfer(
sender,
amount.units(params.normalizedParams.amount),
to,
params.normalizedParams.options
);
const txids = await signSendWait(fromChain, initXfer, signer);
return {
from: fromChain.chain,
to: to.chain,
state: TransferState.SourceInitiated,
originTxs: txids,
params,
};
}
async complete(signer: Signer, receipt: R): Promise<R> {
if (!isAttested(receipt)) {
if (isRedeemed(receipt)) return receipt;
throw new Error(
"The source must be finalized in order to complete the transfer"
);
}
const { toChain } = this.request;
const ntt = await toChain.getProtocol("Ntt", {
ntt: receipt.params.normalizedParams.destinationContracts,
});
const sender = Wormhole.parseAddress(signer.chain(), signer.address());
const completeXfer = ntt.redeem([receipt.attestation.attestation], sender);
const txids = await signSendWait(toChain, completeXfer, signer);
return {
...receipt,
state: TransferState.DestinationInitiated,
destinationTxs: txids,
};
}
async finalize(signer: Signer, receipt: R): Promise<R> {
if (!isRedeemed(receipt))
throw new Error("The transfer must be redeemed in order to finalize");
const {
attestation: { attestation: vaa },
} = receipt;
const { toChain } = this.request;
const ntt = await toChain.getProtocol(
"Ntt",
receipt.params.normalizedParams.destinationContracts
);
const completeTransfer = ntt.completeInboundQueuedTransfer(
toChain.chain,
vaa.payload["nttManagerPayload"],
this.request.destination.id.address
);
const finalizeTxids = await signSendWait(toChain, completeTransfer, signer);
return {
...receipt,
state: TransferState.DestinationFinalized,
destinationTxs: [...(receipt.destinationTxs ?? []), ...finalizeTxids],
};
}
public override async *track(receipt: R, timeout?: number) {
if (isSourceInitiated(receipt) || isSourceFinalized(receipt)) {
const { txid } = receipt.originTxs[receipt.originTxs.length - 1]!;
const vaa = await this.wh.getVaa(txid, "Ntt:WormholeTransfer", timeout);
if (!vaa) throw new Error("No VAA found for transaction: " + txid);
const msgId: WormholeMessageId = {
chain: vaa.emitterChain,
emitter: vaa.emitterAddress,
sequence: vaa.sequence,
};
receipt = {
...receipt,
state: TransferState.Attested,
attestation: {
id: msgId,
attestation: vaa,
},
} satisfies AttestedTransferReceipt<NttRoute.AttestationReceipt> as R;
yield receipt;
}
const { toChain } = this.request;
const ntt = await toChain.getProtocol("Ntt", {
ntt: receipt.params.normalizedParams.destinationContracts,
});
if (isAttested(receipt)) {
const {
attestation: { attestation: vaa },
} = receipt;
if (await ntt.getIsApproved(vaa)) {
receipt = {
...receipt,
state: TransferState.DestinationInitiated,
// TODO: check for destination event transactions to get dest Txids
} satisfies RedeemedTransferReceipt<NttRoute.AttestationReceipt>;
yield receipt;
}
}
if (isRedeemed(receipt)) {
const {
attestation: { attestation: vaa },
} = receipt;
if (await ntt.getIsExecuted(vaa)) {
receipt = {
...receipt,
state: TransferState.DestinationFinalized,
} satisfies CompletedTransferReceipt<NttRoute.AttestationReceipt>;
yield receipt;
}
}
yield receipt;
}
}