@raac/rpc 1.1.0-beta.21 → 1.1.0-beta.23

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.
@@ -2,6 +2,7 @@
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  const getAdapterAddress_1 = require("./getAdapterAddress");
4
4
  const _helpers_1 = require("./_helpers");
5
+ const ethers_1 = require("ethers");
5
6
  async function borrowFromLendingPool(chainId, adapterName, amount, data, signer) {
6
7
  try {
7
8
  const lendingPoolContract = await (0, _helpers_1.getLendingPoolContract)(chainId, signer);
@@ -9,7 +10,8 @@ async function borrowFromLendingPool(chainId, adapterName, amount, data, signer)
9
10
  if (!adapterAddress) {
10
11
  throw new Error(`Adapter ${adapterName} not found for chain ${chainId}`);
11
12
  }
12
- const tx = await lendingPoolContract.borrow(adapterAddress, data, amount);
13
+ const amountInWei = ethers_1.ethers.parseEther(amount);
14
+ const tx = await lendingPoolContract.borrow(adapterAddress, data, amountInWei);
13
15
  const receipt = await tx.wait();
14
16
  return receipt.status === 1;
15
17
  }
@@ -1,7 +1,14 @@
1
1
  "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
2
5
  Object.defineProperty(exports, "__esModule", { value: true });
6
+ const ethers_1 = require("ethers");
3
7
  const getAdapterAddress_1 = require("./getAdapterAddress");
4
8
  const _helpers_1 = require("./_helpers");
9
+ const checkAllowance_1 = __importDefault(require("../../methods/commons/checkAllowance"));
10
+ const approveAsset_1 = __importDefault(require("../../wallet/assets/approveAsset"));
11
+ const calculateInsuranceFee_1 = __importDefault(require("./calculateInsuranceFee"));
5
12
  // TODO: interface changed
6
13
  async function borrowFromLendingPoolWithInsurance(chainId, adapterName, amount, data, signer) {
7
14
  try {
@@ -10,8 +17,27 @@ async function borrowFromLendingPoolWithInsurance(chainId, adapterName, amount,
10
17
  if (!adapterAddress) {
11
18
  throw new Error(`Adapter ${adapterName} not found for chain ${chainId}`);
12
19
  }
13
- const tx = await lendingPoolContract.borrowWithInsurance(adapterAddress, data, amount);
20
+ const userAddress = await signer.getAddress();
21
+ const lendingPoolAddress = await lendingPoolContract.getAddress();
22
+ // Calculate the insurance fee they need to pay
23
+ const insuranceFee = await (0, calculateInsuranceFee_1.default)(chainId, adapterName, userAddress, data, amount, signer.provider);
24
+ console.log(`Insurance fee required: ${ethers_1.ethers.formatEther(insuranceFee)} crvUSD`);
25
+ // Check if the user has approved enough crvUSD for the insurance fee
26
+ const currentAllowance = await (0, checkAllowance_1.default)(chainId, "crvusd", userAddress, lendingPoolAddress, signer.provider);
27
+ // If allowance is less than the insurance fee, approve it
28
+ if (currentAllowance < insuranceFee) {
29
+ const amountToApprove = insuranceFee;
30
+ await (0, approveAsset_1.default)(chainId, "crvusd", lendingPoolAddress, ethers_1.ethers.formatEther(amountToApprove), signer);
31
+ console.log(`Approved ${ethers_1.ethers.formatEther(insuranceFee)} crvUSD for insurance fee`);
32
+ }
33
+ else {
34
+ console.log(`Already approved sufficient crvUSD for insurance fee: ${ethers_1.ethers.formatEther(currentAllowance)}`);
35
+ }
36
+ // Now proceed with the borrow transaction
37
+ const amountInWei = ethers_1.ethers.parseEther(amount);
38
+ const tx = await lendingPoolContract.borrowWithInsurance(adapterAddress, data, amountInWei);
14
39
  const receipt = await tx.wait();
40
+ console.log(`Borrowed with insurance successfully. Transaction hash: ${receipt?.hash}`);
15
41
  return receipt.status === 1;
16
42
  }
17
43
  catch (error) {
@@ -12,7 +12,7 @@ async function calculateInsuranceFee(chainId, adapterName, userAddress, data, am
12
12
  }
13
13
  const lendingPoolContract = await (0, _helpers_1.getLendingPoolContract)(chainId, provider);
14
14
  const amountInWei = ethers_1.ethers.parseEther(amount);
15
- const fee = await lendingPoolContract.calculateInsuranceFee(adapterAddress, userAddress, data, amountInWei.toString());
15
+ const fee = await lendingPoolContract.calculateInsuranceFee(adapterAddress, userAddress, data, amountInWei);
16
16
  console.log(`Insurance fee: ${ethers_1.ethers.formatEther(fee)}`);
17
17
  return fee;
18
18
  }
@@ -1,5 +1,5 @@
1
1
  import { ChainId } from "../../configs/chains";
2
2
  import { LendingPoolAdapterId } from "./types";
3
- import { BigNumberish, BytesLike, Signer } from "ethers";
4
- declare function borrowFromLendingPool(chainId: ChainId, adapterName: LendingPoolAdapterId, amount: BigNumberish, data: BytesLike, signer: Signer): Promise<boolean>;
3
+ import { BytesLike, Signer } from "ethers";
4
+ declare function borrowFromLendingPool(chainId: ChainId, adapterName: LendingPoolAdapterId, amount: string, data: BytesLike, signer: Signer): Promise<boolean>;
5
5
  export default borrowFromLendingPool;
@@ -1,5 +1,5 @@
1
- import { BigNumberish, BytesLike, Signer } from "ethers";
1
+ import { BytesLike, Signer } from "ethers";
2
2
  import { ChainId } from "../../configs/chains";
3
3
  import { LendingPoolAdapterId } from "./types";
4
- declare function borrowFromLendingPoolWithInsurance(chainId: ChainId, adapterName: LendingPoolAdapterId, amount: BigNumberish, data: BytesLike, signer: Signer): Promise<boolean>;
4
+ declare function borrowFromLendingPoolWithInsurance(chainId: ChainId, adapterName: LendingPoolAdapterId, amount: string, data: BytesLike, signer: Signer): Promise<boolean>;
5
5
  export default borrowFromLendingPoolWithInsurance;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@raac/rpc",
3
- "version": "1.1.0-beta.21",
3
+ "version": "1.1.0-beta.23",
4
4
  "description": "RPC Library for RAAC ",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/types/index.d.ts",