@ledgerhq/coin-sui 0.9.0 → 0.10.0-nightly.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.turbo/turbo-build.log +1 -1
- package/.unimportedrc.json +1 -1
- package/CHANGELOG.md +26 -0
- package/lib/api/index.d.ts.map +1 -1
- package/lib/api/index.integration.test.js +21 -2
- package/lib/api/index.integration.test.js.map +1 -1
- package/lib/api/index.js +2 -0
- package/lib/api/index.js.map +1 -1
- package/lib/api/index.test.js +37 -0
- package/lib/api/index.test.js.map +1 -1
- package/lib/bridge/buildTransaction.d.ts +1 -3
- package/lib/bridge/buildTransaction.d.ts.map +1 -1
- package/lib/bridge/buildTransaction.integration.test.js +17 -35
- package/lib/bridge/buildTransaction.integration.test.js.map +1 -1
- package/lib/bridge/buildTransaction.js +10 -21
- package/lib/bridge/buildTransaction.js.map +1 -1
- package/lib/bridge/buildTransaction.test.js +33 -130
- package/lib/bridge/buildTransaction.test.js.map +1 -1
- package/lib/bridge/getFeesForTransaction.d.ts.map +1 -1
- package/lib/bridge/getFeesForTransaction.js +5 -1
- package/lib/bridge/getFeesForTransaction.js.map +1 -1
- package/lib/logic/craftTransaction.d.ts +2 -2
- package/lib/logic/craftTransaction.d.ts.map +1 -1
- package/lib/logic/craftTransaction.integration.test.d.ts +2 -0
- package/lib/logic/craftTransaction.integration.test.d.ts.map +1 -0
- package/lib/logic/craftTransaction.integration.test.js +56 -0
- package/lib/logic/craftTransaction.integration.test.js.map +1 -0
- package/lib/logic/craftTransaction.js +2 -2
- package/lib/logic/craftTransaction.js.map +1 -1
- package/lib/logic/estimateFees.d.ts +1 -1
- package/lib/logic/estimateFees.d.ts.map +1 -1
- package/lib/logic/estimateFees.integration.test.d.ts +2 -0
- package/lib/logic/estimateFees.integration.test.d.ts.map +1 -0
- package/lib/logic/estimateFees.integration.test.js +79 -0
- package/lib/logic/estimateFees.integration.test.js.map +1 -0
- package/lib/logic/estimateFees.js +7 -2
- package/lib/logic/estimateFees.js.map +1 -1
- package/lib/logic/getBalance.d.ts.map +1 -1
- package/lib/logic/getBalance.integration.test.d.ts +2 -0
- package/lib/logic/getBalance.integration.test.d.ts.map +1 -0
- package/lib/logic/getBalance.integration.test.js +56 -0
- package/lib/logic/getBalance.integration.test.js.map +1 -0
- package/lib/logic/getBalance.js +18 -7
- package/lib/logic/getBalance.js.map +1 -1
- package/lib/logic/getBalance.test.js +49 -7
- package/lib/logic/getBalance.test.js.map +1 -1
- package/lib/logic/index.d.ts +1 -0
- package/lib/logic/index.d.ts.map +1 -1
- package/lib/logic/index.js +4 -1
- package/lib/logic/index.js.map +1 -1
- package/lib/logic/staking.d.ts +4 -0
- package/lib/logic/staking.d.ts.map +1 -0
- package/lib/logic/staking.js +36 -0
- package/lib/logic/staking.js.map +1 -0
- package/lib/network/index.d.ts +6 -6
- package/lib/network/index.d.ts.map +1 -1
- package/lib/network/index.js +9 -3
- package/lib/network/index.js.map +1 -1
- package/lib/network/sdk.d.ts +27 -19
- package/lib/network/sdk.d.ts.map +1 -1
- package/lib/network/sdk.integration.test.js +21 -22
- package/lib/network/sdk.integration.test.js.map +1 -1
- package/lib/network/sdk.js +113 -68
- package/lib/network/sdk.js.map +1 -1
- package/lib/network/sdk.test.js +148 -65
- package/lib/network/sdk.test.js.map +1 -1
- package/lib/test/testUtils.d.ts +2 -0
- package/lib/test/testUtils.d.ts.map +1 -0
- package/lib/test/testUtils.js +35 -0
- package/lib/test/testUtils.js.map +1 -0
- package/lib-es/api/index.d.ts.map +1 -1
- package/lib-es/api/index.integration.test.js +21 -2
- package/lib-es/api/index.integration.test.js.map +1 -1
- package/lib-es/api/index.js +3 -1
- package/lib-es/api/index.js.map +1 -1
- package/lib-es/api/index.test.js +37 -0
- package/lib-es/api/index.test.js.map +1 -1
- package/lib-es/bridge/buildTransaction.d.ts +1 -3
- package/lib-es/bridge/buildTransaction.d.ts.map +1 -1
- package/lib-es/bridge/buildTransaction.integration.test.js +13 -31
- package/lib-es/bridge/buildTransaction.integration.test.js.map +1 -1
- package/lib-es/bridge/buildTransaction.js +9 -16
- package/lib-es/bridge/buildTransaction.js.map +1 -1
- package/lib-es/bridge/buildTransaction.test.js +34 -131
- package/lib-es/bridge/buildTransaction.test.js.map +1 -1
- package/lib-es/bridge/getFeesForTransaction.d.ts.map +1 -1
- package/lib-es/bridge/getFeesForTransaction.js +5 -1
- package/lib-es/bridge/getFeesForTransaction.js.map +1 -1
- package/lib-es/logic/craftTransaction.d.ts +2 -2
- package/lib-es/logic/craftTransaction.d.ts.map +1 -1
- package/lib-es/logic/craftTransaction.integration.test.d.ts +2 -0
- package/lib-es/logic/craftTransaction.integration.test.d.ts.map +1 -0
- package/lib-es/logic/craftTransaction.integration.test.js +51 -0
- package/lib-es/logic/craftTransaction.integration.test.js.map +1 -0
- package/lib-es/logic/craftTransaction.js +2 -2
- package/lib-es/logic/craftTransaction.js.map +1 -1
- package/lib-es/logic/estimateFees.d.ts +1 -1
- package/lib-es/logic/estimateFees.d.ts.map +1 -1
- package/lib-es/logic/estimateFees.integration.test.d.ts +2 -0
- package/lib-es/logic/estimateFees.integration.test.d.ts.map +1 -0
- package/lib-es/logic/estimateFees.integration.test.js +74 -0
- package/lib-es/logic/estimateFees.integration.test.js.map +1 -0
- package/lib-es/logic/estimateFees.js +7 -2
- package/lib-es/logic/estimateFees.js.map +1 -1
- package/lib-es/logic/getBalance.d.ts.map +1 -1
- package/lib-es/logic/getBalance.integration.test.d.ts +2 -0
- package/lib-es/logic/getBalance.integration.test.d.ts.map +1 -0
- package/lib-es/logic/getBalance.integration.test.js +51 -0
- package/lib-es/logic/getBalance.integration.test.js.map +1 -0
- package/lib-es/logic/getBalance.js +19 -8
- package/lib-es/logic/getBalance.js.map +1 -1
- package/lib-es/logic/getBalance.test.js +50 -8
- package/lib-es/logic/getBalance.test.js.map +1 -1
- package/lib-es/logic/index.d.ts +1 -0
- package/lib-es/logic/index.d.ts.map +1 -1
- package/lib-es/logic/index.js +1 -0
- package/lib-es/logic/index.js.map +1 -1
- package/lib-es/logic/staking.d.ts +4 -0
- package/lib-es/logic/staking.d.ts.map +1 -0
- package/lib-es/logic/staking.js +8 -0
- package/lib-es/logic/staking.js.map +1 -0
- package/lib-es/network/index.d.ts +6 -6
- package/lib-es/network/index.d.ts.map +1 -1
- package/lib-es/network/index.js +6 -3
- package/lib-es/network/index.js.map +1 -1
- package/lib-es/network/sdk.d.ts +27 -19
- package/lib-es/network/sdk.d.ts.map +1 -1
- package/lib-es/network/sdk.integration.test.js +22 -23
- package/lib-es/network/sdk.integration.test.js.map +1 -1
- package/lib-es/network/sdk.js +106 -64
- package/lib-es/network/sdk.js.map +1 -1
- package/lib-es/network/sdk.test.js +148 -65
- package/lib-es/network/sdk.test.js.map +1 -1
- package/lib-es/test/testUtils.d.ts +2 -0
- package/lib-es/test/testUtils.d.ts.map +1 -0
- package/lib-es/test/testUtils.js +31 -0
- package/lib-es/test/testUtils.js.map +1 -0
- package/package.json +7 -7
- package/src/api/index.integration.test.ts +24 -2
- package/src/api/index.test.ts +40 -0
- package/src/api/index.ts +4 -0
- package/src/bridge/buildTransaction.integration.test.ts +14 -39
- package/src/bridge/buildTransaction.test.ts +37 -160
- package/src/bridge/buildTransaction.ts +12 -21
- package/src/bridge/getFeesForTransaction.ts +6 -1
- package/src/logic/craftTransaction.integration.test.ts +63 -0
- package/src/logic/craftTransaction.ts +4 -4
- package/src/logic/estimateFees.integration.test.ts +89 -0
- package/src/logic/estimateFees.ts +7 -1
- package/src/logic/getBalance.integration.test.ts +66 -0
- package/src/logic/getBalance.test.ts +58 -8
- package/src/logic/getBalance.ts +24 -8
- package/src/logic/index.ts +1 -0
- package/src/logic/staking.ts +10 -0
- package/src/network/index.ts +12 -3
- package/src/network/sdk.integration.test.ts +25 -22
- package/src/network/sdk.test.ts +186 -77
- package/src/network/sdk.ts +149 -93
- package/src/test/testUtils.ts +38 -0
package/src/network/sdk.ts
CHANGED
|
@@ -1,16 +1,18 @@
|
|
|
1
1
|
import {
|
|
2
|
+
BalanceChange,
|
|
2
3
|
Checkpoint,
|
|
3
4
|
ExecuteTransactionBlockParams,
|
|
4
5
|
PaginatedTransactionResponse,
|
|
6
|
+
QueryTransactionBlocksParams,
|
|
5
7
|
SuiCallArg,
|
|
6
8
|
SuiClient,
|
|
9
|
+
SuiHTTPTransport,
|
|
7
10
|
SuiTransactionBlockResponse,
|
|
11
|
+
SuiTransactionBlockResponseOptions,
|
|
12
|
+
DelegatedStake,
|
|
13
|
+
StakeObject,
|
|
8
14
|
TransactionBlockData,
|
|
9
|
-
SuiHTTPTransport,
|
|
10
15
|
TransactionEffects,
|
|
11
|
-
QueryTransactionBlocksParams,
|
|
12
|
-
BalanceChange,
|
|
13
|
-
SuiTransactionBlockResponseOptions,
|
|
14
16
|
} from "@mysten/sui/client";
|
|
15
17
|
import { Transaction } from "@mysten/sui/transactions";
|
|
16
18
|
import { BigNumber } from "bignumber.js";
|
|
@@ -20,6 +22,8 @@ import type {
|
|
|
20
22
|
BlockTransaction,
|
|
21
23
|
BlockOperation,
|
|
22
24
|
Operation as Op,
|
|
25
|
+
Stake,
|
|
26
|
+
StakeState,
|
|
23
27
|
AssetInfo,
|
|
24
28
|
} from "@ledgerhq/coin-framework/api/index";
|
|
25
29
|
import type { Operation, OperationType } from "@ledgerhq/types-live";
|
|
@@ -81,45 +85,29 @@ export async function withApi<T>(execute: AsyncApiFunction<T>) {
|
|
|
81
85
|
return result;
|
|
82
86
|
}
|
|
83
87
|
|
|
84
|
-
export const getBalanceCached = makeLRUCache(
|
|
85
|
-
({ api, owner }: { api: SuiClient; owner: string }) => api.getBalance({ owner }),
|
|
86
|
-
(params: { api: SuiClient; owner: string }) => params.owner,
|
|
87
|
-
minutes(1),
|
|
88
|
-
);
|
|
89
|
-
|
|
90
88
|
export const getAllBalancesCached = makeLRUCache(
|
|
91
|
-
(
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
89
|
+
async (owner: string) =>
|
|
90
|
+
withApi(
|
|
91
|
+
async api =>
|
|
92
|
+
await api.getAllBalances({
|
|
93
|
+
owner,
|
|
94
|
+
}),
|
|
95
|
+
),
|
|
96
|
+
(owner: string) => owner,
|
|
96
97
|
minutes(1),
|
|
97
98
|
);
|
|
98
99
|
|
|
99
|
-
/**
|
|
100
|
-
* Get account balance
|
|
101
|
-
*/
|
|
102
|
-
export const getAccount = async (addr: string) =>
|
|
103
|
-
withApi(async api => {
|
|
104
|
-
const balance = await getBalanceCached({ api, owner: addr });
|
|
105
|
-
return {
|
|
106
|
-
blockHeight: BLOCK_HEIGHT * 2,
|
|
107
|
-
balance: BigNumber(balance.totalBalance),
|
|
108
|
-
};
|
|
109
|
-
});
|
|
110
|
-
|
|
111
100
|
/**
|
|
112
101
|
* Get account balance (native and tokens)
|
|
113
102
|
*/
|
|
114
|
-
export const getAccountBalances = async (addr: string) =>
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
});
|
|
103
|
+
export const getAccountBalances = async (addr: string) => {
|
|
104
|
+
const balances = await getAllBalancesCached(addr);
|
|
105
|
+
return balances.map(({ coinType, totalBalance }) => ({
|
|
106
|
+
coinType,
|
|
107
|
+
blockHeight: BLOCK_HEIGHT * 2,
|
|
108
|
+
balance: BigNumber(totalBalance),
|
|
109
|
+
}));
|
|
110
|
+
};
|
|
123
111
|
|
|
124
112
|
/**
|
|
125
113
|
* Returns true if account is the signer
|
|
@@ -493,75 +481,79 @@ const getTotalGasUsed = (effects?: TransactionEffects | null): bigint => {
|
|
|
493
481
|
);
|
|
494
482
|
};
|
|
495
483
|
|
|
496
|
-
|
|
497
|
-
|
|
498
|
-
|
|
499
|
-
|
|
484
|
+
/**
|
|
485
|
+
* Get coins for a given address and coin type, stopping when we have enough to cover the amount.
|
|
486
|
+
* Returns the minimum coins needed to cover the required amount.
|
|
487
|
+
*/
|
|
488
|
+
export const getCoinsForAmount = async (
|
|
489
|
+
api: SuiClient,
|
|
490
|
+
address: string,
|
|
491
|
+
coinType: string,
|
|
492
|
+
requiredAmount: number,
|
|
493
|
+
) => {
|
|
494
|
+
const coins = [];
|
|
495
|
+
let cursor = null;
|
|
496
|
+
let hasNextPage = true;
|
|
497
|
+
let totalBalance = 0;
|
|
498
|
+
|
|
499
|
+
while (hasNextPage && totalBalance < requiredAmount) {
|
|
500
|
+
const response = await api.getCoins({
|
|
501
|
+
owner: address,
|
|
502
|
+
coinType,
|
|
503
|
+
cursor,
|
|
504
|
+
});
|
|
500
505
|
|
|
501
|
-
|
|
502
|
-
|
|
503
|
-
|
|
504
|
-
|
|
505
|
-
|
|
506
|
-
|
|
507
|
-
|
|
508
|
-
|
|
509
|
-
|
|
510
|
-
|
|
511
|
-
|
|
512
|
-
|
|
513
|
-
const txb = await tx.build({ client: api });
|
|
514
|
-
const dryRunTxResponse = await api.dryRunTransactionBlock({ transactionBlock: txb });
|
|
515
|
-
const fees = getTotalGasUsed(dryRunTxResponse.effects);
|
|
516
|
-
|
|
517
|
-
return {
|
|
518
|
-
gasBudget: dryRunTxResponse.input.gasData.budget,
|
|
519
|
-
totalGasUsed: fees,
|
|
520
|
-
fees,
|
|
521
|
-
};
|
|
522
|
-
} catch (error) {
|
|
523
|
-
console.warn("Fee estimation failed:", error);
|
|
524
|
-
// If dry run fails return a reasonable default gas budget as fallback
|
|
525
|
-
return {
|
|
526
|
-
gasBudget: Array.isArray(coinObjects)
|
|
527
|
-
? FALLBACK_GAS_BUDGET.TOKEN_TRANSFER
|
|
528
|
-
: FALLBACK_GAS_BUDGET.SUI_TRANSFER,
|
|
529
|
-
totalGasUsed: BigInt(1000000),
|
|
530
|
-
fees: BigInt(1000000),
|
|
531
|
-
};
|
|
506
|
+
// Filter out zero-balance coins and sort by balance (largest first)
|
|
507
|
+
const validCoins = response.data
|
|
508
|
+
.filter(coin => parseInt(coin.balance) > 0)
|
|
509
|
+
.sort((a, b) => parseInt(b.balance) - parseInt(a.balance));
|
|
510
|
+
|
|
511
|
+
let currentBalance = totalBalance;
|
|
512
|
+
let i = 0;
|
|
513
|
+
while (i < validCoins.length && currentBalance < requiredAmount) {
|
|
514
|
+
const coin = validCoins[i];
|
|
515
|
+
coins.push(coin);
|
|
516
|
+
currentBalance += parseInt(coin.balance);
|
|
517
|
+
i++;
|
|
532
518
|
}
|
|
533
|
-
|
|
519
|
+
totalBalance = currentBalance;
|
|
534
520
|
|
|
535
|
-
|
|
536
|
-
|
|
537
|
-
|
|
538
|
-
) =>
|
|
539
|
-
withApi(async api => {
|
|
540
|
-
const coinObjectId = null;
|
|
521
|
+
cursor = response.nextCursor;
|
|
522
|
+
hasNextPage = response.hasNextPage && totalBalance < requiredAmount;
|
|
523
|
+
}
|
|
541
524
|
|
|
542
|
-
|
|
543
|
-
|
|
544
|
-
owner: address,
|
|
545
|
-
coinType: transaction.coinType,
|
|
546
|
-
});
|
|
547
|
-
return tokenInfo.data.map(coin => coin.coinObjectId);
|
|
548
|
-
}
|
|
549
|
-
return coinObjectId;
|
|
550
|
-
});
|
|
525
|
+
return coins;
|
|
526
|
+
};
|
|
551
527
|
|
|
528
|
+
/**
|
|
529
|
+
* Creates a Sui transaction block for transferring coins.
|
|
530
|
+
*
|
|
531
|
+
* @param address - The sender's address
|
|
532
|
+
* @param transaction - The transaction details including recipient, amount, and coin type
|
|
533
|
+
* @returns Promise<TransactionBlock> - A built transaction block ready for execution
|
|
534
|
+
*
|
|
535
|
+
*/
|
|
552
536
|
export const createTransaction = async (address: string, transaction: CreateExtrinsicArg) =>
|
|
553
537
|
withApi(async api => {
|
|
554
538
|
const tx = new Transaction();
|
|
555
539
|
tx.setSender(ensureAddressFormat(address));
|
|
556
540
|
|
|
557
|
-
|
|
541
|
+
if (transaction.coinType !== DEFAULT_COIN_TYPE) {
|
|
542
|
+
const requiredAmount = transaction.amount.toNumber();
|
|
558
543
|
|
|
559
|
-
|
|
560
|
-
|
|
561
|
-
if (coins.length
|
|
562
|
-
|
|
544
|
+
const coins = await getCoinsForAmount(api, address, transaction.coinType, requiredAmount);
|
|
545
|
+
|
|
546
|
+
if (coins.length === 0) {
|
|
547
|
+
throw new Error(`No coins found for type ${transaction.coinType}`);
|
|
563
548
|
}
|
|
564
|
-
|
|
549
|
+
|
|
550
|
+
const coinObjects = coins.map(coin => tx.object(coin.coinObjectId));
|
|
551
|
+
|
|
552
|
+
if (coinObjects.length > 1) {
|
|
553
|
+
tx.mergeCoins(coinObjects[0], coinObjects.slice(1));
|
|
554
|
+
}
|
|
555
|
+
|
|
556
|
+
const [coin] = tx.splitCoins(coinObjects[0], [transaction.amount.toNumber()]);
|
|
565
557
|
tx.transferObjects([coin], transaction.recipient);
|
|
566
558
|
} else {
|
|
567
559
|
const [coin] = tx.splitCoins(tx.gas, [transaction.amount.toNumber()]);
|
|
@@ -571,6 +563,22 @@ export const createTransaction = async (address: string, transaction: CreateExtr
|
|
|
571
563
|
return tx.build({ client: api });
|
|
572
564
|
});
|
|
573
565
|
|
|
566
|
+
/**
|
|
567
|
+
* Performs a dry run of a transaction to estimate gas costs and fees
|
|
568
|
+
*/
|
|
569
|
+
export const paymentInfo = async (sender: string, fakeTransaction: TransactionType) =>
|
|
570
|
+
withApi(async api => {
|
|
571
|
+
const txb = await createTransaction(sender, fakeTransaction);
|
|
572
|
+
const dryRunTxResponse = await api.dryRunTransactionBlock({ transactionBlock: txb });
|
|
573
|
+
const fees = getTotalGasUsed(dryRunTxResponse.effects);
|
|
574
|
+
|
|
575
|
+
return {
|
|
576
|
+
gasBudget: dryRunTxResponse.input.gasData.budget,
|
|
577
|
+
totalGasUsed: fees,
|
|
578
|
+
fees,
|
|
579
|
+
};
|
|
580
|
+
});
|
|
581
|
+
|
|
574
582
|
export const executeTransactionBlock = async (params: ExecuteTransactionBlockParams) =>
|
|
575
583
|
withApi(async api => {
|
|
576
584
|
return api.executeTransactionBlock(params);
|
|
@@ -670,3 +678,51 @@ export const queryTransactionsByDigest = async (params: {
|
|
|
670
678
|
|
|
671
679
|
return responses;
|
|
672
680
|
};
|
|
681
|
+
|
|
682
|
+
export const getStakes = (address: string): Promise<Stake[]> =>
|
|
683
|
+
withApi(async api =>
|
|
684
|
+
api
|
|
685
|
+
.getStakes({ owner: address })
|
|
686
|
+
.then(delegations => delegations.flatMap(delegation => toStakes(address, delegation))),
|
|
687
|
+
);
|
|
688
|
+
|
|
689
|
+
export const toStakes = (address: string, delegation: DelegatedStake): Stake[] =>
|
|
690
|
+
delegation.stakes.map(stake => {
|
|
691
|
+
const { deposited, rewarded } = toStakeAmounts(stake);
|
|
692
|
+
return {
|
|
693
|
+
uid: stake.stakedSuiId,
|
|
694
|
+
address: address,
|
|
695
|
+
delegate: delegation.validatorAddress,
|
|
696
|
+
state: toStakeState(stake.status),
|
|
697
|
+
asset: { type: "native" },
|
|
698
|
+
amount: deposited + rewarded,
|
|
699
|
+
amountDeposited: deposited,
|
|
700
|
+
amountRewarded: rewarded,
|
|
701
|
+
details: {
|
|
702
|
+
activeEpoch: Number(stake.stakeActiveEpoch),
|
|
703
|
+
requestEpoch: Number(stake.stakeRequestEpoch),
|
|
704
|
+
},
|
|
705
|
+
};
|
|
706
|
+
});
|
|
707
|
+
|
|
708
|
+
export const toStakeState = (status: "Pending" | "Active" | "Unstaked"): StakeState => {
|
|
709
|
+
switch (status) {
|
|
710
|
+
case "Pending":
|
|
711
|
+
return "activating";
|
|
712
|
+
case "Active":
|
|
713
|
+
return "active";
|
|
714
|
+
case "Unstaked":
|
|
715
|
+
return "inactive";
|
|
716
|
+
}
|
|
717
|
+
};
|
|
718
|
+
|
|
719
|
+
export const toStakeAmounts = (stake: StakeObject): { deposited: bigint; rewarded: bigint } => {
|
|
720
|
+
switch (stake.status) {
|
|
721
|
+
case "Pending":
|
|
722
|
+
return { deposited: BigInt(stake.principal), rewarded: 0n };
|
|
723
|
+
case "Active":
|
|
724
|
+
return { deposited: BigInt(stake.principal), rewarded: BigInt(stake.estimatedReward) };
|
|
725
|
+
case "Unstaked":
|
|
726
|
+
return { deposited: BigInt(stake.principal), rewarded: 0n }; // note: we lose reward information in unstaked state here
|
|
727
|
+
}
|
|
728
|
+
};
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
import { Transaction } from "@mysten/sui/transactions";
|
|
2
|
+
import { SuiClient, getFullnodeUrl } from "@mysten/sui/client";
|
|
3
|
+
|
|
4
|
+
export async function extractCoinTypeFromUnsignedTx(
|
|
5
|
+
unsignedTxBytes: Uint8Array,
|
|
6
|
+
): Promise<string[] | null> {
|
|
7
|
+
const tx = Transaction.from(unsignedTxBytes);
|
|
8
|
+
const data = tx.getData();
|
|
9
|
+
|
|
10
|
+
const gasObjectIds = data.gasData.payment?.map(object => object.objectId) ?? [];
|
|
11
|
+
const inputObjectIds = data.inputs
|
|
12
|
+
.map(input => {
|
|
13
|
+
return input.$kind === "Object" && input.Object.$kind === "ImmOrOwnedObject"
|
|
14
|
+
? input.Object.ImmOrOwnedObject.objectId
|
|
15
|
+
: null;
|
|
16
|
+
})
|
|
17
|
+
.filter((objectId): objectId is string => !!objectId);
|
|
18
|
+
|
|
19
|
+
const suiClient = new SuiClient({ url: getFullnodeUrl("mainnet") });
|
|
20
|
+
const objects = await suiClient.multiGetObjects({
|
|
21
|
+
ids: [...gasObjectIds, ...inputObjectIds],
|
|
22
|
+
options: {
|
|
23
|
+
showBcs: true,
|
|
24
|
+
showPreviousTransaction: true,
|
|
25
|
+
showStorageRebate: true,
|
|
26
|
+
showOwner: true,
|
|
27
|
+
},
|
|
28
|
+
});
|
|
29
|
+
|
|
30
|
+
const coinObjects = objects.filter(obj => {
|
|
31
|
+
const bcsData = obj.data?.bcs as any;
|
|
32
|
+
return bcsData.type.includes("coin");
|
|
33
|
+
});
|
|
34
|
+
|
|
35
|
+
const coinTypes: string[] = coinObjects.map(obj => (obj.data?.bcs as any).type);
|
|
36
|
+
|
|
37
|
+
return coinTypes;
|
|
38
|
+
}
|