Skip to content

Commit

Permalink
update scripts to ethers-v6
Browse files Browse the repository at this point in the history
  • Loading branch information
YouStillAlive committed Dec 5, 2024
1 parent 1fa16cf commit 5f55e3d
Show file tree
Hide file tree
Showing 12 changed files with 6,945 additions and 10,589 deletions.
17,247 changes: 6,813 additions & 10,434 deletions package-lock.json

Large diffs are not rendered by default.

44 changes: 13 additions & 31 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,37 +19,19 @@
"author": "Ashwin Arora",
"license": "MIT",
"dependencies": {
"@ironblocks/firewall-consumer": "^1.0.4",
"@ethersproject/bignumber": "^5.7.0",
"@nomicfoundation/hardhat-chai-matchers": "^1.0.4",
"@nomicfoundation/hardhat-network-helpers": "^1.0.6",
"@nomicfoundation/hardhat-toolbox": "^2.0.0",
"@nomiclabs/hardhat-ethers": "^2.2.1",
"@nomiclabs/hardhat-etherscan": "^3.1.2",
"@openzeppelin/contracts": "^5.0.2",
"@openzeppelin/test-helpers": "^0.5.16",
"@trivago/prettier-plugin-sort-imports": "^4.0.0",
"@typechain/ethers-v5": "^10.1.1",
"@typechain/hardhat": "^6.1.4",
"@types/adm-zip": "^0.5.5",
"@types/chai": "^4.3.4",
"@types/fs-extra": "^9.0.13",
"@types/mocha": "^10.0.0",
"@types/node": "^20.10.4",
"@typescript-eslint/eslint-plugin": "^5.44.0",
"@typescript-eslint/parser": "^5.44.0",
"chai": "^4.3.7",
"ethers": "^5.7.2",
"fs-extra": "^10.1.0",
"hardhat": "^2.15.0",
"@ironblocks/firewall-consumer": "^1.0.17",
"@nomicfoundation/hardhat-chai-matchers": "^2.0.8",
"@nomicfoundation/hardhat-ethers": "^3.0.8",
"@nomicfoundation/hardhat-network-helpers": "^1.0.12",
"@truffle/dashboard-hardhat-plugin": "^0.2.15",
"@typechain/ethers-v6": "^0.5.1",
"@typechain/hardhat": "^9.1.0",
"hardhat": "^2.22.15",
"hardhat-gas-reporter": "^2.1.1",
"lodash": "^4.17.21",
"mocha": "^10.1.0",
"rimraf": "^4.1.2",
"solidity-coverage": "^0.8.2",
"ts-generator": "^0.1.1",
"ts-node": "^10.9.1",
"typechain": "^8.1.1",
"typescript": "^4.9.3"
"solidity-coverage": "^0.8.13",
"ts-node": ">=8.0.0",
"typechain": "^8.3.2",
"@openzeppelin/contracts": "^5.1.0",
"typescript": "^5.4.5"
}
}
7 changes: 2 additions & 5 deletions scripts/constants.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
import { BigNumber } from 'ethers';

export const MAX_UINT256: BigNumber = BigNumber.from(
'115792089237316195423570985008687907853269984665640564039457584007913129639935',
);
export const MAX_UINT256: bigint =
115792089237316195423570985008687907853269984665640564039457584007913129639935n

export const ZERO_ADDRESS: string = '0x0000000000000000000000000000000000000000';

Expand Down
3 changes: 1 addition & 2 deletions scripts/deploy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,5 @@ import { ethers } from 'hardhat';

export const deployed = async <T>(contractName: string, ...args: string[]): Promise<T> => {
const Contract = await ethers.getContractFactory(contractName);
const contract = await Contract.deploy(...args, { gasLimit: GAS_LIMIT });
return contract.deployed() as Promise<T>;
return Contract.deploy(...args, { gasLimit: GAS_LIMIT }) as Promise<T>;
};
8 changes: 4 additions & 4 deletions test/10_CalcUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,18 @@ import { expect } from 'chai';

