UNPKG

descent-js

Version:

A Typescript library for interacting with the Descent Protocol

163 lines (162 loc) 4.59 kB
// src/index.ts import { ethers } from "ethers"; import { IMode } from "./types.mjs"; import { SupportedNetwork } from "./contracts/types.mjs"; import ContractManager from "./contracts.mjs"; import { waitTime } from "./libs/utils.mjs"; import { burnCurrency, collateralizeVault, mintCurrency, withdrawCollateral } from "./services/vault.mjs"; import { Transaction } from "./libs/transactions.mjs"; import { Internal } from "./libs/internal.mjs"; import { getContractAddress } from "./contracts/getContractAddresses.mjs"; import { getCollateralData, getVault } from "./services/getters.mjs"; var DescentClass = class { constructor(signer, provider, collateral, contracts, configMode, chainId) { // Extensions this.internal = new Internal(this); this.transaction = new Transaction(this); this.provider = provider; this.signer = signer; this.collateral = collateral; this.contracts = contracts; this.configMode = configMode; this.chainId = chainId; } /** * @dev Gets a vault detail by it's ID * @param ownerAddress Vault owner * @returns The Vault information */ async getVaultInfo(ownerAddress) { const result = await getVault( this.collateral, ownerAddress, this.chainId, this.contracts, this.internal ); return result; } /** * @dev Gets the information of collateral initialized in `create()` * @returns The collateral information */ async getCollateralInfo() { const result = await getCollateralData( this.collateral, this.chainId, this.contracts ); return result; } /** * @dev borrow xNGN against deposited USDC * @param amount amount of xNGN to borrow * @returns transaction obj */ async borrowCurrency(borrowAmount) { const owner = await this.signer.getAddress(); const result = await mintCurrency( borrowAmount, this.collateral, owner, this.chainId, this.transaction, this.internal, this.contracts ); return result; } /** * @dev repay borrowed xNGN for a particular vault * @param amount amount of xNGN to repay * @returns transaction obj */ async repayCurrency(amount) { const owner = await this.signer.getAddress(); const result = await burnCurrency( amount, this.collateral, owner, this.chainId, this.transaction, this.internal, this.contracts ); return result; } /** * @dev withdraw usdc for a particular vault * @param collateralAmount amount of unlocked collateral to withdraw * @returns transaction obj */ async withdrawCollateral(collateralAmount) { const owner = await this.signer.getAddress(); const result = await withdrawCollateral( collateralAmount, this.collateral, owner, this.chainId, this.transaction, this.internal, this.contracts ); return result; } /** * @dev deposit usdc for a particular vault * @param collateralAmount amount of unlocked collateral to withdraw * @param ownerAddress owner of the vault which should be the caller * @returns transaction obj */ async depositCollateral(collateralAmount) { const owner = await this.signer.getAddress(); const result = await collateralizeVault( collateralAmount, this.collateral, owner, this.chainId, this.transaction, this.internal ); return result; } }; async function create(mode, options) { if (!options.collateral) { throw new Error("Missing required options"); } let provider; let signer; if (mode == IMode.https) { provider = new ethers.JsonRpcProvider(options?.rpcUrl); signer = new ethers.Wallet(options.privateKey, provider); } if (mode == IMode.browser) { provider = new ethers.BrowserProvider(options?.ethereum); signer = await provider?.getSigner(); } const chainId = (await provider.getNetwork()).chainId.toString(10); if (![chainId].includes(SupportedNetwork.GOERLI)) { throw new Error(`chainId '${chainId}' is not supported.`); } const contracts = new ContractManager(signer); const descent = new DescentClass(signer, provider, options.collateral, contracts, mode, chainId); const vaultRouter = getContractAddress("VaultRouter")[chainId]; const relyResponse = (await contracts.getVaultContract()).rely(vaultRouter); (await relyResponse).wait(); await waitTime(50); return descent; } var Descent = { create }; var src_default = Descent; export { DescentClass, src_default as default };