-
Notifications
You must be signed in to change notification settings - Fork 652
/
redeem.ts
416 lines (387 loc) · 11.5 KB
/
redeem.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
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
import {
JsonRpcProvider,
SUI_CLOCK_OBJECT_ID,
TransactionBlock,
} from "@mysten/sui.js";
import {
ACCOUNT_SIZE,
NATIVE_MINT,
TOKEN_PROGRAM_ID,
createCloseAccountInstruction,
createInitializeAccountInstruction,
createTransferInstruction,
getMinimumBalanceForRentExemptAccount,
getMint,
} from "@solana/spl-token";
import {
Commitment,
Connection,
Keypair,
PublicKey,
PublicKeyInitData,
SystemProgram,
Transaction,
} from "@solana/web3.js";
import { MsgExecuteContract } from "@terra-money/terra.js";
import { MsgExecuteContract as XplaMsgExecuteContract } from "@xpla/xpla.js";
import { Algodv2 } from "algosdk";
import { AptosClient, Types } from "aptos";
import BN from "bn.js";
import { Overrides, ethers } from "ethers";
import { fromUint8Array } from "js-base64";
import { FunctionCallOptions } from "near-api-js/lib/account";
import { Provider } from "near-api-js/lib/providers";
import {
TransactionSignerPair,
_parseVAAAlgorand,
_submitVAAAlgorand,
} from "../algorand";
import { completeTransferAndRegister } from "../aptos";
import { Bridge__factory } from "../ethers-contracts";
import {
createCompleteTransferNativeInstruction,
createCompleteTransferWrappedInstruction,
} from "../solana/tokenBridge";
import { getPackageId, getTokenCoinType, uint8ArrayToBCS } from "../sui";
import {
CHAIN_ID_NEAR,
CHAIN_ID_SOLANA,
ChainId,
MAX_VAA_DECIMALS,
callFunctionNear,
hashLookup,
uint8ArrayToHex,
} from "../utils";
import { SignedVaa, parseTokenTransferVaa } from "../vaa";
import { getForeignAssetNear } from "./getForeignAsset";
export async function redeemOnEth(
tokenBridgeAddress: string,
signer: ethers.Signer,
signedVAA: Uint8Array,
overrides: Overrides & { from?: string | Promise<string> } = {}
) {
const bridge = Bridge__factory.connect(tokenBridgeAddress, signer);
const v = await bridge.completeTransfer(signedVAA, overrides);
const receipt = await v.wait();
return receipt;
}
export async function redeemOnEthNative(
tokenBridgeAddress: string,
signer: ethers.Signer,
signedVAA: Uint8Array,
overrides: Overrides & { from?: string | Promise<string> } = {}
) {
const bridge = Bridge__factory.connect(tokenBridgeAddress, signer);
const v = await bridge.completeTransferAndUnwrapETH(signedVAA, overrides);
const receipt = await v.wait();
return receipt;
}
export async function redeemOnTerra(
tokenBridgeAddress: string,
walletAddress: string,
signedVAA: Uint8Array
) {
return new MsgExecuteContract(walletAddress, tokenBridgeAddress, {
submit_vaa: {
data: fromUint8Array(signedVAA),
},
});
}
export function redeemOnXpla(
tokenBridgeAddress: string,
walletAddress: string,
signedVAA: Uint8Array
): XplaMsgExecuteContract {
return new XplaMsgExecuteContract(walletAddress, tokenBridgeAddress, {
submit_vaa: {
data: fromUint8Array(signedVAA),
},
});
}
export async function redeemAndUnwrapOnSolana(
connection: Connection,
bridgeAddress: PublicKeyInitData,
tokenBridgeAddress: PublicKeyInitData,
payerAddress: PublicKeyInitData,
signedVaa: SignedVaa,
commitment?: Commitment
) {
const parsed = parseTokenTransferVaa(signedVaa);
const targetPublicKey = new PublicKey(parsed.to);
const targetAmount = await getMint(connection, NATIVE_MINT, commitment).then(
(info) =>
parsed.amount * BigInt(Math.pow(10, info.decimals - MAX_VAA_DECIMALS))
);
const rentBalance = await getMinimumBalanceForRentExemptAccount(
connection,
commitment
);
if (Buffer.compare(parsed.tokenAddress, NATIVE_MINT.toBuffer()) != 0) {
return Promise.reject("tokenAddress != NATIVE_MINT");
}
const payerPublicKey = new PublicKey(payerAddress);
const ancillaryKeypair = Keypair.generate();
const completeTransferIx = createCompleteTransferNativeInstruction(
tokenBridgeAddress,
bridgeAddress,
payerPublicKey,
signedVaa
);
//This will create a temporary account where the wSOL will be moved
const createAncillaryAccountIx = SystemProgram.createAccount({
fromPubkey: payerPublicKey,
newAccountPubkey: ancillaryKeypair.publicKey,
lamports: rentBalance, //spl token accounts need rent exemption
space: ACCOUNT_SIZE,
programId: TOKEN_PROGRAM_ID,
});
//Initialize the account as a WSOL account, with the original payerAddress as owner
const initAccountIx = createInitializeAccountInstruction(
ancillaryKeypair.publicKey,
NATIVE_MINT,
payerPublicKey
);
//Send in the amount of wSOL which we want converted to SOL
const balanceTransferIx = createTransferInstruction(
targetPublicKey,
ancillaryKeypair.publicKey,
payerPublicKey,
targetAmount.valueOf()
);
//Close the ancillary account for cleanup. Payer address receives any remaining funds
const closeAccountIx = createCloseAccountInstruction(
ancillaryKeypair.publicKey, //account to close
payerPublicKey, //Remaining funds destination
payerPublicKey //authority
);
const { blockhash } = await connection.getLatestBlockhash(commitment);
const transaction = new Transaction();
transaction.recentBlockhash = blockhash;
transaction.feePayer = payerPublicKey;
transaction.add(
completeTransferIx,
createAncillaryAccountIx,
initAccountIx,
balanceTransferIx,
closeAccountIx
);
transaction.partialSign(ancillaryKeypair);
return transaction;
}
export async function redeemOnSolana(
connection: Connection,
bridgeAddress: PublicKeyInitData,
tokenBridgeAddress: PublicKeyInitData,
payerAddress: PublicKeyInitData,
signedVaa: SignedVaa,
feeRecipientAddress?: PublicKeyInitData,
commitment?: Commitment
) {
const parsed = parseTokenTransferVaa(signedVaa);
const createCompleteTransferInstruction =
parsed.tokenChain == CHAIN_ID_SOLANA
? createCompleteTransferNativeInstruction
: createCompleteTransferWrappedInstruction;
const transaction = new Transaction().add(
createCompleteTransferInstruction(
tokenBridgeAddress,
bridgeAddress,
payerAddress,
parsed,
feeRecipientAddress
)
);
const { blockhash } = await connection.getLatestBlockhash(commitment);
transaction.recentBlockhash = blockhash;
transaction.feePayer = new PublicKey(payerAddress);
return transaction;
}
/**
* This basically just submits the VAA to Algorand
* @param client AlgodV2 client
* @param tokenBridgeId Token bridge ID
* @param bridgeId Core bridge ID
* @param vaa The VAA to be redeemed
* @param acct Sending account
* @returns Transaction ID(s)
*/
export async function redeemOnAlgorand(
client: Algodv2,
tokenBridgeId: bigint,
bridgeId: bigint,
vaa: Uint8Array,
senderAddr: string
): Promise<TransactionSignerPair[]> {
return await _submitVAAAlgorand(
client,
tokenBridgeId,
bridgeId,
vaa,
senderAddr
);
}
export async function redeemOnNear(
provider: Provider,
account: string,
tokenBridge: string,
vaa: Uint8Array
): Promise<FunctionCallOptions[]> {
const options: FunctionCallOptions[] = [];
const p = _parseVAAAlgorand(vaa);
if (p.ToChain !== CHAIN_ID_NEAR) {
throw new Error("Not destined for NEAR");
}
const { found, value: receiver } = await hashLookup(
provider,
tokenBridge,
uint8ArrayToHex(p.ToAddress as Uint8Array)
);
if (!found) {
throw new Error(
"Unregistered receiver (receiving account is not registered)"
);
}
const token = await getForeignAssetNear(
provider,
tokenBridge,
p.FromChain as ChainId,
p.Contract as string
);
if (
(p.Contract as string) !==
"0000000000000000000000000000000000000000000000000000000000000000"
) {
if (token === "" || token === null) {
throw new Error("Unregistered token (has it been attested?)");
}
const bal = await callFunctionNear(
provider,
token as string,
"storage_balance_of",
{
account_id: receiver,
}
);
if (bal === null) {
options.push({
contractId: token as string,
methodName: "storage_deposit",
args: { account_id: receiver, registration_only: true },
gas: new BN("100000000000000"),
attachedDeposit: new BN("2000000000000000000000"), // 0.002 NEAR
});
}
if (
p.Fee !== undefined &&
Buffer.compare(
p.Fee,
Buffer.from(
"0000000000000000000000000000000000000000000000000000000000000000",
"hex"
)
) !== 0
) {
const bal = await callFunctionNear(
provider,
token as string,
"storage_balance_of",
{
account_id: account,
}
);
if (bal === null) {
options.push({
contractId: token as string,
methodName: "storage_deposit",
args: { account_id: account, registration_only: true },
gas: new BN("100000000000000"),
attachedDeposit: new BN("2000000000000000000000"), // 0.002 NEAR
});
}
}
}
options.push({
contractId: tokenBridge,
methodName: "submit_vaa",
args: {
vaa: uint8ArrayToHex(vaa),
},
attachedDeposit: new BN("100000000000000000000000"),
gas: new BN("150000000000000"),
});
options.push({
contractId: tokenBridge,
methodName: "submit_vaa",
args: {
vaa: uint8ArrayToHex(vaa),
},
attachedDeposit: new BN("100000000000000000000000"),
gas: new BN("150000000000000"),
});
return options;
}
/**
* Register the token specified in the given VAA in the transfer recipient's account if necessary
* and complete the transfer.
* @param client Client used to transfer data to/from Aptos node
* @param tokenBridgeAddress Address of token bridge
* @param transferVAA Bytes of transfer VAA
* @returns Transaction payload
*/
export function redeemOnAptos(
client: AptosClient,
tokenBridgeAddress: string,
transferVAA: Uint8Array
): Promise<Types.EntryFunctionPayload> {
return completeTransferAndRegister(client, tokenBridgeAddress, transferVAA);
}
export async function redeemOnSui(
provider: JsonRpcProvider,
coreBridgeStateObjectId: string,
tokenBridgeStateObjectId: string,
transferVAA: Uint8Array
): Promise<TransactionBlock> {
const { tokenAddress, tokenChain } = parseTokenTransferVaa(transferVAA);
const coinType = await getTokenCoinType(
provider,
tokenBridgeStateObjectId,
tokenAddress,
tokenChain
);
if (!coinType) {
throw new Error("Unable to fetch token coinType");
}
const [coreBridgePackageId, tokenBridgePackageId] = await Promise.all([
getPackageId(provider, coreBridgeStateObjectId),
getPackageId(provider, tokenBridgeStateObjectId),
]);
const tx = new TransactionBlock();
const [verifiedVAA] = tx.moveCall({
target: `${coreBridgePackageId}::vaa::parse_and_verify`,
arguments: [
tx.object(coreBridgeStateObjectId),
tx.pure(uint8ArrayToBCS(transferVAA)),
tx.object(SUI_CLOCK_OBJECT_ID),
],
});
const [tokenBridgeMessage] = tx.moveCall({
target: `${tokenBridgePackageId}::vaa::verify_only_once`,
arguments: [tx.object(tokenBridgeStateObjectId), verifiedVAA],
});
const [relayerReceipt] = tx.moveCall({
target: `${tokenBridgePackageId}::complete_transfer::authorize_transfer`,
arguments: [tx.object(tokenBridgeStateObjectId), tokenBridgeMessage],
typeArguments: [coinType],
});
const [coins] = tx.moveCall({
target: `${tokenBridgePackageId}::complete_transfer::redeem_relayer_payout`,
arguments: [relayerReceipt],
typeArguments: [coinType],
});
tx.moveCall({
target: `${tokenBridgePackageId}::coin_utils::return_nonzero`,
arguments: [coins],
typeArguments: [coinType],
});
return tx;
}