describe('CalcUtils library tests', function () {
let calcUtils: CalcUtilsMock;
const amount = ethers.utils.parseUnits('100', 18);
const halfRate = ethers.utils.parseUnits('5', 20); // 50%
const amount = ethers.parseUnits('100', 18);
const halfRate = ethers.parseUnits('5', 20); // 50%

before(async () => {
calcUtils = await deployed('CalcUtilsMock');
});

it('should Calc Amount', async () => {
expect(await calcUtils.calcAmount(amount, halfRate)).to.equal(amount.div(2));
expect(await calcUtils.calcAmount(amount, halfRate)).to.equal(amount / 2n);
});

it('should Calc Rate', async () => {
expect(await calcUtils.calcRate(amount.div(2), amount)).to.equal(halfRate);
expect(await calcUtils.calcRate(amount / 2n, amount)).to.equal(halfRate);
});
});
2 changes: 1 addition & 1 deletion test/1_Admin.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { ZERO_ADDRESS } from '../scripts/constants';
import { deployed } from '../scripts/deploy';
import { GovManager, ETHHelper, PozBenefit } from '../typechain-types';
import { SignerWithAddress } from '@nomiclabs/hardhat-ethers/signers';
import { SignerWithAddress } from "@nomicfoundation/hardhat-ethers/signers"
import { expect } from 'chai';
import { ethers } from 'hardhat';

Expand Down
8 changes: 4 additions & 4 deletions test/2_Tokens.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { deployed } from '../scripts/deploy';
import { ERC20Token, ERC721Token } from '../typechain-types';
import { SignerWithAddress } from '@nomiclabs/hardhat-ethers/signers';
import { SignerWithAddress } from "@nomicfoundation/hardhat-ethers/signers"
import { expect } from 'chai';
import { ethers } from 'hardhat';

