@raac/rpc 1.1.0-beta.21 → 1.1.0-beta.22
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.
|
@@ -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,26 @@ async function borrowFromLendingPoolWithInsurance(chainId, adapterName, amount,
|
|
|
10
17
|
if (!adapterAddress) {
|
|
11
18
|
throw new Error(`Adapter ${adapterName} not found for chain ${chainId}`);
|
|
12
19
|
}
|
|
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, ethers_1.ethers.formatEther(amount.toString()), 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.toString();
|
|
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
|
|
13
37
|
const tx = await lendingPoolContract.borrowWithInsurance(adapterAddress, data, amount);
|
|
14
38
|
const receipt = await tx.wait();
|
|
39
|
+
console.log(`Borrowed with insurance successfully. Transaction hash: ${receipt?.hash}`);
|
|
15
40
|
return receipt.status === 1;
|
|
16
41
|
}
|
|
17
42
|
catch (error) {
|