@coinmeca/ethers
Version:
Solidty helpers and utilities for using ethers.
181 lines (180 loc) • 9.96 kB
JavaScript
;
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;
const 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 (key && app)
return Array.isArray(key)
? await Promise.all(key.map(async (k) => typeof k === "string"
? await (typeof app === "function" ? await app() : app)?.getHistory(k)
: await (typeof app === "function" ? await app() : app)?.getHistoryByIndex(k)))
: typeof key === "string"
? await (typeof app === "function" ? await app() : app)?.getHistory(key)
: await (typeof app === "function" ? await app() : app)?.getHistoryByIndex(key);
else
return [];
};
const history = async (...args) => {
const app = args && args?.length > 0 && (typeof args[0] === "object" || typeof args[0] === "function") ? args[0] : contracts?.app;
const h = args &&
(args?.length > 0 && (Array.isArray(args[0]) || typeof args[0] === "string" || typeof args[0] === "number")
? args[0]
: args?.length > 1 && (Array.isArray(args[1]) || typeof args[1] === "string" || typeof args[1] === "number")
? 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);
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(`--------------------------------------------------------------------------------`));
for (let i = 0; i < h.length; i++) {
const p = stringify_1.c[h[i].category].includes("Long") || stringify_1.c[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)(h[i].category));
console.log(utils_1.color.lightGray((0, utils_1._)(`State:`, 14)), (0, stringify_1.state)(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)((0, utils_1.u)(h[i].price)))));
console.log(utils_1.color.lightGray((0, utils_1._)(`Amount:`, 14)), (0, utils_1.f)((0, utils_1.u)(h[i].amount)));
console.log(utils_1.color.lightGray((0, utils_1._)(`Quantity:`, 14)), utils_1.color.yellow((0, utils_1.f)((0, utils_1.u)(h[i].quantity))));
p && console.log(utils_1.color.lightGray((0, utils_1._)(`Leverage:`, 14)), utils_1.color.white((0, utils_1.f)((0, utils_1.u)(h[i].fees))));
p &&
console.log(utils_1.color.lightGray((0, utils_1._)(`Multiplier:`, 14)), utils_1.color.lightGray("x"), utils_1.color.white((0, utils_1.f)((0, utils_1.getMultiplier)((0, utils_1.u)(h[i].amount), (0, utils_1.u)(h[i].fees)))));
console.log(utils_1.color.lightGray(`--------------------------------------------------------------------------------`));
}
}
else
fn(h);
return h;
}
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 };
}