-
Notifications
You must be signed in to change notification settings - Fork 652
/
getIsTransferCompleted.ts
316 lines (297 loc) · 9.09 KB
/
getIsTransferCompleted.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
import { JsonRpcProvider } from "@mysten/sui.js";
import { Commitment, Connection, PublicKeyInitData } from "@solana/web3.js";
import { LCDClient } from "@terra-money/terra.js";
import { LCDClient as XplaLCDClient } from "@xpla/xpla.js";
import { Algodv2, bigIntToBytes } from "algosdk";
import { AptosClient } from "aptos";
import axios from "axios";
import { ethers } from "ethers";
import { fromUint8Array } from "js-base64";
import { Provider } from "near-api-js/lib/providers";
import { redeemOnTerra } from ".";
import { TERRA_REDEEMED_CHECK_WALLET_ADDRESS, ensureHexPrefix } from "..";
import {
BITS_PER_KEY,
MAX_BITS,
_parseVAAAlgorand,
calcLogicSigAccount,
} from "../algorand";
import { TokenBridgeState } from "../aptos/types";
import { getSignedVAAHash } from "../bridge";
import { Bridge__factory } from "../ethers-contracts";
import { getClaim } from "../solana/wormhole";
import { getObjectFields, getTableKeyType } from "../sui/utils";
import { safeBigIntToNumber } from "../utils/bigint";
import { callFunctionNear } from "../utils/near";
import { SignedVaa, parseVaa } from "../vaa/wormhole";
export async function getIsTransferCompletedEth(
tokenBridgeAddress: string,
provider: ethers.Signer | ethers.providers.Provider,
signedVAA: Uint8Array
): Promise<boolean> {
const tokenBridge = Bridge__factory.connect(tokenBridgeAddress, provider);
const signedVAAHash = getSignedVAAHash(signedVAA);
return await tokenBridge.isTransferCompleted(signedVAAHash);
}
// Note: this function is the legacy implementation for terra classic. New
// cosmwasm sdk functions should instead be based on
// `getIsTransferCompletedTerra2`.
export async function getIsTransferCompletedTerra(
tokenBridgeAddress: string,
signedVAA: Uint8Array,
client: LCDClient,
gasPriceUrl: string
): Promise<boolean> {
const msg = await redeemOnTerra(
tokenBridgeAddress,
TERRA_REDEEMED_CHECK_WALLET_ADDRESS,
signedVAA
);
// TODO: remove gasPriceUrl and just use the client's gas prices
const gasPrices = await axios.get(gasPriceUrl).then((result) => result.data);
const account = await client.auth.accountInfo(
TERRA_REDEEMED_CHECK_WALLET_ADDRESS
);
try {
await client.tx.estimateFee(
[
{
sequenceNumber: account.getSequenceNumber(),
publicKey: account.getPublicKey(),
},
],
{
msgs: [msg],
memo: "already redeemed calculation",
feeDenoms: ["uluna"],
gasPrices,
}
);
} catch (e: any) {
// redeemed if the VAA was already executed
if (e.response.data.message.includes("VaaAlreadyExecuted")) {
return true;
} else {
throw e;
}
}
return false;
}
/**
* This function is used to check if a VAA has been redeemed on terra2 by
* querying the token bridge contract.
* @param tokenBridgeAddress The token bridge address (bech32)
* @param signedVAA The signed VAA byte array
* @param client The LCD client. Only used for querying, not transactions will
* be signed
*/
export async function getIsTransferCompletedTerra2(
tokenBridgeAddress: string,
signedVAA: Uint8Array,
client: LCDClient
): Promise<boolean> {
const result: { is_redeemed: boolean } = await client.wasm.contractQuery(
tokenBridgeAddress,
{
is_vaa_redeemed: {
vaa: fromUint8Array(signedVAA),
},
}
);
return result.is_redeemed;
}
export async function getIsTransferCompletedXpla(
tokenBridgeAddress: string,
signedVAA: Uint8Array,
client: XplaLCDClient
): Promise<boolean> {
const result: { is_redeemed: boolean } = await client.wasm.contractQuery(
tokenBridgeAddress,
{
is_vaa_redeemed: {
vaa: fromUint8Array(signedVAA),
},
}
);
return result.is_redeemed;
}
export async function getIsTransferCompletedSolana(
tokenBridgeAddress: PublicKeyInitData,
signedVAA: SignedVaa,
connection: Connection,
commitment?: Commitment
): Promise<boolean> {
const parsed = parseVaa(signedVAA);
return getClaim(
connection,
tokenBridgeAddress,
parsed.emitterAddress,
parsed.emitterChain,
parsed.sequence,
commitment
).catch((e) => false);
}
// Algorand
/**
* This function is used to check if a VAA has been redeemed by looking at a specific bit.
* @param client AlgodV2 client
* @param appId Application Id
* @param addr Wallet address. Someone has to pay for this.
* @param seq The sequence number of the redemption
* @returns true, if the bit was set and VAA was redeemed, false otherwise.
*/
async function checkBitsSet(
client: Algodv2,
appId: bigint,
addr: string,
seq: bigint
): Promise<boolean> {
let retval: boolean = false;
let appState: any[] = [];
const acctInfo = await client.accountInformation(addr).do();
const als = acctInfo["apps-local-state"];
als.forEach((app: any) => {
if (BigInt(app["id"]) === appId) {
appState = app["key-value"];
}
});
if (appState.length === 0) {
return retval;
}
const BIG_MAX_BITS: bigint = BigInt(MAX_BITS);
const BIG_EIGHT: bigint = BigInt(8);
// Start on a MAX_BITS boundary
const start: bigint = (seq / BIG_MAX_BITS) * BIG_MAX_BITS;
// beg should be in the range [0..MAX_BITS]
const beg: number = safeBigIntToNumber(seq - start);
// s should be in the range [0..15]
const s: number = Math.floor(beg / BITS_PER_KEY);
const b: number = Math.floor((beg - s * BITS_PER_KEY) / 8);
const key = Buffer.from(bigIntToBytes(s, 1)).toString("base64");
appState.forEach((kv) => {
if (kv["key"] === key) {
const v = Buffer.from(kv["value"]["bytes"], "base64");
const bt = 1 << safeBigIntToNumber(seq % BIG_EIGHT);
retval = (v[b] & bt) != 0;
return;
}
});
return retval;
}
/**
* <p>Returns true if this transfer was completed on Algorand</p>
* @param client AlgodV2 client
* @param appId Most likely the Token bridge ID
* @param signedVAA VAA to check
* @returns true if VAA has been redeemed, false otherwise
*/
export async function getIsTransferCompletedAlgorand(
client: Algodv2,
appId: bigint,
signedVAA: Uint8Array
): Promise<boolean> {
const parsedVAA = _parseVAAAlgorand(signedVAA);
const seq: bigint = parsedVAA.sequence;
const chainRaw: string = parsedVAA.chainRaw; // this needs to be a hex string
const em: string = parsedVAA.emitter; // this needs to be a hex string
const { doesExist, lsa } = await calcLogicSigAccount(
client,
appId,
seq / BigInt(MAX_BITS),
chainRaw + em
);
if (!doesExist) {
return false;
}
const seqAddr = lsa.address();
const retVal: boolean = await checkBitsSet(client, appId, seqAddr, seq);
return retVal;
}
export async function getIsTransferCompletedNear(
provider: Provider,
tokenBridge: string,
signedVAA: Uint8Array
): Promise<boolean> {
const vaa = Buffer.from(signedVAA).toString("hex");
return (
await callFunctionNear(provider, tokenBridge, "is_transfer_completed", {
vaa,
})
)[1];
}
/**
* Determine whether or not the transfer in the given VAA has completed on Aptos.
* @param client Client used to transfer data to/from Aptos node
* @param tokenBridgeAddress Address of token bridge
* @param transferVAA Bytes of transfer VAA
* @returns True if transfer is completed
*/
export async function getIsTransferCompletedAptos(
client: AptosClient,
tokenBridgeAddress: string,
transferVAA: Uint8Array
): Promise<boolean> {
// get handle
tokenBridgeAddress = ensureHexPrefix(tokenBridgeAddress);
const state = (
await client.getAccountResource(
tokenBridgeAddress,
`${tokenBridgeAddress}::state::State`
)
).data as TokenBridgeState;
const handle = state.consumed_vaas.elems.handle;
// check if vaa hash is in consumed_vaas
const transferVAAHash = getSignedVAAHash(transferVAA);
try {
// when accessing Set<T>, key is type T and value is 0
await client.getTableItem(handle, {
key_type: "vector<u8>",
value_type: "u8",
key: transferVAAHash,
});
return true;
} catch {
return false;
}
}
export async function getIsTransferCompletedSui(
provider: JsonRpcProvider,
tokenBridgeStateObjectId: string,
transferVAA: Uint8Array
): Promise<boolean> {
const tokenBridgeStateFields = await getObjectFields(
provider,
tokenBridgeStateObjectId
);
if (!tokenBridgeStateFields) {
throw new Error("Unable to fetch object fields from token bridge state");
}
const hashes = tokenBridgeStateFields.consumed_vaas?.fields?.hashes;
const tableObjectId = hashes?.fields?.items?.fields?.id?.id;
if (!tableObjectId) {
throw new Error("Unable to fetch consumed VAAs table");
}
const keyType = getTableKeyType(hashes?.fields?.items?.type);
if (!keyType) {
throw new Error("Unable to get key type");
}
const hash = getSignedVAAHash(transferVAA);
const response = await provider.getDynamicFieldObject({
parentId: tableObjectId,
name: {
type: keyType,
value: {
data: [...Buffer.from(hash.slice(2), "hex")],
},
},
});
if (!response.error) {
return true;
}
if (response.error.code === "dynamicFieldNotFound") {
return false;
}
throw new Error(
`Unexpected getDynamicFieldObject response ${response.error}`
);
}