@t2000/sdk 0.5.5 → 0.6.0

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
@@ -438,6 +438,8 @@ var SUI_SYSTEM_STATE = "0x05";
438
438
  var NAVI_BALANCE_DECIMALS = 9;
439
439
  var CONFIG_API = "https://open-api.naviprotocol.io/api/navi/config?env=prod";
440
440
  var POOLS_API = "https://open-api.naviprotocol.io/api/navi/pools?env=prod";
441
+ var PACKAGE_API = "https://open-api.naviprotocol.io/api/package";
442
+ var packageCache = null;
441
443
  function toBigInt(v) {
442
444
  if (typeof v === "bigint") return v;
443
445
  return BigInt(String(v));
@@ -462,9 +464,22 @@ async function fetchJson(url) {
462
464
  const json = await res.json();
463
465
  return json.data ?? json;
464
466
  }
467
+ async function getLatestPackageId() {
468
+ if (packageCache && Date.now() - packageCache.ts < CACHE_TTL) return packageCache.id;
469
+ const res = await fetch(PACKAGE_API);
470
+ if (!res.ok) throw new T2000Error("PROTOCOL_UNAVAILABLE", `NAVI package API error: ${res.status}`);
471
+ const json = await res.json();
472
+ if (!json.packageId) throw new T2000Error("PROTOCOL_UNAVAILABLE", "NAVI package API returned no packageId");
473
+ packageCache = { id: json.packageId, ts: Date.now() };
474
+ return json.packageId;
475
+ }
465
476
  async function getConfig(fresh = false) {
466
477
  if (configCache && !fresh && Date.now() - configCache.ts < CACHE_TTL) return configCache.data;
467
- const data = await fetchJson(CONFIG_API);
478
+ const [data, latestPkg] = await Promise.all([
479
+ fetchJson(CONFIG_API),
480
+ getLatestPackageId()
481
+ ]);
482
+ data.package = latestPkg;
468
483
  configCache = { data, ts: Date.now() };
469
484
  return data;
470
485
  }
@@ -538,14 +553,13 @@ async function fetchCoins(client, owner, coinType) {
538
553
  }
539
554
  return all;
540
555
  }
541
- function mergeCoinsPtb(tx, coins, amount) {
556
+ function mergeCoins(tx, coins) {
542
557
  if (coins.length === 0) throw new T2000Error("INSUFFICIENT_BALANCE", "No coins to merge");
543
558
  const primary = tx.object(coins[0].coinObjectId);
544
559
  if (coins.length > 1) {
545
560
  tx.mergeCoins(primary, coins.slice(1).map((c) => tx.object(c.coinObjectId)));
546
561
  }
547
- const [split] = tx.splitCoins(primary, [amount]);
548
- return split;
562
+ return primary;
549
563
  }
550
564
  async function buildSaveTx(client, address, amount, options = {}) {
551
565
  const rawAmount = Number(usdcToRaw(amount));
@@ -554,7 +568,7 @@ async function buildSaveTx(client, address, amount, options = {}) {
554
568
  if (coins.length === 0) throw new T2000Error("INSUFFICIENT_BALANCE", "No USDC coins found");
555
569
  const tx = new transactions.Transaction();
556
570
  tx.setSender(address);
557
- const coinObj = mergeCoinsPtb(tx, coins, rawAmount);
571
+ const coinObj = mergeCoins(tx, coins);
558
572
  if (options.collectFee) {
559
573
  addCollectFeeToTx(tx, coinObj, "save");
560
574
  }
@@ -588,8 +602,8 @@ async function buildWithdrawTx(client, address, amount) {
588
602
  const rawAmount = Number(usdcToRaw(effectiveAmount));
589
603
  const tx = new transactions.Transaction();
590
604
  tx.setSender(address);
591
- tx.moveCall({
592
- target: `${config.package}::incentive_v3::entry_withdraw_v2`,
605
+ const [balance] = tx.moveCall({
606
+ target: `${config.package}::incentive_v3::withdraw_v2`,
593
607
  arguments: [
594
608
  tx.object(CLOCK),
595
609
  tx.object(config.oracle.priceOracle),
@@ -600,8 +614,15 @@ async function buildWithdrawTx(client, address, amount) {
600
614
  tx.object(config.incentiveV2),
601
615
  tx.object(config.incentiveV3),
602
616
  tx.object(SUI_SYSTEM_STATE)
603
- ]
617
+ ],
618
+ typeArguments: [pool.suiCoinType]
604
619
  });
620
+ const [coin] = tx.moveCall({
621
+ target: "0x2::coin::from_balance",
622
+ arguments: [balance],
623
+ typeArguments: [pool.suiCoinType]
624
+ });
625
+ tx.transferObjects([coin], address);
605
626
  return { tx, effectiveAmount };
606
627
  }
607
628
  async function buildBorrowTx(client, address, amount, options = {}) {
@@ -609,8 +630,8 @@ async function buildBorrowTx(client, address, amount, options = {}) {
609
630
  const [config, pool] = await Promise.all([getConfig(), getUsdcPool()]);
610
631
  const tx = new transactions.Transaction();
611
632
  tx.setSender(address);
612
- tx.moveCall({
613
- target: `${config.package}::incentive_v3::entry_borrow_v2`,
633
+ const [balance] = tx.moveCall({
634
+ target: `${config.package}::incentive_v3::borrow_v2`,
614
635
  arguments: [
615
636
  tx.object(CLOCK),
616
637
  tx.object(config.oracle.priceOracle),
@@ -621,8 +642,15 @@ async function buildBorrowTx(client, address, amount, options = {}) {
621
642
  tx.object(config.incentiveV2),
622
643
  tx.object(config.incentiveV3),
623
644
  tx.object(SUI_SYSTEM_STATE)
624
- ]
645
+ ],
646
+ typeArguments: [pool.suiCoinType]
647
+ });
648
+ const [borrowedCoin] = tx.moveCall({
649
+ target: "0x2::coin::from_balance",
650
+ arguments: [balance],
651
+ typeArguments: [pool.suiCoinType]
625
652
  });
653
+ tx.transferObjects([borrowedCoin], address);
626
654
  return tx;
627
655
  }
628
656
  async function buildRepayTx(client, address, amount) {
@@ -632,7 +660,7 @@ async function buildRepayTx(client, address, amount) {
632
660
  if (coins.length === 0) throw new T2000Error("INSUFFICIENT_BALANCE", "No USDC coins to repay with");
633
661
  const tx = new transactions.Transaction();
634
662
  tx.setSender(address);
635
- const coinObj = mergeCoinsPtb(tx, coins, rawAmount);
663
+ const coinObj = mergeCoins(tx, coins);
636
664
  tx.moveCall({
637
665
  target: `${config.package}::incentive_v3::entry_repay`,
638
666
  arguments: [