Expand All @@ -17,10 +17,10 @@ describe('Token Test', function () {

it('mint ERC20 tokens', async () => {
const oldBalance = await ERC20Token.balanceOf(owner.address);
const mintAmount = ethers.utils.parseUnits('500000', 18);
const mintAmount = ethers.parseUnits('500000', 18);
await ERC20Token.FreeTest();
const newBalance = await ERC20Token.balanceOf(owner.address);
expect(newBalance).to.be.equal(oldBalance.add(mintAmount));
expect(newBalance).to.be.equal(oldBalance + mintAmount);
});

it('mint ERC721 token', async () => {
Expand All @@ -29,6 +29,6 @@ describe('Token Test', function () {
const nftURI = 'TEST 1';
await NFT.awardItem(owner.address, nftURI);
const newBalance = await NFT.balanceOf(owner.address);
expect(newBalance).to.be.equal(oldBalance.add(1));
expect(newBalance).to.be.equal(oldBalance + 1n);
});
});
122 changes: 61 additions & 61 deletions test/3_Fee.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import { ZERO_ADDRESS } from '../scripts/constants';
import { deployed } from '../scripts/deploy';
import { ERC20Token, FeeHelper, WhiteListMock } from '../typechain-types';
import { SignerWithAddress } from '@nomiclabs/hardhat-ethers/signers';
import { SignerWithAddress } from "@nomicfoundation/hardhat-ethers/signers"
import { expect } from 'chai';
import { ethers } from 'hardhat';

describe('Fee Helper Test', function () {
const fee: number = 100000;
const fee: bigint = 100000n;
let token: ERC20Token;
let payer: SignerWithAddress, receiver: SignerWithAddress;
let feeHelper: FeeHelper;
Expand All @@ -20,40 +20,40 @@ describe('Fee Helper Test', function () {
});

it('zero fee', async () => {
const gasPrice = 2500000000;
const oldBal = await payer.getBalance();
const feePaid = await feeHelper.connect(payer).callStatic.MethodWithFee({ gasPrice: gasPrice });
const gasPrice = 2500000000n;
const oldBal = await ethers.provider.getBalance(await payer.getAddress());
const feePaid = await feeHelper.connect(payer).MethodWithFee.staticCall({ gasPrice: gasPrice });
const tx = await feeHelper.connect(payer).MethodWithFee({ gasPrice: gasPrice });
const txReceipt = await tx.wait();
const actualBal = await payer.getBalance();
const gas = txReceipt.gasUsed.mul(gasPrice);
const expectedBal = oldBal.sub(gas);
const txReceipt = (await tx.wait()) as any;

Check warning on line 27 in test/3_Fee.ts

View check run for this annotation

codefactor.io / CodeFactor

test/3_Fee.ts#L27

Unexpected any. Specify a different type. (@typescript-eslint/no-explicit-any)
const actualBal = await payer.provider.getBalance(await payer.getAddress());
const gasCost = ethers.toBigInt(txReceipt.gasUsed) * ethers.toBigInt(tx.gasPrice)
const expectedBal = oldBal - gasCost;
expect(actualBal).to.be.equal(expectedBal);
expect(feePaid).to.be.equal(0);
});

describe('test ERC20 token', async () => {
it('should set fee token and amount', async () => {
await feeHelper.setFee(token.address, fee);
await feeHelper.setFee(await token.getAddress(), fee);
const feeToken = await feeHelper.feeToken();
const actualFee = await feeHelper.feeAmount();
expect(actualFee).to.be.equal(fee);
expect(feeToken).to.be.equal(token.address);
expect(feeToken).to.be.equal(await token.getAddress());
});

it('should pay', async () => {
await token.connect(payer).approve(feeHelper.address, fee);
const oldBal = await token.balanceOf(feeHelper.address);
const feePaid = await feeHelper.connect(payer).callStatic.MethodWithFee();
await token.connect(payer).approve(await feeHelper.getAddress(), fee);
const oldBal = await token.balanceOf(await feeHelper.getAddress());
const feePaid = await feeHelper.connect(payer).MethodWithFee.staticCall();
await feeHelper.connect(payer).MethodWithFee();
const actualBal = await token.balanceOf(feeHelper.address);
expect(actualBal).to.be.equal(oldBal.add(fee));
const actualBal = await token.balanceOf(await feeHelper.getAddress());
expect(actualBal).to.be.equal(oldBal + fee);
expect(feePaid).to.be.equal(fee);
});

it('should withdraw', async () => {
await feeHelper.withdrawFee(token.address, payer.address);
const actualBal = await token.balanceOf(payer.address);
await feeHelper.withdrawFee(await token.getAddress(), await payer.getAddress());
const actualBal = await token.balanceOf(await payer.getAddress());
expect(actualBal).to.be.equal(await token.totalSupply());
});
});
Expand All @@ -68,94 +68,94 @@ describe('Fee Helper Test', function () {
});

it('should pay', async () => {
const oldBal = await ethers.provider.getBalance(feeHelper.address);
const feePaid = await feeHelper.connect(payer).callStatic.MethodWithFee({ value: fee });
const oldBal = await ethers.provider.getBalance(await feeHelper.getAddress());
const feePaid = await feeHelper.connect(payer).MethodWithFee.staticCall({ value: fee });
await feeHelper.connect(payer).MethodWithFee({ value: fee });
const actualBal = await ethers.provider.getBalance(feeHelper.address);
expect(actualBal).to.be.equal(oldBal.add(fee));
const actualBal = await ethers.provider.getBalance(await feeHelper.getAddress());
expect(actualBal).to.be.equal(oldBal + fee);
expect(feePaid).to.be.equal(fee);
});

it("should revert when fee is not paid", async () => {
await expect(feeHelper.connect(payer).MethodWithFee({value: fee / 2})).to.be.revertedWithCustomError(feeHelper, "NotEnoughFeeProvided");
await expect(feeHelper.connect(payer).MethodWithFee({value: fee / 2n})).to.be.revertedWithCustomError(feeHelper, "NotEnoughFeeProvided");
})

it('should withdraw', async () => {
const oldBal = await ethers.provider.getBalance(receiver.address);
await feeHelper.withdrawFee(ZERO_ADDRESS, receiver.address);
const actualBal = await ethers.provider.getBalance(receiver.address);
expect(actualBal).to.be.equal(oldBal.add(fee));
const oldBal = await ethers.provider.getBalance(await receiver.getAddress());
await feeHelper.withdrawFee(ZERO_ADDRESS, await receiver.getAddress());
const actualBal = await ethers.provider.getBalance(await receiver.getAddress());
expect(actualBal).to.be.equal(oldBal + fee);
});
});

describe("Whitelist Settings", async () => {

it("should get 0 credits when whitelistAddress is not set", async () => {
const credits = await feeHelper.getCredits(payer.address);
const credits = await feeHelper.getCredits(await payer.getAddress());
expect(credits).to.be.equal(0);
})

it("should set whitelist address", async () => {
const oldWhiteList = await feeHelper.whiteListAddress()
await feeHelper.setupNewWhitelist(Whitelist.address)
await feeHelper.setupNewWhitelist(await Whitelist.getAddress())
const whiteList = await feeHelper.whiteListAddress()
const whiteListId = await feeHelper.whiteListId()
expect(whiteList).to.be.equal(Whitelist.address)
expect(whiteList).to.be.equal(await Whitelist.getAddress())
expect(whiteList).to.not.equal(oldWhiteList)
expect(whiteListId).to.be.equal(1)
})

it("should add and remove new users", async () => {
const credits: number = fee * 5;
await feeHelper.addUsers([payer.address], [credits])
const _credits = await feeHelper.getCredits(payer.address);
const credits: bigint = fee * 5n;
await feeHelper.addUsers([await payer.getAddress()], [credits])
const _credits = await feeHelper.getCredits(await payer.getAddress());
expect(credits).to.be.equal(_credits);
await feeHelper.removeUsers([payer.address])
const _credits2 = await feeHelper.getCredits(payer.address);
await feeHelper.removeUsers([await payer.getAddress()])
const _credits2 = await feeHelper.getCredits(await payer.getAddress());
expect(0).to.be.equal(_credits2);
})

it("should take full fee when user has not credits", async () => {
await feeHelper.setFee(token.address, fee);
await feeHelper.addUsers([payer.address], [0])
const beforePayerBalance = await token.balanceOf(payer.address);
await token.connect(payer).approve(feeHelper.address, fee);
const feePaid = await feeHelper.connect(payer).callStatic.MethodWithFee();
await feeHelper.setFee(await token.getAddress(), fee);
await feeHelper.addUsers([await payer.getAddress()], [0])
const beforePayerBalance = await token.balanceOf(await payer.getAddress());
await token.connect(payer).approve(await feeHelper.getAddress(), fee);
const feePaid = await feeHelper.connect(payer).MethodWithFee.staticCall();
const tx = await feeHelper.connect(payer).MethodWithFee();
const afterPayerBalance = await token.balanceOf(payer.address);
expect(beforePayerBalance.sub(afterPayerBalance)).to.be.equal(fee);
expect(await feeHelper.feeToken()).to.be.equal(token.address);
const afterPayerBalance = await token.balanceOf(await payer.getAddress());
expect(beforePayerBalance - afterPayerBalance).to.be.equal(fee);
expect(await feeHelper.feeToken()).to.be.equal(await token.getAddress());
expect(feePaid).to.be.equal(fee);
await expect(tx).to.emit(feeHelper, "TransferIn").withArgs(fee, payer.address, token.address);
await expect(tx).to.emit(feeHelper, "TransferIn").withArgs(fee, await payer.getAddress(), await token.getAddress());
})

it("should not take fee when user has credits", async () => {
await feeHelper.setFee(token.address, fee);
const credits = fee * 5;
await feeHelper.addUsers([payer.address], [credits])
const beforePayerBalance = await token.balanceOf(payer.address);
const feePaid = await feeHelper.connect(payer).callStatic.MethodWithFee();
await feeHelper.setFee(await token.getAddress(), fee);
const credits = fee * 5n;
await feeHelper.addUsers([await payer.getAddress()], [credits])
const beforePayerBalance = await token.balanceOf(await payer.getAddress());
const feePaid = await feeHelper.connect(payer).MethodWithFee.staticCall();
const tx = await feeHelper.connect(payer).MethodWithFee();
const afterPayerBalance = await token.balanceOf(payer.address);
const afterPayerBalance = await token.balanceOf(await payer.getAddress());
expect(beforePayerBalance).to.be.equal(afterPayerBalance);
expect(await feeHelper.feeToken()).to.be.equal(token.address);
expect(await feeHelper.feeToken()).to.be.equal(await token.getAddress());
expect(feePaid).to.be.equal(0);
await expect(tx).to.not.emit(feeHelper, "TransferIn");
})

it("should take partial fee when user has less credits than fee", async () => {
await feeHelper.setFee(token.address, fee);
const credits = fee / 2;
await feeHelper.addUsers([payer.address], [credits])
const beforePayerBalance = await token.balanceOf(payer.address);
await token.connect(payer).approve(feeHelper.address, fee - credits);
const feePaid = await feeHelper.connect(payer).callStatic.MethodWithFee();
await feeHelper.setFee(await token.getAddress(), fee);
const credits = fee / 2n;
await feeHelper.addUsers([await payer.getAddress()], [credits])
const beforePayerBalance = await token.balanceOf(await payer.getAddress());
await token.connect(payer).approve(await feeHelper.getAddress(), fee - credits);
const feePaid = await feeHelper.connect(payer).MethodWithFee.staticCall();
const tx = await feeHelper.connect(payer).MethodWithFee();
const afterPayerBalance = await token.balanceOf(payer.address);
expect(beforePayerBalance.sub(afterPayerBalance)).to.be.equal(fee - credits);
expect(await feeHelper.feeToken()).to.be.equal(token.address);
const afterPayerBalance = await token.balanceOf(await payer.getAddress());
expect(beforePayerBalance - afterPayerBalance).to.be.equal(fee - credits);
expect(await feeHelper.feeToken()).to.be.equal(await token.getAddress());
expect(feePaid).to.be.equal(fee - credits);
await expect(tx).to.emit(feeHelper, "TransferIn").withArgs(fee - credits, payer.address, token.address);
await expect(tx).to.emit(feeHelper, "TransferIn").withArgs(fee - credits, await payer.getAddress(), await token.getAddress());
})

})
Expand Down
Loading

0 comments on commit 5f55e3d

Please sign in to comment.