contracts-imtbl/test/testCECDistributor.ts
2024-09-08 10:13:09 +08:00

111 lines
4.7 KiB
TypeScript

import { expect } from 'chai'
import hre from "hardhat";
import {
getBytes,
solidityPackedKeccak256,
} from 'ethers'
import {
loadFixture,
} from "@nomicfoundation/hardhat-toolbox/network-helpers";
import { expandDecimals, increaseTime, mineBlock, print } from './shared/utilities';
const ONE_DAY = 3600 * 24
const ONE_MONTH = ONE_DAY * 30
describe('TestCECDistributor', function() {
async function deployOneContract() {
// Contracts are deployed using the first signer/account by default
const [owner, user0, user1, user2] = await hre.ethers.getSigners();
const Cec = await hre.ethers.getContractFactory("MintableBaseToken");
const cec = await Cec.deploy("test cec", "cec");
await cec.setMinter(owner.address, true)
const CECDistributor = await hre.ethers.getContractFactory("CECDistributor");
const lockDuration = ONE_DAY; // one day
const start = (Date.now() / 1000 + 3600) | 0 // one hour later
const distributor = await CECDistributor.deploy("first", cec.target, start);
await cec.mint(distributor.target, expandDecimals(15000, 18))
const chainId = hre.network.config.chainId
expect(await distributor.name()).to.equal("first")
await distributor.updateInfo([user1.address], [[expandDecimals(1000, 18), 10, 300000, lockDuration]])
return { distributor, owner, user0, user1, user2, chainId, cec, start };
}
describe("Deployment", function () {
it('should deploy CECDistributor', async function() {
const { distributor, user0, user1, user2, cec } = await loadFixture(deployOneContract);
expect(await distributor.name()).to.equal("first")
});
it('should success claim', async function() {
const { distributor, owner, user0, user1, user2, cec, start } = await loadFixture(deployOneContract);
const wallet = owner
const provider = wallet.provider;
await increaseTime(provider, 3601)
await mineBlock(provider)
const claimAmount1 = await distributor.calcClaimAmount(user1.address)
expect(claimAmount1).to.equal(expandDecimals(300, 18))
// @ts-ignore
await distributor.connect(user1).claim(user1.address)
expect(await cec.balanceOf(user1.address)).to.equal(expandDecimals(300, 18))
const claimAmount2 = await distributor.calcClaimAmount(user1.address)
expect(claimAmount2).to.equal(0)
await increaseTime(provider, ONE_DAY)
await mineBlock(provider)
const claimAmount3 = await distributor.calcClaimAmount(user1.address)
expect(claimAmount3).to.equal(0)
await increaseTime(provider, ONE_MONTH)
await mineBlock(provider)
const claimAmount4 = await distributor.calcClaimAmount(user1.address)
expect(claimAmount4).to.equal(expandDecimals(70, 18))
await increaseTime(provider, ONE_MONTH)
await mineBlock(provider)
const claimAmount5 = await distributor.calcClaimAmount(user1.address)
expect(claimAmount5).to.equal(expandDecimals(140, 18))
// @ts-ignore
await distributor.connect(user1).changeAddress(user1.address, user2.address)
const claimAmount6 = await distributor.calcClaimAmount(user2.address)
expect(claimAmount6).to.equal(expandDecimals(140, 18))
await expect(distributor.calcClaimAmount(user1.address)).to.be.revertedWith("CECDistributor: not in whitelist");
});
it('should revert claim for not start', async function() {
const { distributor, owner, user0, user1, user2, cec, start } = await loadFixture(deployOneContract);
// @ts-ignore
await expect(distributor.connect(user1).claim(user1.address)).to.be.revertedWith("CECDistributor: not in claim time");
});
it('should revert claim for not in whitelist', async function() {
const { distributor, owner, user0, user1, user2, cec, start } = await loadFixture(deployOneContract);
// @ts-ignore
await expect(distributor.connect(user2).claim(user1.address)).to.be.revertedWith("CECDistributor: not in whitelist");
});
it('should revert claim for pause', async function() {
const { distributor, owner, user0, user1, user2, cec, start } = await loadFixture(deployOneContract);
await distributor.pause()
// @ts-ignore
await expect(distributor.connect(user2).claim(user1.address)).to.be.revertedWith("Pausable: paused");
});
it('should change gov success', async function() {
const { distributor, owner, user0, user1, user2, cec, start } = await loadFixture(deployOneContract);
await distributor.setGov(user2.address)
expect(await distributor.gov()).to.equal(user2.address)
// @ts-ignore
await distributor.connect(user2).pause()
expect(await distributor.paused()).to.equal(true)
});
})
})