@t2000/sdk 0.2.1 → 0.2.4

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/README.md CHANGED
@@ -35,13 +35,13 @@ console.log(`$${balance.available} USDC available`);
35
35
  await agent.send({ to: '0x...', amount: 10 });
36
36
 
37
37
  // Save (earn yield via NAVI Protocol)
38
- await agent.save({ amount: 50 });
38
+ await agent.save({ amount: 50, asset: 'USDC' });
39
39
 
40
40
  // Swap USDC → SUI (via Cetus DEX)
41
41
  await agent.swap({ from: 'USDC', to: 'SUI', amount: 5 });
42
42
 
43
43
  // Borrow against savings
44
- await agent.borrow({ amount: 20 });
44
+ await agent.borrow({ amount: 20, asset: 'USDC' });
45
45
  ```
46
46
 
47
47
  ## API Reference
@@ -65,11 +65,11 @@ const agent = await T2000.create({
65
65
  |--------|-------------|---------|
66
66
  | `agent.balance()` | Available USDC + savings + gas reserve | `BalanceResponse` |
67
67
  | `agent.send({ to, amount })` | Transfer USDC to any Sui address | `SendResult` |
68
- | `agent.save({ amount })` | Deposit USDC to NAVI Protocol (earn APY) | `SaveResult` |
69
- | `agent.withdraw({ amount })` | Withdraw USDC from savings | `WithdrawResult` |
68
+ | `agent.save({ amount, asset: 'USDC' })` | Deposit USDC to NAVI Protocol (earn APY) | `SaveResult` |
69
+ | `agent.withdraw({ amount, asset: 'USDC' })` | Withdraw USDC from savings | `WithdrawResult` |
70
70
  | `agent.swap({ from, to, amount })` | Swap via Cetus CLMM DEX | `SwapResult` |
71
- | `agent.borrow({ amount })` | Borrow USDC against collateral | `BorrowResult` |
72
- | `agent.repay({ amount })` | Repay outstanding borrows | `RepayResult` |
71
+ | `agent.borrow({ amount, asset: 'USDC' })` | Borrow USDC against collateral | `BorrowResult` |
72
+ | `agent.repay({ amount, asset: 'USDC' })` | Repay outstanding borrows | `RepayResult` |
73
73
 
74
74
  ### Query Methods
75
75
 
package/dist/index.cjs CHANGED
@@ -274,12 +274,14 @@ async function buildSendTx({
274
274
  }
275
275
 
276
276
  // src/wallet/balance.ts
277
- var _cachedSuiPrice = 3.5;
277
+ var _cachedSuiPrice = 0;
278
278
  var _priceLastFetched = 0;
279
279
  var PRICE_CACHE_TTL_MS = 6e4;
280
280
  async function fetchSuiPrice(client) {
281
281
  const now = Date.now();
282
- if (now - _priceLastFetched < PRICE_CACHE_TTL_MS) return _cachedSuiPrice;
282
+ if (_cachedSuiPrice > 0 && now - _priceLastFetched < PRICE_CACHE_TTL_MS) {
283
+ return _cachedSuiPrice;
284
+ }
283
285
  try {
284
286
  const pool = await client.getObject({
285
287
  id: CETUS_USDC_SUI_POOL,
@@ -292,7 +294,7 @@ async function fetchSuiPrice(client) {
292
294
  const Q64 = 2n ** 64n;
293
295
  const sqrtPriceFloat = Number(currentSqrtPrice) / Number(Q64);
294
296
  const rawPrice = sqrtPriceFloat * sqrtPriceFloat;
295
- const price = rawPrice * 1e3;
297
+ const price = 1e3 / rawPrice;
296
298
  if (price > 0.01 && price < 1e3) {
297
299
  _cachedSuiPrice = price;
298
300
  _priceLastFetched = now;
@@ -1058,6 +1060,10 @@ var T2000 = class _T2000 extends eventemitter3.EventEmitter {
1058
1060
  }
1059
1061
  // -- Savings --
1060
1062
  async save(params) {
1063
+ const asset = (params.asset ?? "USDC").toUpperCase();
1064
+ if (asset !== "USDC") {
1065
+ throw new T2000Error("ASSET_NOT_SUPPORTED", `Only USDC is supported for save. Got: ${asset}`);
1066
+ }
1061
1067
  let amount;
1062
1068
  if (params.amount === "all") {
1063
1069
  const bal = await queryBalance(this.client, this._address);
@@ -1082,6 +1088,12 @@ var T2000 = class _T2000 extends eventemitter3.EventEmitter {
1082
1088
  const rates = await getRates(this.client);
1083
1089
  reportFee(this._address, "save", fee.amount, fee.rate, gasResult.digest);
1084
1090
  this.emitBalanceChange("USDC", saveAmount, "save", gasResult.digest);
1091
+ let savingsBalance = saveAmount - fee.amount;
1092
+ try {
1093
+ const positions = await this.positions();
1094
+ savingsBalance = positions.positions.filter((p) => p.type === "save").reduce((sum, p) => sum + p.amount, 0);
1095
+ } catch {
1096
+ }
1085
1097
  return {
1086
1098
  success: true,
1087
1099
  tx: gasResult.digest,
@@ -1089,10 +1101,15 @@ var T2000 = class _T2000 extends eventemitter3.EventEmitter {
1089
1101
  apy: rates.USDC.saveApy,
1090
1102
  fee: fee.amount,
1091
1103
  gasCost: gasResult.gasCostSui,
1092
- gasMethod: gasResult.gasMethod
1104
+ gasMethod: gasResult.gasMethod,
1105
+ savingsBalance
1093
1106
  };
1094
1107
  }
1095
1108
  async withdraw(params) {
1109
+ const asset = (params.asset ?? "USDC").toUpperCase();
1110
+ if (asset !== "USDC") {
1111
+ throw new T2000Error("ASSET_NOT_SUPPORTED", `Only USDC is supported for withdraw. Got: ${asset}`);
1112
+ }
1096
1113
  let amount;
1097
1114
  if (params.amount === "all") {
1098
1115
  const maxResult = await this.maxWithdraw();
@@ -1139,6 +1156,10 @@ var T2000 = class _T2000 extends eventemitter3.EventEmitter {
1139
1156
  }
1140
1157
  // -- Borrowing --
1141
1158
  async borrow(params) {
1159
+ const asset = (params.asset ?? "USDC").toUpperCase();
1160
+ if (asset !== "USDC") {
1161
+ throw new T2000Error("ASSET_NOT_SUPPORTED", `Only USDC is supported for borrow. Got: ${asset}`);
1162
+ }
1142
1163
  const maxResult = await this.maxBorrow();
1143
1164
  if (params.amount > maxResult.maxAmount) {
1144
1165
  throw new T2000Error("HEALTH_FACTOR_TOO_LOW", `Max safe borrow: $${maxResult.maxAmount.toFixed(2)}`, {
@@ -1167,6 +1188,10 @@ var T2000 = class _T2000 extends eventemitter3.EventEmitter {
1167
1188
  };
1168
1189
  }
1169
1190
  async repay(params) {
1191
+ const asset = (params.asset ?? "USDC").toUpperCase();
1192
+ if (asset !== "USDC") {
1193
+ throw new T2000Error("ASSET_NOT_SUPPORTED", `Only USDC is supported for repay. Got: ${asset}`);
1194
+ }
1170
1195
  let amount;
1171
1196
  if (params.amount === "all") {
1172
1197
  const hf2 = await this.healthFactor();