-
Notifications
You must be signed in to change notification settings - Fork 6
/
chaincode.go
200 lines (164 loc) · 6.1 KB
/
chaincode.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
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
package cpaper
import (
"fmt"
"time"
"github.com/hyperledger-labs/cckit/router"
"github.com/hyperledger-labs/cckit/router/param"
)
type TaskState string
const (
CommercialPaperTypeName = "CommercialPaper"
CommercialPaperIssued TaskState = "issued"
CommercialPaperTrading TaskState = "trading"
CommercialPaperRedeemed TaskState = "redeemed"
)
type CommercialPaper struct {
Issuer string `json:"issuer,omitempty"`
PaperNumber string `json:"paper_number,omitempty"`
Owner string `json:"owner,omitempty"`
IssueDate time.Time `json:"issue_date,omitempty"`
MaturityDate time.Time `json:"maturity_date,omitempty"`
FaceValue int32 `json:"face_value,omitempty"`
State TaskState `json:"state,omitempty"`
}
// Key commercial paper has a composite key < CommercialPaper, Issuer, PaperNumber >
func (c CommercialPaper) Key() ([]string, error) {
return []string{CommercialPaperTypeName, c.Issuer, c.PaperNumber}, nil
}
type IssueCommercialPaper struct {
Issuer string `json:"issuer,omitempty"`
PaperNumber string `json:"paper_number,omitempty"`
IssueDate time.Time `json:"issue_date,omitempty"`
MaturityDate time.Time `json:"maturity_date,omitempty"`
FaceValue int32 `json:"face_value,omitempty"`
}
type BuyCommercialPaper struct {
Issuer string `json:"issuer,omitempty"`
PaperNumber string `json:"paper_number,omitempty"`
CurrentOwner string `json:"current_owner,omitempty"`
NewOwner string `json:"new_owner,omitempty"`
Price int32 `json:"price,omitempty"`
PurchaseDate time.Time `json:"purchase_date,omitempty"`
}
type RedeemCommercialPaper struct {
Issuer string `json:"issuer,omitempty"`
PaperNumber string `json:"paper_number,omitempty"`
RedeemingOwner string `json:"redeeming_owner,omitempty"`
RedeemDate time.Time `json:"redeem_date,omitempty"`
}
func NewCC() *router.Chaincode {
r := router.New(`commercial_paper`)
r.Init(func(context router.Context) (i interface{}, e error) {
// No implementation required with this example
// It could be where data migration is performed, if necessary
return nil, nil
})
r.
// Read methods
Query(`list`, list).
// Get method has 2 params - commercial paper primary key components
Query(`get`, get, param.String("issuer"), param.String("paper_number")).
// Transaction methods
Invoke(`issue`, issue, param.Struct("issueData", &IssueCommercialPaper{})).
Invoke(`buy`, buy, param.Struct("buyData", &BuyCommercialPaper{})).
Invoke(`redeem`, redeem, param.Struct("redeemData", &RedeemCommercialPaper{}))
return router.NewChaincode(r)
}
func get(c router.Context) (interface{}, error) {
var (
issuer = c.ParamString("issuer")
paperNumber = c.ParamString("paper_number")
)
return c.State().Get(&CommercialPaper{
Issuer: issuer,
PaperNumber: paperNumber,
})
}
func list(c router.Context) (interface{}, error) {
// List method retrieves all entries from the ledger using GetStateByPartialCompositeKey method and passing it the
// namespace of our contract type, in this example that's `CommercialPaper`, then it unmarshalls received bytes via
// json.Unmarshal method and creates a JSON array of CommercialPaper entities
return c.State().List(CommercialPaperTypeName, &CommercialPaper{})
}
func issue(c router.Context) (interface{}, error) {
var (
issueData = c.Param("issueData").(IssueCommercialPaper) // Assert the chaincode parameter
commercialPaper = &CommercialPaper{
Issuer: issueData.Issuer,
PaperNumber: issueData.PaperNumber,
Owner: issueData.Issuer,
IssueDate: issueData.IssueDate,
MaturityDate: issueData.MaturityDate,
FaceValue: issueData.FaceValue,
State: CommercialPaperIssued, // Initial state
}
)
return commercialPaper, c.State().Insert(commercialPaper)
}
func buy(c router.Context) (interface{}, error) {
var (
commercialPaper CommercialPaper
// Buy transaction payload
buyData = c.Param("buyData").(BuyCommercialPaper)
// Get the current commercial paper state
cp, err = c.State().Get(&CommercialPaper{
Issuer: buyData.Issuer,
PaperNumber: buyData.PaperNumber,
}, &CommercialPaper{})
)
if err != nil {
return nil, fmt.Errorf("paper not found: %w", err)
}
commercialPaper = cp.(CommercialPaper)
// Validate current owner
if commercialPaper.Owner != buyData.CurrentOwner {
return nil, fmt.Errorf(
"paper %s %s is not owned by %s",
commercialPaper.Issuer, commercialPaper.PaperNumber, buyData.CurrentOwner)
}
// First buyData moves state from ISSUED to TRADING
if commercialPaper.State == CommercialPaperIssued {
commercialPaper.State = CommercialPaperTrading
}
// Check paper is not already REDEEMED
if commercialPaper.State == CommercialPaperTrading {
commercialPaper.Owner = buyData.NewOwner
} else {
return nil, fmt.Errorf(
"paper %s %s is not trading.current state = %s",
commercialPaper.Issuer, commercialPaper.PaperNumber, commercialPaper.State)
}
return commercialPaper, c.State().Put(commercialPaper)
}
func redeem(c router.Context) (interface{}, error) {
var (
commercialPaper CommercialPaper
// Buy transaction payload
redeemData = c.Param("redeemData").(RedeemCommercialPaper)
// Get the current commercial paper state
cp, err = c.State().Get(&CommercialPaper{
Issuer: redeemData.Issuer,
PaperNumber: redeemData.PaperNumber,
}, &CommercialPaper{})
)
if err != nil {
return nil, fmt.Errorf("paper not found: %w", err)
}
commercialPaper = cp.(CommercialPaper)
// Check paper is not REDEEMED
if commercialPaper.State == CommercialPaperRedeemed {
return nil, fmt.Errorf(
"paper %s %s is already redeemed",
commercialPaper.Issuer, commercialPaper.PaperNumber)
}
// Verify that the redeemer owns the commercial paper before redeeming it
if commercialPaper.Owner == redeemData.RedeemingOwner {
commercialPaper.Owner = redeemData.Issuer
commercialPaper.State = CommercialPaperRedeemed
} else {
return nil, fmt.Errorf(
"redeeming owner does not own paper %s %s",
commercialPaper.Issuer, commercialPaper.PaperNumber)
}
return commercialPaper, c.State().Put(commercialPaper)
}