Skip to content

Instantly share code, notes, and snippets.

@dvgui
Created June 24, 2022 14:37
Show Gist options
  • Save dvgui/3fe78aa35cf688cdf88b2141e145724c to your computer and use it in GitHub Desktop.
Save dvgui/3fe78aa35cf688cdf88b2141e145724c to your computer and use it in GitHub Desktop.
timeskip eth
const { ethers, upgrades, network } = require("hardhat");
module.exports.toWei = (n) => ethers.BigNumber.from(10).pow(18).mul(n);
module.exports.toEther = (n) => ethers.utils.formatEther(n);
module.exports.toBN = (n) => ethers.BigNumber.from(n);
module.exports.numberLastBlock = async () =>
(await ethers.provider.getBlock("latest")).number;
module.exports.timeStampLastBlock = async () =>
(await ethers.provider.getBlock("latest")).timestamp;
module.exports.extractCost = (tx, additionalData = {}, COIN_PRICE = 400) => {
const GAS_SPENT = +tx.gasUsed;
const GAS_PRICE = 5e9;
const WEI_PRICE = COIN_PRICE / 1e18;
const COST_BNB = +((GAS_SPENT * GAS_PRICE) / 1e18).toFixed(6);
const COST_USD = +(GAS_SPENT * GAS_PRICE * WEI_PRICE).toFixed(2);
const BLOCK_NUMBER = tx.blockNumber;
const ACCOUNT = tx.from.slice(0, 8);
let res = {
GAS_SPENT,
COST_BNB,
COST_USD,
BLOCK_NUMBER,
ACCOUNT,
};
return Object.assign(res, additionalData);
};
module.exports.passTime = async (ms) => {
await network.provider.send("evm_increaseTime", [ms]);
await network.provider.send("evm_mine");
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment