descent-js
Version:
A Typescript library for interacting with the Descent Protocol
112 lines (111 loc) • 6.01 kB
JavaScript
;
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 utils_exports = {};
__export(utils_exports, {
approveUSDC: () => approveUSDC,
approvexNGN: () => approvexNGN,
createError: () => createError,
getxNGNBalance: () => getxNGNBalance,
setMinterRole: () => setMinterRole,
updateTestPrice: () => updateTestPrice,
waitTime: () => waitTime
});
module.exports = __toCommonJS(utils_exports);
var import_contracts = __toESM(require("../contracts"), 1);
var import_getContractAddresses = require("../contracts/getContractAddresses");
var import_generated = require("../generated");
const createError = (message) => {
var _a, _b, _c, _d, _e, _f, _g, _h, _i, _j, _k;
const defaultMsg = message && typeof message === "string" ? message : "Something went wrong. Please try again later.";
const contractErrorMessage = ((_a = message == null ? void 0 : message.error) == null ? void 0 : _a.body) && ((_d = (_c = JSON.parse((_b = message == null ? void 0 : message.error) == null ? void 0 : _b.body)) == null ? void 0 : _c.error) == null ? void 0 : _d.message);
const systemMessage = (_e = message == null ? void 0 : message.errorArgs) == null ? void 0 : _e[0];
const contractRevertErrorMessage = ((_g = (_f = message == null ? void 0 : message.error) == null ? void 0 : _f.error) == null ? void 0 : _g.body) && ((_k = (_j = JSON.parse((_i = (_h = message == null ? void 0 : message.error) == null ? void 0 : _h.error) == null ? void 0 : _i.body)) == null ? void 0 : _j.error) == null ? void 0 : _k.message);
return new Error(
contractErrorMessage || contractRevertErrorMessage || systemMessage || defaultMsg
);
};
const approveUSDC = async (spender, amount, signer, transaction, internal) => {
var _a, _b;
const chainId = (_b = await ((_a = signer == null ? void 0 : signer.provider) == null ? void 0 : _a.getNetwork())) == null ? void 0 : _b.chainId.toString();
const owner = await signer.getAddress();
const to = (0, import_getContractAddresses.getContractAddress)("USDC")[chainId];
let iface = internal.getInterface(import_generated.USDC__factory.abi);
const data = iface.encodeFunctionData("approve", [spender, amount]);
const txConfig = await internal.getTransactionConfig({
from: owner,
to,
data
});
await transaction.send(txConfig, {});
await waitTime(50);
};
const waitTime = (seconds) => new Promise((resolve) => setTimeout(resolve, seconds * 1e3));
const updateTestPrice = async (signer) => {
var _a, _b;
const chainId = (_b = await ((_a = signer == null ? void 0 : signer.provider) == null ? void 0 : _a.getNetwork())) == null ? void 0 : _b.chainId.toString();
const collateralAddress = (0, import_getContractAddresses.getContractAddress)("USDC")[chainId];
const feedContractAddress = (0, import_getContractAddresses.getContractAddress)("Feed")[chainId];
const contract = new import_contracts.default(signer);
await (await contract.getVaultContract()).updateFeedContract(feedContractAddress);
await waitTime(50);
const price = BigInt(1100) * BigInt(1e6);
const priceUpdate = (await contract.getFeedContract()).mockUpdatePrice(collateralAddress, price);
(await priceUpdate).wait();
await waitTime(50);
};
const setMinterRole = async (signer, owner) => {
var _a, _b;
const chainId = (_b = await ((_a = signer == null ? void 0 : signer.provider) == null ? void 0 : _a.getNetwork())) == null ? void 0 : _b.chainId.toString();
const vaultContractAddress = (0, import_getContractAddresses.getContractAddress)("Vault")[chainId];
const contract = new import_contracts.default(signer);
await (await contract.getCurrencyContract()).setMinterRole(vaultContractAddress);
await waitTime(50);
};
const getxNGNBalance = async (owner, signer) => {
const contract = new import_contracts.default(signer);
const balance = await (await contract.getCurrencyContract()).balanceOf(owner);
await waitTime(50);
return balance * BigInt(1e19);
};
const approvexNGN = async (spender, amount, signer, transaction, internal) => {
var _a, _b;
const chainId = (_b = await ((_a = signer == null ? void 0 : signer.provider) == null ? void 0 : _a.getNetwork())) == null ? void 0 : _b.chainId.toString();
const owner = await signer.getAddress();
const _amount = BigInt(amount) * BigInt(1e19);
const to = (0, import_getContractAddresses.getContractAddress)("Currency")[chainId];
let iface = internal.getInterface(import_generated.Currency__factory.abi);
const data = iface.encodeFunctionData("approve", [spender, _amount]);
const txConfig = await internal.getTransactionConfig({
from: owner,
to,
data
});
await transaction.send(txConfig, {});
await waitTime(50);
};