-
Notifications
You must be signed in to change notification settings - Fork 3.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[FAB-17140] Add go commercial paper contract (#102)
Signed-off-by: Andrew Hurt <andrew.hurt1@ibm.com>
- Loading branch information
Showing
38 changed files
with
2,418 additions
and
27 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
# | ||
# SPDX-License-Identifier: Apache-2.0 | ||
# | ||
|
||
steps: | ||
- script: go test ./... | ||
workingDirectory: commercial-paper/organization/magnetocorp/contract-go | ||
displayName: Go unit test magnetocorp | ||
- script: go test ./... | ||
workingDirectory: commercial-paper/organization/digibank/contract-go | ||
displayName: Go unit test digibank | ||
- script: bash start.sh | ||
workingDirectory: basic-network | ||
displayName: Start Fabric | ||
- script: | | ||
docker-compose -f docker-compose.yml up -d cliMagnetoCorp | ||
docker exec cliMagnetoCorp bash -c 'cd /opt/gopath/src/github.com/hyperledger/fabric-samples/commercial-paper/organization/magnetocorp/contract-go && go mod vendor' | ||
docker exec cliMagnetoCorp peer lifecycle chaincode package cp.tar.gz --lang golang --path github.com/hyperledger/fabric-samples/commercial-paper/organization/magnetocorp/contract-go --label cp_0 | ||
docker exec cliMagnetoCorp peer lifecycle chaincode install cp.tar.gz | ||
export PACKAGE_ID=$(docker exec cliMagnetoCorp peer lifecycle chaincode queryinstalled 2>&1 | awk -F "[, ]+" '/Label: /{print $3}') | ||
docker exec cliMagnetoCorp peer lifecycle chaincode approveformyorg --channelID mychannel --name papercontract -v 0 --package-id $PACKAGE_ID --sequence 1 --signature-policy "AND ('Org1MSP.member')" | ||
docker exec cliMagnetoCorp peer lifecycle chaincode commit -o orderer.example.com:7050 --channelID mychannel --name papercontract -v 0 --sequence 1 --waitForEvent --signature-policy "AND ('Org1MSP.member')" | ||
docker exec cliMagnetoCorp peer chaincode invoke -o orderer.example.com:7050 --channelID mychannel --name papercontract -c '{"Args":["org.papernet.commercialpaper:instantiate"]}' --waitForEvent | ||
workingDirectory: commercial-paper/organization/magnetocorp/configuration/cli | ||
displayName: Setup Commercial Paper contract | ||
- script: retry -- npm install | ||
workingDirectory: commercial-paper/organization/magnetocorp/application | ||
displayName: Install Magnetocorp application | ||
- script: | | ||
set -ex | ||
node addToWallet.js | ||
node issue.js | ||
workingDirectory: commercial-paper/organization/magnetocorp/application | ||
displayName: Magnetocorp issue paper | ||
- script: retry -- npm install | ||
workingDirectory: commercial-paper/organization/digibank/application | ||
displayName: Install Digibank application | ||
- script: | | ||
set -ex | ||
node addToWallet.js | ||
node buy.js | ||
node redeem.js | ||
workingDirectory: commercial-paper/organization/digibank/application | ||
displayName: Digibank issue paper |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
12 changes: 6 additions & 6 deletions
12
commercial-paper/organization/digibank/application/package-lock.json
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
139 changes: 139 additions & 0 deletions
139
commercial-paper/organization/digibank/contract-go/commercial-paper/paper.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,139 @@ | ||
/* | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package commercialpaper | ||
|
||
import ( | ||
"encoding/json" | ||
"fmt" | ||
|
||
ledgerapi "github.com/hyperledger/fabric-samples/commercial-paper/organization/digibank/contract-go/ledger-api" | ||
) | ||
|
||
// State enum for commercial paper state property | ||
type State uint | ||
|
||
const ( | ||
// ISSUED state for when a paper has been issued | ||
ISSUED State = iota + 1 | ||
// TRADING state for when a paper is trading | ||
TRADING | ||
// REDEEMED state for when a paper has been redeemed | ||
REDEEMED | ||
) | ||
|
||
func (state State) String() string { | ||
names := []string{"ISSUED", "TRADING", "REDEEMED"} | ||
|
||
if state < ISSUED || state > REDEEMED { | ||
return "UNKNOWN" | ||
} | ||
|
||
return names[state-1] | ||
} | ||
|
||
// CreateCommercialPaperKey creates a key for commercial papers | ||
func CreateCommercialPaperKey(issuer string, paperNumber string) string { | ||
return ledgerapi.MakeKey(issuer, paperNumber) | ||
} | ||
|
||
// Used for managing the fact status is private but want it in world state | ||
type commercialPaperAlias CommercialPaper | ||
type jsonCommercialPaper struct { | ||
*commercialPaperAlias | ||
State State `json:"currentState"` | ||
Class string `json:"class"` | ||
Key string `json:"key"` | ||
} | ||
|
||
// CommercialPaper defines a commercial paper | ||
type CommercialPaper struct { | ||
PaperNumber string `json:"paperNumber"` | ||
Issuer string `json:"issuer"` | ||
IssueDateTime string `json:"issueDateTime"` | ||
FaceValue int `json:"faceValue"` | ||
MaturityDateTime string `json:"maturityDateTime"` | ||
Owner string `json:"owner"` | ||
state State `metadata:"currentState"` | ||
class string `metadata:"class"` | ||
key string `metadata:"key"` | ||
} | ||
|
||
// UnmarshalJSON special handler for managing JSON marshalling | ||
func (cp *CommercialPaper) UnmarshalJSON(data []byte) error { | ||
jcp := jsonCommercialPaper{commercialPaperAlias: (*commercialPaperAlias)(cp)} | ||
|
||
err := json.Unmarshal(data, &jcp) | ||
|
||
if err != nil { | ||
return err | ||
} | ||
|
||
cp.state = jcp.State | ||
|
||
return nil | ||
} | ||
|
||
// MarshalJSON special handler for managing JSON marshalling | ||
func (cp CommercialPaper) MarshalJSON() ([]byte, error) { | ||
jcp := jsonCommercialPaper{commercialPaperAlias: (*commercialPaperAlias)(&cp), State: cp.state, Class: "org.papernet.commercialpaper", Key: ledgerapi.MakeKey(cp.Issuer, cp.PaperNumber)} | ||
|
||
return json.Marshal(&jcp) | ||
} | ||
|
||
// GetState returns the state | ||
func (cp *CommercialPaper) GetState() State { | ||
return cp.state | ||
} | ||
|
||
// SetIssued returns the state to issued | ||
func (cp *CommercialPaper) SetIssued() { | ||
cp.state = ISSUED | ||
} | ||
|
||
// SetTrading sets the state to trading | ||
func (cp *CommercialPaper) SetTrading() { | ||
cp.state = TRADING | ||
} | ||
|
||
// SetRedeemed sets the state to redeemed | ||
func (cp *CommercialPaper) SetRedeemed() { | ||
cp.state = REDEEMED | ||
} | ||
|
||
// IsIssued returns true if state is issued | ||
func (cp *CommercialPaper) IsIssued() bool { | ||
return cp.state == ISSUED | ||
} | ||
|
||
// IsTrading returns true if state is trading | ||
func (cp *CommercialPaper) IsTrading() bool { | ||
return cp.state == TRADING | ||
} | ||
|
||
// IsRedeemed returns true if state is redeemed | ||
func (cp *CommercialPaper) IsRedeemed() bool { | ||
return cp.state == REDEEMED | ||
} | ||
|
||
// GetSplitKey returns values which should be used to form key | ||
func (cp *CommercialPaper) GetSplitKey() []string { | ||
return []string{cp.Issuer, cp.PaperNumber} | ||
} | ||
|
||
// Serialize formats the commercial paper as JSON bytes | ||
func (cp *CommercialPaper) Serialize() ([]byte, error) { | ||
return json.Marshal(cp) | ||
} | ||
|
||
// Deserialize formats the commercial paper from JSON bytes | ||
func Deserialize(bytes []byte, cp *CommercialPaper) error { | ||
err := json.Unmarshal(bytes, cp) | ||
|
||
if err != nil { | ||
return fmt.Errorf("Error deserializing commercial paper. %s", err.Error()) | ||
} | ||
|
||
return nil | ||
} |
Oops, something went wrong.