UNPKG

descent-js

Version:

A Typescript library for interacting with the Descent Protocol

187 lines (186 loc) 6.38 kB
"use strict"; var __create = Object.create; var __defProp = Object.defineProperty; var __getOwnPropDesc = Object.getOwnPropertyDescriptor; var __getOwnPropNames = Object.getOwnPropertyNames; var __getProtoOf = Object.getPrototypeOf; var __hasOwnProp = Object.prototype.hasOwnProperty; var __export = (target, all) => { for (var name in all) __defProp(target, name, { get: all[name], enumerable: true }); }; var __copyProps = (to, from, except, desc) => { if (from && typeof from === "object" || typeof from === "function") { for (let key of __getOwnPropNames(from)) if (!__hasOwnProp.call(to, key) && key !== except) __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable }); } return to; }; var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps( // If the importer is in node compatibility mode or this is not an ESM // file that has been converted to a CommonJS file using a Babel- // compatible transform (i.e. "__esModule" has not been set), then set // "default" to the CommonJS "module.exports" for node compatibility. isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target, mod )); var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod); var src_exports = {}; __export(src_exports, { DescentClass: () => DescentClass, default: () => src_default }); module.exports = __toCommonJS(src_exports); var import_ethers = require("ethers"); var import_types = require("./types"); var import_types2 = require("./contracts/types"); var import_contracts = __toESM(require("./contracts"), 1); var import_utils = require("./libs/utils"); var import_vault = require("./services/vault"); var import_transactions = require("./libs/transactions"); var import_internal = require("./libs/internal"); var import_getContractAddresses = require("./contracts/getContractAddresses"); var import_getters = require("./services/getters"); class DescentClass { constructor(signer, provider, collateral, contracts, configMode, chainId) { // Extensions this.internal = new import_internal.Internal(this); this.transaction = new import_transactions.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 (0, import_getters.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 (0, import_getters.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 (0, import_vault.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 (0, import_vault.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 (0, import_vault.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 (0, import_vault.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 == import_types.IMode.https) { provider = new import_ethers.ethers.JsonRpcProvider(options == null ? void 0 : options.rpcUrl); signer = new import_ethers.ethers.Wallet(options.privateKey, provider); } if (mode == import_types.IMode.browser) { provider = new import_ethers.ethers.BrowserProvider(options == null ? void 0 : options.ethereum); signer = await (provider == null ? void 0 : provider.getSigner()); } const chainId = (await provider.getNetwork()).chainId.toString(10); if (![chainId].includes(import_types2.SupportedNetwork.GOERLI)) { throw new Error(`chainId '${chainId}' is not supported.`); } const contracts = new import_contracts.default(signer); const descent = new DescentClass(signer, provider, options.collateral, contracts, mode, chainId); const vaultRouter = (0, import_getContractAddresses.getContractAddress)("VaultRouter")[chainId]; const relyResponse = (await contracts.getVaultContract()).rely(vaultRouter); (await relyResponse).wait(); await (0, import_utils.waitTime)(50); return descent; } const Descent = { create }; var src_default = Descent;