UNPKG

@coinmeca/ethers

Version:

Solidty helpers and utilities for using ethers.

203 lines (202 loc) 11.2 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.signers = void 0; exports.Accounts = Accounts; const hardhat_1 = require("hardhat"); const utils_1 = require("./utils"); const stringify_1 = require("./types/stringify"); const ERC20_1 = require("./interfaces/ERC20"); async function Accounts(contracts) { if (!exports.signers) { exports.signers = { ...(await Promise.all((await hardhat_1.ethers.getSigners()).map(async (a) => { return a; }))), }; exports.signers.deployer = exports.signers[0]; } const User = (indexOrName) => { const name = typeof indexOrName === "string" ? indexOrName : indexOrName.toString(); const signer = exports.signers[name]; const address = signer.address; const balance = async (token, display) => { const tokens = token ? typeof token === "string" ? [await (0, ERC20_1.ERC20)(token)] : [token] : typeof contracts?.tokens === "object" ? Object.values(contracts?.tokens) : Array.isArray(contracts?.tokens) ? contracts?.tokens : undefined; if (tokens) { let token = await tokens[0].balanceOf(User(name)); if (tokens?.length > 1 || (tokens?.length === 1 && display)) { console.log(utils_1.color.lightGray(`--------------------- User: '${name}' Wallet ----------------------`)); (0, utils_1.a)(User(name), true); console.log(utils_1.color.lightGray(`-------------------------------------------------------------`)); if (tokens?.length > 0) { for (let i = 0; i < tokens?.length; i++) { if (tokens[i]) { const symbol = tokens[i].symbol; const balance = tokens?.length === 1 && i === 0 ? token : await tokens[i].balanceOf(User(name)); if (symbol == "MECA") console.log(utils_1.color.lightGray(`-------------------------------------------------------------`)); console.log(`${(0, utils_1._)(`${symbol}:`, 14)}${utils_1.font.bold(utils_1.color.yellow((0, utils_1.f)(balance)))}`); } } } else { console.log(`There are no tokens deployed yet.`); } console.log(utils_1.color.lightGray(`-------------------------------------------------------------`)); } return tokens?.length == 1 ? token : tokens; } }; const send = async (token, to, amount) => { const t = typeof token === "string" ? await (0, ERC20_1.ERC20)(token) : token; return await t.use(User(name)).transfer(to, amount); }; const faucet = async (token, amount, display) => { const t = typeof token === "string" ? await (0, ERC20_1.ERC20)(token) : token; const result = await t.use(User(name)).faucet(User(name), amount); if (result && display) { console.log(utils_1.color.lightGray(`-------------------------------------------------------------`)); console.log(`-> ✨ '${name}' earn '${amount} ${t.symbol}'`); console.log(utils_1.color.lightGray(`-------------------------------------------------------------`)); } return result; }; const allowance = async (token, spender) => { const t = typeof token === "string" ? await (0, ERC20_1.ERC20)(token) : token; return await t.use(User(name)).allowance(address, (0, utils_1.a)(spender)); }; const approve = async (token, to, amount) => { const t = typeof token === "string" ? await (0, ERC20_1.ERC20)(token) : token; return await t.use(User(name)).approve((0, utils_1.a)(to), amount); }; const getHistory = async (...args) => { const key = args ? Array.isArray(args[0]) && (typeof args[0][0] === "string" || typeof args[0][0] === "number") ? args[0] : typeof args[0] === "string" || typeof args[0] === "number" ? args[0] : Array.isArray(args[1]) && (typeof args[1][0] === "string" || typeof args[1][0] === "number") ? args[1] : typeof args[1] === "string" || typeof args[1] === "number" ? args[1] : undefined : undefined; let app = args ? args?.length > 0 && (typeof args[0] === "object" || typeof args[0] === "function") ? args[0] : args?.length > 1 && (typeof args[1] === "object" || typeof args[1] === "function") ? args[1] : contracts?.app : contracts?.app; if (typeof key !== "undefined" && app) { app = typeof app === "function" ? await app() : app; return Array.isArray(key) ? await Promise.all(key.map(async (k) => typeof k === "string" ? await app?.getHistory(k) : await app?.["getHistoryByIndex(address, uint)"]((0, utils_1.a)(User(name)), k))) : typeof key === "string" ? await app?.getHistory(key) : await app?.["getHistoryByIndex(address, uint)"]((0, utils_1.a)(User(name)), key); } else return Array.isArray(key) ? [] : undefined; }; const history = async (...args) => { let app = args && args?.length > 0 && (typeof args[0] === "object" || typeof args[0] === "function") ? args[0] : contracts?.app; app = typeof app === "function" ? await app() : app; let h = args && (args?.length > 0 && (Array.isArray(args[0]) || typeof args[0] === "string" || typeof args[0] === "number") ? typeof args[0] === "string" ? await app?.getHistory(args[0]) : await app?.["getHistoryByIndex(address, uint)"]((0, utils_1.a)(User(name)), args[0]) : args?.length > 1 && (Array.isArray(args[1]) || typeof args[1] === "string" || typeof args[1] === "number") ? typeof args[1] === "string" ? await app?.getHistory(args[1]) : await app?.["getHistoryByIndex(address, uint)"]((0, utils_1.a)(User(name)), args[1]) : await app?.getAllHistory((0, utils_1.a)(User(name)))); const fn = args && (args?.length === 3 && typeof args[2] === "function" ? args[2] : args?.length === 2 && typeof args[1] === "function" ? args[1] : undefined); const t = Array.isArray(h?.[0]); if (h) { if (!fn || typeof fn !== "function") { console.log(utils_1.color.lightGray(`--------------------------- User: '${name}' History ---------------------------`)); console.log(`Total: ${h?.length}`); console.log(utils_1.color.lightGray(`--------------------------------------------------------------------------------`)); h = (Array.isArray(h?.[0]) ? h : [h])?.map((o) => { return { key: o.key, owner: o.owner, time: Number(o.time), market: o.market, category: stringify_1.c[o.category], state: stringify_1.s[o.state], pay: o.pay, item: o.item, price: (0, utils_1.u)(o.price), amount: (0, utils_1.u)(o.amount), quantity: (0, utils_1.u)(o.quantity), fees: (0, utils_1.u)(o.fees), }; }); for (let i = 0; i < h.length; i++) { const p = h[i].category?.includes("Long") || h[i].category?.includes("Short"); console.log(utils_1.color.lightGray((0, utils_1._)(`Key:`, 14)), h[i].key); console.log(utils_1.color.lightGray((0, utils_1._)(`Market:`, 14)), utils_1.color.lightGray(h[i].market)); console.log(utils_1.color.lightGray((0, utils_1._)(`Category:`, 14)), (0, stringify_1.category)(stringify_1.c?.indexOf(h[i].category))); console.log(utils_1.color.lightGray((0, utils_1._)(`State:`, 14)), (0, stringify_1.state)(stringify_1.s?.indexOf(h[i].state))); console.log(utils_1.color.lightGray((0, utils_1._)(`Price:`, 14)), utils_1.font.bold(utils_1.color.yellow((0, utils_1.f)(h[i].price)))); console.log(utils_1.color.lightGray((0, utils_1._)(`Amount:`, 14)), (0, utils_1.f)(h[i].amount)); console.log(utils_1.color.lightGray((0, utils_1._)(`Quantity:`, 14)), utils_1.color.yellow((0, utils_1.f)(h[i].quantity))); p && console.log(utils_1.color.lightGray((0, utils_1._)(`Debt:`, 14)), utils_1.color.white((0, utils_1.f)(h[i].fees))); p && console.log(utils_1.color.lightGray((0, utils_1._)(`Leverage:`, 14)), utils_1.color.lightGray("x"), utils_1.color.white((0, utils_1.f)((0, utils_1.getLeverage)(h[i].amount, h[i].fees)))); console.log(utils_1.color.lightGray(`--------------------------------------------------------------------------------`)); } } else { h = await Promise.all((t ? h : [h])?.map(async (a) => fn(a))); } return t ? h : h?.[0]; } return []; }; const as = async (address) => { const signer = await hardhat_1.ethers.getSigner(address); exports.signers = { ...exports.signers, [`${name}`]: signer, }; return module; }; const set = (name) => { exports.signers = { ...exports.signers, [`${name}`]: signer, }; return module; }; const module = { ...signer, signer, address, name, as, set, faucet, balance, allowance, approve, send, history, getHistory, }; if (!address || typeof signer !== "object") return { as }; return module; }; return { User }; }