-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path2-ido-test.js
231 lines (186 loc) · 8.28 KB
/
2-ido-test.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
const { assert, expect } = require("chai")
const chai = require("chai")
chai.use(require('chai-as-promised'))
const PolCoin = artifacts.require("PolCoin")
const TestToken = artifacts.require("TestToken")
const Staker = artifacts.require("Staker")
const IDO = artifacts.require("RaffleWrapTest")
function timeout(s) {
return new Promise(resolve => setTimeout(resolve, s * 1000));
}
contract("IDO", accounts => {
var idoAmount = '10000000000000000000000' // 10000 Tokens
var totalPrice = '10000000000000000000' // 0.001 * 10000 = 10 Eth
var poolMin = [
'100000000000000000000',
'500000000000000000000',
'1000000000000000000000',
'2000000000000000000000',
'4000000000000000000000'
]
let polCoin, testToken, staker, ido
beforeEach(async () => {
polCoin = await PolCoin.new()
testToken = await TestToken.new()
staker = await Staker.new(polCoin.address)
ido = await IDO.new(
staker.address,
polCoin.address,
testToken.address,
idoAmount,
totalPrice
)
// Transfer PolCoins to accounts
await polCoin.transfer(accounts[1], poolMin[0], { from: accounts[0] })
await polCoin.transfer(accounts[2], poolMin[0], { from: accounts[0] })
await polCoin.transfer(accounts[3], poolMin[1], { from: accounts[0] })
await polCoin.transfer(accounts[4], poolMin[2], { from: accounts[0] })
await polCoin.transfer(accounts[5], poolMin[3], { from: accounts[0] })
await polCoin.transfer(accounts[6], poolMin[4], { from: accounts[0] })
await polCoin.transfer(accounts[7], poolMin[4], { from: accounts[0] })
// Approve Transfer to Staker ontract
await polCoin.approve(staker.address, poolMin[0], { from: accounts[1] })
await polCoin.approve(staker.address, poolMin[0], { from: accounts[2] })
await polCoin.approve(staker.address, poolMin[1], { from: accounts[3] })
await polCoin.approve(staker.address, poolMin[2], { from: accounts[4] })
await polCoin.approve(staker.address, poolMin[3], { from: accounts[5] })
await polCoin.approve(staker.address, poolMin[4], { from: accounts[6] })
await polCoin.approve(staker.address, poolMin[4], { from: accounts[7] })
// Accounts Stake in Staker
await staker.stake(poolMin[0], { from: accounts[1] })
await staker.stake(poolMin[0], { from: accounts[2] })
await staker.stake(poolMin[1], { from: accounts[3] })
await staker.stake(poolMin[2], { from: accounts[4] })
await staker.stake(poolMin[3], { from: accounts[5] })
await staker.stake(poolMin[4], { from: accounts[6] })
await staker.stake(poolMin[4], { from: accounts[7] })
await staker.addIDO(ido.address, { from: accounts[0] })
await testToken.transfer(ido.address, idoAmount, { from: accounts[0] })
})
describe('Testing IDO Without Initializing', () => {
it('Pool No Test', async () => {
const poolNo = (await ido.getPoolNo(accounts[1])).toNumber()
// account 1 didn't register to any pool yet
// so pool no should be 0
assert.equal(poolNo, 0)
})
it('Registration(fail test)', async () => {
await expect(
ido.register(1, { from: accounts[0] })
).to.be.rejected
})
it('Buy(fail test)', async () => {
await expect(
ido.buyNow({ from: accounts[0] })
).to.be.rejected
})
})
describe('Testing IDO Registration(While Initialized)', () => {
beforeEach(async () => {
var initTime = Math.floor(Date.now() / 1000) + 5
await ido.initialize(initTime, { from: accounts[0] })
})
it('Registration before time fails', async () => {
await expect(
ido.register(1, { from: accounts[1] })
).to.be.rejected
})
it('Register in Registration period', async () => {
await timeout(5) // wait 5s for registration to start
await ido.register(1, { from: accounts[1] })
})
it('Registration after time fails', async () => {
await timeout(5 + 48 + 2) // wait for registration to end
await expect(
ido.register(1, { from: accounts[1] })
).to.be.rejected
})
it('Multiple Registration And Unstake After Registration Fails', async () => {
await timeout(5) // wait for registration to start
// Expect registration in invalid pool to fail
await expect(
// account 1 does not have enough tokens
// staked to participate in pool no 4
ido.register(4, { from: accounts[3] })
).to.be.rejected
await ido.register(2, { from: accounts[3] })
// Expecting 2nd registration from same user to fail
await expect(
ido.register(1, { from: accounts[3] })
).to.be.rejected
// expecting unstaking to fail as user is locked
await expect(
staker.unstake(poolMin[1], { from: accounts[3] })
).to.be.rejected
})
})
describe('Testing IDO Purchase', async () => {
let saleStartsAfter, initTime
beforeEach(async () => {
initTime = Math.floor(Date.now() / 1000) + 1
await ido.initialize(initTime, { from: accounts[0] })
saleStartsAfter = 48 + 24
await timeout(1)
await ido.register(1, { from: accounts[1] })
await ido.register(1, { from: accounts[2] })
await ido.register(2, { from: accounts[3] })
await ido.register(3, { from: accounts[4] })
await ido.register(4, { from: accounts[5] })
await ido.register(5, { from: accounts[6] })
await ido.register(5, { from: accounts[7] })
})
it('Pool No Test', async () => {
const poolNo = (await ido.getPoolNo(accounts[4])).toNumber()
// account 4 registered in poolNo 3
assert.equal(poolNo, 3)
})
it('Purchase before time(fails)', async () => {
const price = (await ido.tokensAndPriceByPoolNo(1))[1]
await expect(
ido.buyNow({ from: accounts[1], value: price })
).to.be.rejected
})
it('Purchase in Sale period', async () => {
await timeout(saleStartsAfter)
const price = (await ido.tokensAndPriceByPoolNo(1))[1]
await ido.buyNow({ from: accounts[1], value: price })
const usr = await ido.userlog(accounts[1])
assert.equal(usr.isRegistered, true);
assert.equal(usr.registeredPool.toNumber(), 1)
assert.equal(usr.purchased, true)
// Multiple Purchase Order expected to Fail
await expect(
ido.buyNow({ from: accounts[1], value: price })
).to.be.rejected
})
it('Purchase After Sale Period(fails)', async () => {
await timeout(saleStartsAfter + 12 + 1)
let price = (await ido.tokensAndPriceByPoolNo(1))[1]
await expect(
ido.buyNow({ from: accounts[1], value: price })
).to.be.rejected
})
})
describe('Recover', async () => {
let saleStartsAfter, initTime
beforeEach(async () => {
initTime = Math.floor(Date.now() / 1000) + 1
await ido.initialize(initTime, { from: accounts[0] })
saleStartsAfter = 48 + 24
await timeout(1)
await ido.register(1, { from: accounts[1] })
})
it('Recover ERC20', async () => {
await ido.recoverERC20(polCoin.address, accounts[0], { from: accounts[0] })
})
it('Recover Eth', async () => {
await timeout(saleStartsAfter) // Wait for sale to start
let price = (await ido.tokensAndPriceByPoolNo(1))[1]
await ido.buyNow({
from: accounts[1],
value: price
}) // A participant buys tokens and eth added to contract
await ido.recoverEth(accounts[0], { from: accounts[0] }) // Recover the Eth
})
})
})