-
Notifications
You must be signed in to change notification settings - Fork 6
/
erc20_test.go
174 lines (140 loc) · 4.84 KB
/
erc20_test.go
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
package erc20_test
import (
"testing"
"github.com/hyperledger-labs/cckit/examples/erc20"
idtestdata "github.com/hyperledger-labs/cckit/identity/testdata"
testcc "github.com/hyperledger-labs/cckit/testing"
expectcc "github.com/hyperledger-labs/cckit/testing/expect"
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
)
func TestErc20(t *testing.T) {
RegisterFailHandler(Fail)
RunSpecs(t, "ERC-20 Suite")
}
var (
ids = idtestdata.MustIdentities(idtestdata.Certificates, idtestdata.DefaultMSP)
// load actor certificates
TokenOwner = ids[0]
AccountHolder1 = ids[1]
Spender1 = ids[2]
)
var _ = Describe(`ERC-20`, func() {
const TokenSymbol = `HLF`
const TokenName = `HLFCoin`
const TotalSupply = 10000
const Decimals = 3
//Create chaincode mock
erc20fs := testcc.NewMockStub(`erc20`, erc20.NewErc20FixedSupply())
BeforeSuite(func() {
// init token haincode
expectcc.ResponseOk(erc20fs.From(TokenOwner).Init(TokenSymbol, TokenName, TotalSupply, Decimals))
})
Describe("ERC-20 creation", func() {
It("Allow everyone to get token symbol", func() {
expectcc.PayloadString(erc20fs.Query(`symbol`), TokenSymbol)
})
It("Allow everyone to get token name", func() {
expectcc.PayloadString(erc20fs.Query(`name`), TokenName)
})
It("Allow everyone to get token total supply", func() {
expectcc.PayloadInt(erc20fs.Query(`totalSupply`), TotalSupply)
})
It("Allow everyone to get owner's token balance", func() {
expectcc.PayloadInt(
erc20fs.Query(
`balanceOf`, TokenOwner.MspID, TokenOwner.GetID()), TotalSupply)
})
It("Allow everyone to get holder's token balance", func() {
expectcc.PayloadInt(
erc20fs.Query(
`balanceOf`, AccountHolder1.MspID, AccountHolder1.GetID()), 0)
})
})
Describe("ERC-20 transfer", func() {
It("Disallow to transfer token to same account", func() {
expectcc.ResponseError(
erc20fs.From(TokenOwner).Invoke(
`transfer`, TokenOwner.MspID, TokenOwner.GetID(), 100),
erc20.ErrForbiddenToTransferToSameAccount)
})
It("Disallow token holder with zero balance to transfer tokens", func() {
expectcc.ResponseError(
erc20fs.From(AccountHolder1).Invoke(
`transfer`, TokenOwner.MspID, TokenOwner.GetID(), 100),
erc20.ErrNotEnoughFunds)
})
It("Allow token holder with non zero balance to transfer tokens", func() {
expectcc.PayloadInt(
erc20fs.From(TokenOwner).Invoke(
`transfer`, AccountHolder1.MspID, AccountHolder1.GetID(), 100),
TotalSupply-100)
expectcc.PayloadInt(
erc20fs.Query(
`balanceOf`, TokenOwner.MspID, TokenOwner.GetID()), TotalSupply-100)
expectcc.PayloadInt(
erc20fs.Query(
`balanceOf`, AccountHolder1.MspID, AccountHolder1.GetID()), 100)
})
})
Describe("ERC-20 transfer allowance", func() {
It("Allow everyone to check token transfer allowance - zero initially", func() {
expectcc.PayloadInt(
erc20fs.Query(
`allowance`,
TokenOwner.MspID, TokenOwner.GetID(),
Spender1.MspID, Spender1.GetID()), 0)
})
It("Allow token owner to set transfer allowance", func() {
expectcc.ResponseOk(
erc20fs.From(TokenOwner).Invoke(
`approve`, Spender1.MspID, Spender1.GetID(), 10))
})
It("Allow everyone to check token transfer allowance", func() {
expectcc.PayloadInt(
erc20fs.Query(
`allowance`,
TokenOwner.MspID, TokenOwner.GetID(),
Spender1.MspID, Spender1.GetID()), 10)
})
It("Allow everyone to check token transfer allowance", func() {
expectcc.PayloadInt(
erc20fs.Query(
`allowance`,
TokenOwner.MspID, TokenOwner.GetID(),
Spender1.MspID, Spender1.GetID()), 10)
})
It("Disallow nonspender to initiate payment from owner waller", func() {
expectcc.ResponseError(
erc20fs.From(AccountHolder1).Invoke(
`transferFrom`,
TokenOwner.MspID, TokenOwner.GetID(),
AccountHolder1.MspID, AccountHolder1.GetID(), 10), erc20.ErrSpenderNotHaveAllowance)
})
It("Disallow spender to initiate payment more than allowance from wallet owner", func() {
expectcc.ResponseError(
erc20fs.From(AccountHolder1).Invoke(
`transferFrom`,
TokenOwner.MspID, TokenOwner.GetID(),
AccountHolder1.MspID, AccountHolder1.GetID(), 11), erc20.ErrSpenderNotHaveAllowance)
})
It("Allow spender to initiate payment from owner waller", func() {
expectcc.PayloadInt(
erc20fs.From(Spender1).Invoke(
`transferFrom`,
TokenOwner.MspID, TokenOwner.GetID(),
AccountHolder1.MspID, AccountHolder1.GetID(), 9), TotalSupply-100-9)
expectcc.PayloadInt(
erc20fs.Query(
`balanceOf`, AccountHolder1.MspID, AccountHolder1.GetID()), 100+9)
expectcc.PayloadInt(
erc20fs.Query(
`balanceOf`, TokenOwner.MspID, TokenOwner.GetID()), TotalSupply-100-9)
expectcc.PayloadInt(
erc20fs.Query(
`allowance`,
TokenOwner.MspID, TokenOwner.GetID(),
Spender1.MspID, Spender1.GetID()), 1)
})
})
})