@t2000/sdk 0.14.1 → 0.15.1

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
@@ -63,7 +63,13 @@ await agent.investBuy({ asset: 'ETH', usdAmount: 200 });
63
63
  const portfolio = await agent.getPortfolio();
64
64
  console.log(`P&L: ${portfolio.unrealizedPnL}`);
65
65
 
66
- // Sell position
66
+ // Earn yield on investment (deposit into best-rate lending)
67
+ await agent.investEarn({ asset: 'SUI' });
68
+
69
+ // Stop earning (withdraw from lending, keep in portfolio)
70
+ await agent.investUnearn({ asset: 'SUI' });
71
+
72
+ // Sell position (auto-withdraws if earning first)
67
73
  await agent.investSell({ asset: 'SUI', usdAmount: 'all' });
68
74
  ```
69
75
 
@@ -170,7 +176,9 @@ const agent = T2000.fromPrivateKey('suiprivkey1q...');
170
176
  | Method | Description | Returns |
171
177
  |--------|-------------|---------|
172
178
  | `agent.investBuy({ asset, usdAmount, maxSlippage? })` | Buy crypto asset with USD | `InvestResult` |
173
- | `agent.investSell({ asset, usdAmount \| 'all', maxSlippage? })` | Sell crypto back to USDC | `InvestResult` |
179
+ | `agent.investSell({ asset, usdAmount \| 'all', maxSlippage? })` | Sell crypto back to USDC (auto-withdraws if earning) | `InvestResult` |
180
+ | `agent.investEarn({ asset })` | Deposit invested asset into best-rate lending for yield | `InvestEarnResult` |
181
+ | `agent.investUnearn({ asset })` | Withdraw from lending, keep in portfolio | `InvestUnearnResult` |
174
182
  | `agent.getPortfolio()` | Investment positions + P&L | `PortfolioResult` |
175
183
 
176
184
  ### Key Management
@@ -41,7 +41,7 @@ var SUPPORTED_ASSETS = {
41
41
  displayName: "SUI"
42
42
  },
43
43
  BTC: {
44
- type: "0xaafb102dd0902f5055cadecd687fb5b71ca82ef0e0285d90afde828ec58ca96b::btc::BTC",
44
+ type: "0x0041f9f9344cac094454cd574e333c4fdb132d7bcc9379bcd4aab485b2a63942::wbtc::WBTC",
45
45
  decimals: 8,
46
46
  symbol: "BTC",
47
47
  displayName: "Bitcoin"
@@ -173,7 +173,11 @@ var ProtocolRegistry = class {
173
173
  }
174
174
  async allRatesAcrossAssets() {
175
175
  const results = [];
176
- for (const asset of STABLE_ASSETS) {
176
+ const allAssets = [...STABLE_ASSETS, ...Object.keys(INVESTMENT_ASSETS)];
177
+ const seen = /* @__PURE__ */ new Set();
178
+ for (const asset of allAssets) {
179
+ if (seen.has(asset)) continue;
180
+ seen.add(asset);
177
181
  for (const adapter of this.lending.values()) {
178
182
  if (!adapter.supportedAssets.includes(asset)) continue;
179
183
  try {
@@ -338,16 +342,23 @@ function resolvePoolSymbol(pool) {
338
342
  }
339
343
  return pool.token?.symbol ?? "UNKNOWN";
340
344
  }
345
+ function resolveAssetInfo(asset) {
346
+ if (asset in SUPPORTED_ASSETS) {
347
+ const info = SUPPORTED_ASSETS[asset];
348
+ return { type: info.type, decimals: info.decimals, displayName: info.displayName };
349
+ }
350
+ throw new T2000Error("ASSET_NOT_SUPPORTED", `Unknown asset: ${asset}`);
351
+ }
341
352
  async function getPool(asset = "USDC") {
342
353
  const pools = await getPools();
343
- const targetType = SUPPORTED_ASSETS[asset].type;
354
+ const { type: targetType, displayName } = resolveAssetInfo(asset);
344
355
  const pool = pools.find(
345
356
  (p) => matchesCoinType(p.suiCoinType || p.coinType || "", targetType)
346
357
  );
347
358
  if (!pool) {
348
359
  throw new T2000Error(
349
360
  "ASSET_NOT_SUPPORTED",
350
- `${SUPPORTED_ASSETS[asset].displayName} pool not found on NAVI. Try: ${STABLE_ASSETS.filter((a) => a !== asset).join(", ")}`
361
+ `${displayName} pool not found on NAVI`
351
362
  );
352
363
  }
353
364
  return pool;
@@ -370,13 +381,14 @@ function addOracleUpdate(tx, config, pool) {
370
381
  ]
371
382
  });
372
383
  }
373
- function refreshStableOracles(tx, config, pools) {
374
- const stableTypes = STABLE_ASSETS.map((a) => SUPPORTED_ASSETS[a].type);
375
- const stablePools = pools.filter((p) => {
384
+ function refreshOracles(tx, config, pools, opts) {
385
+ const assetsToRefresh = NAVI_SUPPORTED_ASSETS;
386
+ const targetTypes = assetsToRefresh.map((a) => SUPPORTED_ASSETS[a].type);
387
+ const matchedPools = pools.filter((p) => {
376
388
  const ct = p.suiCoinType || p.coinType || "";
377
- return stableTypes.some((t) => matchesCoinType(ct, t));
389
+ return targetTypes.some((t) => matchesCoinType(ct, t));
378
390
  });
379
- for (const pool of stablePools) {
391
+ for (const pool of matchedPools) {
380
392
  addOracleUpdate(tx, config, pool);
381
393
  }
382
394
  }
@@ -450,7 +462,7 @@ async function buildSaveTx(client, address, amount, options = {}) {
450
462
  throw new T2000Error("INVALID_AMOUNT", "Save amount must be a positive number");
451
463
  }
452
464
  const asset = options.asset ?? "USDC";
453
- const assetInfo = SUPPORTED_ASSETS[asset];
465
+ const assetInfo = resolveAssetInfo(asset);
454
466
  const rawAmount = Number(stableToRaw(amount, assetInfo.decimals));
455
467
  const [config, pool] = await Promise.all([getConfig(), getPool(asset)]);
456
468
  const coins = await fetchCoins(client, address, assetInfo.type);
@@ -479,7 +491,7 @@ async function buildSaveTx(client, address, amount, options = {}) {
479
491
  }
480
492
  async function buildWithdrawTx(client, address, amount, options = {}) {
481
493
  const asset = options.asset ?? "USDC";
482
- const assetInfo = SUPPORTED_ASSETS[asset];
494
+ const assetInfo = resolveAssetInfo(asset);
483
495
  const [config, pool, pools, states] = await Promise.all([
484
496
  getConfig(),
485
497
  getPool(asset),
@@ -496,7 +508,7 @@ async function buildWithdrawTx(client, address, amount, options = {}) {
496
508
  }
497
509
  const tx = new transactions.Transaction();
498
510
  tx.setSender(address);
499
- refreshStableOracles(tx, config, pools);
511
+ refreshOracles(tx, config, pools);
500
512
  const [balance] = tx.moveCall({
501
513
  target: `${config.package}::incentive_v3::withdraw_v2`,
502
514
  arguments: [
@@ -522,7 +534,7 @@ async function buildWithdrawTx(client, address, amount, options = {}) {
522
534
  }
523
535
  async function addWithdrawToTx(tx, client, address, amount, options = {}) {
524
536
  const asset = options.asset ?? "USDC";
525
- const assetInfo = SUPPORTED_ASSETS[asset];
537
+ const assetInfo = resolveAssetInfo(asset);
526
538
  const [config, pool, pools, states] = await Promise.all([
527
539
  getConfig(),
528
540
  getPool(asset),
@@ -541,7 +553,7 @@ async function addWithdrawToTx(tx, client, address, amount, options = {}) {
541
553
  });
542
554
  return { coin: coin2, effectiveAmount: 0 };
543
555
  }
544
- refreshStableOracles(tx, config, pools);
556
+ refreshOracles(tx, config, pools);
545
557
  const [balance] = tx.moveCall({
546
558
  target: `${config.package}::incentive_v3::withdraw_v2`,
547
559
  arguments: [
@@ -590,7 +602,7 @@ async function addSaveToTx(tx, _client, _address, coin, options = {}) {
590
602
  typeArguments: [pool.suiCoinType]
591
603
  });
592
604
  }
593
- async function addRepayToTx(tx, client, _address, coin, options = {}) {
605
+ async function addRepayToTx(tx, _client, _address, coin, options = {}) {
594
606
  const asset = options.asset ?? "USDC";
595
607
  const [config, pool] = await Promise.all([getConfig(), getPool(asset)]);
596
608
  addOracleUpdate(tx, config, pool);
@@ -620,7 +632,7 @@ async function buildBorrowTx(client, address, amount, options = {}) {
620
632
  throw new T2000Error("INVALID_AMOUNT", "Borrow amount must be a positive number");
621
633
  }
622
634
  const asset = options.asset ?? "USDC";
623
- const assetInfo = SUPPORTED_ASSETS[asset];
635
+ const assetInfo = resolveAssetInfo(asset);
624
636
  const rawAmount = Number(stableToRaw(amount, assetInfo.decimals));
625
637
  const [config, pool, pools] = await Promise.all([
626
638
  getConfig(),
@@ -629,7 +641,7 @@ async function buildBorrowTx(client, address, amount, options = {}) {
629
641
  ]);
630
642
  const tx = new transactions.Transaction();
631
643
  tx.setSender(address);
632
- refreshStableOracles(tx, config, pools);
644
+ refreshOracles(tx, config, pools);
633
645
  const [balance] = tx.moveCall({
634
646
  target: `${config.package}::incentive_v3::borrow_v2`,
635
647
  arguments: [
@@ -661,7 +673,7 @@ async function buildRepayTx(client, address, amount, options = {}) {
661
673
  throw new T2000Error("INVALID_AMOUNT", "Repay amount must be a positive number");
662
674
  }
663
675
  const asset = options.asset ?? "USDC";
664
- const assetInfo = SUPPORTED_ASSETS[asset];
676
+ const assetInfo = resolveAssetInfo(asset);
665
677
  const rawAmount = Number(stableToRaw(amount, assetInfo.decimals));
666
678
  const [config, pool] = await Promise.all([getConfig(), getPool(asset)]);
667
679
  const coins = await fetchCoins(client, address, assetInfo.type);
@@ -760,11 +772,12 @@ async function getHealthFactor(client, addressOrKeypair) {
760
772
  liquidationThreshold: liqThreshold
761
773
  };
762
774
  }
775
+ var NAVI_SUPPORTED_ASSETS = [...STABLE_ASSETS, "SUI", "ETH"];
763
776
  async function getRates(client) {
764
777
  try {
765
778
  const pools = await getPools();
766
779
  const result = {};
767
- for (const asset of STABLE_ASSETS) {
780
+ for (const asset of NAVI_SUPPORTED_ASSETS) {
768
781
  const targetType = SUPPORTED_ASSETS[asset].type;
769
782
  const pool = pools.find((p) => matchesCoinType(p.suiCoinType || p.coinType || "", targetType));
770
783
  if (!pool) continue;
@@ -853,7 +866,7 @@ var NaviAdapter = class {
853
866
  name = "NAVI Protocol";
854
867
  version = "1.0.0";
855
868
  capabilities = ["save", "withdraw", "borrow", "repay"];
856
- supportedAssets = [...STABLE_ASSETS];
869
+ supportedAssets = [...STABLE_ASSETS, "SUI", "ETH"];
857
870
  supportsSameAssetBorrow = true;
858
871
  client;
859
872
  async init(client) {
@@ -880,23 +893,23 @@ var NaviAdapter = class {
880
893
  return getHealthFactor(this.client, address);
881
894
  }
882
895
  async buildSaveTx(address, amount, asset, options) {
883
- const stableAsset = normalizeAsset(asset);
884
- const tx = await buildSaveTx(this.client, address, amount, { ...options, asset: stableAsset });
896
+ const normalized = normalizeAsset(asset);
897
+ const tx = await buildSaveTx(this.client, address, amount, { ...options, asset: normalized });
885
898
  return { tx };
886
899
  }
887
900
  async buildWithdrawTx(address, amount, asset) {
888
- const stableAsset = normalizeAsset(asset);
889
- const result = await buildWithdrawTx(this.client, address, amount, { asset: stableAsset });
901
+ const normalized = normalizeAsset(asset);
902
+ const result = await buildWithdrawTx(this.client, address, amount, { asset: normalized });
890
903
  return { tx: result.tx, effectiveAmount: result.effectiveAmount };
891
904
  }
892
905
  async buildBorrowTx(address, amount, asset, options) {
893
- const stableAsset = normalizeAsset(asset);
894
- const tx = await buildBorrowTx(this.client, address, amount, { ...options, asset: stableAsset });
906
+ const normalized = normalizeAsset(asset);
907
+ const tx = await buildBorrowTx(this.client, address, amount, { ...options, asset: normalized });
895
908
  return { tx };
896
909
  }
897
910
  async buildRepayTx(address, amount, asset) {
898
- const stableAsset = normalizeAsset(asset);
899
- const tx = await buildRepayTx(this.client, address, amount, { asset: stableAsset });
911
+ const normalized = normalizeAsset(asset);
912
+ const tx = await buildRepayTx(this.client, address, amount, { asset: normalized });
900
913
  return { tx };
901
914
  }
902
915
  async maxWithdraw(address, _asset) {
@@ -906,16 +919,16 @@ var NaviAdapter = class {
906
919
  return maxBorrowAmount(this.client, address);
907
920
  }
908
921
  async addWithdrawToTx(tx, address, amount, asset) {
909
- const stableAsset = normalizeAsset(asset);
910
- return addWithdrawToTx(tx, this.client, address, amount, { asset: stableAsset });
922
+ const normalized = normalizeAsset(asset);
923
+ return addWithdrawToTx(tx, this.client, address, amount, { asset: normalized });
911
924
  }
912
925
  async addSaveToTx(tx, address, coin, asset, options) {
913
- const stableAsset = normalizeAsset(asset);
914
- return addSaveToTx(tx, this.client, address, coin, { ...options, asset: stableAsset });
926
+ const normalized = normalizeAsset(asset);
927
+ return addSaveToTx(tx, this.client, address, coin, { ...options, asset: normalized });
915
928
  }
916
929
  async addRepayToTx(tx, address, coin, asset) {
917
- const stableAsset = normalizeAsset(asset);
918
- return addRepayToTx(tx, this.client, address, coin, { asset: stableAsset });
930
+ const normalized = normalizeAsset(asset);
931
+ return addRepayToTx(tx, this.client, address, coin, { asset: normalized });
919
932
  }
920
933
  };
921
934
  var DEFAULT_SLIPPAGE_BPS = 300;
@@ -1226,7 +1239,7 @@ var SuilendAdapter = class {
1226
1239
  name = "Suilend";
1227
1240
  version = "2.0.0";
1228
1241
  capabilities = ["save", "withdraw", "borrow", "repay"];
1229
- supportedAssets = [...STABLE_ASSETS];
1242
+ supportedAssets = [...STABLE_ASSETS, "SUI", "ETH", "BTC"];
1230
1243
  supportsSameAssetBorrow = false;
1231
1244
  client;
1232
1245
  publishedAt = null;