Skip to content

Commit 0fb1b32

Browse files
committed
Added validation of TokenGrant.initialize parameters
1 parent 7456c67 commit 0fb1b32

File tree

2 files changed

+166
-2
lines changed

2 files changed

+166
-2
lines changed

contracts/grant/TokenGrant.sol

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ import "@openzeppelin/contracts/token/ERC20/utils/SafeERC20.sol";
1414
/// according to the provided staking policy.
1515
contract TokenGrant {
1616
// TODO Not implemented yet:
17-
// TODO - TokenGrantFactory, master clone factory TokenGrant contract
17+
// TODO - TokenGrantFactory, master clone factory TokenGrant contract
1818
// TODO initialization prevention.
1919
// TODO - Staking, including checking the policy, allowed staking
2020
// TODO contracts, and calling the staking contract.
@@ -54,7 +54,15 @@ contract TokenGrant {
5454
uint256 _start,
5555
uint256 _cliff,
5656
IGrantStakingPolicy _stakingPolicy
57-
) public {
57+
) external {
58+
require(address(_token) != address(0), "Token must not be 0x0");
59+
require(_grantee != address(0), "Grantee must not be 0x0");
60+
require(_amount != 0, "Amount must not be 0");
61+
require(_duration != 0, "Duration must not be 0");
62+
require(_start != 0, "Start timestamp must not be 0");
63+
require(_cliff != 0, "Cliff timestamp must not be 0");
64+
// TODO: validate staking policy is not 0x0
65+
5866
token = _token;
5967
grantee = _grantee;
6068
revocable = _revocable;

test/grant/TokenGrant.test.js

Lines changed: 156 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,162 @@ describe("TokenGrant", () => {
1919
await token.deployed()
2020
})
2121

22+
describe("initialize", () => {
23+
const revocable = true
24+
const amount = to1e18(120000) // 120k tokens
25+
const duration = 15552000 // 180 days
26+
let start
27+
let cliff
28+
const stakingPolicy = ZERO_ADDRESS
29+
30+
let tokenGrant
31+
32+
beforeEach(async () => {
33+
now = await lastBlockTime()
34+
start = now
35+
cliff = now
36+
37+
const TokenGrant = await ethers.getContractFactory("TokenGrant")
38+
tokenGrant = await TokenGrant.deploy()
39+
tokenGrant.deployed()
40+
})
41+
42+
context("when token is zero address", () => {
43+
it("should revert", async () => {
44+
await expect(
45+
tokenGrant.initialize(
46+
ZERO_ADDRESS,
47+
grantee.address,
48+
revocable,
49+
amount,
50+
duration,
51+
start,
52+
cliff,
53+
stakingPolicy
54+
)
55+
).to.be.revertedWith("Token must not be 0x0")
56+
})
57+
})
58+
59+
context("when grantee is zero address", () => {
60+
it("should revert", async () => {
61+
await expect(
62+
tokenGrant.initialize(
63+
token.address,
64+
ZERO_ADDRESS,
65+
revocable,
66+
amount,
67+
duration,
68+
start,
69+
cliff,
70+
stakingPolicy
71+
)
72+
).to.be.revertedWith("Grantee must not be 0x0")
73+
})
74+
})
75+
76+
context("when amount is 0", () => {
77+
it("should revert", async () => {
78+
await expect(
79+
tokenGrant.initialize(
80+
token.address,
81+
grantee.address,
82+
revocable,
83+
0,
84+
duration,
85+
start,
86+
cliff,
87+
stakingPolicy
88+
)
89+
).to.be.revertedWith("Amount must not be 0")
90+
})
91+
})
92+
93+
context("when duration is 0", () => {
94+
it("should revert", async () => {
95+
await expect(
96+
tokenGrant.initialize(
97+
token.address,
98+
grantee.address,
99+
revocable,
100+
amount,
101+
0,
102+
start,
103+
cliff,
104+
stakingPolicy
105+
)
106+
).to.be.revertedWith("Duration must not be 0")
107+
})
108+
})
109+
110+
context("when start is 0", () => {
111+
it("should revert", async () => {
112+
await expect(
113+
tokenGrant.initialize(
114+
token.address,
115+
grantee.address,
116+
revocable,
117+
amount,
118+
duration,
119+
0,
120+
cliff,
121+
stakingPolicy
122+
)
123+
).to.be.revertedWith("Start timestamp must not be 0")
124+
})
125+
})
126+
127+
context("when cliff is 0", () => {
128+
it("should revert", async () => {
129+
await expect(
130+
tokenGrant.initialize(
131+
token.address,
132+
grantee.address,
133+
revocable,
134+
amount,
135+
duration,
136+
start,
137+
0,
138+
stakingPolicy
139+
)
140+
).to.be.revertedWith("Cliff timestamp must not be 0")
141+
})
142+
})
143+
144+
context("when all parameters are valid", () => {
145+
const amount = to1e18(41211)
146+
147+
beforeEach(async () => {
148+
await token.connect(deployer).mint(deployer.address, amount)
149+
await token.connect(deployer).approve(tokenGrant.address, amount)
150+
151+
await tokenGrant.initialize(
152+
token.address,
153+
grantee.address,
154+
revocable,
155+
amount,
156+
duration,
157+
start,
158+
cliff,
159+
ZERO_ADDRESS
160+
)
161+
})
162+
163+
it("should transfer tokens to the grant", async () => {
164+
expect(await token.balanceOf(tokenGrant.address)).to.equal(amount)
165+
})
166+
167+
it("should initialize all fields", async () => {
168+
expect(await tokenGrant.token()).to.equal(token.address)
169+
expect(await tokenGrant.revocable()).to.equal(revocable)
170+
expect(await tokenGrant.amount()).to.equal(amount)
171+
expect(await tokenGrant.duration()).to.equal(duration)
172+
expect(await tokenGrant.start()).to.equal(start)
173+
expect(await tokenGrant.cliff()).to.equal(cliff)
174+
})
175+
})
176+
})
177+
22178
describe("unlockedAmount", () => {
23179
const assertionPrecision = to1e18(1) // +- 1 token
24180

0 commit comments

Comments
 (0)