@t2000/sdk 0.18.29 → 0.18.30
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 +88 -43
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +6 -5
- package/dist/index.d.ts +6 -5
- package/dist/index.js +88 -43
- package/dist/index.js.map +1 -1
- package/package.json +1 -1
package/dist/index.d.cts
CHANGED
|
@@ -620,6 +620,9 @@ interface GasExecutionResult {
|
|
|
620
620
|
* 2. Auto-topup (swap USDC→SUI, then self-fund)
|
|
621
621
|
* 3. Gas Station sponsored (fallback)
|
|
622
622
|
* 4. Fail with INSUFFICIENT_GAS
|
|
623
|
+
*
|
|
624
|
+
* After every successful transaction, proactively tops up SUI if it
|
|
625
|
+
* dropped below threshold — so the user never hits a "no gas" wall.
|
|
623
626
|
*/
|
|
624
627
|
declare function executeWithGas(client: SuiJsonRpcClient, keypair: Ed25519Keypair, buildTx: () => Transaction | Promise<Transaction>, options?: {
|
|
625
628
|
metadata?: TxMetadata;
|
|
@@ -634,11 +637,9 @@ interface AutoTopUpResult {
|
|
|
634
637
|
}
|
|
635
638
|
declare function shouldAutoTopUp(client: SuiJsonRpcClient, address: string): Promise<boolean>;
|
|
636
639
|
/**
|
|
637
|
-
*
|
|
638
|
-
*
|
|
639
|
-
*
|
|
640
|
-
* This avoids the chicken-and-egg problem of needing gas station sponsorship
|
|
641
|
-
* to get gas, and works even when the gas station is down.
|
|
640
|
+
* Swap USDC→SUI to replenish gas. Tries self-funding first; if the agent
|
|
641
|
+
* doesn't have enough SUI to pay for the swap itself, falls back to
|
|
642
|
+
* gas station sponsorship — eliminating the chicken-and-egg problem.
|
|
642
643
|
*/
|
|
643
644
|
declare function executeAutoTopUp(client: SuiJsonRpcClient, keypair: Ed25519Keypair): Promise<AutoTopUpResult>;
|
|
644
645
|
|
package/dist/index.d.ts
CHANGED
|
@@ -620,6 +620,9 @@ interface GasExecutionResult {
|
|
|
620
620
|
* 2. Auto-topup (swap USDC→SUI, then self-fund)
|
|
621
621
|
* 3. Gas Station sponsored (fallback)
|
|
622
622
|
* 4. Fail with INSUFFICIENT_GAS
|
|
623
|
+
*
|
|
624
|
+
* After every successful transaction, proactively tops up SUI if it
|
|
625
|
+
* dropped below threshold — so the user never hits a "no gas" wall.
|
|
623
626
|
*/
|
|
624
627
|
declare function executeWithGas(client: SuiJsonRpcClient, keypair: Ed25519Keypair, buildTx: () => Transaction | Promise<Transaction>, options?: {
|
|
625
628
|
metadata?: TxMetadata;
|
|
@@ -634,11 +637,9 @@ interface AutoTopUpResult {
|
|
|
634
637
|
}
|
|
635
638
|
declare function shouldAutoTopUp(client: SuiJsonRpcClient, address: string): Promise<boolean>;
|
|
636
639
|
/**
|
|
637
|
-
*
|
|
638
|
-
*
|
|
639
|
-
*
|
|
640
|
-
* This avoids the chicken-and-egg problem of needing gas station sponsorship
|
|
641
|
-
* to get gas, and works even when the gas station is down.
|
|
640
|
+
* Swap USDC→SUI to replenish gas. Tries self-funding first; if the agent
|
|
641
|
+
* doesn't have enough SUI to pay for the swap itself, falls back to
|
|
642
|
+
* gas station sponsorship — eliminating the chicken-and-egg problem.
|
|
642
643
|
*/
|
|
643
644
|
declare function executeAutoTopUp(client: SuiJsonRpcClient, keypair: Ed25519Keypair): Promise<AutoTopUpResult>;
|
|
644
645
|
|
package/dist/index.js
CHANGED
|
@@ -55977,49 +55977,6 @@ function solveHashcash(challenge) {
|
|
|
55977
55977
|
}
|
|
55978
55978
|
}
|
|
55979
55979
|
|
|
55980
|
-
// src/gas/autoTopUp.ts
|
|
55981
|
-
var AUTO_TOPUP_MIN_SUI_FOR_GAS = 5000000n;
|
|
55982
|
-
async function shouldAutoTopUp(client, address) {
|
|
55983
|
-
const [suiBalance, usdcBalance] = await Promise.all([
|
|
55984
|
-
client.getBalance({ owner: address, coinType: SUPPORTED_ASSETS.SUI.type }),
|
|
55985
|
-
client.getBalance({ owner: address, coinType: SUPPORTED_ASSETS.USDC.type })
|
|
55986
|
-
]);
|
|
55987
|
-
const suiRaw = BigInt(suiBalance.totalBalance);
|
|
55988
|
-
const usdcRaw = BigInt(usdcBalance.totalBalance);
|
|
55989
|
-
return suiRaw < AUTO_TOPUP_THRESHOLD && suiRaw >= AUTO_TOPUP_MIN_SUI_FOR_GAS && usdcRaw >= AUTO_TOPUP_MIN_USDC;
|
|
55990
|
-
}
|
|
55991
|
-
async function executeAutoTopUp(client, keypair) {
|
|
55992
|
-
const address = keypair.getPublicKey().toSuiAddress();
|
|
55993
|
-
const topupAmountHuman = Number(AUTO_TOPUP_AMOUNT) / 1e6;
|
|
55994
|
-
const { tx } = await buildSwapTx({
|
|
55995
|
-
client,
|
|
55996
|
-
address,
|
|
55997
|
-
fromAsset: "USDC",
|
|
55998
|
-
toAsset: "SUI",
|
|
55999
|
-
amount: topupAmountHuman
|
|
56000
|
-
});
|
|
56001
|
-
const result = await client.signAndExecuteTransaction({
|
|
56002
|
-
signer: keypair,
|
|
56003
|
-
transaction: tx,
|
|
56004
|
-
options: { showEffects: true, showBalanceChanges: true }
|
|
56005
|
-
});
|
|
56006
|
-
await client.waitForTransaction({ digest: result.digest });
|
|
56007
|
-
let suiReceived = 0;
|
|
56008
|
-
if (result.balanceChanges) {
|
|
56009
|
-
for (const change of result.balanceChanges) {
|
|
56010
|
-
if (change.coinType === SUPPORTED_ASSETS.SUI.type && change.owner && typeof change.owner === "object" && "AddressOwner" in change.owner && change.owner.AddressOwner === address) {
|
|
56011
|
-
suiReceived += Number(change.amount) / Number(MIST_PER_SUI);
|
|
56012
|
-
}
|
|
56013
|
-
}
|
|
56014
|
-
}
|
|
56015
|
-
return {
|
|
56016
|
-
success: true,
|
|
56017
|
-
tx: result.digest,
|
|
56018
|
-
usdcSpent: topupAmountHuman,
|
|
56019
|
-
suiReceived: Math.abs(suiReceived)
|
|
56020
|
-
};
|
|
56021
|
-
}
|
|
56022
|
-
|
|
56023
55980
|
// src/gas/gasStation.ts
|
|
56024
55981
|
async function requestGasSponsorship(txJson, sender, type, txBcsBytes) {
|
|
56025
55982
|
const payload = { sender, type };
|
|
@@ -56082,6 +56039,83 @@ async function getGasStatus(address) {
|
|
|
56082
56039
|
return await res.json();
|
|
56083
56040
|
}
|
|
56084
56041
|
|
|
56042
|
+
// src/gas/autoTopUp.ts
|
|
56043
|
+
async function shouldAutoTopUp(client, address) {
|
|
56044
|
+
const [suiBalance, usdcBalance] = await Promise.all([
|
|
56045
|
+
client.getBalance({ owner: address, coinType: SUPPORTED_ASSETS.SUI.type }),
|
|
56046
|
+
client.getBalance({ owner: address, coinType: SUPPORTED_ASSETS.USDC.type })
|
|
56047
|
+
]);
|
|
56048
|
+
const suiRaw = BigInt(suiBalance.totalBalance);
|
|
56049
|
+
const usdcRaw = BigInt(usdcBalance.totalBalance);
|
|
56050
|
+
return suiRaw < AUTO_TOPUP_THRESHOLD && usdcRaw >= AUTO_TOPUP_MIN_USDC;
|
|
56051
|
+
}
|
|
56052
|
+
async function executeAutoTopUp(client, keypair) {
|
|
56053
|
+
const address = keypair.getPublicKey().toSuiAddress();
|
|
56054
|
+
const topupAmountHuman = Number(AUTO_TOPUP_AMOUNT) / 1e6;
|
|
56055
|
+
const { tx } = await buildSwapTx({
|
|
56056
|
+
client,
|
|
56057
|
+
address,
|
|
56058
|
+
fromAsset: "USDC",
|
|
56059
|
+
toAsset: "SUI",
|
|
56060
|
+
amount: topupAmountHuman
|
|
56061
|
+
});
|
|
56062
|
+
tx.setSender(address);
|
|
56063
|
+
let result;
|
|
56064
|
+
try {
|
|
56065
|
+
result = await client.signAndExecuteTransaction({
|
|
56066
|
+
signer: keypair,
|
|
56067
|
+
transaction: tx,
|
|
56068
|
+
options: { showEffects: true, showBalanceChanges: true }
|
|
56069
|
+
});
|
|
56070
|
+
} catch {
|
|
56071
|
+
const { tx: freshTx } = await buildSwapTx({
|
|
56072
|
+
client,
|
|
56073
|
+
address,
|
|
56074
|
+
fromAsset: "USDC",
|
|
56075
|
+
toAsset: "SUI",
|
|
56076
|
+
amount: topupAmountHuman
|
|
56077
|
+
});
|
|
56078
|
+
freshTx.setSender(address);
|
|
56079
|
+
let txJson;
|
|
56080
|
+
let txBcsBase64;
|
|
56081
|
+
try {
|
|
56082
|
+
txJson = freshTx.serialize();
|
|
56083
|
+
} catch {
|
|
56084
|
+
const bcsBytes = await freshTx.build({ client });
|
|
56085
|
+
txBcsBase64 = Buffer.from(bcsBytes).toString("base64");
|
|
56086
|
+
}
|
|
56087
|
+
const sponsored = await requestGasSponsorship(
|
|
56088
|
+
txJson ?? "",
|
|
56089
|
+
address,
|
|
56090
|
+
"auto-topup",
|
|
56091
|
+
txBcsBase64
|
|
56092
|
+
);
|
|
56093
|
+
const sponsoredTxBytes = Buffer.from(sponsored.txBytes, "base64");
|
|
56094
|
+
const { signature: agentSig } = await keypair.signTransaction(sponsoredTxBytes);
|
|
56095
|
+
result = await client.executeTransactionBlock({
|
|
56096
|
+
transactionBlock: sponsored.txBytes,
|
|
56097
|
+
signature: [agentSig, sponsored.sponsorSignature],
|
|
56098
|
+
options: { showEffects: true, showBalanceChanges: true }
|
|
56099
|
+
});
|
|
56100
|
+
reportGasUsage(address, result.digest, 0, 0, "auto-topup");
|
|
56101
|
+
}
|
|
56102
|
+
await client.waitForTransaction({ digest: result.digest });
|
|
56103
|
+
let suiReceived = 0;
|
|
56104
|
+
if (result.balanceChanges) {
|
|
56105
|
+
for (const change of result.balanceChanges) {
|
|
56106
|
+
if (change.coinType === SUPPORTED_ASSETS.SUI.type && change.owner && typeof change.owner === "object" && "AddressOwner" in change.owner && change.owner.AddressOwner === address) {
|
|
56107
|
+
suiReceived += Number(change.amount) / Number(MIST_PER_SUI);
|
|
56108
|
+
}
|
|
56109
|
+
}
|
|
56110
|
+
}
|
|
56111
|
+
return {
|
|
56112
|
+
success: true,
|
|
56113
|
+
tx: result.digest,
|
|
56114
|
+
usdcSpent: topupAmountHuman,
|
|
56115
|
+
suiReceived: Math.abs(suiReceived)
|
|
56116
|
+
};
|
|
56117
|
+
}
|
|
56118
|
+
|
|
56085
56119
|
// src/gas/manager.ts
|
|
56086
56120
|
function extractGasCost(effects) {
|
|
56087
56121
|
if (!effects?.gasUsed) return 0;
|
|
@@ -56172,6 +56206,17 @@ async function executeWithGas(client, keypair, buildTx, options) {
|
|
|
56172
56206
|
if (options?.enforcer && options?.metadata) {
|
|
56173
56207
|
options.enforcer.check(options.metadata);
|
|
56174
56208
|
}
|
|
56209
|
+
const result = await resolveGas(client, keypair, buildTx);
|
|
56210
|
+
try {
|
|
56211
|
+
const address = keypair.getPublicKey().toSuiAddress();
|
|
56212
|
+
if (await shouldAutoTopUp(client, address)) {
|
|
56213
|
+
await executeAutoTopUp(client, keypair);
|
|
56214
|
+
}
|
|
56215
|
+
} catch {
|
|
56216
|
+
}
|
|
56217
|
+
return result;
|
|
56218
|
+
}
|
|
56219
|
+
async function resolveGas(client, keypair, buildTx) {
|
|
56175
56220
|
const errors = [];
|
|
56176
56221
|
try {
|
|
56177
56222
|
const tx = await buildTx();
|