@hypurrquant/defi-cli 1.0.4 → 1.0.6

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/main.js CHANGED
@@ -227,7 +227,7 @@ var init_dist = __esm({
227
227
  aggregators;
228
228
  effectiveRpcUrl() {
229
229
  const chainEnv = this.name.toUpperCase().replace(/ /g, "_") + "_RPC_URL";
230
- return process.env[chainEnv] ?? process.env["HYPEREVM_RPC_URL"] ?? this.rpc_url;
230
+ return process.env[chainEnv] ?? this.rpc_url;
231
231
  }
232
232
  };
233
233
  ProtocolCategory = /* @__PURE__ */ ((ProtocolCategory2) => {
@@ -5617,6 +5617,9 @@ var init_dist2 = __esm({
5617
5617
  "function borrowRatePerBlock() external view returns (uint256)",
5618
5618
  "function totalSupply() external view returns (uint256)",
5619
5619
  "function totalBorrows() external view returns (uint256)",
5620
+ "function balanceOf(address account) external view returns (uint256)",
5621
+ "function exchangeRateStored() external view returns (uint256)",
5622
+ "function borrowBalanceStored(address account) external view returns (uint256)",
5620
5623
  "function mint(uint256 mintAmount) external returns (uint256)",
5621
5624
  "function redeem(uint256 redeemTokens) external returns (uint256)",
5622
5625
  "function borrow(uint256 borrowAmount) external returns (uint256)",
@@ -5732,7 +5735,7 @@ var init_dist2 = __esm({
5732
5735
  total_borrow: 0n
5733
5736
  };
5734
5737
  }
5735
- const [supplyRate, borrowRate, totalSupply, totalBorrows] = await Promise.all([
5738
+ const [supplyRate, borrowRate, totalSupplyVtoken, totalBorrows, exchangeRate] = await Promise.all([
5736
5739
  client.readContract({ address: vtoken, abi: CTOKEN_ABI, functionName: "supplyRatePerBlock" }).catch((e) => {
5737
5740
  throw DefiError.rpcError(`[${this.protocolName}] supplyRatePerBlock failed: ${e}`);
5738
5741
  }),
@@ -5740,29 +5743,62 @@ var init_dist2 = __esm({
5740
5743
  throw DefiError.rpcError(`[${this.protocolName}] borrowRatePerBlock failed: ${e}`);
5741
5744
  }),
5742
5745
  client.readContract({ address: vtoken, abi: CTOKEN_ABI, functionName: "totalSupply" }).catch(() => 0n),
5743
- client.readContract({ address: vtoken, abi: CTOKEN_ABI, functionName: "totalBorrows" }).catch(() => 0n)
5746
+ client.readContract({ address: vtoken, abi: CTOKEN_ABI, functionName: "totalBorrows" }).catch(() => 0n),
5747
+ client.readContract({ address: vtoken, abi: CTOKEN_ABI, functionName: "exchangeRateStored" }).catch(() => 0n)
5744
5748
  ]);
5745
5749
  const supplyPerBlock = Number(supplyRate) / 1e18;
5746
5750
  const borrowPerBlock = Number(borrowRate) / 1e18;
5747
5751
  const supplyApy = supplyPerBlock * BSC_BLOCKS_PER_YEAR * 100;
5748
5752
  const borrowApy = borrowPerBlock * BSC_BLOCKS_PER_YEAR * 100;
5749
- const supplyF = Number(totalSupply);
5753
+ const totalSupplyUnderlying = exchangeRate > 0n ? totalSupplyVtoken * exchangeRate / 10n ** 18n : totalSupplyVtoken;
5754
+ const supplyF = Number(totalSupplyUnderlying);
5750
5755
  const borrowF = Number(totalBorrows);
5751
- const utilization = supplyF > 0 ? borrowF / supplyF * 100 : 0;
5756
+ const utilization = supplyF > 0 ? Math.round(borrowF / supplyF * 1e4) / 100 : 0;
5752
5757
  return {
5753
5758
  protocol: this.protocolName,
5754
5759
  asset,
5755
5760
  supply_apy: supplyApy,
5756
5761
  borrow_variable_apy: borrowApy,
5757
5762
  utilization,
5758
- total_supply: totalSupply,
5763
+ total_supply: totalSupplyUnderlying,
5759
5764
  total_borrow: totalBorrows
5760
5765
  };
5761
5766
  }
5762
- async getUserPosition(_user) {
5763
- throw DefiError.unsupported(
5764
- `[${this.protocolName}] User position requires querying individual vToken balances`
5765
- );
5767
+ async getUserPosition(user) {
5768
+ if (!this.rpcUrl) throw DefiError.rpcError("No RPC URL configured");
5769
+ const client = createPublicClient13({ transport: http13(this.rpcUrl) });
5770
+ const ERC20_ABI42 = parseAbi17([
5771
+ "function symbol() external view returns (string)"
5772
+ ]);
5773
+ const supplies = [];
5774
+ const borrows = [];
5775
+ await Promise.all(this.vTokenCandidates.map(async (vtoken) => {
5776
+ try {
5777
+ const [vtokenBal, rate, borrowed, underlying] = await Promise.all([
5778
+ client.readContract({ address: vtoken, abi: CTOKEN_ABI, functionName: "balanceOf", args: [user] }),
5779
+ client.readContract({ address: vtoken, abi: CTOKEN_ABI, functionName: "exchangeRateStored" }),
5780
+ client.readContract({ address: vtoken, abi: CTOKEN_ABI, functionName: "borrowBalanceStored", args: [user] }),
5781
+ client.readContract({ address: vtoken, abi: CTOKEN_ABI, functionName: "underlying" }).catch(() => null)
5782
+ ]);
5783
+ if (vtokenBal === 0n && borrowed === 0n) return;
5784
+ const assetAddr = underlying ?? vtoken;
5785
+ const symbol = await client.readContract({
5786
+ address: assetAddr,
5787
+ abi: ERC20_ABI42,
5788
+ functionName: "symbol"
5789
+ }).catch(() => "?");
5790
+ const supplyUnderlying = vtokenBal * rate / 10n ** 18n;
5791
+ if (supplyUnderlying > 0n) supplies.push({ asset: assetAddr, symbol, amount: supplyUnderlying });
5792
+ if (borrowed > 0n) borrows.push({ asset: assetAddr, symbol, amount: borrowed });
5793
+ } catch {
5794
+ }
5795
+ }));
5796
+ return {
5797
+ protocol: this.protocolName,
5798
+ user,
5799
+ supplies,
5800
+ borrows
5801
+ };
5766
5802
  }
5767
5803
  };
5768
5804
  COMET_ABI = parseAbi18([
@@ -5772,6 +5808,8 @@ var init_dist2 = __esm({
5772
5808
  "function getBorrowRate(uint256 utilization) external view returns (uint64)",
5773
5809
  "function totalSupply() external view returns (uint256)",
5774
5810
  "function totalBorrow() external view returns (uint256)",
5811
+ "function balanceOf(address account) external view returns (uint256)",
5812
+ "function borrowBalanceOf(address account) external view returns (uint256)",
5775
5813
  "function supply(address asset, uint256 amount) external",
5776
5814
  "function withdraw(address asset, uint256 amount) external"
5777
5815
  ]);
@@ -5902,10 +5940,30 @@ var init_dist2 = __esm({
5902
5940
  total_borrow: totalBorrow
5903
5941
  };
5904
5942
  }
5905
- async getUserPosition(_user) {
5906
- throw DefiError.unsupported(
5907
- `[${this.protocolName}] User position requires querying Comet balanceOf + borrowBalanceOf`
5908
- );
5943
+ async getUserPosition(user) {
5944
+ if (!this.rpcUrl) throw DefiError.rpcError("No RPC URL configured");
5945
+ const client = createPublicClient14({ transport: http14(this.rpcUrl) });
5946
+ const ERC20_ABI42 = parseAbi18([
5947
+ "function symbol() external view returns (string)"
5948
+ ]);
5949
+ const [supplyBal, borrowBal, baseToken] = await Promise.all([
5950
+ client.readContract({ address: this.comet, abi: COMET_ABI, functionName: "balanceOf", args: [user] }).catch(() => 0n),
5951
+ client.readContract({ address: this.comet, abi: COMET_ABI, functionName: "borrowBalanceOf", args: [user] }).catch(() => 0n),
5952
+ client.readContract({ address: this.comet, abi: COMET_ABI, functionName: "baseToken" })
5953
+ ]);
5954
+ const baseSymbol = await client.readContract({
5955
+ address: baseToken,
5956
+ abi: ERC20_ABI42,
5957
+ functionName: "symbol"
5958
+ }).catch(() => "?");
5959
+ const supplies = supplyBal > 0n ? [{ asset: baseToken, symbol: baseSymbol, amount: supplyBal }] : [];
5960
+ const borrows = borrowBal > 0n ? [{ asset: baseToken, symbol: baseSymbol, amount: borrowBal }] : [];
5961
+ return {
5962
+ protocol: this.protocolName,
5963
+ user,
5964
+ supplies,
5965
+ borrows
5966
+ };
5909
5967
  }
5910
5968
  };
5911
5969
  EULER_VAULT_ABI = parseAbi19([
@@ -11514,15 +11572,19 @@ function registerSetup(program2) {
11514
11572
  }
11515
11573
  }
11516
11574
  console.log(pc2.cyan(pc2.bold("\n RPC URLs")) + pc2.gray(" (press Enter to use public defaults)"));
11517
- const hyperevmRpc = await ask(rl, " HyperEVM RPC URL: ");
11518
- if (hyperevmRpc) {
11519
- newEnv.HYPEREVM_RPC_URL = hyperevmRpc;
11520
- console.log(` ${pc2.green("OK")} HyperEVM RPC set`);
11521
- }
11522
- const mantleRpc = await ask(rl, " Mantle RPC URL: ");
11523
- if (mantleRpc) {
11524
- newEnv.MANTLE_RPC_URL = mantleRpc;
11525
- console.log(` ${pc2.green("OK")} Mantle RPC set`);
11575
+ const rpcPrompts = [
11576
+ { env: "HYPEREVM_RPC_URL", label: "HyperEVM" },
11577
+ { env: "MANTLE_RPC_URL", label: "Mantle" },
11578
+ { env: "BASE_RPC_URL", label: "Base" },
11579
+ { env: "BNB_RPC_URL", label: "BNB" },
11580
+ { env: "MONAD_RPC_URL", label: "Monad" }
11581
+ ];
11582
+ for (const { env, label } of rpcPrompts) {
11583
+ const value = await ask(rl, ` ${label} RPC URL: `);
11584
+ if (value) {
11585
+ newEnv[env] = value;
11586
+ console.log(` ${pc2.green("OK")} ${label} RPC set`);
11587
+ }
11526
11588
  }
11527
11589
  const finalEnv = { ...existing, ...newEnv };
11528
11590
  writeEnvFile(finalEnv);
@@ -11534,11 +11596,16 @@ function registerSetup(program2) {
11534
11596
  if (finalEnv.DEFI_PRIVATE_KEY) {
11535
11597
  console.log(` Key: ${pc2.green("configured")}`);
11536
11598
  }
11537
- if (finalEnv.HYPEREVM_RPC_URL) {
11538
- console.log(` HyperEVM RPC: ${pc2.gray(finalEnv.HYPEREVM_RPC_URL)}`);
11539
- }
11540
- if (finalEnv.MANTLE_RPC_URL) {
11541
- console.log(` Mantle RPC: ${pc2.gray(finalEnv.MANTLE_RPC_URL)}`);
11599
+ const rpcSummary = [
11600
+ ["HYPEREVM_RPC_URL", "HyperEVM RPC"],
11601
+ ["MANTLE_RPC_URL", "Mantle RPC "],
11602
+ ["BASE_RPC_URL", "Base RPC "],
11603
+ ["BNB_RPC_URL", "BNB RPC "],
11604
+ ["MONAD_RPC_URL", "Monad RPC "]
11605
+ ];
11606
+ for (const [k, label] of rpcSummary) {
11607
+ const v = finalEnv[k];
11608
+ if (v) console.log(` ${label}: ${pc2.gray(v)}`);
11542
11609
  }
11543
11610
  console.log(pc2.bold(pc2.white("\n Next steps:")));
11544
11611
  console.log(` ${pc2.green("defi portfolio")} view balances & positions`);