@msafe/sui3-sdk 1.0.23 → 1.0.24
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/dist/index.cjs +27 -4
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +16 -1
- package/dist/index.d.ts +16 -1
- package/dist/index.js +27 -4
- package/dist/index.js.map +1 -1
- package/package.json +1 -1
- package/src/utils/gasFunding.ts +43 -6
package/dist/index.cjs
CHANGED
|
@@ -47,6 +47,7 @@ __export(src_exports, {
|
|
|
47
47
|
LOCAL_API_URL: () => LOCAL_API_URL,
|
|
48
48
|
LOCAL_SYNCING_URL: () => LOCAL_SYNCING_URL,
|
|
49
49
|
MAINNET_RPC_URL: () => MAINNET_RPC_URL,
|
|
50
|
+
MAX_GAS_PAYMENT_OBJECTS: () => MAX_GAS_PAYMENT_OBJECTS,
|
|
50
51
|
MSafeAccount: () => MSafeAccount,
|
|
51
52
|
MSafeClient: () => MSafeClient,
|
|
52
53
|
MSafeEnabledDto: () => MSafeEnabledDto,
|
|
@@ -74,6 +75,7 @@ __export(src_exports, {
|
|
|
74
75
|
isCoinReservationDigest: () => isCoinReservationDigest,
|
|
75
76
|
msafeChainToSuiNetwork: () => msafeChainToSuiNetwork,
|
|
76
77
|
parkedPayloadHex: () => parkedPayloadHex,
|
|
78
|
+
pickGasPaymentCoins: () => pickGasPaymentCoins,
|
|
77
79
|
preferClassicGasPayment: () => preferClassicGasPayment,
|
|
78
80
|
prepareGasFunding: () => prepareGasFunding,
|
|
79
81
|
selectGasFunding: () => selectGasFunding,
|
|
@@ -485,6 +487,8 @@ async function getAllCoins(input) {
|
|
|
485
487
|
|
|
486
488
|
// src/utils/gasFunding.ts
|
|
487
489
|
var GAS_SAFE_OVERHEAD = 1000n;
|
|
490
|
+
var MAX_GAS_PAYMENT_OBJECTS = 256;
|
|
491
|
+
var ESTIMATE_GAS_COVER = 1000000000n;
|
|
488
492
|
var InsufficientGasFundsError = class extends Error {
|
|
489
493
|
gasBudget;
|
|
490
494
|
coinBalance;
|
|
@@ -505,6 +509,25 @@ function computeGasBudget(gasUsed, gasPrice) {
|
|
|
505
509
|
const safeOverhead = GAS_SAFE_OVERHEAD * gasPrice;
|
|
506
510
|
return BigInt(computationCost) + BigInt(storageCost) + safeOverhead;
|
|
507
511
|
}
|
|
512
|
+
function pickGasPaymentCoins(coins, cover) {
|
|
513
|
+
if (cover <= 0n) {
|
|
514
|
+
return [];
|
|
515
|
+
}
|
|
516
|
+
const sorted = [...coins].filter((coin) => coin.balance > 0n).sort((a, b) => a.balance === b.balance ? 0 : a.balance < b.balance ? 1 : -1);
|
|
517
|
+
const selected = [];
|
|
518
|
+
let sum = 0n;
|
|
519
|
+
for (const coin of sorted) {
|
|
520
|
+
if (selected.length >= MAX_GAS_PAYMENT_OBJECTS) {
|
|
521
|
+
break;
|
|
522
|
+
}
|
|
523
|
+
selected.push(coin);
|
|
524
|
+
sum += coin.balance;
|
|
525
|
+
if (sum >= cover) {
|
|
526
|
+
break;
|
|
527
|
+
}
|
|
528
|
+
}
|
|
529
|
+
return selected;
|
|
530
|
+
}
|
|
508
531
|
function collectUsedObjectIds(tx) {
|
|
509
532
|
const used = tx.getData().inputs.reduce((acc, input) => {
|
|
510
533
|
const immOrOwned = input.Object?.ImmOrOwnedObject?.objectId;
|
|
@@ -649,7 +672,7 @@ async function selectGasFunding(input) {
|
|
|
649
672
|
throw new InsufficientGasFundsError(gasBudget, coinBalance, addressBalance);
|
|
650
673
|
}
|
|
651
674
|
if (paymentCoins.length > 0 && coinBalance >= gasBudget) {
|
|
652
|
-
tx.setGasPayment(toObjectRefs(paymentCoins));
|
|
675
|
+
tx.setGasPayment(toObjectRefs(pickGasPaymentCoins(paymentCoins, gasBudget)));
|
|
653
676
|
return { mode: "classic", gasBudget, coinBalance, addressBalance };
|
|
654
677
|
}
|
|
655
678
|
if (paymentCoins.length > 0 && total >= gasBudget) {
|
|
@@ -658,7 +681,7 @@ async function selectGasFunding(input) {
|
|
|
658
681
|
tx,
|
|
659
682
|
suiClient,
|
|
660
683
|
owner,
|
|
661
|
-
paymentCoins,
|
|
684
|
+
paymentCoins: pickGasPaymentCoins(paymentCoins, 1n),
|
|
662
685
|
topUpAmount
|
|
663
686
|
});
|
|
664
687
|
return { mode: "mixedTopUp", gasBudget, coinBalance, addressBalance };
|
|
@@ -704,7 +727,7 @@ async function estimateGasBudget(input) {
|
|
|
704
727
|
probe.setSender(owner);
|
|
705
728
|
probe.setGasPrice(gasPrice);
|
|
706
729
|
if (funding.paymentCoins.length > 0) {
|
|
707
|
-
probe.setGasPayment(toObjectRefs(funding.paymentCoins));
|
|
730
|
+
probe.setGasPayment(toObjectRefs(pickGasPaymentCoins(funding.paymentCoins, ESTIMATE_GAS_COVER)));
|
|
708
731
|
}
|
|
709
732
|
let built;
|
|
710
733
|
try {
|
|
@@ -779,7 +802,7 @@ async function preferClassicGasPayment(input) {
|
|
|
779
802
|
if (funding.paymentCoins.length === 0) {
|
|
780
803
|
return false;
|
|
781
804
|
}
|
|
782
|
-
tx.setGasPayment(toObjectRefs(funding.paymentCoins));
|
|
805
|
+
tx.setGasPayment(toObjectRefs(pickGasPaymentCoins(funding.paymentCoins, ESTIMATE_GAS_COVER)));
|
|
783
806
|
return true;
|
|
784
807
|
}
|
|
785
808
|
|