Files
fhevm-solidity/test/utils.ts
Joseph-André Turk 69464f8b29 feat: added mocked oracle and automated predeploy deployment
fix: typo .env.example

fix: ci tests

fix: ci test

fix: removed ciphertext verification in oracle

fix: typo OraclePredeploy

fix: gaslimit

fix: bigger ci machine

fix: bigger ci machine

fix: increased ci sleep time

fix: longer sleep for ci

fix: longer sleep

fix: longer sleep

fix: change sleeping times

fix: simplest test

fix: ci

fix: add compile

ci: show logs/errs of different services

fix: test

fix: typo
2024-03-20 10:41:27 +01:00

99 lines
2.9 KiB
TypeScript

import { ContractMethodArgs, Typed } from 'ethers';
import { ethers } from 'hardhat';
import type { Counter } from '../types';
import { TypedContractMethod } from '../types/common';
import { getSigners } from './signers';
export const waitForBlock = (blockNumber: bigint | number) => {
if (process.env.HARDHAT_NETWORK === 'hardhat') {
return new Promise((resolve, reject) => {
const intervalId = setInterval(async () => {
try {
const currentBlock = await ethers.provider.getBlockNumber();
if (BigInt(currentBlock) >= blockNumber) {
clearInterval(intervalId);
resolve(currentBlock);
}
} catch (error) {
clearInterval(intervalId);
reject(error);
}
}, 50); // Check every 50 milliseconds
});
} else {
return new Promise((resolve, reject) => {
const waitBlock = async (currentBlock: number) => {
if (blockNumber <= BigInt(currentBlock)) {
await ethers.provider.off('block', waitBlock);
resolve(blockNumber);
}
};
ethers.provider.on('block', waitBlock).catch((err) => {
reject(err);
});
});
}
};
export const waitNBlocks = async (Nblocks: number) => {
const currentBlock = await ethers.provider.getBlockNumber();
if (process.env.HARDHAT_NETWORK === 'hardhat') {
await produceDummyTransactions(Nblocks);
}
await waitForBlock(currentBlock + Nblocks);
};
export const waitForBalance = async (address: string): Promise<void> => {
return new Promise((resolve, reject) => {
const checkBalance = async () => {
const balance = await ethers.provider.getBalance(address);
if (balance > 0) {
await ethers.provider.off('block', checkBalance);
resolve();
}
};
ethers.provider.on('block', checkBalance).catch((err) => {
reject(err);
});
});
};
export const createTransaction = async <A extends [...{ [I in keyof A]-?: A[I] | Typed }]>(
method: TypedContractMethod<A>,
...params: A
) => {
const gasLimit = await method.estimateGas(...params);
const updatedParams: ContractMethodArgs<A> = [
...params,
{ gasLimit: Math.min(Math.round(+gasLimit.toString() * 1.2), 10000000) },
];
return method(...updatedParams);
};
export const produceDummyTransactions = async (blockCount: number) => {
const contract = await deployCounterContract();
let counter = blockCount;
while (counter > 0) {
counter--;
const tx = await contract.increment();
const _ = await tx.wait();
}
};
async function deployCounterContract(): Promise<Counter> {
const signers = await getSigners();
const contractFactory = await ethers.getContractFactory('Counter');
const contract = await contractFactory.connect(signers.dave).deploy();
await contract.waitForDeployment();
return contract;
}
export const mineNBlocks = async (n: number) => {
for (let index = 0; index < n; index++) {
await ethers.provider.send('evm_mine');
}
};