@t2000/sdk 0.18.29 → 0.18.31
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 +118 -64
- 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 +118 -64
- package/dist/index.js.map +1 -1
- package/package.json +1 -1
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();
|
|
@@ -57082,13 +57127,6 @@ To access invested funds: t2000 invest sell ${params.amount} ${asset}`,
|
|
|
57082
57127
|
trackedCostBasis[pos.asset] = (trackedCostBasis[pos.asset] ?? 0) + pos.costBasis;
|
|
57083
57128
|
if (pos.earning) earningAssetSet.add(pos.asset);
|
|
57084
57129
|
}
|
|
57085
|
-
for (const key of this.portfolio.getAllStrategyKeys()) {
|
|
57086
|
-
for (const sp of this.portfolio.getStrategyPositions(key)) {
|
|
57087
|
-
if (!(sp.asset in INVESTMENT_ASSETS)) continue;
|
|
57088
|
-
trackedAmounts[sp.asset] = (trackedAmounts[sp.asset] ?? 0) + sp.totalAmount;
|
|
57089
|
-
trackedCostBasis[sp.asset] = (trackedCostBasis[sp.asset] ?? 0) + sp.costBasis;
|
|
57090
|
-
}
|
|
57091
|
-
}
|
|
57092
57130
|
let investmentValue = 0;
|
|
57093
57131
|
let investmentCostBasis = 0;
|
|
57094
57132
|
let trackedValue = 0;
|
|
@@ -58898,13 +58936,38 @@ To sell investment: t2000 invest sell ${params.amount} ${fromAsset}`,
|
|
|
58898
58936
|
strategyPositions[key] = enrichedStrat;
|
|
58899
58937
|
}
|
|
58900
58938
|
}
|
|
58901
|
-
const
|
|
58902
|
-
const
|
|
58903
|
-
|
|
58939
|
+
const strategyAmountByAsset = {};
|
|
58940
|
+
for (const strats of Object.values(strategyPositions)) {
|
|
58941
|
+
for (const sp of strats) {
|
|
58942
|
+
const prev = strategyAmountByAsset[sp.asset] ?? { amount: 0, costBasis: 0 };
|
|
58943
|
+
strategyAmountByAsset[sp.asset] = {
|
|
58944
|
+
amount: prev.amount + sp.totalAmount,
|
|
58945
|
+
costBasis: prev.costBasis + sp.costBasis
|
|
58946
|
+
};
|
|
58947
|
+
}
|
|
58948
|
+
}
|
|
58949
|
+
const directOnly = enriched.map((pos) => {
|
|
58950
|
+
const strat = strategyAmountByAsset[pos.asset];
|
|
58951
|
+
if (!strat) return pos;
|
|
58952
|
+
const directAmt = pos.totalAmount - strat.amount;
|
|
58953
|
+
if (directAmt <= 1e-6) return null;
|
|
58954
|
+
const directCost = pos.costBasis - strat.costBasis;
|
|
58955
|
+
const currentValue = directAmt * (pos.currentPrice ?? 0);
|
|
58956
|
+
return {
|
|
58957
|
+
...pos,
|
|
58958
|
+
totalAmount: directAmt,
|
|
58959
|
+
costBasis: directCost,
|
|
58960
|
+
currentValue,
|
|
58961
|
+
unrealizedPnL: currentValue - directCost,
|
|
58962
|
+
unrealizedPnLPct: directCost > 0 ? (currentValue - directCost) / directCost * 100 : 0
|
|
58963
|
+
};
|
|
58964
|
+
}).filter((p) => p !== null);
|
|
58965
|
+
const totalInvested = enriched.reduce((sum, p) => sum + p.costBasis, 0);
|
|
58966
|
+
const totalValue = enriched.reduce((sum, p) => sum + p.currentValue, 0);
|
|
58904
58967
|
const totalUnrealizedPnL = totalValue - totalInvested;
|
|
58905
58968
|
const totalUnrealizedPnLPct = totalInvested > 0 ? totalUnrealizedPnL / totalInvested * 100 : 0;
|
|
58906
58969
|
const result = {
|
|
58907
|
-
positions:
|
|
58970
|
+
positions: directOnly,
|
|
58908
58971
|
totalInvested,
|
|
58909
58972
|
totalValue,
|
|
58910
58973
|
unrealizedPnL: totalUnrealizedPnL,
|
|
@@ -59256,16 +59319,7 @@ To sell investment: t2000 invest sell ${params.amount} ${fromAsset}`,
|
|
|
59256
59319
|
async getFreeBalance(asset) {
|
|
59257
59320
|
if (!(asset in INVESTMENT_ASSETS)) return Infinity;
|
|
59258
59321
|
const pos = this.portfolio.getPosition(asset);
|
|
59259
|
-
const
|
|
59260
|
-
let strategyTotal = 0;
|
|
59261
|
-
for (const key of this.portfolio.getAllStrategyKeys()) {
|
|
59262
|
-
for (const sp of this.portfolio.getStrategyPositions(key)) {
|
|
59263
|
-
if (sp.asset === asset && sp.totalAmount > 0) {
|
|
59264
|
-
strategyTotal += sp.totalAmount;
|
|
59265
|
-
}
|
|
59266
|
-
}
|
|
59267
|
-
}
|
|
59268
|
-
const walletInvested = Math.max(directAmount, strategyTotal);
|
|
59322
|
+
const walletInvested = pos && pos.totalAmount > 0 && !pos.earning ? pos.totalAmount : 0;
|
|
59269
59323
|
if (walletInvested <= 0) return Infinity;
|
|
59270
59324
|
const assetInfo = SUPPORTED_ASSETS[asset];
|
|
59271
59325
|
const balance = await this.client.getBalance({ owner: this._address, coinType: assetInfo.type });
|