contracts-imtbl/test/shared/utilities.ts
2024-08-27 15:13:22 +08:00

193 lines
5.2 KiB
TypeScript

import { ethers, MaxUint256, formatUnits } from "ethers"
const maxUint256 = MaxUint256
export function newWallet() {
return ethers.Wallet.createRandom()
}
export function bigNumberify(n: any) {
return BigInt(n)
}
export function expandDecimals(n: any, decimals: number) {
return BigInt(n) * (BigInt(10)**bigNumberify(decimals))
}
export async function send(provider: any, method: string, params: any[] = []) {
await provider.send(method, params)
}
export async function mineBlock(provider: any) {
await send(provider, "evm_mine")
}
export async function increaseTime(provider: any, seconds: number) {
await send(provider, "evm_increaseTime", [seconds])
}
export async function gasUsed(provider: any, tx: any) {
return (await provider.getTransactionReceipt(tx.hash)).gasUsed
}
export async function getNetworkFee(provider: any, tx: any) {
const gas = await gasUsed(provider, tx)
return gas.mul(tx.gasPrice)
}
export async function reportGasUsed(provider: any, tx: any, label: any) {
const { gasUsed } = await provider.getTransactionReceipt(tx.hash)
console.info(label, gasUsed.toString())
return gasUsed
}
export async function getBlockTime(provider: any) {
const blockNumber = await provider.getBlockNumber()
const block = await provider.getBlock(blockNumber)
return block.timestamp
}
export async function getTxnBalances(provider: any, user: any, txn: any, callback: any) {
const balance0 = await provider.getBalance(user.address)
const tx = await txn()
const fee = await getNetworkFee(provider, tx)
const balance1 = await provider.getBalance(user.address)
callback(balance0, balance1, fee)
}
export function print(label: any, value: any, decimals: any) {
if (decimals === 0) {
console.log(label, value.toString())
return
}
const valueStr = formatUnits(value, decimals)
console.log(label, valueStr)
}
// export function getPriceBitArray(prices: number[]) {
// let priceBitArray = []
// let shouldExit = false
// for (let i = 0; i < parseInt((prices.length - 1) / 8) + 1; i++) {
// let priceBits = 0n
// for (let j = 0; j < 8; j++) {
// let index = i * 8 + j
// if (index >= prices.length) {
// shouldExit = true
// break
// }
// const price = BigInt(prices[index])
// if (price.gt(BigInt("2147483648"))) { // 2^31
// throw new Error(`price exceeds bit limit ${price.toString()}`)
// }
// priceBits = priceBits.or(price.shln(j * 32))
// }
// priceBitArray.push(priceBits.toString())
// if (shouldExit) { break }
// }
// return priceBitArray
// }
// export function getPriceBits(prices: any) {
// if (prices.length > 8) {
// throw new Error("max prices.length exceeded")
// }
// let priceBits = BigInt('0')
// for (let j = 0; j < 8; j++) {
// let index = j
// if (index >= prices.length) {
// break
// }
// const price = BigInt(prices[index])
// if (price.gt(BigInt("2147483648"))) { // 2^31
// throw new Error(`price exceeds bit limit ${price.toString()}`)
// }
// priceBits = priceBits.or(price.shln(j * 32))
// }
// return priceBits.toString()
// }
const limitDecimals = (amount: number, maxDecimals: number) => {
let amountStr = amount.toString();
if (maxDecimals === undefined) {
return amountStr;
}
if (maxDecimals === 0) {
return amountStr.split(".")[0];
}
const dotIndex = amountStr.indexOf(".");
if (dotIndex !== -1) {
let decimals = amountStr.length - dotIndex - 1;
if (decimals > maxDecimals) {
amountStr = amountStr.substr(0, amountStr.length - (decimals - maxDecimals));
}
}
return amountStr;
}
const padDecimals = (amount: number, minDecimals: number) => {
let amountStr = amount.toString();
const dotIndex = amountStr.indexOf(".");
if (dotIndex !== -1) {
const decimals = amountStr.length - dotIndex - 1;
if (decimals < minDecimals) {
amountStr = amountStr.padEnd(amountStr.length + (minDecimals - decimals), "0");
}
} else {
amountStr = amountStr + ".0000";
}
return amountStr;
}
// const parseValue = (value: number, tokenDecimals: number) => {
// const pValue = parseFloat(value);
// if (isNaN(pValue)) {
// return undefined;
// }
// value = limitDecimals(value, tokenDecimals);
// const amount = ethers.utils.parseUnits(value, tokenDecimals);
// return bigNumberify(amount);
// }
export function numberWithCommas(x: any) {
if (!x) {
return "...";
}
var parts = x.toString().split(".");
parts[0] = parts[0].replace(/\B(?=(\d{3})+(?!\d))/g, ",");
return parts.join(".");
}
// const formatAmount = (amount: number, tokenDecimals: number, displayDecimals: number, useCommas: any, defaultValue: any) => {
// if (!defaultValue) {
// defaultValue = "...";
// }
// if (amount === undefined || amount.toString().length === 0) {
// return defaultValue;
// }
// if (displayDecimals === undefined) {
// displayDecimals = 4;
// }
// let amountStr = formatUnits(amount, tokenDecimals);
// amountStr = limitDecimals(amountStr, displayDecimals);
// if (displayDecimals !== 0) {
// amountStr = padDecimals(amountStr, displayDecimals);
// }
// if (useCommas) {
// return numberWithCommas(amountStr);
// }
// return amountStr;
// };