@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 CHANGED
@@ -55980,49 +55980,6 @@ function solveHashcash(challenge) {
55980
55980
  }
55981
55981
  }
55982
55982
 
55983
- // src/gas/autoTopUp.ts
55984
- var AUTO_TOPUP_MIN_SUI_FOR_GAS = 5000000n;
55985
- async function shouldAutoTopUp(client, address) {
55986
- const [suiBalance, usdcBalance] = await Promise.all([
55987
- client.getBalance({ owner: address, coinType: SUPPORTED_ASSETS.SUI.type }),
55988
- client.getBalance({ owner: address, coinType: SUPPORTED_ASSETS.USDC.type })
55989
- ]);
55990
- const suiRaw = BigInt(suiBalance.totalBalance);
55991
- const usdcRaw = BigInt(usdcBalance.totalBalance);
55992
- return suiRaw < AUTO_TOPUP_THRESHOLD && suiRaw >= AUTO_TOPUP_MIN_SUI_FOR_GAS && usdcRaw >= AUTO_TOPUP_MIN_USDC;
55993
- }
55994
- async function executeAutoTopUp(client, keypair) {
55995
- const address = keypair.getPublicKey().toSuiAddress();
55996
- const topupAmountHuman = Number(AUTO_TOPUP_AMOUNT) / 1e6;
55997
- const { tx } = await buildSwapTx({
55998
- client,
55999
- address,
56000
- fromAsset: "USDC",
56001
- toAsset: "SUI",
56002
- amount: topupAmountHuman
56003
- });
56004
- const result = await client.signAndExecuteTransaction({
56005
- signer: keypair,
56006
- transaction: tx,
56007
- options: { showEffects: true, showBalanceChanges: true }
56008
- });
56009
- await client.waitForTransaction({ digest: result.digest });
56010
- let suiReceived = 0;
56011
- if (result.balanceChanges) {
56012
- for (const change of result.balanceChanges) {
56013
- if (change.coinType === SUPPORTED_ASSETS.SUI.type && change.owner && typeof change.owner === "object" && "AddressOwner" in change.owner && change.owner.AddressOwner === address) {
56014
- suiReceived += Number(change.amount) / Number(MIST_PER_SUI);
56015
- }
56016
- }
56017
- }
56018
- return {
56019
- success: true,
56020
- tx: result.digest,
56021
- usdcSpent: topupAmountHuman,
56022
- suiReceived: Math.abs(suiReceived)
56023
- };
56024
- }
56025
-
56026
55983
  // src/gas/gasStation.ts
56027
55984
  async function requestGasSponsorship(txJson, sender, type, txBcsBytes) {
56028
55985
  const payload = { sender, type };
@@ -56085,6 +56042,83 @@ async function getGasStatus(address) {
56085
56042
  return await res.json();
56086
56043
  }
56087
56044
 
56045
+ // src/gas/autoTopUp.ts
56046
+ async function shouldAutoTopUp(client, address) {
56047
+ const [suiBalance, usdcBalance] = await Promise.all([
56048
+ client.getBalance({ owner: address, coinType: SUPPORTED_ASSETS.SUI.type }),
56049
+ client.getBalance({ owner: address, coinType: SUPPORTED_ASSETS.USDC.type })
56050
+ ]);
56051
+ const suiRaw = BigInt(suiBalance.totalBalance);
56052
+ const usdcRaw = BigInt(usdcBalance.totalBalance);
56053
+ return suiRaw < AUTO_TOPUP_THRESHOLD && usdcRaw >= AUTO_TOPUP_MIN_USDC;
56054
+ }
56055
+ async function executeAutoTopUp(client, keypair) {
56056
+ const address = keypair.getPublicKey().toSuiAddress();
56057
+ const topupAmountHuman = Number(AUTO_TOPUP_AMOUNT) / 1e6;
56058
+ const { tx } = await buildSwapTx({
56059
+ client,
56060
+ address,
56061
+ fromAsset: "USDC",
56062
+ toAsset: "SUI",
56063
+ amount: topupAmountHuman
56064
+ });
56065
+ tx.setSender(address);
56066
+ let result;
56067
+ try {
56068
+ result = await client.signAndExecuteTransaction({
56069
+ signer: keypair,
56070
+ transaction: tx,
56071
+ options: { showEffects: true, showBalanceChanges: true }
56072
+ });
56073
+ } catch {
56074
+ const { tx: freshTx } = await buildSwapTx({
56075
+ client,
56076
+ address,
56077
+ fromAsset: "USDC",
56078
+ toAsset: "SUI",
56079
+ amount: topupAmountHuman
56080
+ });
56081
+ freshTx.setSender(address);
56082
+ let txJson;
56083
+ let txBcsBase64;
56084
+ try {
56085
+ txJson = freshTx.serialize();
56086
+ } catch {
56087
+ const bcsBytes = await freshTx.build({ client });
56088
+ txBcsBase64 = Buffer.from(bcsBytes).toString("base64");
56089
+ }
56090
+ const sponsored = await requestGasSponsorship(
56091
+ txJson ?? "",
56092
+ address,
56093
+ "auto-topup",
56094
+ txBcsBase64
56095
+ );
56096
+ const sponsoredTxBytes = Buffer.from(sponsored.txBytes, "base64");
56097
+ const { signature: agentSig } = await keypair.signTransaction(sponsoredTxBytes);
56098
+ result = await client.executeTransactionBlock({
56099
+ transactionBlock: sponsored.txBytes,
56100
+ signature: [agentSig, sponsored.sponsorSignature],
56101
+ options: { showEffects: true, showBalanceChanges: true }
56102
+ });
56103
+ reportGasUsage(address, result.digest, 0, 0, "auto-topup");
56104
+ }
56105
+ await client.waitForTransaction({ digest: result.digest });
56106
+ let suiReceived = 0;
56107
+ if (result.balanceChanges) {
56108
+ for (const change of result.balanceChanges) {
56109
+ if (change.coinType === SUPPORTED_ASSETS.SUI.type && change.owner && typeof change.owner === "object" && "AddressOwner" in change.owner && change.owner.AddressOwner === address) {
56110
+ suiReceived += Number(change.amount) / Number(MIST_PER_SUI);
56111
+ }
56112
+ }
56113
+ }
56114
+ return {
56115
+ success: true,
56116
+ tx: result.digest,
56117
+ usdcSpent: topupAmountHuman,
56118
+ suiReceived: Math.abs(suiReceived)
56119
+ };
56120
+ }
56121
+
56088
56122
  // src/gas/manager.ts
56089
56123
  function extractGasCost(effects) {
56090
56124
  if (!effects?.gasUsed) return 0;
@@ -56175,6 +56209,17 @@ async function executeWithGas(client, keypair, buildTx, options) {
56175
56209
  if (options?.enforcer && options?.metadata) {
56176
56210
  options.enforcer.check(options.metadata);
56177
56211
  }
56212
+ const result = await resolveGas(client, keypair, buildTx);
56213
+ try {
56214
+ const address = keypair.getPublicKey().toSuiAddress();
56215
+ if (await shouldAutoTopUp(client, address)) {
56216
+ await executeAutoTopUp(client, keypair);
56217
+ }
56218
+ } catch {
56219
+ }
56220
+ return result;
56221
+ }
56222
+ async function resolveGas(client, keypair, buildTx) {
56178
56223
  const errors = [];
56179
56224
  try {
56180
56225
  const tx = await buildTx();