Skip to content

Commit 95aac31

Browse files
committedDec 11, 2022
✨ feat: delay start time
1 parent 4a92f56 commit 95aac31

File tree

4 files changed

+104
-2
lines changed

4 files changed

+104
-2
lines changed
 

‎contracts/CampaignBase.sol

+11
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,17 @@ contract CampaignBase is ICampaign, OwnableUpgradeable, ERC721Upgradeable, Autom
9797
emit EvCampaignUriSet(campaignUri);
9898
}
9999

100+
/**
101+
* @dev delay the startTime
102+
*/
103+
function delayStartTime(uint256 delaySeconds) external onlyOwner {
104+
if (block.timestamp >= startTime) {
105+
revert DelayStartTooLate();
106+
}
107+
startTime += delaySeconds;
108+
emit StartTimeDelay(startTime);
109+
}
110+
100111
//
101112
/**
102113
* @dev user stake token and want to participate this campaign

‎contracts/interface/ICampaign.sol

+5
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,9 @@ import '@openzeppelin/contracts-upgradeable/token/ERC20/IERC20Upgradeable.sol';
55
import { Consts } from '../Consts.sol';
66

77
interface ICampaign {
8+
/// @dev delay too late.
9+
error DelayStartTooLate();
10+
811
//for voted: true = voted; false = not voted;
912
//for choice: true = think cheat; false = think not cheat;
1013
struct Voter {
@@ -122,6 +125,8 @@ interface ICampaign {
122125

123126
event EvCampaignUriSet(string newUri);
124127

128+
event StartTimeDelay(uint256 newStartTime);
129+
125130
event EvSignUp(uint256 tokenId);
126131

127132
event EvRegisterSuccessfully(uint256 tokenId);

‎test/campaignBase.ts

+86
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
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+
});

‎test/utils.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@ export async function getCurrentTime() {
55
return (await ethers.provider.getBlock(await ethers.provider.getBlockNumber())).timestamp;
66
}
77

8-
export async function TimeGo(s: number) {
9-
await network.provider.send('evm_mine', [(await getCurrentTime()) + s]);
8+
export async function TimeGo(s: number | BigInt) {
9+
await network.provider.send('evm_mine', [(await getCurrentTime()) + Number(s)]);
1010
}
1111

1212
export async function getContract<T extends Contract>(contractName: string) {

0 commit comments

Comments
 (0)