@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.
@@ -231,9 +231,12 @@ var ProtocolRegistry = class {
231
231
  function stableToRaw(amount, decimals) {
232
232
  return BigInt(Math.round(amount * 10 ** decimals));
233
233
  }
234
- new Map(
234
+ var ASSET_LOOKUP = new Map(
235
235
  Object.keys(SUPPORTED_ASSETS).map((k) => [k.toUpperCase(), k])
236
236
  );
237
+ function normalizeAsset(input) {
238
+ return ASSET_LOOKUP.get(input.toUpperCase()) ?? input;
239
+ }
237
240
 
238
241
  // src/protocols/protocolFee.ts
239
242
  var FEE_RATES = {
@@ -325,6 +328,13 @@ function matchesCoinType(poolType, targetType) {
325
328
  const targetSuffix = targetType.split("::").slice(1).join("::").toLowerCase();
326
329
  return poolSuffix === targetSuffix;
327
330
  }
331
+ function resolvePoolSymbol(pool) {
332
+ const coinType = pool.suiCoinType || pool.coinType || "";
333
+ for (const [key, info] of Object.entries(SUPPORTED_ASSETS)) {
334
+ if (matchesCoinType(coinType, info.type)) return key;
335
+ }
336
+ return pool.token?.symbol ?? "UNKNOWN";
337
+ }
328
338
  async function getPool(asset = "USDC") {
329
339
  const pools = await getPools();
330
340
  const targetType = SUPPORTED_ASSETS[asset].type;
@@ -357,6 +367,20 @@ function addOracleUpdate(tx, config, pool) {
357
367
  ]
358
368
  });
359
369
  }
370
+ function addOracleUpdatesForPositions(tx, config, pools, states, primaryPool) {
371
+ const updated = /* @__PURE__ */ new Set();
372
+ addOracleUpdate(tx, config, primaryPool);
373
+ updated.add(primaryPool.id);
374
+ for (const state of states) {
375
+ if (updated.has(state.assetId)) continue;
376
+ const pool = pools.find((p) => p.id === state.assetId);
377
+ if (!pool) continue;
378
+ const feed = config.oracle.feeds?.find((f2) => f2.assetId === pool.id);
379
+ if (!feed) continue;
380
+ addOracleUpdate(tx, config, pool);
381
+ updated.add(pool.id);
382
+ }
383
+ }
360
384
  function rateToApy(rawRate) {
361
385
  if (!rawRate || rawRate === "0") return 0;
362
386
  return Number(BigInt(rawRate)) / 10 ** RATE_DECIMALS * 100;
@@ -469,7 +493,7 @@ async function buildWithdrawTx(client, address, amount, options = {}) {
469
493
  const rawAmount = Number(stableToRaw(effectiveAmount, assetInfo.decimals));
470
494
  const tx = new transactions.Transaction();
471
495
  tx.setSender(address);
472
- addOracleUpdate(tx, config, pool);
496
+ addOracleUpdatesForPositions(tx, config, pools, states, pool);
473
497
  const [balance] = tx.moveCall({
474
498
  target: `${config.package}::incentive_v3::withdraw_v2`,
475
499
  arguments: [
@@ -500,10 +524,15 @@ async function buildBorrowTx(client, address, amount, options = {}) {
500
524
  const asset = options.asset ?? "USDC";
501
525
  const assetInfo = SUPPORTED_ASSETS[asset];
502
526
  const rawAmount = Number(stableToRaw(amount, assetInfo.decimals));
503
- const [config, pool] = await Promise.all([getConfig(), getPool(asset)]);
527
+ const [config, pool, pools, states] = await Promise.all([
528
+ getConfig(),
529
+ getPool(asset),
530
+ getPools(),
531
+ getUserState(client, address)
532
+ ]);
504
533
  const tx = new transactions.Transaction();
505
534
  tx.setSender(address);
506
- addOracleUpdate(tx, config, pool);
535
+ addOracleUpdatesForPositions(tx, config, pools, states, pool);
507
536
  const [balance] = tx.moveCall({
508
537
  target: `${config.package}::incentive_v3::borrow_v2`,
509
538
  arguments: [
@@ -661,7 +690,7 @@ async function getPositions(client, addressOrKeypair) {
661
690
  for (const state of states) {
662
691
  const pool = pools.find((p) => p.id === state.assetId);
663
692
  if (!pool) continue;
664
- const symbol = pool.token?.symbol ?? "UNKNOWN";
693
+ const symbol = resolvePoolSymbol(pool);
665
694
  const supplyBal = compoundBalance(state.supplyBalance, pool.currentSupplyIndex);
666
695
  const borrowBal = compoundBalance(state.borrowBalance, pool.currentBorrowIndex);
667
696
  if (supplyBal > 1e-4) {
@@ -738,10 +767,10 @@ var NaviAdapter = class {
738
767
  }
739
768
  async getRates(asset) {
740
769
  const rates = await getRates(this.client);
741
- const key = asset.toUpperCase();
742
- const r = rates[key];
770
+ const normalized = normalizeAsset(asset);
771
+ const r = rates[normalized];
743
772
  if (!r) throw new T2000Error("ASSET_NOT_SUPPORTED", `NAVI does not support ${asset}`);
744
- return { asset, saveApy: r.saveApy, borrowApy: r.borrowApy };
773
+ return { asset: normalized, saveApy: r.saveApy, borrowApy: r.borrowApy };
745
774
  }
746
775
  async getPositions(address) {
747
776
  const result = await getPositions(this.client, address);
@@ -754,22 +783,22 @@ var NaviAdapter = class {
754
783
  return getHealthFactor(this.client, address);
755
784
  }
756
785
  async buildSaveTx(address, amount, asset, options) {
757
- const stableAsset = asset?.toUpperCase() === "USDC" ? "USDC" : asset;
786
+ const stableAsset = normalizeAsset(asset);
758
787
  const tx = await buildSaveTx(this.client, address, amount, { ...options, asset: stableAsset });
759
788
  return { tx };
760
789
  }
761
790
  async buildWithdrawTx(address, amount, asset) {
762
- const stableAsset = asset?.toUpperCase() === "USDC" ? "USDC" : asset;
791
+ const stableAsset = normalizeAsset(asset);
763
792
  const result = await buildWithdrawTx(this.client, address, amount, { asset: stableAsset });
764
793
  return { tx: result.tx, effectiveAmount: result.effectiveAmount };
765
794
  }
766
795
  async buildBorrowTx(address, amount, asset, options) {
767
- const stableAsset = asset?.toUpperCase() === "USDC" ? "USDC" : asset;
796
+ const stableAsset = normalizeAsset(asset);
768
797
  const tx = await buildBorrowTx(this.client, address, amount, { ...options, asset: stableAsset });
769
798
  return { tx };
770
799
  }
771
800
  async buildRepayTx(address, amount, asset) {
772
- const stableAsset = asset?.toUpperCase() === "USDC" ? "USDC" : asset;
801
+ const stableAsset = normalizeAsset(asset);
773
802
  const tx = await buildRepayTx(this.client, address, amount, { asset: stableAsset });
774
803
  return { tx };
775
804
  }