@t2000/sdk 0.8.1 → 0.8.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/dist/index.cjs CHANGED
@@ -538,6 +538,13 @@ function matchesCoinType(poolType, targetType) {
538
538
  const targetSuffix = targetType.split("::").slice(1).join("::").toLowerCase();
539
539
  return poolSuffix === targetSuffix;
540
540
  }
541
+ function resolvePoolSymbol(pool) {
542
+ const coinType = pool.suiCoinType || pool.coinType || "";
543
+ for (const [key, info] of Object.entries(SUPPORTED_ASSETS)) {
544
+ if (matchesCoinType(coinType, info.type)) return key;
545
+ }
546
+ return pool.token?.symbol ?? "UNKNOWN";
547
+ }
541
548
  async function getPool(asset = "USDC") {
542
549
  const pools = await getPools();
543
550
  const targetType = SUPPORTED_ASSETS[asset].type;
@@ -570,6 +577,20 @@ function addOracleUpdate(tx, config, pool) {
570
577
  ]
571
578
  });
572
579
  }
580
+ function addOracleUpdatesForPositions(tx, config, pools, states, primaryPool) {
581
+ const updated = /* @__PURE__ */ new Set();
582
+ addOracleUpdate(tx, config, primaryPool);
583
+ updated.add(primaryPool.id);
584
+ for (const state of states) {
585
+ if (updated.has(state.assetId)) continue;
586
+ const pool = pools.find((p) => p.id === state.assetId);
587
+ if (!pool) continue;
588
+ const feed = config.oracle.feeds?.find((f2) => f2.assetId === pool.id);
589
+ if (!feed) continue;
590
+ addOracleUpdate(tx, config, pool);
591
+ updated.add(pool.id);
592
+ }
593
+ }
573
594
  function rateToApy(rawRate) {
574
595
  if (!rawRate || rawRate === "0") return 0;
575
596
  return Number(BigInt(rawRate)) / 10 ** RATE_DECIMALS * 100;
@@ -682,7 +703,7 @@ async function buildWithdrawTx(client, address, amount, options = {}) {
682
703
  const rawAmount = Number(stableToRaw(effectiveAmount, assetInfo.decimals));
683
704
  const tx = new transactions.Transaction();
684
705
  tx.setSender(address);
685
- addOracleUpdate(tx, config, pool);
706
+ addOracleUpdatesForPositions(tx, config, pools, states, pool);
686
707
  const [balance] = tx.moveCall({
687
708
  target: `${config.package}::incentive_v3::withdraw_v2`,
688
709
  arguments: [
@@ -713,10 +734,15 @@ async function buildBorrowTx(client, address, amount, options = {}) {
713
734
  const asset = options.asset ?? "USDC";
714
735
  const assetInfo = SUPPORTED_ASSETS[asset];
715
736
  const rawAmount = Number(stableToRaw(amount, assetInfo.decimals));
716
- const [config, pool] = await Promise.all([getConfig(), getPool(asset)]);
737
+ const [config, pool, pools, states] = await Promise.all([
738
+ getConfig(),
739
+ getPool(asset),
740
+ getPools(),
741
+ getUserState(client, address)
742
+ ]);
717
743
  const tx = new transactions.Transaction();
718
744
  tx.setSender(address);
719
- addOracleUpdate(tx, config, pool);
745
+ addOracleUpdatesForPositions(tx, config, pools, states, pool);
720
746
  const [balance] = tx.moveCall({
721
747
  target: `${config.package}::incentive_v3::borrow_v2`,
722
748
  arguments: [
@@ -874,7 +900,7 @@ async function getPositions(client, addressOrKeypair) {
874
900
  for (const state of states) {
875
901
  const pool = pools.find((p) => p.id === state.assetId);
876
902
  if (!pool) continue;
877
- const symbol = pool.token?.symbol ?? "UNKNOWN";
903
+ const symbol = resolvePoolSymbol(pool);
878
904
  const supplyBal = compoundBalance(state.supplyBalance, pool.currentSupplyIndex);
879
905
  const borrowBal = compoundBalance(state.borrowBalance, pool.currentBorrowIndex);
880
906
  if (supplyBal > 1e-4) {
@@ -1325,10 +1351,10 @@ var NaviAdapter = class {
1325
1351
  }
1326
1352
  async getRates(asset) {
1327
1353
  const rates = await getRates(this.client);
1328
- const key = asset.toUpperCase();
1329
- const r = rates[key];
1354
+ const normalized = normalizeAsset(asset);
1355
+ const r = rates[normalized];
1330
1356
  if (!r) throw new T2000Error("ASSET_NOT_SUPPORTED", `NAVI does not support ${asset}`);
1331
- return { asset, saveApy: r.saveApy, borrowApy: r.borrowApy };
1357
+ return { asset: normalized, saveApy: r.saveApy, borrowApy: r.borrowApy };
1332
1358
  }
1333
1359
  async getPositions(address) {
1334
1360
  const result = await getPositions(this.client, address);
@@ -1341,22 +1367,22 @@ var NaviAdapter = class {
1341
1367
  return getHealthFactor(this.client, address);
1342
1368
  }
1343
1369
  async buildSaveTx(address, amount, asset, options) {
1344
- const stableAsset = asset?.toUpperCase() === "USDC" ? "USDC" : asset;
1370
+ const stableAsset = normalizeAsset(asset);
1345
1371
  const tx = await buildSaveTx(this.client, address, amount, { ...options, asset: stableAsset });
1346
1372
  return { tx };
1347
1373
  }
1348
1374
  async buildWithdrawTx(address, amount, asset) {
1349
- const stableAsset = asset?.toUpperCase() === "USDC" ? "USDC" : asset;
1375
+ const stableAsset = normalizeAsset(asset);
1350
1376
  const result = await buildWithdrawTx(this.client, address, amount, { asset: stableAsset });
1351
1377
  return { tx: result.tx, effectiveAmount: result.effectiveAmount };
1352
1378
  }
1353
1379
  async buildBorrowTx(address, amount, asset, options) {
1354
- const stableAsset = asset?.toUpperCase() === "USDC" ? "USDC" : asset;
1380
+ const stableAsset = normalizeAsset(asset);
1355
1381
  const tx = await buildBorrowTx(this.client, address, amount, { ...options, asset: stableAsset });
1356
1382
  return { tx };
1357
1383
  }
1358
1384
  async buildRepayTx(address, amount, asset) {
1359
- const stableAsset = asset?.toUpperCase() === "USDC" ? "USDC" : asset;
1385
+ const stableAsset = normalizeAsset(asset);
1360
1386
  const tx = await buildRepayTx(this.client, address, amount, { asset: stableAsset });
1361
1387
  return { tx };
1362
1388
  }