descent-js
Version:
A Typescript library for interacting with the Descent Protocol
133 lines (132 loc) • 5 kB
JavaScript
// src/services/vault.ts
import {
ethers,
formatUnits,
formatEther
} from "ethers";
import { getContractAddress } from "../contracts/getContractAddresses.mjs";
import { VaultRouter__factory } from "../generated/factories.mjs";
var VaultHealthFactor = /* @__PURE__ */ ((VaultHealthFactor2) => {
VaultHealthFactor2["UNSAFE"] = "UNSAFE";
VaultHealthFactor2["SAFE"] = "SAFE";
return VaultHealthFactor2;
})(VaultHealthFactor || {});
var VaultOperations = /* @__PURE__ */ ((VaultOperations2) => {
VaultOperations2[VaultOperations2["DepositCollateral"] = 0] = "DepositCollateral";
VaultOperations2[VaultOperations2["WithdrawCollateral"] = 1] = "WithdrawCollateral";
VaultOperations2[VaultOperations2["MintCurrency"] = 2] = "MintCurrency";
VaultOperations2[VaultOperations2["BurnCurrency"] = 3] = "BurnCurrency";
return VaultOperations2;
})(VaultOperations || {});
var collateralizeVault = async (amount, collateral, owner, chainId, transaction, internal) => {
const collateralAddress = getContractAddress(collateral)[chainId];
const vaultContractAddress = getContractAddress("Vault")[chainId];
const _amount = BigInt(amount) * BigInt(1e6);
const to = getContractAddress("VaultRouter")[chainId];
let iface = internal.getInterface(VaultRouter__factory.abi);
const data = iface.encodeFunctionData("multiInteract", [
[],
[],
[],
[],
[]
]);
const txConfig = await internal.getTransactionConfig({
from: owner,
to,
data
});
const depositResResult = await transaction.send(txConfig, {});
return depositResResult;
};
var withdrawCollateral = async (amount, collateral, owner, chainId, transaction, internal, contract) => {
const collateralAddress = getContractAddress(collateral)[chainId];
const vaultContractAddress = getContractAddress("Vault")[chainId];
const _amount = BigInt(amount) * BigInt(1e6);
const to = getContractAddress("VaultRouter")[chainId];
let iface = internal.getInterface(VaultRouter__factory.abi);
const maxWithdrawable = (await contract.getVaultGetterContract()).getMaxWithdrawable(
vaultContractAddress,
collateralAddress,
owner
);
const formattedMaxWithdrawable = formatUnits((await maxWithdrawable).toString(), 6);
if (Number(amount) > Number(formattedMaxWithdrawable.toString())) {
throw new Error(" Withdrawal amount is more than available collateral balance");
}
const data = iface.encodeFunctionData("multiInteract", [
[],
[],
[],
[],
[]
]);
const txConfig = await internal.getTransactionConfig({
from: owner,
to,
data
});
const withdrawResResult = await transaction.send(txConfig, {});
return withdrawResResult;
};
var mintCurrency = async (amount, collateral, owner, chainId, transaction, internal, contract) => {
const collateralAddress = getContractAddress(collateral)[chainId];
const vaultContractAddress = getContractAddress("Vault")[chainId];
const _amount = BigInt(amount) * BigInt(1e18);
const maxBorrowable = (await contract.getVaultGetterContract()).getMaxBorrowable(
vaultContractAddress,
collateralAddress,
owner
);
const formattedmaxBorrowable = formatEther((await maxBorrowable).toString());
const to = getContractAddress("VaultRouter")[chainId];
let iface = internal.getInterface(VaultRouter__factory.abi);
const data = iface.encodeFunctionData("multiInteract", [
[],
[],
[],
[],
[]
]);
const txConfig = await internal.getTransactionConfig({
from: owner,
to,
data
});
const mintResResult = await transaction.send(txConfig, {});
return mintResResult;
};
var burnCurrency = async (amount, collateral, owner, chainId, transaction, internal, contract) => {
const collateralAddress = getContractAddress(collateral)[chainId];
const vaultContractAddress = getContractAddress("Vault")[chainId];
const _amount = BigInt(amount) * BigInt(1e18);
const balance = await (await contract.getCurrencyContract()).balanceOf(owner);
const formattedBalance = await formatEther(balance.toString());
if (Number(amount) > Number(formattedBalance.toString())) {
throw new Error("Payback xNGN: Insufficient funds");
}
const to = getContractAddress("VaultRouter")[chainId];
let iface = internal.getInterface(VaultRouter__factory.abi);
const data = iface.encodeFunctionData("multiInteract", [
[],
[],
[],
[],
[]
]);
const txConfig = await internal.getTransactionConfig({
from: owner,
to,
data
});
const burnResult = await transaction.send(txConfig, {});
return burnResult;
};
export {
VaultHealthFactor,
VaultOperations,
burnCurrency,
collateralizeVault,
mintCurrency,
withdrawCollateral
};