forked from s7techlab/cckit
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patherc20_test.go
129 lines (102 loc) · 3.71 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
package erc20
import (
"testing"
examplecert "github.com/s7techlab/cckit/examples/cert"
testcc "github.com/s7techlab/cckit/testing"
expectcc "github.com/s7techlab/cckit/testing/expect"
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
)
func TestCars(t *testing.T) {
RegisterFailHandler(Fail)
RunSpecs(t, "Cars Suite")
}
var _ = Describe(`ERC-20`, func() {
const TokenSymbol = `HLF`
const TokenName = `HLFCoin`
const TotalSupply = 10000
const Decimals = 3
//Create chaincode mock
erc20fs := testcc.NewMockStub(`erc20`, NewErc20FixedSupply())
// load actor certificates
actors, err := testcc.IdentitiesFromFiles(`SOME_MSP`, map[string]string{
`token_owner`: `s7techlab.pem`,
`account_holder1`: `victor-nosov.pem`,
//`accoubt_holder2`: `victor-nosov.pem`
}, examplecert.Content)
if err != nil {
panic(err)
}
BeforeSuite(func() {
// init token haincode
expectcc.ResponseOk(erc20fs.From(actors[`token_owner`]).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`, actors[`token_owner`].MspId, actors[`token_owner`].GetID()), TotalSupply)
})
It("Allow everyone to get holder's token balance", func() {
expectcc.PayloadInt(
erc20fs.Query(
`balanceOf`, actors[`account_holder1`].MspId, actors[`account_holder1`].GetID()), 0)
})
})
Describe("ERC-20 transfer", func() {
It("Disallow to transfer token to same account", func() {
expectcc.ResponseError(
erc20fs.From(actors[`token_owner`]).Invoke(
`transfer`, actors[`token_owner`].MspId, actors[`token_owner`].GetID(), 100),
ErrForbiddenToTransferToSameAccount)
})
It("Disallow token holder with zero balance to transfer tokens", func() {
expectcc.ResponseError(
erc20fs.From(actors[`account_holder1`]).Invoke(
`transfer`, actors[`token_owner`].MspId, actors[`token_owner`].GetID(), 100),
ErrNotEnoughFunds)
})
It("Allow token holder with non zero balance to transfer tokens", func() {
expectcc.PayloadInt(
erc20fs.From(actors[`token_owner`]).Invoke(
`transfer`, actors[`account_holder1`].MspId, actors[`account_holder1`].GetID(), 100),
TotalSupply-100)
expectcc.PayloadInt(
erc20fs.Query(
`balanceOf`, actors[`token_owner`].MspId, actors[`token_owner`].GetID()), TotalSupply-100)
expectcc.PayloadInt(
erc20fs.Query(
`balanceOf`, actors[`account_holder1`].MspId, actors[`account_holder1`].GetID()), 100)
})
})
Describe("ERC-20 transfer allowance", func() {
It("Allow everyone to check token transfer allowance - zero initially", func() {
expectcc.PayloadInt(
erc20fs.Query(
`allowance`,
actors[`token_owner`].MspId, actors[`token_owner`].GetID(),
actors[`account_holder1`].MspId, actors[`account_holder1`].GetID()), 0)
})
It("Allow token owner to set transfer allowance", func() {
expectcc.ResponseOk(
erc20fs.From(actors[`token_owner`]).Invoke(
`approve`, actors[`account_holder1`].MspId, actors[`account_holder1`].GetID(), 10))
})
It("Allow everyone to check token transfer allowance", func() {
expectcc.PayloadInt(
erc20fs.Query(
`allowance`,
actors[`token_owner`].MspId, actors[`token_owner`].GetID(),
actors[`account_holder1`].MspId, actors[`account_holder1`].GetID()), 10)
})
})
})