-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathindex.ts
439 lines (427 loc) · 18.1 KB
/
index.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
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
import type { TransferTransaction } from '@hashgraph/sdk';
import { isHex } from 'viem';
import { EvmContractHandler } from './helpers/evmContractHandler.js';
import { createHashportStore } from './helpers/hashportTransactionStore.js';
import { HederaTxFactory } from './helpers/hederaTxFactory.js';
import { HashportApiClient } from 'clients/hashportApiClient/index.js';
import { MirrorNodeClient } from 'clients/mirrorNodeClient/index.js';
import { erc20ABI, erc721ABI } from 'constants/abi.js';
import { HashportTransactionData, HashportTransactionState } from 'types/state';
import { BridgeParams, EvmBridgeStep, HederaBridgeStep, PollBridgeStep } from 'types/api/bridge';
import { HashportClientConfig } from 'types/clients';
import { EvmSigner } from 'types/signers/evmSigner';
import { HederaSigner } from 'types/signers/hederaSigner';
import { ValidatorPollResponse } from 'types/validator';
import { assertHederaTokenId, assertHexString } from 'utils/assert.js';
import { sleep } from 'utils/async.js';
import { HashportError } from 'utils/error.js';
import { Fetcher } from 'utils/fetch.js';
import { formatPollingId, formatTransactionId, formatUrl } from 'utils/formatters.js';
import { Logger } from 'utils/logger.js';
/**
* Initializes a client for validating and executing bridging operations on hashport.
*/
export class HashportClient {
protected logger: Logger;
apiClient: HashportApiClient;
mirrorNodeClient: MirrorNodeClient;
mode: 'mainnet' | 'testnet';
evmSigner: EvmSigner;
hederaSigner: HederaSigner;
transactionStore: ReturnType<ReturnType<typeof createHashportStore>['getState']>;
subscribe: ReturnType<typeof createHashportStore>['subscribe'];
constructor({
evmSigner,
hederaSigner,
mode = 'mainnet',
customMirrorNodeCredentials,
customMirrorNodeUrl,
debug = false,
persistOptions,
}: HashportClientConfig) {
this.evmSigner = evmSigner;
this.hederaSigner = hederaSigner;
this.mode = mode;
this.apiClient = new HashportApiClient(mode);
const store = createHashportStore(persistOptions);
this.transactionStore = store.getState();
this.subscribe = store.subscribe;
this.mirrorNodeClient = new MirrorNodeClient(
mode,
customMirrorNodeUrl,
customMirrorNodeCredentials,
);
this.logger = new Logger(debug);
}
/**
* @private
* Checks that the user has sufficient fungible token balance for the transaction.
*/
private async checkFungibleBalances(sourceAsset: string, amount: bigint) {
let isSufficientBalance: boolean;
if (isHex(sourceAsset)) {
const sourceAssetContract = this.evmSigner.getContract(erc20ABI, sourceAsset);
const [balance] = await sourceAssetContract.read<[bigint]>([
{ functionName: 'balanceOf', args: [this.evmSigner.getAddress()] },
]);
isSufficientBalance = balance >= amount;
} else {
const hederaAsset = assertHederaTokenId(sourceAsset);
const { balances } = await this.mirrorNodeClient.getBalances({
['account.id']: this.hederaSigner.accountId,
});
const balance =
hederaAsset === 'HBAR'
? balances[0].balance
: balances[0].tokens.find(({ token_id }) => token_id === sourceAsset)?.balance;
isSufficientBalance = !!balance && balance >= amount;
}
if (!isSufficientBalance) {
throw new HashportError(`Insufficient ${sourceAsset} balance`, 'PORTING_EXECUTION');
}
}
/**
* @private
* Checks the ownership of the given NFT serial.
*/
private async checkNftOwnership(sourceAsset: string, tokenIdOrSerial: string) {
if (isHex(sourceAsset)) {
const sourceAssetContract = this.evmSigner.getContract(erc721ABI, sourceAsset);
const [owner] = await sourceAssetContract.read<[`0x${string}`]>([
{
functionName: 'ownerOf',
args: [BigInt(tokenIdOrSerial)],
},
]);
const evmAccount = this.evmSigner.getAddress();
const isOwner = owner.toLowerCase() === evmAccount.toLowerCase();
if (!isOwner) {
throw new HashportError(
`${evmAccount} does not own ${sourceAsset} #${tokenIdOrSerial}`,
'PORTING_EXECUTION',
);
}
} else {
const nftInfo = await this.mirrorNodeClient.getNftInfoBySerial(
sourceAsset,
tokenIdOrSerial,
);
const hederaAccount = this.hederaSigner.accountId;
const isOwner = nftInfo.account_id === hederaAccount;
if (!isOwner) {
throw new HashportError(
`${hederaAccount} does not own ${sourceAsset} #${tokenIdOrSerial}`,
'PORTING_EXECUTION',
);
}
}
}
/**
* @private
* Verifies that the user has the proper fungible balance or NFT serial ownership.
*/
private async verifyBalances(params: BridgeParams) {
if (params.amount) {
// Fungible Transactions
await this.checkFungibleBalances(params.sourceAssetId, BigInt(params.amount));
} else if (params.tokenId) {
// Nonfungible Transactions
await this.checkNftOwnership(params.sourceAssetId, params.tokenId);
}
}
/**
* Validates and queues a hashport bridging transaction.
*/
async queueTransaction(params: BridgeParams): Promise<string> {
const isValid = await this.apiClient.validateBridgeParams(params);
if (!isValid.valid) {
throw new HashportError(isValid.message, 'INVALID_PARAMS');
}
await this.verifyBalances(params);
const steps = await this.apiClient.bridge(params);
const transactionStateId = this.transactionStore.queueTransaction(params, steps);
return transactionStateId;
}
/**
* Executes all the necessary contract calls for a given hashport transaction.
* @param {string} id The internal id of the hashport transaction
* @returns {Promise<Object>} The final state of the completed transactions. Useful for
* building a confirmation receipt.
*/
async execute(id: string): Promise<HashportTransactionData['state']> {
const { steps, params } = this.transactionStore.getTransactionData(id);
if (
params.recipient.toLowerCase() !== this.evmSigner.getAddress().toLowerCase() &&
params.recipient !== this.hederaSigner.accountId
) {
throw new HashportError(
'Recipient does not match any connected account',
'INVALID_PARAMS',
);
}
const requiresBalanceCheck = steps.some(
step =>
'amount' in step ||
// Represents burnERC721
(step.type === 'evm' && params.tokenId && !isHex(params.recipient)),
);
if (requiresBalanceCheck) {
await this.verifyBalances(params);
}
for (const step of steps) {
let result: HashportTransactionState;
switch (step.type) {
case 'evm': {
result = await this.handleEvmStep(step, id);
break;
}
case 'Hedera': {
result = await this.handleHederaStep(step, id);
break;
}
case 'poll': {
result = await this.handlePollStep(step, id);
break;
}
default: {
throw new HashportError('Invalid step', 'INVALID_STATE');
}
}
this.transactionStore.updateTransactionState(id, result, true);
}
const { state } = this.transactionStore.getTransactionData(id);
if (!state.confirmationTransactionHashOrId) {
throw new HashportError('Missing confirmation hash/id', 'INVALID_STATE');
}
const receiptCopy = { ...state };
this.transactionStore.deleteTransaction(id);
return receiptCopy;
}
/**
* Executes all the hashport transaction that are in the queue. Returns completed state from
* each transaction.
*/
async executeAll() {
if (this.transactionStore.queue.size === 0) {
throw new HashportError('No transactions have been queued.', 'INVALID_STATE');
}
const completedTransactions: HashportTransactionData['state'][] = [];
for (const [id] of this.transactionStore.queue) {
try {
const confirmation = await this.execute(id);
completedTransactions.push(confirmation);
} catch (error) {
this.logger.error(error);
}
}
return completedTransactions;
}
/**
* @private
* Checks token association for a given Hedera token. If the token is not associated,
* the token will be associated automatically. Returns null if the token is already associated,
* or the transaction id of the successful association transaction.
*/
private async handleTokenAssociation(id: string, tokenId: string) {
if (tokenId === 'HBAR') return null;
const hederaTokenId = assertHederaTokenId(tokenId);
const hederaTxFactory = new HederaTxFactory(this.hederaSigner.accountId);
const isAssociated = await this.mirrorNodeClient.checkTokenAssociation(
this.hederaSigner.accountId,
hederaTokenId,
);
if (!isAssociated) {
this.transactionStore.updateTransactionState(id, {
tokenAssociationStatus: 'ASSOCIATING',
});
const associateTx = hederaTxFactory.createAssociation([hederaTokenId]);
const associateRx = await this.hederaSigner.associateToken(associateTx);
// Confirm association with exponential backoff
for (let i = 0; i < 5; i++) {
try {
const transactionsResult = await this.mirrorNodeClient.getTransactions({
transactionId: formatTransactionId(associateRx.transactionId),
});
const successfulTx = transactionsResult?.transactions?.find(
({ name, result }) => name === 'TOKENASSOCIATE' && result === 'SUCCESS',
);
if (successfulTx) {
return successfulTx.transaction_id;
}
} catch (error) {
this.logger.error(error);
}
await sleep(2 ** i * 1000);
}
throw new HashportError('Failed to confirm token association', 'PORTING_EXECUTION');
}
return null;
}
/**
* @private
* Executes steps related to the Hedera network: token association and checks, deposit
* transactions, NFT approvals, and NFT transfers.
*/
private async handleHederaStep(
step: HederaBridgeStep,
id: string,
): Promise<HashportTransactionState> {
const hederaTxFactory = new HederaTxFactory(this.hederaSigner.accountId);
if (step.tokenId && step.target === 'AccountBalanceQuery') {
const associationResult = await this.handleTokenAssociation(id, step.tokenId);
return { tokenAssociationStatus: associationResult || 'ALREADY_ASSOCIATED' };
} else if (step.amount) {
const transactionData = this.transactionStore.getTransactionData(id);
if (!transactionData) {
throw new HashportError(`Missing porting state: ${id}`, 'INVALID_STATE');
}
const { state, params } = transactionData;
if (state?.hederaDepositTransactionId && state?.validatorPollingId) {
const { hederaDepositTransactionId, validatorPollingId } = state;
return { hederaDepositTransactionId, validatorPollingId };
}
let transferTx: TransferTransaction;
if (params.tokenId) {
// NFT Deposit Transaction
transferTx = hederaTxFactory.createNftFeeTransfer(
step.target,
step.amount,
step.memo,
step.feeTransfers || [],
);
} else {
// FT Deposit Transaction
const { sourceAssetId } = params;
if (isHex(sourceAssetId))
throw new HashportError(
`Invalid source asset for hedera transaction: ${sourceAssetId}`,
'INVALID_STATE',
);
transferTx = hederaTxFactory.createTransfer(
step.target,
step.amount,
step.memo,
assertHederaTokenId(sourceAssetId),
);
}
const transferResult = await this.hederaSigner.transfer(transferTx);
const txId = formatTransactionId(transferResult.transactionId);
return {
hederaDepositTransactionId: txId,
validatorPollingId: txId,
};
} else if (
step.spender &&
step.tokenId &&
step.serialNumber &&
step.target === 'CryptoApproveAllowance'
) {
const approveNft = hederaTxFactory.createNftApproval(
step.spender,
step.tokenId,
step.serialNumber,
);
const transferResult = await this.hederaSigner.approveNftAllowance(approveNft);
const txId = formatTransactionId(transferResult.transactionId);
return {
nftApprovalTransactionId: txId,
};
} else if (step.tokenId && step.target && step.receiver && step.serialNumber) {
const nftTransfer = hederaTxFactory.createNftTransfer(
step.tokenId,
step.serialNumber,
step.target,
);
const transferResult = await this.hederaSigner.transferApprovedNft(nftTransfer);
return {
confirmationTransactionHashOrId: transferResult.transactionId.toString(),
};
} else {
throw new HashportError('Invalid Hedera step', 'INVALID_STATE');
}
}
/**
* @private
* Executes contract methods on the router contract.
*/
private async handleEvmStep(
step: EvmBridgeStep,
id: string,
): Promise<HashportTransactionState> {
const transactionData = this.transactionStore.getTransactionData(id);
const {
params: { sourceAssetId, sourceNetworkId, targetNetworkId },
} = transactionData;
// Check for Hedera token association
if (isHex(sourceAssetId)) {
const sourceAsset = await this.apiClient.networkAssets(
parseInt(sourceNetworkId),
sourceAssetId,
);
const targetAsset = sourceAsset?.bridgeableNetworks?.[targetNetworkId]?.wrappedAsset;
if (targetAsset) await this.handleTokenAssociation(id, targetAsset);
}
const contractHandler = new EvmContractHandler(this.evmSigner, step, transactionData);
const { result, confirmations } = await contractHandler.execute();
const { key, value, hash } = result;
if (hash) {
this.transactionStore.updateTransactionState(id, { [key]: hash });
const receipt = await this.evmSigner.waitForTransaction(hash, confirmations);
if (confirmations) {
const routerContractAddress = assertHexString(step.target);
const pollingId = formatPollingId(receipt, routerContractAddress);
return {
[key]: receipt.transactionHash,
validatorPollingId: pollingId,
};
} else {
return { [key]: receipt.transactionHash };
}
} else {
return { [key]: value };
}
}
/**
* @private
* Queries the validators to either get transaction confirmation or to get the signatures
* required to complete a bridging operation.
*/
private async handlePollStep(
step: PollBridgeStep,
id: string,
): Promise<HashportTransactionState> {
const { validatorPollingId } = this.transactionStore.getTransactionData(id).state;
if (!validatorPollingId) throw new HashportError('Missing polling id', 'INVALID_STATE');
const pollingUrl = formatUrl(step.target, validatorPollingId);
const POLLING_INTERVAL = step.polling * 1000;
const THREE_MINUTES = 180000 / POLLING_INTERVAL;
for (let i = 0; i < THREE_MINUTES; i++) {
try {
const validatorResponse = await new Fetcher<ValidatorPollResponse | string>(
pollingUrl,
);
if (typeof validatorResponse === `string`) {
const result = await this.mirrorNodeClient.getTransactions({
transactionId: validatorResponse,
});
const successfulTx = result.transactions.find(
({ name, result }) =>
(name === 'CRYPTOTRANSFER' || name === 'CRYPTOAPPROVEALLOWANCE') &&
result === 'SUCCESS',
);
if (successfulTx) {
return {
confirmationTransactionHashOrId: validatorResponse,
};
}
} else if (validatorResponse?.majority) {
return { validatorPollResult: validatorResponse };
}
} catch (error) {
this.logger.error(error);
}
await sleep(POLLING_INTERVAL);
}
throw new HashportError('Polling timeout', 'PORTING_EXECUTION');
}
}