forked from spaceswap/spaceswap
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathShakeERC20.spec.ts
162 lines (134 loc) · 7.62 KB
/
ShakeERC20.spec.ts
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
import chai, { expect } from 'chai'
import { Contract } from 'ethers'
import { MaxUint256 } from 'ethers/constants'
import { bigNumberify, hexlify, keccak256, defaultAbiCoder, toUtf8Bytes } from 'ethers/utils'
import { solidity, MockProvider, deployContract } from 'ethereum-waffle'
import { ecsign } from 'ethereumjs-util'
import { expandTo18Decimals } from './shared/utilities'
import ShakeERC20 from '../build/ShakeERC20.json'
chai.use(solidity)
const MAX_TOTAL_SUPPLY = 10000
const TEST_AMOUNT = 10
const AMOUNT_MINT = 100
const provider = new MockProvider({
hardfork: 'istanbul',
mnemonic: 'horn horn horn horn horn horn horn horn horn horn horn horn',
gasLimit: 9999999
})
const [walletMinter, walletShake, wallet1, wallet2] = provider.getWallets();
describe('ShakeERC20', () => {
let token: Contract
beforeEach(async () => {
token = await deployContract(walletMinter, ShakeERC20)
await token.connect(walletMinter).mint(walletShake.address, AMOUNT_MINT)
})
it('name, symbol, decimals, totalSupply, balanceOf', async () => {
const name = await token.name()
expect(name).to.eq('SHAKE token by SpaceSwap v2')
expect(await token.symbol()).to.eq('SHAKE')
expect(await token.decimals()).to.eq(18)
expect(await token.MAX_TOTAL_SUPPLY()).to.eq(expandTo18Decimals(MAX_TOTAL_SUPPLY))
expect(await token.totalSupply()).to.eq(AMOUNT_MINT)
expect(await token.balanceOf(walletShake.address)).to.eq(AMOUNT_MINT)
})
it('approve', async () => {
await expect(token.connect(walletShake).approve(wallet1.address, TEST_AMOUNT))
.to.emit(token, 'Approval')
.withArgs(walletShake.address, wallet1.address, TEST_AMOUNT)
expect(await token.allowance(walletShake.address, wallet1.address)).to.eq(TEST_AMOUNT)
})
it('transfer', async () => {
await expect(token.connect(walletShake).transfer(wallet2.address, TEST_AMOUNT))
.to.emit(token, 'Transfer')
.withArgs(walletShake.address, wallet2.address, TEST_AMOUNT);
expect(await token.balanceOf(walletShake.address)).to.eq(AMOUNT_MINT-TEST_AMOUNT)
expect(await token.balanceOf(wallet2.address)).to.eq(TEST_AMOUNT)
})
it('transfer:fail', async () => {
await expect(token.connect(walletShake).transfer(wallet2.address, AMOUNT_MINT+1)).to.be.reverted;
await expect(token.connect(wallet2).transfer(walletShake.address, 1)).to.be.reverted;
})
it('transferFrom', async () => {
await token.connect(walletShake).approve(wallet1.address, TEST_AMOUNT)
await expect(token.connect(wallet1).transferFrom(walletShake.address, wallet2.address, TEST_AMOUNT))
.to.emit(token, 'Transfer')
.withArgs(walletShake.address, wallet2.address, TEST_AMOUNT)
expect(await token.allowance(walletShake.address, wallet1.address)).to.eq(0)
expect(await token.balanceOf(walletShake.address)).to.eq(AMOUNT_MINT-TEST_AMOUNT)
expect(await token.balanceOf(wallet2.address)).to.eq(TEST_AMOUNT)
})
it('transferFrom: allowance is more than balance', async () => {
await token.connect(walletShake).approve(wallet1.address, MaxUint256)
await expect(token.connect(wallet1).transferFrom(walletShake.address, wallet2.address, MaxUint256)).to.be.reverted;
expect(await token.allowance(walletShake.address, wallet1.address)).to.eq(MaxUint256);
expect(await token.balanceOf(walletShake.address)).to.eq(AMOUNT_MINT);
expect(await token.balanceOf(wallet2.address)).to.eq(0);
await token.connect(walletShake).approve(wallet1.address, MaxUint256)
await expect(token.connect(wallet1).transferFrom(walletShake.address, wallet2.address, TEST_AMOUNT))
.to.emit(token, 'Transfer')
.withArgs(walletShake.address, wallet2.address, TEST_AMOUNT)
expect(await token.allowance(walletShake.address, wallet1.address)).to.eq(MaxUint256.sub(TEST_AMOUNT));
expect(await token.balanceOf(walletShake.address)).to.eq(AMOUNT_MINT - TEST_AMOUNT);
expect(await token.balanceOf(wallet2.address)).to.eq(TEST_AMOUNT);
})
it('increaseAllowance', async () => {
await expect(token.connect(walletShake).increaseAllowance(wallet1.address, TEST_AMOUNT))
.to.emit(token, 'Approval')
.withArgs(walletShake.address, wallet1.address, TEST_AMOUNT)
expect(await token.allowance(walletShake.address, wallet1.address)).to.eq(TEST_AMOUNT)
await token.connect(wallet1).transferFrom(walletShake.address, wallet2.address, TEST_AMOUNT);
expect(await token.allowance(walletShake.address, wallet1.address)).to.eq(0)
expect(await token.balanceOf(walletShake.address)).to.eq(AMOUNT_MINT-10)
expect(await token.balanceOf(wallet2.address)).to.eq(10)
})
it('decreaseAllowance', async () => {
await expect(token.connect(walletShake).increaseAllowance(wallet1.address, TEST_AMOUNT))
.to.emit(token, 'Approval')
.withArgs(walletShake.address, wallet1.address, TEST_AMOUNT)
expect(await token.allowance(walletShake.address, wallet1.address)).to.eq(TEST_AMOUNT)
await expect(token.connect(walletShake).decreaseAllowance(wallet1.address, TEST_AMOUNT))
.to.emit(token, 'Approval')
.withArgs(walletShake.address, wallet1.address, 0)
expect(await token.allowance(walletShake.address, wallet1.address)).to.eq(0)
await expect(token.connect(wallet1).transferFrom(walletShake.address, wallet2.address, TEST_AMOUNT)).to.be.reverted
})
it('only minter can call method "mint"', async () => {
await expect(token.connect(walletShake).mint(wallet1.address, TEST_AMOUNT)).to.be.reverted;
await expect(token.connect(walletMinter).mint(wallet1.address, TEST_AMOUNT))
.to.emit(token, 'Transfer')
.withArgs('0x0000000000000000000000000000000000000000', wallet1.address, TEST_AMOUNT)
expect(await token.balanceOf(wallet1.address)).to.eq(TEST_AMOUNT)
expect(await token.totalSupply()).to.eq(AMOUNT_MINT+TEST_AMOUNT)
})
it('minter can not mint more than MAX_TOTAL_SUPPLY', async () => {
//before "it" tokens has already been minted in the amount AMOUNT_MINT
await expect(token.connect(walletMinter).mint(wallet1.address, expandTo18Decimals(MAX_TOTAL_SUPPLY))).to.be.reverted
})
it('minter can not mint to address(0)', async () => {
await expect(token.connect(walletMinter).mint('0x0000000000000000000000000000000000000000', expandTo18Decimals(TEST_AMOUNT))).to.be.reverted
})
it('minter can add other minter to contract (wallet2)', async () => {
await token.connect(walletMinter).addMinter(wallet2.address);
await expect(token.connect(wallet2).mint(wallet1.address, TEST_AMOUNT))
.to.emit(token, 'Transfer')
.withArgs('0x0000000000000000000000000000000000000000', wallet1.address, TEST_AMOUNT)
expect(await token.balanceOf(wallet1.address)).to.eq(TEST_AMOUNT)
expect(await token.totalSupply()).to.eq(AMOUNT_MINT+TEST_AMOUNT)
await token.connect(wallet2).renounceMinter();
await expect(token.connect(wallet2).mint(wallet1.address, TEST_AMOUNT)).to.be.reverted
})
it('minter can burn tokens', async () => {
await expect(token.connect(wallet1).burn(walletShake.address, AMOUNT_MINT)).to.be.reverted
await expect(token.connect(walletMinter).burn(walletShake.address, AMOUNT_MINT))
.to.emit(token, 'Transfer')
.withArgs(walletShake.address, '0x0000000000000000000000000000000000000000', AMOUNT_MINT)
expect(await token.balanceOf(walletShake.address)).to.eq(0)
expect(await token.totalSupply()).to.eq(0)
await expect(token.connect(walletMinter).burn(walletShake.address, 1)).to.be.reverted
})
it('minter burns 0 tokens', async () => {
await token.connect(walletMinter).burn(walletShake.address, 0)
expect(await token.balanceOf(walletShake.address)).to.eq(AMOUNT_MINT)
expect(await token.totalSupply()).to.eq(AMOUNT_MINT)
})
})