aave-protocol-v2/helpers/misc-utils.ts

43 lines
1.6 KiB
TypeScript
Raw Normal View History

2020-07-13 08:54:08 +00:00
import BigNumber from 'bignumber.js';
import BN = require('bn.js');
import low from 'lowdb';
import FileSync from 'lowdb/adapters/FileSync';
import {WAD} from './constants';
import {Wallet} from 'ethers';
import {BuidlerRuntimeEnvironment} from '@nomiclabs/buidler/types';
2020-05-29 14:55:31 +00:00
2020-07-13 08:54:08 +00:00
export const toWad = (value: string | number) => new BigNumber(value).times(WAD).toFixed();
2020-05-29 14:55:31 +00:00
2020-07-13 08:54:08 +00:00
export const bnToBigNumber = (amount: BN): BigNumber => new BigNumber(<any>amount);
export const stringToBigNumber = (amount: string): BigNumber => new BigNumber(amount);
2020-05-29 14:55:31 +00:00
2020-07-13 08:54:08 +00:00
export const getDb = () => low(new FileSync('./deployed-contracts.json'));
2020-05-29 14:55:31 +00:00
export let BRE: BuidlerRuntimeEnvironment = {} as BuidlerRuntimeEnvironment;
export const setBRE = (_BRE: BuidlerRuntimeEnvironment) => {
BRE = _BRE;
};
export const sleep = (milliseconds: number) => {
return new Promise((resolve) => setTimeout(resolve, milliseconds));
};
export const createRandomAddress = () => Wallet.createRandom().address;
2020-07-13 08:54:08 +00:00
export const evmSnapshot = async () => await BRE.ethereum.send('evm_snapshot', []);
2020-07-13 08:54:08 +00:00
export const evmRevert = async (id: string) => BRE.ethereum.send('evm_revert', [id]);
export const timeLatest = async () => {
2020-07-13 08:54:08 +00:00
const block = await BRE.ethers.provider.getBlock('latest');
return new BigNumber(block.timestamp);
};
export const advanceBlock = async (timestamp: number) =>
2020-07-13 08:54:08 +00:00
await BRE.ethers.provider.send('evm_mine', [timestamp]);
2020-07-13 08:54:08 +00:00
export const increaseTime = async (secondsToIncrease: number) => {
await BRE.ethers.provider.send('evm_increaseTime', [secondsToIncrease]);
await BRE.ethers.provider.send('evm_mine', []);
};