-
-
Notifications
You must be signed in to change notification settings - Fork 233
/
grantTokens.js
225 lines (189 loc) · 6.44 KB
/
grantTokens.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
// TODO: cleanup this test
// ignoring that rule is needed when using the `describeOrskip` workaround
const { assert } = require('chai')
const { ethers, network } = require('hardhat')
const {
deployContracts,
deployLock,
ADDRESS_ZERO,
createUniswapV2Exchange,
compareBigNumbers,
increaseTime,
} = require('../helpers')
let unlock, udt, lock, oracle, weth
let protocolOwner, minter, referrer, keyBuyer
// skip on coverage until solidity-coverage supports EIP-1559
const describeOrSkip = process.env.IS_COVERAGE ? describe.skip : describe
const estimateGas = 252166 * 2
// test with various chainIds
const scenarios = [
1, // mainnet
100, // xdai gnosis
137, // polygon
]
const mintAmount = ethers.utils.parseUnits('1000000', 'ether')
const round = (bn) => {
const [integral, decimals] = bn.split('.')
const remainer = Math.round(`0.${decimals.slice(0, 4)}`).toString()
return ethers.BigNumber.from(integral).add(remainer)
}
describe('UnlockDiscountToken (l2/sidechain) / granting Tokens', () => {
let rate
before(async function () {
;[protocolOwner, minter, referrer, keyBuyer] = await ethers.getSigners()
;({ unlock, udt } = await deployContracts())
lock = await deployLock({ unlock })
// Deploy the exchange
;({ oracle, weth } = await createUniswapV2Exchange({
protocolOwner,
minter,
udtAddress: udt.address,
}))
// default config Unlock oracle
await unlock.configUnlock(
udt.address,
weth.address,
estimateGas,
await unlock.globalTokenSymbol(),
await unlock.globalBaseTokenURI(),
1
)
await unlock.setOracle(udt.address, oracle.address)
// Advance time so 1 full period has past and then update again so we have data point to read
await increaseTime(30)
await oracle.update(weth.address, udt.address)
// Purchase a valid key for the referrer
await lock.purchase(
[],
[referrer.address],
[ADDRESS_ZERO],
[ADDRESS_ZERO],
[[]],
{
value: await lock.keyPrice(),
}
)
rate = await oracle.consult(
udt.address,
ethers.utils.parseUnits('1', 'ether'),
weth.address
)
// Mint another 1000000
await udt.connect(minter).mint(unlock.address, mintAmount)
})
it('exchange rate is > 0', async () => {
assert.notEqual(ethers.utils.formatUnits(rate), 0)
// 1 UDT is worth ~0.000042 ETH
assert.equal(Math.floor(ethers.utils.formatUnits(rate, 12)), 42)
})
it('referrer has 0 UDT to start', async () => {
const actual = await udt.balanceOf(referrer.address)
compareBigNumbers(actual, '0')
})
it('owner starts with 0 UDT', async () => {
compareBigNumbers(await udt.balanceOf(await unlock.owner()), '0')
})
it('unlock has some 0 UDT', async () => {
compareBigNumbers(await udt.balanceOf(await unlock.address), mintAmount)
})
scenarios.forEach((chainId) => {
let balanceReferrer
describe(`behaviour on chain with ${chainId}`, () => {
before(async () => {
await unlock.configUnlock(
udt.address,
weth.address,
estimateGas,
await unlock.globalTokenSymbol(),
await unlock.globalBaseTokenURI(),
chainId
)
})
describeOrSkip('grant by gas price', () => {
let gasSpent
before(async () => {
// Let's set GDP to be very low (1 wei) so that we know that growth of supply is cap by gas
await unlock.resetTrackedValue(ethers.utils.parseUnits('1', 'wei'), 0)
const balanceReferrerBefore = await udt.balanceOf(referrer.address)
const { blockNumber } = await lock
.connect(keyBuyer)
.purchase(
[],
[keyBuyer.address],
[referrer.address],
[ADDRESS_ZERO],
[[]],
{
value: await lock.keyPrice(),
}
)
const { baseFeePerGas } = await ethers.provider.getBlock(blockNumber)
// using estimatedGas instead of the actual gas used so this test does
// not regress as other features are implemented
gasSpent = baseFeePerGas.mul(estimateGas)
balanceReferrer = (await udt.balanceOf(referrer.address)).sub(
balanceReferrerBefore
)
})
it('referrer has received some UDT now', async () => {
assert.notEqual(balanceReferrer.toString(), '0')
})
it('amount granted for referrer ~= gas spent', async () => {
// 120 UDT granted * 0.000042 ETH/UDT == 0.005 ETH spent
compareBigNumbers(
gasSpent,
round(ethers.utils.formatEther(balanceReferrer.mul(rate)))
)
})
})
describeOrSkip('grant capped by % growth', () => {
before(async () => {
// Goal: distribution is 10 UDT (8 for referrer, 2 for dev reward)
// With 1,000,000 to distribute, that is 0.00001% supply
// which translates in a gdp growth of 0.002%
// So we need a GDP of 500 eth
// Example: ETH = 2000 USD
// Total value exchanged = 1M USD
// Key purchase 0.01 ETH = 20 USD
// user earns 10UDT or
await unlock.resetTrackedValue(
ethers.utils.parseUnits('500', 'ether'),
0
)
const baseFeePerGas = 1000000000 // in gwei
await network.provider.send('hardhat_setNextBlockBaseFeePerGas', [
ethers.BigNumber.from(baseFeePerGas).toHexString(16),
])
const balanceReferrerBefore = await udt.balanceOf(referrer.address)
await lock
.connect(keyBuyer)
.purchase(
[],
[keyBuyer.address],
[referrer.address],
[ADDRESS_ZERO],
[[]],
{
value: await lock.keyPrice(),
gasPrice: ethers.BigNumber.from(baseFeePerGas)
.mul(2)
.toHexString(16),
}
)
balanceReferrer = (await udt.balanceOf(referrer.address)).sub(
balanceReferrerBefore
)
})
it('referrer has some UDT now', async () => {
assert.notEqual(balanceReferrer.toString(), 0)
})
it('amount granted for referrer ~= 10 UDT', async () => {
assert.equal(
Math.round(ethers.utils.formatEther(balanceReferrer)),
'10'
)
})
})
})
})
})