Skip to content

Commit

Permalink
Added validation of TokenGrant.initialize parameters
Browse files Browse the repository at this point in the history
  • Loading branch information
pdyraga committed Jul 30, 2021
1 parent 7456c67 commit 0fb1b32
Show file tree
Hide file tree
Showing 2 changed files with 166 additions and 2 deletions.
12 changes: 10 additions & 2 deletions contracts/grant/TokenGrant.sol
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import "@openzeppelin/contracts/token/ERC20/utils/SafeERC20.sol";
/// according to the provided staking policy.
contract TokenGrant {
// TODO Not implemented yet:
// TODO - TokenGrantFactory, master clone factory TokenGrant contract
// TODO - TokenGrantFactory, master clone factory TokenGrant contract
// TODO initialization prevention.
// TODO - Staking, including checking the policy, allowed staking
// TODO contracts, and calling the staking contract.
Expand Down Expand Up @@ -54,7 +54,15 @@ contract TokenGrant {
uint256 _start,
uint256 _cliff,
IGrantStakingPolicy _stakingPolicy
) public {
) external {
require(address(_token) != address(0), "Token must not be 0x0");
require(_grantee != address(0), "Grantee must not be 0x0");
require(_amount != 0, "Amount must not be 0");
require(_duration != 0, "Duration must not be 0");
require(_start != 0, "Start timestamp must not be 0");
require(_cliff != 0, "Cliff timestamp must not be 0");
// TODO: validate staking policy is not 0x0

token = _token;
grantee = _grantee;
revocable = _revocable;
Expand Down
156 changes: 156 additions & 0 deletions test/grant/TokenGrant.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,162 @@ describe("TokenGrant", () => {
await token.deployed()
})

describe("initialize", () => {
const revocable = true
const amount = to1e18(120000) // 120k tokens
const duration = 15552000 // 180 days
let start
let cliff
const stakingPolicy = ZERO_ADDRESS

let tokenGrant

beforeEach(async () => {
now = await lastBlockTime()
start = now
cliff = now

const TokenGrant = await ethers.getContractFactory("TokenGrant")
tokenGrant = await TokenGrant.deploy()
tokenGrant.deployed()
})

context("when token is zero address", () => {
it("should revert", async () => {
await expect(
tokenGrant.initialize(
ZERO_ADDRESS,
grantee.address,
revocable,
amount,
duration,
start,
cliff,
stakingPolicy
)
).to.be.revertedWith("Token must not be 0x0")
})
})

context("when grantee is zero address", () => {
it("should revert", async () => {
await expect(
tokenGrant.initialize(
token.address,
ZERO_ADDRESS,
revocable,
amount,
duration,
start,
cliff,
stakingPolicy
)
).to.be.revertedWith("Grantee must not be 0x0")
})
})

context("when amount is 0", () => {
it("should revert", async () => {
await expect(
tokenGrant.initialize(
token.address,
grantee.address,
revocable,
0,
duration,
start,
cliff,
stakingPolicy
)
).to.be.revertedWith("Amount must not be 0")
})
})

context("when duration is 0", () => {
it("should revert", async () => {
await expect(
tokenGrant.initialize(
token.address,
grantee.address,
revocable,
amount,
0,
start,
cliff,
stakingPolicy
)
).to.be.revertedWith("Duration must not be 0")
})
})

context("when start is 0", () => {
it("should revert", async () => {
await expect(
tokenGrant.initialize(
token.address,
grantee.address,
revocable,
amount,
duration,
0,
cliff,
stakingPolicy
)
).to.be.revertedWith("Start timestamp must not be 0")
})
})

context("when cliff is 0", () => {
it("should revert", async () => {
await expect(
tokenGrant.initialize(
token.address,
grantee.address,
revocable,
amount,
duration,
start,
0,
stakingPolicy
)
).to.be.revertedWith("Cliff timestamp must not be 0")
})
})

context("when all parameters are valid", () => {
const amount = to1e18(41211)

beforeEach(async () => {
await token.connect(deployer).mint(deployer.address, amount)
await token.connect(deployer).approve(tokenGrant.address, amount)

await tokenGrant.initialize(
token.address,
grantee.address,
revocable,
amount,
duration,
start,
cliff,
ZERO_ADDRESS
)
})

it("should transfer tokens to the grant", async () => {
expect(await token.balanceOf(tokenGrant.address)).to.equal(amount)
})

it("should initialize all fields", async () => {
expect(await tokenGrant.token()).to.equal(token.address)
expect(await tokenGrant.revocable()).to.equal(revocable)
expect(await tokenGrant.amount()).to.equal(amount)
expect(await tokenGrant.duration()).to.equal(duration)
expect(await tokenGrant.start()).to.equal(start)
expect(await tokenGrant.cliff()).to.equal(cliff)
})
})
})

describe("unlockedAmount", () => {
const assertionPrecision = to1e18(1) // +- 1 token

Expand Down

0 comments on commit 0fb1b32

Please sign in to comment.