Skip to content

Commit

Permalink
inter
Browse files Browse the repository at this point in the history
  • Loading branch information
aalavandhan committed Nov 6, 2024
1 parent b4bee0f commit 9067972
Show file tree
Hide file tree
Showing 6 changed files with 277 additions and 433 deletions.
1 change: 1 addition & 0 deletions contracts/external/ampl.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
import { UFragments } from "ampleforth-contracts/contracts/UFragments.sol";
125 changes: 0 additions & 125 deletions test/helper.js

This file was deleted.

106 changes: 106 additions & 0 deletions test/helper.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
import { ethers } from "hardhat";
import { promisify } from "util";
import { expect } from "chai";

const AMPL_DECIMALS = 9;

function $AMPL(x: number) {
return ethers.parseUnits(x.toString(), AMPL_DECIMALS);
}

// Perc has to be a whole number
async function invokeRebase(ampl, perc) {
await ampl.rebase(1, (await ampl.totalSupply()) * BigInt(perc) / 100n);
}

function checkAmplAprox(x, y) {
checkAprox(x, $AMPL(y), 10 ** 6);
}

function checkSharesAprox(x, y) {
checkAprox(x, y, 10 ** 12);
}

function checkAprox(x, y, delta_) {
const delta = BigInt(parseInt(delta_));
const upper = y.add(delta);
const lower = y.sub(delta);
expect(x).to.be.bignumber.at.least(lower).and.bignumber.at.most(upper);
}

export const TimeHelpers = {
secondsFromNow: async (secondsFromNow: number): Promise<number> => {
return (await TimeHelpers.currentTime()) + secondsFromNow;
},

increaseTime: async (seconds: number): Promise<void> => {
await hre.network.provider.send("evm_increaseTime", [seconds]);
await hre.network.provider.send("evm_mine");
},

setNextBlockTimestamp: async (timestamp: number): Promise<void> => {
await ethers.provider.send("evm_setNextBlockTimestamp", [timestamp]);
await hre.network.provider.send("evm_mine");
},

currentTime: async (): Promise<number> => {
const res = await hre.network.provider.send("eth_getBlockByNumber", ["latest", false]);
const timestamp = parseInt(res.timestamp, 16);
return timestamp;
},

increaseTimeForNextTransaction: async (diff: number): Promise<void> => {
await promisify(ethers.provider.send.bind(ethers.provider))({
jsonrpc: "2.0",
method: "evm_increaseTime",
params: [diff],
id: new Date().getTime(),
});
},

setTimeForNextTransaction: async (target: number | BigInt): Promise<void> => {
if (!BigInt.isBigNumber(target)) {
target = BigInt(target);
}

const now = await TimeHelpers.currentTime();

if (target.lt(now))
throw Error(
`Cannot increase current time (${now}) to a moment in the past (${target})`,
);
const diff = target.sub(now);
await TimeHelpers.increaseTime(diff.toNumber());
},
};

async function printMethodOutput(r) {
console.log(r.logs);
}

async function printStatus(dist) {
console.log("Total Locked: ", await dist.totalLocked.call().toString());
console.log("Total UnLocked: ", await dist.totalUnlocked.call().toString());
const c = (await dist.unlockScheduleCount.call()).toNumber();
console.log(await dist.unlockScheduleCount.call().toString());

for (let i = 0; i < c; i++) {
console.log(await dist.unlockSchedules.call(i).toString());
}
// TODO: Print the following variables:
// await dist.totalLocked.call()
// await dist.totalUnlocked.call()
// await dist.unlockScheduleCount.call()
// dist.updateAccounting.call() // and all the logs
// dist.unlockSchedules.call(1)
}

module.exports = {
checkAmplAprox,
checkSharesAprox,
invokeRebase,
$AMPL,
TimeHelpers,
printMethodOutput,
printStatus,
};
Loading

0 comments on commit 9067972

Please sign in to comment.