@t2000/sdk 0.9.4 → 0.9.5

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.
@@ -390,7 +390,7 @@ function compoundBalance(rawBalance, currentIndex) {
390
390
  const result = (rawBalance * BigInt(currentIndex) + half) / scale;
391
391
  return Number(result) / 10 ** NAVI_BALANCE_DECIMALS;
392
392
  }
393
- async function getUserState(client, address) {
393
+ async function getUserState(client, address, includeZero = false) {
394
394
  const config = await getConfig();
395
395
  const tx = new Transaction();
396
396
  tx.moveCall({
@@ -403,11 +403,13 @@ async function getUserState(client, address) {
403
403
  });
404
404
  const decoded = decodeDevInspect(result, bcs.vector(UserStateInfo));
405
405
  if (!decoded) return [];
406
- return decoded.map((s) => ({
406
+ const mapped = decoded.map((s) => ({
407
407
  assetId: s.asset_id,
408
408
  supplyBalance: toBigInt(s.supply_balance),
409
409
  borrowBalance: toBigInt(s.borrow_balance)
410
- })).filter((s) => s.supplyBalance !== 0n || s.borrowBalance !== 0n);
410
+ }));
411
+ if (includeZero) return mapped;
412
+ return mapped.filter((s) => s.supplyBalance !== 0n || s.borrowBalance !== 0n);
411
413
  }
412
414
  async function fetchCoins(client, owner, coinType) {
413
415
  const all = [];
@@ -464,20 +466,20 @@ async function buildSaveTx(client, address, amount, options = {}) {
464
466
  async function buildWithdrawTx(client, address, amount, options = {}) {
465
467
  const asset = options.asset ?? "USDC";
466
468
  const assetInfo = SUPPORTED_ASSETS[asset];
467
- const [config, pool, pools, states] = await Promise.all([
469
+ const [config, pool, pools, allStates] = await Promise.all([
468
470
  getConfig(),
469
471
  getPool(asset),
470
472
  getPools(),
471
- getUserState(client, address)
473
+ getUserState(client, address, true)
472
474
  ]);
473
- const assetState = states.find((s) => s.assetId === pool.id);
475
+ const assetState = allStates.find((s) => s.assetId === pool.id);
474
476
  const deposited = assetState ? compoundBalance(assetState.supplyBalance, pool.currentSupplyIndex) : 0;
475
477
  const effectiveAmount = Math.min(amount, Math.max(0, deposited - WITHDRAW_DUST_BUFFER));
476
478
  if (effectiveAmount <= 0) throw new T2000Error("NO_COLLATERAL", `Nothing to withdraw for ${assetInfo.displayName} on NAVI`);
477
479
  const rawAmount = Number(stableToRaw(effectiveAmount, assetInfo.decimals));
478
480
  const tx = new Transaction();
479
481
  tx.setSender(address);
480
- addOracleUpdatesForPositions(tx, config, pools, states, pool);
482
+ addOracleUpdatesForPositions(tx, config, pools, allStates, pool);
481
483
  const [balance] = tx.moveCall({
482
484
  target: `${config.package}::incentive_v3::withdraw_v2`,
483
485
  arguments: [
@@ -504,18 +506,18 @@ async function buildWithdrawTx(client, address, amount, options = {}) {
504
506
  async function addWithdrawToTx(tx, client, address, amount, options = {}) {
505
507
  const asset = options.asset ?? "USDC";
506
508
  const assetInfo = SUPPORTED_ASSETS[asset];
507
- const [config, pool, pools, states] = await Promise.all([
509
+ const [config, pool, pools, allStates] = await Promise.all([
508
510
  getConfig(),
509
511
  getPool(asset),
510
512
  getPools(),
511
- getUserState(client, address)
513
+ getUserState(client, address, true)
512
514
  ]);
513
- const assetState = states.find((s) => s.assetId === pool.id);
515
+ const assetState = allStates.find((s) => s.assetId === pool.id);
514
516
  const deposited = assetState ? compoundBalance(assetState.supplyBalance, pool.currentSupplyIndex) : 0;
515
517
  const effectiveAmount = Math.min(amount, Math.max(0, deposited - WITHDRAW_DUST_BUFFER));
516
518
  if (effectiveAmount <= 0) throw new T2000Error("NO_COLLATERAL", `Nothing to withdraw for ${assetInfo.displayName} on NAVI`);
517
519
  const rawAmount = Number(stableToRaw(effectiveAmount, assetInfo.decimals));
518
- addOracleUpdatesForPositions(tx, config, pools, states, pool);
520
+ addOracleUpdatesForPositions(tx, config, pools, allStates, pool);
519
521
  const [balance] = tx.moveCall({
520
522
  target: `${config.package}::incentive_v3::withdraw_v2`,
521
523
  arguments: [
@@ -596,15 +598,15 @@ async function buildBorrowTx(client, address, amount, options = {}) {
596
598
  const asset = options.asset ?? "USDC";
597
599
  const assetInfo = SUPPORTED_ASSETS[asset];
598
600
  const rawAmount = Number(stableToRaw(amount, assetInfo.decimals));
599
- const [config, pool, pools, states] = await Promise.all([
601
+ const [config, pool, pools, allStates] = await Promise.all([
600
602
  getConfig(),
601
603
  getPool(asset),
602
604
  getPools(),
603
- getUserState(client, address)
605
+ getUserState(client, address, true)
604
606
  ]);
605
607
  const tx = new Transaction();
606
608
  tx.setSender(address);
607
- addOracleUpdatesForPositions(tx, config, pools, states, pool);
609
+ addOracleUpdatesForPositions(tx, config, pools, allStates, pool);
608
610
  const [balance] = tx.moveCall({
609
611
  target: `${config.package}::incentive_v3::borrow_v2`,
610
612
  arguments: [