@t2000/sdk 0.1.6 → 0.1.8
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 +13 -63
- package/dist/index.cjs.map +1 -1
- package/dist/index.js +13 -63
- package/dist/index.js.map +1 -1
- package/package.json +1 -1
package/dist/index.js
CHANGED
|
@@ -45,8 +45,6 @@ var DEFAULT_RPC_URL = "https://fullnode.mainnet.sui.io:443";
|
|
|
45
45
|
var DEFAULT_KEY_PATH = "~/.t2000/wallet.key";
|
|
46
46
|
var API_BASE_URL = process.env.T2000_API_URL ?? "https://api.t2000.ai";
|
|
47
47
|
var CETUS_USDC_SUI_POOL = "0x51e883ba7c0b566a26cbc8a94cd33eb0abd418a77cc1e60ad22fd9b1f29cd2ab";
|
|
48
|
-
var CETUS_GLOBAL_CONFIG = "0xdaa46292632c3c4d8f31f23ea0f9b36a28ff3677e9684980e4438403a67a3d8f";
|
|
49
|
-
var CETUS_PACKAGE = "0x1eabed72c53feb3805120a081dc15963c204dc8d091542592abaf7a35689b2fb";
|
|
50
48
|
|
|
51
49
|
// src/errors.ts
|
|
52
50
|
var T2000Error = class extends Error {
|
|
@@ -548,7 +546,7 @@ function getCetusSDK() {
|
|
|
548
546
|
return _cetusSDK;
|
|
549
547
|
}
|
|
550
548
|
async function buildSwapTx(params) {
|
|
551
|
-
const { address, fromAsset, toAsset, amount, maxSlippageBps = DEFAULT_SLIPPAGE_BPS } = params;
|
|
549
|
+
const { client, address, fromAsset, toAsset, amount, maxSlippageBps = DEFAULT_SLIPPAGE_BPS } = params;
|
|
552
550
|
const a2b = isA2B(fromAsset);
|
|
553
551
|
const fromInfo = SUPPORTED_ASSETS[fromAsset];
|
|
554
552
|
const toInfo = SUPPORTED_ASSETS[toAsset];
|
|
@@ -789,51 +787,17 @@ async function shouldAutoTopUp(client, address) {
|
|
|
789
787
|
}
|
|
790
788
|
async function executeAutoTopUp(client, keypair) {
|
|
791
789
|
const address = keypair.getPublicKey().toSuiAddress();
|
|
792
|
-
const
|
|
793
|
-
tx
|
|
794
|
-
|
|
795
|
-
|
|
796
|
-
|
|
797
|
-
|
|
798
|
-
|
|
799
|
-
throw new T2000Error("AUTO_TOPUP_FAILED", "No USDC coins available for auto-topup");
|
|
800
|
-
}
|
|
801
|
-
const coinIds = usdcCoins.data.map((c) => c.coinObjectId);
|
|
802
|
-
let usdcCoin;
|
|
803
|
-
if (coinIds.length === 1) {
|
|
804
|
-
usdcCoin = tx.splitCoins(tx.object(coinIds[0]), [AUTO_TOPUP_AMOUNT]);
|
|
805
|
-
} else {
|
|
806
|
-
const primary = tx.object(coinIds[0]);
|
|
807
|
-
if (coinIds.length > 1) {
|
|
808
|
-
tx.mergeCoins(primary, coinIds.slice(1).map((id) => tx.object(id)));
|
|
809
|
-
}
|
|
810
|
-
usdcCoin = tx.splitCoins(primary, [AUTO_TOPUP_AMOUNT]);
|
|
811
|
-
}
|
|
812
|
-
const MIN_SQRT_PRICE = "4295048016";
|
|
813
|
-
const [receivedCoin, returnedCoin] = tx.moveCall({
|
|
814
|
-
target: `${CETUS_PACKAGE}::pool_script::swap_a2b`,
|
|
815
|
-
arguments: [
|
|
816
|
-
tx.object(CETUS_GLOBAL_CONFIG),
|
|
817
|
-
tx.object(CETUS_USDC_SUI_POOL),
|
|
818
|
-
usdcCoin,
|
|
819
|
-
tx.pure.bool(true),
|
|
820
|
-
// by_amount_in
|
|
821
|
-
tx.pure.u64(AUTO_TOPUP_AMOUNT),
|
|
822
|
-
tx.pure.u128(MIN_SQRT_PRICE),
|
|
823
|
-
tx.object(CLOCK_ID)
|
|
824
|
-
],
|
|
825
|
-
typeArguments: [SUPPORTED_ASSETS.USDC.type, SUPPORTED_ASSETS.SUI.type]
|
|
790
|
+
const topupAmountHuman = Number(AUTO_TOPUP_AMOUNT) / 1e6;
|
|
791
|
+
const { tx } = await buildSwapTx({
|
|
792
|
+
client,
|
|
793
|
+
address,
|
|
794
|
+
fromAsset: "USDC",
|
|
795
|
+
toAsset: "SUI",
|
|
796
|
+
amount: topupAmountHuman
|
|
826
797
|
});
|
|
827
|
-
tx.transferObjects([receivedCoin], address);
|
|
828
|
-
tx.transferObjects([returnedCoin], address);
|
|
829
798
|
const txBytes = await tx.build({ client, onlyTransactionKind: true });
|
|
830
799
|
const txBytesBase64 = Buffer.from(txBytes).toString("base64");
|
|
831
|
-
|
|
832
|
-
try {
|
|
833
|
-
sponsoredResult = await requestGasSponsorship(txBytesBase64, address, "auto-topup");
|
|
834
|
-
} catch {
|
|
835
|
-
throw new T2000Error("AUTO_TOPUP_FAILED", "Gas station unavailable for auto-topup sponsorship");
|
|
836
|
-
}
|
|
800
|
+
const sponsoredResult = await requestGasSponsorship(txBytesBase64, address, "auto-topup");
|
|
837
801
|
const sponsoredTxBytes = Buffer.from(sponsoredResult.txBytes, "base64");
|
|
838
802
|
const { signature: agentSig } = await keypair.signTransaction(sponsoredTxBytes);
|
|
839
803
|
const result = await client.executeTransactionBlock({
|
|
@@ -854,7 +818,7 @@ async function executeAutoTopUp(client, keypair) {
|
|
|
854
818
|
return {
|
|
855
819
|
success: true,
|
|
856
820
|
tx: result.digest,
|
|
857
|
-
usdcSpent:
|
|
821
|
+
usdcSpent: topupAmountHuman,
|
|
858
822
|
suiReceived: Math.abs(suiReceived)
|
|
859
823
|
};
|
|
860
824
|
}
|
|
@@ -890,11 +854,7 @@ async function tryAutoTopUpThenSelfFund(client, keypair, tx) {
|
|
|
890
854
|
const address = keypair.getPublicKey().toSuiAddress();
|
|
891
855
|
const canTopUp = await shouldAutoTopUp(client, address);
|
|
892
856
|
if (!canTopUp) return null;
|
|
893
|
-
|
|
894
|
-
await executeAutoTopUp(client, keypair);
|
|
895
|
-
} catch {
|
|
896
|
-
return null;
|
|
897
|
-
}
|
|
857
|
+
await executeAutoTopUp(client, keypair);
|
|
898
858
|
tx.setSender(address);
|
|
899
859
|
const result = await client.signAndExecuteTransaction({
|
|
900
860
|
signer: keypair,
|
|
@@ -912,19 +872,9 @@ async function tryAutoTopUpThenSelfFund(client, keypair, tx) {
|
|
|
912
872
|
async function trySponsored(client, keypair, tx) {
|
|
913
873
|
const address = keypair.getPublicKey().toSuiAddress();
|
|
914
874
|
tx.setSender(address);
|
|
915
|
-
|
|
916
|
-
try {
|
|
917
|
-
txBytes = await tx.build({ client, onlyTransactionKind: true });
|
|
918
|
-
} catch {
|
|
919
|
-
return null;
|
|
920
|
-
}
|
|
875
|
+
const txBytes = await tx.build({ client, onlyTransactionKind: true });
|
|
921
876
|
const txBytesBase64 = Buffer.from(txBytes).toString("base64");
|
|
922
|
-
|
|
923
|
-
try {
|
|
924
|
-
sponsoredResult = await requestGasSponsorship(txBytesBase64, address);
|
|
925
|
-
} catch {
|
|
926
|
-
return null;
|
|
927
|
-
}
|
|
877
|
+
const sponsoredResult = await requestGasSponsorship(txBytesBase64, address);
|
|
928
878
|
const sponsoredTxBytes = Buffer.from(sponsoredResult.txBytes, "base64");
|
|
929
879
|
const { signature: agentSig } = await keypair.signTransaction(sponsoredTxBytes);
|
|
930
880
|
const result = await client.executeTransactionBlock({
|