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