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