UNPKG

@descent-protocol/sdk

Version:

A Typescript library for interacting with the Descent Protocol

81 lines (80 loc) 4.37 kB
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 utils_exports = {}; __export(utils_exports, { approveUSDC: () => approveUSDC, approvexNGN: () => approvexNGN, createError: () => createError, getxNGNBalance: () => getxNGNBalance, waitTime: () => waitTime }); module.exports = __toCommonJS(utils_exports); var import_getContractAddresses = require("../contracts/getContractAddresses"); var import_generated = require("../generated"); var import_contracts = require("../contracts"); 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 getxNGNBalance = async (owner, 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 currencyContractAddress = (0, import_getContractAddresses.getContractAddress)("Currency", chainId); const currencyContract = (0, import_contracts.Contract)(currencyContractAddress, import_generated.Currency__factory.abi, signer); const balance = await currencyContract.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); };