@msafe/sui3-sdk 1.0.17 → 1.0.19
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 +55 -4
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +0 -6
- package/dist/index.d.ts +0 -6
- package/dist/index.js +58 -7
- package/dist/index.js.map +1 -1
- package/package.json +3 -3
- package/src/utils/gasFunding.ts +92 -5
package/dist/index.cjs
CHANGED
|
@@ -514,12 +514,37 @@ function collectUsedObjectIds(tx) {
|
|
|
514
514
|
return used;
|
|
515
515
|
}, /* @__PURE__ */ new Set());
|
|
516
516
|
}
|
|
517
|
+
function txConsumesOwnedSuiCoins(tx) {
|
|
518
|
+
return tx.getData().commands.some((cmd) => {
|
|
519
|
+
if (cmd.$kind !== "$Intent" || cmd.$Intent.name !== "CoinWithBalance") {
|
|
520
|
+
return false;
|
|
521
|
+
}
|
|
522
|
+
const type = cmd.$Intent.data?.type;
|
|
523
|
+
if (!type || type === "gas") {
|
|
524
|
+
return false;
|
|
525
|
+
}
|
|
526
|
+
try {
|
|
527
|
+
return (0, import_utils4.normalizeStructTag)(type) === (0, import_utils4.normalizeStructTag)(SUI_COIN);
|
|
528
|
+
} catch {
|
|
529
|
+
return false;
|
|
530
|
+
}
|
|
531
|
+
});
|
|
532
|
+
}
|
|
517
533
|
async function loadSuiGasFunding(suiClient, owner, tx) {
|
|
518
534
|
const usedObjectIds = collectUsedObjectIds(tx);
|
|
519
535
|
const [coins, balanceRes] = await Promise.all([
|
|
520
536
|
getAllCoins({ suiClient, owner, coinType: SUI_COIN }),
|
|
521
537
|
suiClient.getBalance({ owner, coinType: SUI_COIN })
|
|
522
538
|
]);
|
|
539
|
+
const addressBalance = BigInt(balanceRes.balance.addressBalance);
|
|
540
|
+
if (txConsumesOwnedSuiCoins(tx)) {
|
|
541
|
+
return {
|
|
542
|
+
paymentCoins: [],
|
|
543
|
+
coinBalance: 0n,
|
|
544
|
+
addressBalance,
|
|
545
|
+
total: addressBalance
|
|
546
|
+
};
|
|
547
|
+
}
|
|
523
548
|
const paymentCoins = coins.filter((coin) => !usedObjectIds.has((0, import_utils4.normalizeSuiObjectId)(coin.objectId)) && BigInt(coin.balance) > 0n).map((coin) => ({
|
|
524
549
|
objectId: coin.objectId,
|
|
525
550
|
version: coin.version,
|
|
@@ -527,7 +552,6 @@ async function loadSuiGasFunding(suiClient, owner, tx) {
|
|
|
527
552
|
balance: BigInt(coin.balance)
|
|
528
553
|
}));
|
|
529
554
|
const coinBalance = paymentCoins.reduce((sum, coin) => sum + coin.balance, 0n);
|
|
530
|
-
const addressBalance = BigInt(balanceRes.balance.addressBalance);
|
|
531
555
|
return {
|
|
532
556
|
paymentCoins,
|
|
533
557
|
coinBalance,
|
|
@@ -592,13 +616,33 @@ async function selectGasFunding(input) {
|
|
|
592
616
|
}
|
|
593
617
|
throw new InsufficientGasFundsError(gasBudget, coinBalance, addressBalance);
|
|
594
618
|
}
|
|
619
|
+
function cloneWithClearedGasConfig(tx) {
|
|
620
|
+
const data = structuredClone(tx.getData());
|
|
621
|
+
data.gasData.budget = null;
|
|
622
|
+
data.gasData.price = null;
|
|
623
|
+
data.gasData.payment = null;
|
|
624
|
+
data.expiration = null;
|
|
625
|
+
return import_transactions.Transaction.from(JSON.stringify(data));
|
|
626
|
+
}
|
|
627
|
+
function isPositiveBudget(budget) {
|
|
628
|
+
if (budget == null) {
|
|
629
|
+
return false;
|
|
630
|
+
}
|
|
631
|
+
try {
|
|
632
|
+
return BigInt(budget) > 0n;
|
|
633
|
+
} catch {
|
|
634
|
+
return false;
|
|
635
|
+
}
|
|
636
|
+
}
|
|
595
637
|
async function estimateGasBudget(input) {
|
|
596
638
|
const { tx, suiClient, owner, gasPrice } = input;
|
|
597
639
|
const funding = await loadSuiGasFunding(suiClient, owner, tx);
|
|
598
640
|
if (funding.total <= 0n) {
|
|
599
641
|
throw new InsufficientGasFundsError(0n, funding.coinBalance, funding.addressBalance);
|
|
600
642
|
}
|
|
601
|
-
const
|
|
643
|
+
const { budget, payment } = tx.getData().gasData;
|
|
644
|
+
const mustResetGas = payment != null && payment.length === 0 || budget != null && !isPositiveBudget(budget);
|
|
645
|
+
const probe = mustResetGas ? cloneWithClearedGasConfig(tx) : import_transactions.Transaction.from(tx);
|
|
602
646
|
probe.setSender(owner);
|
|
603
647
|
probe.setGasPrice(gasPrice);
|
|
604
648
|
if (funding.paymentCoins.length > 0) {
|
|
@@ -630,14 +674,21 @@ async function prepareGasFunding(input) {
|
|
|
630
674
|
const { payment, budget: existingBudget } = tx.getData().gasData;
|
|
631
675
|
const bakedAddressBalanceGas = payment != null && payment.length === 0;
|
|
632
676
|
let { gasBudget } = input;
|
|
677
|
+
if (gasBudget != null && gasBudget <= 0n) {
|
|
678
|
+
gasBudget = void 0;
|
|
679
|
+
}
|
|
680
|
+
const existingPositive = isPositiveBudget(existingBudget) ? BigInt(existingBudget) : null;
|
|
633
681
|
if (gasBudget == null) {
|
|
634
|
-
if (bakedAddressBalanceGas ||
|
|
682
|
+
if (bakedAddressBalanceGas || existingPositive == null) {
|
|
635
683
|
gasBudget = await estimateGasBudget({ tx, suiClient, owner, gasPrice });
|
|
636
684
|
} else {
|
|
637
|
-
gasBudget =
|
|
685
|
+
gasBudget = existingPositive;
|
|
638
686
|
}
|
|
639
687
|
}
|
|
640
688
|
tx.setGasBudget(gasBudget);
|
|
689
|
+
if (bakedAddressBalanceGas) {
|
|
690
|
+
tx.setExpiration(null);
|
|
691
|
+
}
|
|
641
692
|
return selectGasFunding({ tx, suiClient, owner, gasBudget });
|
|
642
693
|
}
|
|
643
694
|
async function preferClassicGasPayment(input) {
|