@t2000/sdk 5.0.0 → 5.1.0
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/browser.cjs +293 -51
- package/dist/browser.cjs.map +1 -1
- package/dist/browser.d.cts +1 -1
- package/dist/browser.d.ts +1 -1
- package/dist/browser.js +293 -51
- package/dist/browser.js.map +1 -1
- package/dist/index.cjs +132 -51
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +2 -2
- package/dist/index.d.ts +2 -2
- package/dist/index.js +133 -52
- package/dist/index.js.map +1 -1
- package/dist/{types-CgZWnHhs.d.cts → types-C01EGu76.d.cts} +8 -0
- package/dist/{types-CgZWnHhs.d.ts → types-C01EGu76.d.ts} +8 -0
- package/package.json +3 -4
package/dist/index.d.cts
CHANGED
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
import { EventEmitter } from 'eventemitter3';
|
|
2
2
|
import { SuiGrpcClient } from '@mysten/sui/grpc';
|
|
3
3
|
import { Ed25519Keypair } from '@mysten/sui/keypairs/ed25519';
|
|
4
|
-
import { W as TxMetadata, p as SafeguardConfig, T as T2000Error, az as T2000Options, U as TransactionSigner, P as PayOptions, l as PayResult, aA as SwapResult, aB as SwapQuoteResult, y as SupportedAsset, t as SendResult, B as BalanceResponse, R as TransactionRecord, f as DepositInfo, aC as PaymentRequest, a2 as ZkLoginProof, aD as SuiCoreClient, aE as SendableAsset, k as OverlayFeeConfig, aF as SponsoredCoinMergeCache, z as SwapRouteResult } from './types-
|
|
5
|
-
export { aG as CETUS_USDC_SUI_POOL, C as CLOCK_ID, a as COIN_REGISTRY, b as ClassifyBalanceChange, c as ClassifyResult, d as CoinMeta, aH as CoinPage, aI as DEFAULT_GRPC_URL, D as DEFAULT_NETWORK, e as DEFAULT_SAFEGUARD_CONFIG, E as ETH_TYPE, g as ExtractedTransfer, aJ as GASLESS_MIN_STABLE_AMOUNT, aK as GASLESS_STABLE_TYPES, G as GAS_RESERVE_MIN, I as IKA_TYPE, K as KNOWN_TARGETS, h as KeypairSigner, L as LABEL_PATTERNS, i as LOFI_TYPE, M as MANIFEST_TYPE, j as MIST_PER_SUI, N as NAVX_TYPE, aL as OPERATION_ASSETS, O as OVERLAY_FEE_RATE, aM as Operation, aN as SENDABLE_ASSETS, S as STABLE_ASSETS, m as SUI_DECIMALS, n as SUI_TYPE, o as SUPPORTED_ASSETS, q as SafeguardError, r as SafeguardErrorDetails, s as SafeguardRule, aO as SelectAndSplitResult, aP as SerializedCetusRoute, aQ as SerializedCetusRoutePath, aR as SerializedRouterDataV3, u as SimulationResult, v as StableAsset, w as SuiHolding, x as SuiRpcTxBlock, A as T2000ErrorCode, F as T2000ErrorData, H as T2000_OVERLAY_FEE_WALLET, J as TOKEN_MAP, Q as TransactionLeg, V as TxDirection, X as USDC_DECIMALS, Y as USDC_TYPE, Z as USDE_TYPE, _ as USDSUI_TYPE, $ as USDT_TYPE, a0 as WAL_TYPE, a1 as WBTC_TYPE, a3 as ZkLoginSigner, aS as addSwapToTx, aT as assertAllowedAsset, a4 as buildSwapTx, a5 as classifyAction, a6 as classifyLabel, a7 as classifyTransaction, aU as deserializeCetusRoute, a8 as executeTx, a9 as extractAllUserLegs, aa as extractTransferDetails, ab as extractTxCommands, ac as extractTxSender, ad as fallbackLabel, aV as fetchAllCoins, ae as findSwapRoute, af as formatAssetAmount, ag as formatSui, ah as formatUsd, aW as getCoinMeta, ai as getDecimals, aj as getDecimalsForCoinType, aX as getSuiClient, aY as getSuiGrpcClient, aZ as isAllowedAsset, a_ as isCetusRouteFresh, a$ as isInRegistry, ak as mapMoveAbortCode, al as mapWalletError, am as mistToSui, b0 as normalizeAsset, b1 as normalizeCoinType, an as parseSuiRpcTx, ao as payWithMpp, b2 as queryHistory, b3 as queryTransaction, ap as rawToStable, aq as rawToUsdc, ar as refineLendingLabel, as as resolveSymbol, at as resolveTokenType, b4 as selectAndSplitCoin, b5 as selectSuiCoin, b6 as serializeCetusRoute, b7 as simulateTransaction, au as stableToRaw, av as suiToMist, b8 as throwIfSimulationFailed, aw as truncateAddress, ax as usdcToRaw, ay as validateAddress, b9 as verifyCetusRouteCoinMatch } from './types-
|
|
4
|
+
import { W as TxMetadata, p as SafeguardConfig, T as T2000Error, az as T2000Options, U as TransactionSigner, P as PayOptions, l as PayResult, aA as SwapResult, aB as SwapQuoteResult, y as SupportedAsset, t as SendResult, B as BalanceResponse, R as TransactionRecord, f as DepositInfo, aC as PaymentRequest, a2 as ZkLoginProof, aD as SuiCoreClient, aE as SendableAsset, k as OverlayFeeConfig, aF as SponsoredCoinMergeCache, z as SwapRouteResult } from './types-C01EGu76.cjs';
|
|
5
|
+
export { aG as CETUS_USDC_SUI_POOL, C as CLOCK_ID, a as COIN_REGISTRY, b as ClassifyBalanceChange, c as ClassifyResult, d as CoinMeta, aH as CoinPage, aI as DEFAULT_GRPC_URL, D as DEFAULT_NETWORK, e as DEFAULT_SAFEGUARD_CONFIG, E as ETH_TYPE, g as ExtractedTransfer, aJ as GASLESS_MIN_STABLE_AMOUNT, aK as GASLESS_STABLE_TYPES, G as GAS_RESERVE_MIN, I as IKA_TYPE, K as KNOWN_TARGETS, h as KeypairSigner, L as LABEL_PATTERNS, i as LOFI_TYPE, M as MANIFEST_TYPE, j as MIST_PER_SUI, N as NAVX_TYPE, aL as OPERATION_ASSETS, O as OVERLAY_FEE_RATE, aM as Operation, aN as SENDABLE_ASSETS, S as STABLE_ASSETS, m as SUI_DECIMALS, n as SUI_TYPE, o as SUPPORTED_ASSETS, q as SafeguardError, r as SafeguardErrorDetails, s as SafeguardRule, aO as SelectAndSplitResult, aP as SerializedCetusRoute, aQ as SerializedCetusRoutePath, aR as SerializedRouterDataV3, u as SimulationResult, v as StableAsset, w as SuiHolding, x as SuiRpcTxBlock, A as T2000ErrorCode, F as T2000ErrorData, H as T2000_OVERLAY_FEE_WALLET, J as TOKEN_MAP, Q as TransactionLeg, V as TxDirection, X as USDC_DECIMALS, Y as USDC_TYPE, Z as USDE_TYPE, _ as USDSUI_TYPE, $ as USDT_TYPE, a0 as WAL_TYPE, a1 as WBTC_TYPE, a3 as ZkLoginSigner, aS as addSwapToTx, aT as assertAllowedAsset, a4 as buildSwapTx, a5 as classifyAction, a6 as classifyLabel, a7 as classifyTransaction, aU as deserializeCetusRoute, a8 as executeTx, a9 as extractAllUserLegs, aa as extractTransferDetails, ab as extractTxCommands, ac as extractTxSender, ad as fallbackLabel, aV as fetchAllCoins, ae as findSwapRoute, af as formatAssetAmount, ag as formatSui, ah as formatUsd, aW as getCoinMeta, ai as getDecimals, aj as getDecimalsForCoinType, aX as getSuiClient, aY as getSuiGrpcClient, aZ as isAllowedAsset, a_ as isCetusRouteFresh, a$ as isInRegistry, ak as mapMoveAbortCode, al as mapWalletError, am as mistToSui, b0 as normalizeAsset, b1 as normalizeCoinType, an as parseSuiRpcTx, ao as payWithMpp, b2 as queryHistory, b3 as queryTransaction, ap as rawToStable, aq as rawToUsdc, ar as refineLendingLabel, as as resolveSymbol, at as resolveTokenType, b4 as selectAndSplitCoin, b5 as selectSuiCoin, b6 as serializeCetusRoute, b7 as simulateTransaction, au as stableToRaw, av as suiToMist, b8 as throwIfSimulationFailed, aw as truncateAddress, ax as usdcToRaw, ay as validateAddress, b9 as verifyCetusRouteCoinMatch } from './types-C01EGu76.cjs';
|
|
6
6
|
import { Transaction, TransactionObjectArgument } from '@mysten/sui/transactions';
|
|
7
7
|
import { SuinsClient } from '@mysten/suins';
|
|
8
8
|
import '@mysten/sui/client';
|
package/dist/index.d.ts
CHANGED
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
import { EventEmitter } from 'eventemitter3';
|
|
2
2
|
import { SuiGrpcClient } from '@mysten/sui/grpc';
|
|
3
3
|
import { Ed25519Keypair } from '@mysten/sui/keypairs/ed25519';
|
|
4
|
-
import { W as TxMetadata, p as SafeguardConfig, T as T2000Error, az as T2000Options, U as TransactionSigner, P as PayOptions, l as PayResult, aA as SwapResult, aB as SwapQuoteResult, y as SupportedAsset, t as SendResult, B as BalanceResponse, R as TransactionRecord, f as DepositInfo, aC as PaymentRequest, a2 as ZkLoginProof, aD as SuiCoreClient, aE as SendableAsset, k as OverlayFeeConfig, aF as SponsoredCoinMergeCache, z as SwapRouteResult } from './types-
|
|
5
|
-
export { aG as CETUS_USDC_SUI_POOL, C as CLOCK_ID, a as COIN_REGISTRY, b as ClassifyBalanceChange, c as ClassifyResult, d as CoinMeta, aH as CoinPage, aI as DEFAULT_GRPC_URL, D as DEFAULT_NETWORK, e as DEFAULT_SAFEGUARD_CONFIG, E as ETH_TYPE, g as ExtractedTransfer, aJ as GASLESS_MIN_STABLE_AMOUNT, aK as GASLESS_STABLE_TYPES, G as GAS_RESERVE_MIN, I as IKA_TYPE, K as KNOWN_TARGETS, h as KeypairSigner, L as LABEL_PATTERNS, i as LOFI_TYPE, M as MANIFEST_TYPE, j as MIST_PER_SUI, N as NAVX_TYPE, aL as OPERATION_ASSETS, O as OVERLAY_FEE_RATE, aM as Operation, aN as SENDABLE_ASSETS, S as STABLE_ASSETS, m as SUI_DECIMALS, n as SUI_TYPE, o as SUPPORTED_ASSETS, q as SafeguardError, r as SafeguardErrorDetails, s as SafeguardRule, aO as SelectAndSplitResult, aP as SerializedCetusRoute, aQ as SerializedCetusRoutePath, aR as SerializedRouterDataV3, u as SimulationResult, v as StableAsset, w as SuiHolding, x as SuiRpcTxBlock, A as T2000ErrorCode, F as T2000ErrorData, H as T2000_OVERLAY_FEE_WALLET, J as TOKEN_MAP, Q as TransactionLeg, V as TxDirection, X as USDC_DECIMALS, Y as USDC_TYPE, Z as USDE_TYPE, _ as USDSUI_TYPE, $ as USDT_TYPE, a0 as WAL_TYPE, a1 as WBTC_TYPE, a3 as ZkLoginSigner, aS as addSwapToTx, aT as assertAllowedAsset, a4 as buildSwapTx, a5 as classifyAction, a6 as classifyLabel, a7 as classifyTransaction, aU as deserializeCetusRoute, a8 as executeTx, a9 as extractAllUserLegs, aa as extractTransferDetails, ab as extractTxCommands, ac as extractTxSender, ad as fallbackLabel, aV as fetchAllCoins, ae as findSwapRoute, af as formatAssetAmount, ag as formatSui, ah as formatUsd, aW as getCoinMeta, ai as getDecimals, aj as getDecimalsForCoinType, aX as getSuiClient, aY as getSuiGrpcClient, aZ as isAllowedAsset, a_ as isCetusRouteFresh, a$ as isInRegistry, ak as mapMoveAbortCode, al as mapWalletError, am as mistToSui, b0 as normalizeAsset, b1 as normalizeCoinType, an as parseSuiRpcTx, ao as payWithMpp, b2 as queryHistory, b3 as queryTransaction, ap as rawToStable, aq as rawToUsdc, ar as refineLendingLabel, as as resolveSymbol, at as resolveTokenType, b4 as selectAndSplitCoin, b5 as selectSuiCoin, b6 as serializeCetusRoute, b7 as simulateTransaction, au as stableToRaw, av as suiToMist, b8 as throwIfSimulationFailed, aw as truncateAddress, ax as usdcToRaw, ay as validateAddress, b9 as verifyCetusRouteCoinMatch } from './types-
|
|
4
|
+
import { W as TxMetadata, p as SafeguardConfig, T as T2000Error, az as T2000Options, U as TransactionSigner, P as PayOptions, l as PayResult, aA as SwapResult, aB as SwapQuoteResult, y as SupportedAsset, t as SendResult, B as BalanceResponse, R as TransactionRecord, f as DepositInfo, aC as PaymentRequest, a2 as ZkLoginProof, aD as SuiCoreClient, aE as SendableAsset, k as OverlayFeeConfig, aF as SponsoredCoinMergeCache, z as SwapRouteResult } from './types-C01EGu76.js';
|
|
5
|
+
export { aG as CETUS_USDC_SUI_POOL, C as CLOCK_ID, a as COIN_REGISTRY, b as ClassifyBalanceChange, c as ClassifyResult, d as CoinMeta, aH as CoinPage, aI as DEFAULT_GRPC_URL, D as DEFAULT_NETWORK, e as DEFAULT_SAFEGUARD_CONFIG, E as ETH_TYPE, g as ExtractedTransfer, aJ as GASLESS_MIN_STABLE_AMOUNT, aK as GASLESS_STABLE_TYPES, G as GAS_RESERVE_MIN, I as IKA_TYPE, K as KNOWN_TARGETS, h as KeypairSigner, L as LABEL_PATTERNS, i as LOFI_TYPE, M as MANIFEST_TYPE, j as MIST_PER_SUI, N as NAVX_TYPE, aL as OPERATION_ASSETS, O as OVERLAY_FEE_RATE, aM as Operation, aN as SENDABLE_ASSETS, S as STABLE_ASSETS, m as SUI_DECIMALS, n as SUI_TYPE, o as SUPPORTED_ASSETS, q as SafeguardError, r as SafeguardErrorDetails, s as SafeguardRule, aO as SelectAndSplitResult, aP as SerializedCetusRoute, aQ as SerializedCetusRoutePath, aR as SerializedRouterDataV3, u as SimulationResult, v as StableAsset, w as SuiHolding, x as SuiRpcTxBlock, A as T2000ErrorCode, F as T2000ErrorData, H as T2000_OVERLAY_FEE_WALLET, J as TOKEN_MAP, Q as TransactionLeg, V as TxDirection, X as USDC_DECIMALS, Y as USDC_TYPE, Z as USDE_TYPE, _ as USDSUI_TYPE, $ as USDT_TYPE, a0 as WAL_TYPE, a1 as WBTC_TYPE, a3 as ZkLoginSigner, aS as addSwapToTx, aT as assertAllowedAsset, a4 as buildSwapTx, a5 as classifyAction, a6 as classifyLabel, a7 as classifyTransaction, aU as deserializeCetusRoute, a8 as executeTx, a9 as extractAllUserLegs, aa as extractTransferDetails, ab as extractTxCommands, ac as extractTxSender, ad as fallbackLabel, aV as fetchAllCoins, ae as findSwapRoute, af as formatAssetAmount, ag as formatSui, ah as formatUsd, aW as getCoinMeta, ai as getDecimals, aj as getDecimalsForCoinType, aX as getSuiClient, aY as getSuiGrpcClient, aZ as isAllowedAsset, a_ as isCetusRouteFresh, a$ as isInRegistry, ak as mapMoveAbortCode, al as mapWalletError, am as mistToSui, b0 as normalizeAsset, b1 as normalizeCoinType, an as parseSuiRpcTx, ao as payWithMpp, b2 as queryHistory, b3 as queryTransaction, ap as rawToStable, aq as rawToUsdc, ar as refineLendingLabel, as as resolveSymbol, at as resolveTokenType, b4 as selectAndSplitCoin, b5 as selectSuiCoin, b6 as serializeCetusRoute, b7 as simulateTransaction, au as stableToRaw, av as suiToMist, b8 as throwIfSimulationFailed, aw as truncateAddress, ax as usdcToRaw, ay as validateAddress, b9 as verifyCetusRouteCoinMatch } from './types-C01EGu76.js';
|
|
6
6
|
import { Transaction, TransactionObjectArgument } from '@mysten/sui/transactions';
|
|
7
7
|
import { SuinsClient } from '@mysten/suins';
|
|
8
8
|
import '@mysten/sui/client';
|
package/dist/index.js
CHANGED
|
@@ -5,7 +5,7 @@ import { EventEmitter } from 'eventemitter3';
|
|
|
5
5
|
import { createPaymentTransactionUri } from '@mysten/payment-kit';
|
|
6
6
|
import { SuiGrpcClient } from '@mysten/sui/grpc';
|
|
7
7
|
import { SuiGraphQLClient } from '@mysten/sui/graphql';
|
|
8
|
-
import { normalizeSuiAddress, isValidSuiAddress } from '@mysten/sui/utils';
|
|
8
|
+
import { normalizeSuiAddress, isValidSuiAddress, fromBase64 } from '@mysten/sui/utils';
|
|
9
9
|
import { Ed25519Keypair } from '@mysten/sui/keypairs/ed25519';
|
|
10
10
|
import { decodeSuiPrivateKey } from '@mysten/sui/cryptography';
|
|
11
11
|
import { access, mkdir, writeFile, readFile } from 'fs/promises';
|
|
@@ -570,8 +570,8 @@ async function addSwapToTx(tx, client, address, input) {
|
|
|
570
570
|
inputCoin = result.coin;
|
|
571
571
|
effectiveRaw = result.effectiveAmount;
|
|
572
572
|
} else {
|
|
573
|
-
const { selectAndSplitCoin:
|
|
574
|
-
const result = await
|
|
573
|
+
const { selectAndSplitCoin: selectAndSplitCoin2 } = await Promise.resolve().then(() => (init_coinSelection(), coinSelection_exports));
|
|
574
|
+
const result = await selectAndSplitCoin2(tx, client, address, fromType, requestedRaw, {
|
|
575
575
|
sponsoredContext: input.sponsoredContext ?? false,
|
|
576
576
|
mergeCache: input.coinMergeCache
|
|
577
577
|
});
|
|
@@ -991,55 +991,126 @@ async function executeTx(client, signer, buildTx, options = {}) {
|
|
|
991
991
|
return { digest: txn.digest, gasCostSui, effects };
|
|
992
992
|
}
|
|
993
993
|
|
|
994
|
-
// src/mpp-cost.ts
|
|
995
|
-
function parseChallengeAmount(challenge) {
|
|
996
|
-
const raw = challenge?.request?.amount;
|
|
997
|
-
const n = typeof raw === "string" ? Number(raw) : typeof raw === "number" ? raw : Number.NaN;
|
|
998
|
-
return Number.isFinite(n) ? n : void 0;
|
|
999
|
-
}
|
|
1000
|
-
|
|
1001
994
|
// src/wallet/pay.ts
|
|
995
|
+
init_errors();
|
|
1002
996
|
async function payWithMpp(args) {
|
|
1003
997
|
const { signer, client, options } = args;
|
|
1004
|
-
const { Mppx } = await import('mppx/client');
|
|
1005
|
-
const { sui, USDC } = await import('@suimpp/mpp/client');
|
|
1006
|
-
const { SuiGrpcClient: SuiGrpcClient2 } = await import('@mysten/sui/grpc');
|
|
1007
|
-
const signerAddress = signer.getAddress();
|
|
1008
|
-
let paymentDigest;
|
|
1009
|
-
let gasCostSui = 0;
|
|
1010
|
-
let chargedAmount;
|
|
1011
|
-
const network = client.network === "testnet" ? "testnet" : "mainnet";
|
|
1012
|
-
const grpcBaseUrl = network === "testnet" ? "https://fullnode.testnet.sui.io" : "https://fullnode.mainnet.sui.io";
|
|
1013
|
-
const grpcClient = new SuiGrpcClient2({ baseUrl: grpcBaseUrl, network });
|
|
1014
|
-
const mppx = Mppx.create({
|
|
1015
|
-
polyfill: false,
|
|
1016
|
-
onChallenge: async (challenge) => {
|
|
1017
|
-
const parsed = parseChallengeAmount(challenge);
|
|
1018
|
-
if (parsed !== void 0) chargedAmount = parsed;
|
|
1019
|
-
return void 0;
|
|
1020
|
-
},
|
|
1021
|
-
methods: [sui({
|
|
1022
|
-
client,
|
|
1023
|
-
currency: USDC,
|
|
1024
|
-
signer: {
|
|
1025
|
-
toSuiAddress: () => signerAddress,
|
|
1026
|
-
signPersonalMessage: (bytes) => signer.signPersonalMessage(bytes)
|
|
1027
|
-
},
|
|
1028
|
-
execute: async (tx) => {
|
|
1029
|
-
const result = await executeTx(client, signer, () => tx, { buildClient: grpcClient });
|
|
1030
|
-
paymentDigest = result.digest;
|
|
1031
|
-
gasCostSui = result.gasCostSui;
|
|
1032
|
-
return { digest: result.digest };
|
|
1033
|
-
}
|
|
1034
|
-
})]
|
|
1035
|
-
});
|
|
1036
998
|
const method = (options.method ?? "GET").toUpperCase();
|
|
1037
999
|
const canHaveBody = method !== "GET" && method !== "HEAD";
|
|
1038
|
-
const
|
|
1000
|
+
const reqInit = {
|
|
1039
1001
|
method,
|
|
1040
1002
|
headers: options.headers,
|
|
1041
1003
|
body: canHaveBody ? options.body : void 0
|
|
1004
|
+
};
|
|
1005
|
+
const probe = await fetch(options.url, reqInit);
|
|
1006
|
+
if (probe.status !== 402) {
|
|
1007
|
+
return finalize(probe, { paid: false });
|
|
1008
|
+
}
|
|
1009
|
+
const requirements = await pickSuiExactRequirements(probe, client.network);
|
|
1010
|
+
if (!requirements) {
|
|
1011
|
+
throw new T2000Error(
|
|
1012
|
+
"FACILITATOR_REJECTION",
|
|
1013
|
+
`Endpoint returned 402 without an x402 'exact' / sui:${client.network} payment requirement. This SDK only speaks the x402 dialect.`
|
|
1014
|
+
);
|
|
1015
|
+
}
|
|
1016
|
+
return payViaX402({ signer, client, options, reqInit, requirements });
|
|
1017
|
+
}
|
|
1018
|
+
async function pickSuiExactRequirements(response, network) {
|
|
1019
|
+
try {
|
|
1020
|
+
const body = await response.clone().json();
|
|
1021
|
+
const want = `sui:${network === "testnet" ? "testnet" : "mainnet"}`;
|
|
1022
|
+
return body.accepts?.find((a) => a.scheme === "exact" && a.network === want);
|
|
1023
|
+
} catch {
|
|
1024
|
+
return void 0;
|
|
1025
|
+
}
|
|
1026
|
+
}
|
|
1027
|
+
async function payViaX402(args) {
|
|
1028
|
+
const { signer, client, options, reqInit, requirements } = args;
|
|
1029
|
+
const { buildX402SignedPayment, X402_PAYMENT_HEADER, X402_PAYMENT_RESPONSE_HEADER } = await import('@suimpp/mpp/x402');
|
|
1030
|
+
const amountRaw = BigInt(requirements.maxAmountRequired);
|
|
1031
|
+
const migrationGasSui = await ensureAddressBalanceCovers({
|
|
1032
|
+
signer,
|
|
1033
|
+
client,
|
|
1034
|
+
asset: requirements.asset,
|
|
1035
|
+
amountRaw
|
|
1036
|
+
});
|
|
1037
|
+
const signerAdapter = {
|
|
1038
|
+
toSuiAddress: () => signer.getAddress(),
|
|
1039
|
+
signTransaction: (bytes) => signer.signTransaction(bytes)
|
|
1040
|
+
};
|
|
1041
|
+
const { header } = await buildX402SignedPayment({ requirements, signer: signerAdapter });
|
|
1042
|
+
const res = await fetch(options.url, {
|
|
1043
|
+
...reqInit,
|
|
1044
|
+
headers: { ...options.headers ?? {}, [X402_PAYMENT_HEADER]: header }
|
|
1042
1045
|
});
|
|
1046
|
+
const settleHeader = res.headers.get(X402_PAYMENT_RESPONSE_HEADER);
|
|
1047
|
+
const paid = !!settleHeader;
|
|
1048
|
+
let digest;
|
|
1049
|
+
if (settleHeader) {
|
|
1050
|
+
try {
|
|
1051
|
+
digest = JSON.parse(new TextDecoder().decode(fromBase64(settleHeader))).transaction;
|
|
1052
|
+
} catch {
|
|
1053
|
+
digest = void 0;
|
|
1054
|
+
}
|
|
1055
|
+
}
|
|
1056
|
+
const result = await finalize(res, { paid });
|
|
1057
|
+
if (!paid) return { ...result, dialect: "x402" };
|
|
1058
|
+
return {
|
|
1059
|
+
...result,
|
|
1060
|
+
dialect: "x402",
|
|
1061
|
+
cost: atomicToHuman(amountRaw, await assetDecimals(requirements.asset)),
|
|
1062
|
+
gasCostSui: migrationGasSui,
|
|
1063
|
+
receipt: digest ? { reference: digest, timestamp: (/* @__PURE__ */ new Date()).toISOString() } : result.receipt
|
|
1064
|
+
};
|
|
1065
|
+
}
|
|
1066
|
+
async function ensureAddressBalanceCovers(args) {
|
|
1067
|
+
const { signer, client, asset, amountRaw } = args;
|
|
1068
|
+
const owner = signer.getAddress();
|
|
1069
|
+
const balanceResp = await client.core.getBalance({ owner, coinType: asset });
|
|
1070
|
+
const total = BigInt(balanceResp.balance.balance);
|
|
1071
|
+
if (total < amountRaw) {
|
|
1072
|
+
throw new T2000Error("INSUFFICIENT_BALANCE", `Insufficient ${asset} to pay`, {
|
|
1073
|
+
available: total.toString(),
|
|
1074
|
+
required: amountRaw.toString()
|
|
1075
|
+
});
|
|
1076
|
+
}
|
|
1077
|
+
let coinSum = 0n;
|
|
1078
|
+
let cursor;
|
|
1079
|
+
let hasNext = true;
|
|
1080
|
+
while (hasNext) {
|
|
1081
|
+
const page = await client.core.listCoins({ owner, coinType: asset, cursor: cursor ?? void 0 });
|
|
1082
|
+
for (const c of page.objects) coinSum += BigInt(c.balance);
|
|
1083
|
+
cursor = page.cursor;
|
|
1084
|
+
hasNext = page.hasNextPage;
|
|
1085
|
+
}
|
|
1086
|
+
const addressBalance = total - coinSum;
|
|
1087
|
+
if (addressBalance >= amountRaw) return 0;
|
|
1088
|
+
const shortfall = amountRaw - addressBalance;
|
|
1089
|
+
const { Transaction: Transaction6 } = await import('@mysten/sui/transactions');
|
|
1090
|
+
const { selectAndSplitCoin: selectAndSplitCoin2 } = await Promise.resolve().then(() => (init_coinSelection(), coinSelection_exports));
|
|
1091
|
+
const grpcClient = await makeGrpcBuildClient(client);
|
|
1092
|
+
const migration = await executeTx(
|
|
1093
|
+
client,
|
|
1094
|
+
signer,
|
|
1095
|
+
async () => {
|
|
1096
|
+
const tx = new Transaction6();
|
|
1097
|
+
const { coin } = await selectAndSplitCoin2(tx, client, owner, asset, shortfall, {
|
|
1098
|
+
sponsoredContext: true,
|
|
1099
|
+
// coin-objects-only — never the address balance
|
|
1100
|
+
allowSwapAll: false
|
|
1101
|
+
});
|
|
1102
|
+
tx.moveCall({
|
|
1103
|
+
target: "0x2::coin::send_funds",
|
|
1104
|
+
typeArguments: [asset],
|
|
1105
|
+
arguments: [coin, tx.pure.address(owner)]
|
|
1106
|
+
});
|
|
1107
|
+
return tx;
|
|
1108
|
+
},
|
|
1109
|
+
{ buildClient: grpcClient }
|
|
1110
|
+
);
|
|
1111
|
+
return migration.gasCostSui;
|
|
1112
|
+
}
|
|
1113
|
+
async function finalize(response, opts) {
|
|
1043
1114
|
const contentType = response.headers.get("content-type") ?? "";
|
|
1044
1115
|
let body;
|
|
1045
1116
|
try {
|
|
@@ -1047,15 +1118,25 @@ async function payWithMpp(args) {
|
|
|
1047
1118
|
} catch {
|
|
1048
1119
|
body = null;
|
|
1049
1120
|
}
|
|
1050
|
-
|
|
1051
|
-
|
|
1052
|
-
|
|
1053
|
-
|
|
1054
|
-
|
|
1055
|
-
|
|
1056
|
-
|
|
1057
|
-
|
|
1058
|
-
|
|
1121
|
+
return { status: response.status, body, paid: opts.paid };
|
|
1122
|
+
}
|
|
1123
|
+
async function makeGrpcBuildClient(client) {
|
|
1124
|
+
const { SuiGrpcClient: SuiGrpcClient2 } = await import('@mysten/sui/grpc');
|
|
1125
|
+
const network = client.network === "testnet" ? "testnet" : "mainnet";
|
|
1126
|
+
const baseUrl = network === "testnet" ? "https://fullnode.testnet.sui.io" : "https://fullnode.mainnet.sui.io";
|
|
1127
|
+
return new SuiGrpcClient2({ baseUrl, network });
|
|
1128
|
+
}
|
|
1129
|
+
function atomicToHuman(raw, decimals) {
|
|
1130
|
+
return Number(raw) / 10 ** decimals;
|
|
1131
|
+
}
|
|
1132
|
+
async function assetDecimals(coinType) {
|
|
1133
|
+
try {
|
|
1134
|
+
const { getDecimalsForCoinType: getDecimalsForCoinType2 } = await Promise.resolve().then(() => (init_token_registry(), token_registry_exports));
|
|
1135
|
+
const d = getDecimalsForCoinType2(coinType);
|
|
1136
|
+
return typeof d === "number" ? d : 6;
|
|
1137
|
+
} catch {
|
|
1138
|
+
return 6;
|
|
1139
|
+
}
|
|
1059
1140
|
}
|
|
1060
1141
|
|
|
1061
1142
|
// src/wallet/zkLoginSigner.ts
|