@descent-protocol/sdk
Version:
A Typescript library for interacting with the Descent Protocol
258 lines (257 loc) • 7.77 kB
JavaScript
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
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 __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_types2 = require("./contracts/types");
var import_vault = require("./services/vault");
var import_transactions = require("./libs/transactions");
var import_internal = require("./libs/internal");
var import_getters = require("./services/getters");
var import_utility = require("./services/utility");
class DescentClass {
signer;
provider;
collateral;
configMode;
chainId;
// Extensions
internal = new import_internal.Internal(this);
transaction = new import_transactions.Transaction(this);
constructor(signer, provider, collateral, configMode, chainId) {
this.provider = provider;
this.signer = signer;
this.collateral = collateral;
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.internal,
this.signer
);
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.signer);
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.signer
);
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.signer
);
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.signer
);
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;
}
/**
* @dev initializes a vault for a an address
* @returns transaction obj
*/
async setupVault() {
const owner = await this.signer.getAddress();
const result = await (0, import_vault.setupVault)(owner, this.chainId, this.transaction, this.internal);
return result;
}
/**
* @dev Check if a vault has been initialized
* @returns boolean
*/
async getVaultSetupStatus() {
const owner = await this.signer.getAddress();
const result = await (0, import_getters.checkVaultSetupStatus)(owner, this.chainId, this.signer);
return result;
}
// UTILITY HELPER FUNCTIONS FUNCTIONS
/**
* @dev gets the collateral token balance of an address
* @param owner address of the owner
* @returns balance
*/
async getCollateralTokenBalance(owner) {
const result = await (0, import_utility.getCollateralTokenBalance)(
this.collateral,
owner,
this.chainId,
this.signer
);
return result;
}
/**
* @dev approve the vault to take a certain amount of collateral
* @param amount amount of allowance
* @returns tx object
*/
async approveCollateral(amount) {
const owner = await this.signer.getAddress();
const result = await (0, import_utility.approveCollateralToken)(
this.collateral,
owner,
amount,
this.chainId,
this.internal,
this.transaction
);
return result;
}
/**
* @dev approve the vault to take a certain amount of collateral
* @param amount amount of allowance
* @returns tx object
*/
async collateralTokenAllowance(approver) {
const result = await (0, import_utility.getCollateralTokenAllowance)(
this.collateral,
approver,
this.chainId,
this.signer
);
return result;
}
/**
* @dev gets the xNGN balalnce of an address
* @param owner address of the owner
* @returns balance
*/
async getxNGNBalance(owner) {
const result = await (0, import_utility.getCurrencyTokenBalance)(owner, this.chainId, this.signer);
return result;
}
/**
* @dev approve the vault to take a certain amount of xNGN
* @param amount amount of allowance
* @returns tx object
*/
async approvexNGN(amount) {
const owner = await this.signer.getAddress();
const result = await (0, import_utility.approveCurrencyToken)(
owner,
amount,
this.chainId,
this.internal,
this.transaction
);
return result;
}
}
async function create(mode, options) {
const supportedNetworks = [import_types2.SupportedNetwork.BASE_GOERLI, import_types2.SupportedNetwork.BASE_SEPOLIA];
if (!options.collateral) {
throw new Error("Missing required options");
}
let provider;
let signer;
if (mode == "https") {
provider = new import_ethers.ethers.JsonRpcProvider(options == null ? void 0 : options.rpcUrl);
signer = new import_ethers.ethers.Wallet(options.privateKey, provider);
}
if (mode == "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 (!supportedNetworks.includes(chainId)) {
throw new Error(`chainId '${chainId}' is not supported.`);
}
const descent = new DescentClass(signer, provider, options.collateral, mode, chainId);
return descent;
}
const Descent = {
create
};
var src_default = Descent;