|
| 1 | +import { deployments } from 'hardhat'; |
| 2 | + |
| 3 | +import { getCurrentTime, getContract, TimeGo } from './utils'; |
| 4 | +import { CampaignBase, TestERC20 } from '../typechain'; |
| 5 | +import { BigNumber, Wallet } from 'ethers'; |
| 6 | +import { parseEther } from 'ethers/lib/utils'; |
| 7 | +import { expect } from 'chai'; |
| 8 | + |
| 9 | +const requiredAmount = 10n * 10n ** 18n; |
| 10 | +const dayLength = 86400n; |
| 11 | +const defaultChallengeLength = 86400; |
| 12 | + |
| 13 | +const setupTest = deployments.createFixture( |
| 14 | + // eslint-disable-next-line no-unused-vars |
| 15 | + async ({ deployments, getNamedAccounts, ethers }, options) => { |
| 16 | + const [host, A, B, ...users] = await ethers.getSigners(); |
| 17 | + |
| 18 | + // make C as a account with fix address |
| 19 | + const C = new Wallet('0x0000000000000000000000000000000000000000000000000000000000000001', A.provider); |
| 20 | + A.sendTransaction({ to: C.address, value: parseEther('2') }); |
| 21 | + |
| 22 | + const testErc20 = await getContract<TestERC20>('TestERC20'); |
| 23 | + const campaign = await getContract<CampaignBase>('CampaignBase'); |
| 24 | + await deployments.fixture(); // ensure you start from a fresh deployments |
| 25 | + |
| 26 | + const now = await getCurrentTime(); |
| 27 | + // initialize the campaign with 3 days |
| 28 | + campaign.initialize( |
| 29 | + host.address, |
| 30 | + testErc20.address, |
| 31 | + requiredAmount, |
| 32 | + 'TestCampaign', |
| 33 | + 'TC', |
| 34 | + BigInt(now) + dayLength / 2n, |
| 35 | + 30, |
| 36 | + dayLength, |
| 37 | + defaultChallengeLength, |
| 38 | + 'ipfs://', |
| 39 | + ); |
| 40 | + |
| 41 | + // mint erc20 |
| 42 | + await testErc20.connect(C).mint(C.address, requiredAmount); |
| 43 | + // signUp |
| 44 | + await testErc20.connect(C).approve(campaign.address, requiredAmount); |
| 45 | + await campaign.connect(C).signUp(); |
| 46 | + // admit |
| 47 | + await campaign.admit([0]); |
| 48 | + |
| 49 | + return { |
| 50 | + testErc20: testErc20, |
| 51 | + campaign: campaign, |
| 52 | + host: host, |
| 53 | + A: A, |
| 54 | + B: B, |
| 55 | + C: C, |
| 56 | + users: users, |
| 57 | + }; |
| 58 | + }, |
| 59 | +); |
| 60 | + |
| 61 | +describe('CampaignBase', function () { |
| 62 | + it('Should No Delay Work properly', async () => { |
| 63 | + const { campaign, C } = await setupTest(); |
| 64 | + |
| 65 | + await TimeGo(dayLength); |
| 66 | + await expect(campaign.connect(C).checkIn('', 0)).to.be.emit(campaign, 'EvCheckIn').withArgs(0, 0, ''); |
| 67 | + }); |
| 68 | + it('Should Delay Start Time Work properly', async () => { |
| 69 | + const { campaign, C } = await setupTest(); |
| 70 | + |
| 71 | + const startTimeBefore = await campaign.startTime(); |
| 72 | + |
| 73 | + await expect(campaign.delayStartTime(dayLength * 2n)) |
| 74 | + .to.be.emit(campaign, 'StartTimeDelay') |
| 75 | + .withArgs(startTimeBefore.add(BigNumber.from(dayLength * 2n))); |
| 76 | + |
| 77 | + await TimeGo(dayLength); |
| 78 | + await expect(campaign.connect(C).checkIn('', 0)).to.be.revertedWith('Campaign: not start'); |
| 79 | + }); |
| 80 | + it('Can not delay after the campaign start', async () => { |
| 81 | + const { campaign } = await setupTest(); |
| 82 | + |
| 83 | + await TimeGo(dayLength); |
| 84 | + await expect(campaign.delayStartTime(dayLength * 2n)).to.be.revertedWith('DelayStartTooLate'); |
| 85 | + }); |
| 86 | +}); |
0 commit comments