@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.
@@ -339,7 +339,7 @@ var init_dist = __esm({
339
339
  aggregators;
340
340
  effectiveRpcUrl() {
341
341
  const chainEnv = this.name.toUpperCase().replace(/ /g, "_") + "_RPC_URL";
342
- return process.env[chainEnv] ?? process.env["HYPEREVM_RPC_URL"] ?? this.rpc_url;
342
+ return process.env[chainEnv] ?? this.rpc_url;
343
343
  }
344
344
  };
345
345
  ProtocolCategory = /* @__PURE__ */ ((ProtocolCategory2) => {
@@ -5729,6 +5729,9 @@ var init_dist2 = __esm({
5729
5729
  "function borrowRatePerBlock() external view returns (uint256)",
5730
5730
  "function totalSupply() external view returns (uint256)",
5731
5731
  "function totalBorrows() external view returns (uint256)",
5732
+ "function balanceOf(address account) external view returns (uint256)",
5733
+ "function exchangeRateStored() external view returns (uint256)",
5734
+ "function borrowBalanceStored(address account) external view returns (uint256)",
5732
5735
  "function mint(uint256 mintAmount) external returns (uint256)",
5733
5736
  "function redeem(uint256 redeemTokens) external returns (uint256)",
5734
5737
  "function borrow(uint256 borrowAmount) external returns (uint256)",
@@ -5844,7 +5847,7 @@ var init_dist2 = __esm({
5844
5847
  total_borrow: 0n
5845
5848
  };
5846
5849
  }
5847
- const [supplyRate, borrowRate, totalSupply, totalBorrows] = await Promise.all([
5850
+ const [supplyRate, borrowRate, totalSupplyVtoken, totalBorrows, exchangeRate] = await Promise.all([
5848
5851
  client.readContract({ address: vtoken, abi: CTOKEN_ABI, functionName: "supplyRatePerBlock" }).catch((e) => {
5849
5852
  throw DefiError.rpcError(`[${this.protocolName}] supplyRatePerBlock failed: ${e}`);
5850
5853
  }),
@@ -5852,29 +5855,62 @@ var init_dist2 = __esm({
5852
5855
  throw DefiError.rpcError(`[${this.protocolName}] borrowRatePerBlock failed: ${e}`);
5853
5856
  }),
5854
5857
  client.readContract({ address: vtoken, abi: CTOKEN_ABI, functionName: "totalSupply" }).catch(() => 0n),
5855
- client.readContract({ address: vtoken, abi: CTOKEN_ABI, functionName: "totalBorrows" }).catch(() => 0n)
5858
+ client.readContract({ address: vtoken, abi: CTOKEN_ABI, functionName: "totalBorrows" }).catch(() => 0n),
5859
+ client.readContract({ address: vtoken, abi: CTOKEN_ABI, functionName: "exchangeRateStored" }).catch(() => 0n)
5856
5860
  ]);
5857
5861
  const supplyPerBlock = Number(supplyRate) / 1e18;
5858
5862
  const borrowPerBlock = Number(borrowRate) / 1e18;
5859
5863
  const supplyApy = supplyPerBlock * BSC_BLOCKS_PER_YEAR * 100;
5860
5864
  const borrowApy = borrowPerBlock * BSC_BLOCKS_PER_YEAR * 100;
5861
- const supplyF = Number(totalSupply);
5865
+ const totalSupplyUnderlying = exchangeRate > 0n ? totalSupplyVtoken * exchangeRate / 10n ** 18n : totalSupplyVtoken;
5866
+ const supplyF = Number(totalSupplyUnderlying);
5862
5867
  const borrowF = Number(totalBorrows);
5863
- const utilization = supplyF > 0 ? borrowF / supplyF * 100 : 0;
5868
+ const utilization = supplyF > 0 ? Math.round(borrowF / supplyF * 1e4) / 100 : 0;
5864
5869
  return {
5865
5870
  protocol: this.protocolName,
5866
5871
  asset,
5867
5872
  supply_apy: supplyApy,
5868
5873
  borrow_variable_apy: borrowApy,
5869
5874
  utilization,
5870
- total_supply: totalSupply,
5875
+ total_supply: totalSupplyUnderlying,
5871
5876
  total_borrow: totalBorrows
5872
5877
  };
5873
5878
  }
5874
- async getUserPosition(_user) {
5875
- throw DefiError.unsupported(
5876
- `[${this.protocolName}] User position requires querying individual vToken balances`
5877
- );
5879
+ async getUserPosition(user) {
5880
+ if (!this.rpcUrl) throw DefiError.rpcError("No RPC URL configured");
5881
+ const client = createPublicClient13({ transport: http13(this.rpcUrl) });
5882
+ const ERC20_ABI42 = parseAbi17([
5883
+ "function symbol() external view returns (string)"
5884
+ ]);
5885
+ const supplies = [];
5886
+ const borrows = [];
5887
+ await Promise.all(this.vTokenCandidates.map(async (vtoken) => {
5888
+ try {
5889
+ const [vtokenBal, rate, borrowed, underlying] = await Promise.all([
5890
+ client.readContract({ address: vtoken, abi: CTOKEN_ABI, functionName: "balanceOf", args: [user] }),
5891
+ client.readContract({ address: vtoken, abi: CTOKEN_ABI, functionName: "exchangeRateStored" }),
5892
+ client.readContract({ address: vtoken, abi: CTOKEN_ABI, functionName: "borrowBalanceStored", args: [user] }),
5893
+ client.readContract({ address: vtoken, abi: CTOKEN_ABI, functionName: "underlying" }).catch(() => null)
5894
+ ]);
5895
+ if (vtokenBal === 0n && borrowed === 0n) return;
5896
+ const assetAddr = underlying ?? vtoken;
5897
+ const symbol = await client.readContract({
5898
+ address: assetAddr,
5899
+ abi: ERC20_ABI42,
5900
+ functionName: "symbol"
5901
+ }).catch(() => "?");
5902
+ const supplyUnderlying = vtokenBal * rate / 10n ** 18n;
5903
+ if (supplyUnderlying > 0n) supplies.push({ asset: assetAddr, symbol, amount: supplyUnderlying });
5904
+ if (borrowed > 0n) borrows.push({ asset: assetAddr, symbol, amount: borrowed });
5905
+ } catch {
5906
+ }
5907
+ }));
5908
+ return {
5909
+ protocol: this.protocolName,
5910
+ user,
5911
+ supplies,
5912
+ borrows
5913
+ };
5878
5914
  }
5879
5915
  };
5880
5916
  COMET_ABI = parseAbi18([
@@ -5884,6 +5920,8 @@ var init_dist2 = __esm({
5884
5920
  "function getBorrowRate(uint256 utilization) external view returns (uint64)",
5885
5921
  "function totalSupply() external view returns (uint256)",
5886
5922
  "function totalBorrow() external view returns (uint256)",
5923
+ "function balanceOf(address account) external view returns (uint256)",
5924
+ "function borrowBalanceOf(address account) external view returns (uint256)",
5887
5925
  "function supply(address asset, uint256 amount) external",
5888
5926
  "function withdraw(address asset, uint256 amount) external"
5889
5927
  ]);
@@ -6014,10 +6052,30 @@ var init_dist2 = __esm({
6014
6052
  total_borrow: totalBorrow
6015
6053
  };
6016
6054
  }
6017
- async getUserPosition(_user) {
6018
- throw DefiError.unsupported(
6019
- `[${this.protocolName}] User position requires querying Comet balanceOf + borrowBalanceOf`
6020
- );
6055
+ async getUserPosition(user) {
6056
+ if (!this.rpcUrl) throw DefiError.rpcError("No RPC URL configured");
6057
+ const client = createPublicClient14({ transport: http14(this.rpcUrl) });
6058
+ const ERC20_ABI42 = parseAbi18([
6059
+ "function symbol() external view returns (string)"
6060
+ ]);
6061
+ const [supplyBal, borrowBal, baseToken] = await Promise.all([
6062
+ client.readContract({ address: this.comet, abi: COMET_ABI, functionName: "balanceOf", args: [user] }).catch(() => 0n),
6063
+ client.readContract({ address: this.comet, abi: COMET_ABI, functionName: "borrowBalanceOf", args: [user] }).catch(() => 0n),
6064
+ client.readContract({ address: this.comet, abi: COMET_ABI, functionName: "baseToken" })
6065
+ ]);
6066
+ const baseSymbol = await client.readContract({
6067
+ address: baseToken,
6068
+ abi: ERC20_ABI42,
6069
+ functionName: "symbol"
6070
+ }).catch(() => "?");
6071
+ const supplies = supplyBal > 0n ? [{ asset: baseToken, symbol: baseSymbol, amount: supplyBal }] : [];
6072
+ const borrows = borrowBal > 0n ? [{ asset: baseToken, symbol: baseSymbol, amount: borrowBal }] : [];
6073
+ return {
6074
+ protocol: this.protocolName,
6075
+ user,
6076
+ supplies,
6077
+ borrows
6078
+ };
6021
6079
  }
6022
6080
  };
6023
6081
  EULER_VAULT_ABI = parseAbi19([