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