-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtest.js
127 lines (102 loc) · 4.28 KB
/
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
import { tokens, ether, ETHER_ADDRESS, EVM_REVERT, wait } from './helpers'
const Token = artifacts.require('./Token')
const DecentralisedBank = artifacts.require('./dBank')
require('chai')
.use(require('chai-as-promised'))
.should()
contract('dBank', ([deployer, user]) => {
let dbank, token
const interestPerSecond = 31668017 //(10% APY) for min. deposit (0.01 ETH)
beforeEach(async () => {
token = await Token.new()
dbank = await DecentralisedBank.new(token.address)
await token.passMinterRole(dbank.address, {from: deployer})
})
describe('testing token contract...', () => {
describe('success', () => {
it('checking token name', async () => {
expect(await token.name()).to.be.eq('Deposit Interest Token')
})
it('checking token symbol', async () => {
expect(await token.symbol()).to.be.eq('DIT')
})
it('checking token initial total supply', async () => {
expect(Number(await token.totalSupply())).to.eq(0)
})
it('dBank should have Token minter role', async () => {
expect(await token.minter()).to.eq(dbank.address)
})
})
describe('failure', () => {
it('passing minter role should be rejected', async () => {
await token.passMinterRole(user, {from: deployer}).should.be.rejectedWith(EVM_REVERT)
})
it('tokens minting should be rejected', async () => {
await token.mint(user, '1', {from: deployer}).should.be.rejectedWith(EVM_REVERT) //unauthorized minter
})
})
})
describe('testing deposit...', () => {
let balance
describe('success', () => {
beforeEach(async () => {
await dbank.deposit({value: 10**16, from: user}) //0.01 ETH
})
it('balance should increase', async () => {
expect(Number(await dbank.getEtherBalance(user))).to.eq(10**16)
})
it('deposit time should > 0', async () => {
expect(Number(await dbank.depositStart(user))).to.be.above(0)
// console.log(Number(await dbank.depositStart(user)))
})
it('deposit status should eq true', async () => {
expect(await dbank.isDeposited(user)).to.eq(true)
})
})
describe('failure', () => {
it('deposit should be rejected for small amount', async () => {
await dbank.deposit({value: 10**15, from: user}).should.be.rejectedWith(EVM_REVERT) //to small amount
})
it('deposit should be rejected for 1000 Wei', async () => {
await dbank.deposit({value: 1000, from: user}).should.be.rejectedWith(EVM_REVERT) //to small amount
})
})
})
describe('testing withdraw...', () => {
let balance
describe('success', () => {
beforeEach(async () => {
await dbank.deposit({value: 10**16, from: user}) //0.01 ETH
await wait(2) //accruing interest
balance = await web3.eth.getBalance(user)
await dbank.withdraw({from: user})
})
it('balances should decrease', async () => {
expect(Number(await web3.eth.getBalance(dbank.address))).to.eq(0)
expect(Number(await dbank.getEtherBalance(user))).to.eq(0)
})
it('user should receive ether back', async () => {
expect(Number(await web3.eth.getBalance(user))).to.be.above(Number(balance))
})
it('user should receive proper amount of interest', async () => {
//time synchronization problem make us check the 1-3s range for 2s deposit time
balance = Number(await token.balanceOf(user))
expect(balance).to.be.above(0)
expect(balance%interestPerSecond).to.eq(0)
expect(balance).to.be.below(interestPerSecond*4)
})
it('depositer data should reset', async () => {
expect(Number(await dbank.depositStart(user))).to.eq(0)
expect(Number(await dbank.getEtherBalance(user))).to.eq(0)
expect(await dbank.isDeposited(user)).to.eq(false)
})
})
})
describe('failure', () => {
it('withdrawing should be rejected', async () =>{
await dbank.deposit({value: 10**16, from: user}) //0.01 ETH
await wait(2) //accruing interest
await dbank.withdraw({from: deployer}).should.be.rejectedWith(EVM_REVERT) //wrong user
})
})
})