-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathpapercontract.js
281 lines (223 loc) · 8.25 KB
/
papercontract.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
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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
/*
SPDX-License-Identifier: Apache-2.0
*/
'use strict';
// Fabric smart contract classes
const { Contract, Context } = require('fabric-contract-api');
// PaperNet specifc classes
const CommercialPaper = require('./paper.js');
const PaperList = require('./paperlist.js');
/**
* A custom context provides easy access to list of all commercial papers
*/
class CommercialPaperContext extends Context {
constructor() {
super();
// All papers are held in a list of papers
this.paperList = new PaperList(this);
}
}
/**
* Define commercial paper smart contract by extending Fabric Contract class
*
*/
class CommercialPaperContract extends Contract {
constructor() {
// Unique namespace when multiple contracts per chaincode file
super('org.papernet.commercialpaper');
}
/**
* Define a custom context for commercial paper
*/
createContext() {
return new CommercialPaperContext();
}
/**
* Instantiate to perform any setup of the ledger that might be required.
* @param {Context} ctx the transaction context
*/
async instantiate(ctx) {
// No implementation required with this example
// It could be where data migration is performed, if necessary
console.log('Instantiate the contract');
}
/**
* Issue commercial paper
*
* @param {Context} ctx the transaction context
* @param {String} issuer commercial paper issuer
* @param {Integer} paperNumber paper number for this issuer
* @param {String} issueDateTime paper issue date
* @param {String} maturityDateTime paper maturity date
* @param {Integer} faceValue face value of paper
*/
async issue(ctx, issuer, paperNumber, issueDateTime, maturityDateTime, faceValue) {
// create an instance of the paper
let paper = CommercialPaper.createInstance(issuer, paperNumber, issueDateTime, maturityDateTime, faceValue);
// Smart contract, rather than paper, moves paper into ISSUED state
paper.setIssued();
// Newly issued paper is owned by the issuer
paper.setOwner(issuer);
// Add the paper to the list of all similar commercial papers in the ledger world state
await ctx.paperList.addPaper(paper);
// Must return a serialized paper to caller of smart contract
return paper.toBuffer();
}
/**
* Buy commercial paper
*
* @param {Context} ctx the transaction context
* @param {String} issuer commercial paper issuer
* @param {Integer} paperNumber paper number for this issuer
* @param {String} currentOwner current owner of paper
* @param {String} newOwner new owner of paper
* @param {Integer} price price paid for this paper
* @param {String} purchaseDateTime time paper was purchased (i.e. traded)
*/
async buy(ctx, issuer, paperNumber, currentOwner, newOwner, price, purchaseDateTime) {
// Retrieve the current paper using key fields provided
let paperKey = CommercialPaper.makeKey([issuer, paperNumber]);
let paper = await ctx.paperList.getPaper(paperKey);
// Validate current owner
if (paper.getOwner() !== currentOwner) {
throw new Error('Paper ' + issuer + paperNumber + ' is not owned by ' + currentOwner);
}
// First buy moves state from ISSUED to TRADING
if (paper.isIssued()) {
paper.setTrading();
}
// Check paper is not already REDEEMED
if (paper.isTrading()) {
paper.setOwner(newOwner);
} else {
throw new Error('Paper ' + issuer + paperNumber + ' is not trading. Current state = ' + paper.getCurrentState());
}
// Update the paper
await ctx.paperList.updatePaper(paper);
return paper.toBuffer();
}
/**
* Redeem commercial paper
*
* @param {Context} ctx the transaction context
* @param {String} issuer commercial paper issuer
* @param {Integer} paperNumber paper number for this issuer
* @param {String} redeemingOwner redeeming owner of paper
* @param {String} redeemDateTime time paper was redeemed
*/
async redeem(ctx, issuer, paperNumber, redeemingOwner, redeemDateTime) {
let paperKey = CommercialPaper.makeKey([issuer, paperNumber]);
let paper = await ctx.paperList.getPaper(paperKey);
// Check paper is not REDEEMED
if (paper.isRedeemed()) {
throw new Error('Paper ' + issuer + paperNumber + ' already redeemed');
}
// Verify that the redeemer owns the commercial paper before redeeming it
if (paper.getOwner() === redeemingOwner) {
paper.setOwner(paper.getIssuer());
paper.setRedeemed();
} else {
throw new Error('Redeeming owner does not own paper' + issuer + paperNumber);
}
await ctx.paperList.updatePaper(paper);
return paper.toBuffer();
}
/**
* Query by Issuer
*
* @param {Context} ctx the transaction context
* @param {String} issuer commercial paper issuer
*/
async queryByIssuer(ctx, issuer) {
let queryString = {
selector: {
issuer: issuer
},
use_index: ['_design/issuerIndexDoc', 'issuerIndex']
};
let queryResults = await this.queryWithQueryString(ctx, JSON.stringify(queryString));
return queryResults;
}
/**
* Query by owner
*
* @param {Context} ctx the transaction context
* @param {String} owner commercial paper owner
*/
async queryByOwner(ctx, owner) {
let queryString = {
selector: {
owner: owner
},
use_index: ['_design/ownerIndexDoc', 'ownerIndex']
};
let queryResults = await this.queryWithQueryString(ctx, JSON.stringify(queryString));
return queryResults;
}
/**
* Query by current state
*
* @param {Context} ctx the transaction context
* @param {String} currentState current state number of the commercial paper. Refer to paper.js for state values
*/
async queryByCurrentState(ctx, currentState) {
let state = parseInt(currentState);
let queryString = {
selector: {
currentState: state
},
use_index: ['_design/currentStateIndexDoc', 'currentStateIndex']
};
let queryResults = await this.queryWithQueryString(ctx, JSON.stringify(queryString));
return queryResults;
}
/**
* Query by Issuer
*
* @param {Context} ctx the transaction context
* @param {String} issuer commercial paper issuer
*/
async queryAll(ctx) {
let queryString = {
selector: {}
};
let queryResults = await this.queryWithQueryString(ctx, JSON.stringify(queryString));
return queryResults;
}
/**
* Evaluate a queryString
*
* @param {Context} ctx the transaction context
* @param {String} queryString the query string to be evaluated
*/
async queryWithQueryString(ctx, queryString) {
console.log('query String');
console.log(JSON.stringify(queryString));
let resultsIterator = await ctx.stub.getQueryResult(queryString);
let allResults = [];
// eslint-disable-next-line no-constant-condition
while (true) {
let res = await resultsIterator.next();
if (res.value && res.value.value.toString()) {
let jsonRes = {};
console.log(res.value.value.toString('utf8'));
jsonRes.Key = res.value.key;
try {
jsonRes.Record = JSON.parse(res.value.value.toString('utf8'));
} catch (err) {
console.log(err);
jsonRes.Record = res.value.value.toString('utf8');
}
allResults.push(jsonRes);
}
if (res.done) {
console.log('end of data');
await resultsIterator.close();
console.info(allResults);
console.log(JSON.stringify(allResults));
return JSON.stringify(allResults);
}
}
}
}
module.exports = CommercialPaperContract;