Alibaba Cloud BaaS — Part III: Developing and Deploying a Chain Code

"registrar": [
{
"enrollId": "username",
"enrollSecret": "password"
}
]
BAAS\HLComposer> $P=$PWD.Path.Replace("\", "/")
BAAS\HLComposer> docker run --rm -ti -v $P/composer:/home/composer/.composer -e PORT=8080 -p 8080:8080 registry.cn-hangzhou.aliyun composer:0.20.0
info: [Hyperledger-Composer] :PlaygroundAPI            :createServer()            Playground API started on port 8080
  • Participant: A person who can perform an action or own something, for example in context of the current fund network it is nothing but a Banker who submits the transfer request onto the system.
  • Asset: Anything which holds some value, in our case it is the request asking for a transfer for a single transaction or multiple because it holds the entire information of the transfer
  • Event: When any changes happen to the asset, we can let others know that the change is happen, it is similar to sending a notification once the transfer is successful
  • Transaction: When there is some trade which is happening on the system, that include the creation of assets also
  • CTO: The extension that is used as an extension for participants, assets, transactions and events.
  • Transaction Functions: some code that consists of a logic for a transaction.
  • Namespace: They are basically a way to organize onto groups, to keep things clean in case when the codebase grows.
/**
* Fund clearing business network definition.
*/
namespace org.clearingparticipant BankingParticipant identified by bankingId {
o String bankingId
o String bankingName
o Currency workingCurrency default = 'USD'
o Long fundBalance default = 5000000
}
asset TransferRequest identified by requestId {
o String requestId
o Transfer details
o TransferRequestState fromBankState default = 'PRE_PROCESS_COMPLETE'
o TransferRequestState toBankState
o TransferRequestState state
--> BankingParticipant fromBank
--> BankingParticipant toBank
}
asset BatchTransferRequest identified by batchId {
o String batchId
o Settlement settlement
o BatchState state
--> BankingParticipant[] parties
--> TransferRequest[] transferRequests
}
transaction SubmitTransferRequest {
o String transferId
o String toBank
o TransferRequestState state
o Transfer details
}
transaction CreateBatch {
o String batchId
o UsdExchangeRate[] usdRates
}
transaction MarkPreProcessComplete {
o String batchId
}

transaction CompleteSettlement {
o String batchId
o UsdExchangeRate[] usdRates
}

transaction MarkPostProcessComplete {
o String batchId
}
event BatchCreatedEvent {
o String batchId
}
concept Settlement {  
o Double amount
o Currency currency
--> BankingParticipant creditorBank
--> BankingParticipant debtorBank
}
concept UsdExchangeRate {
o String to
o Double rate
}
concept Transfer {
o Currency currency
o Double amount
o Integer fromAccount
o Integer toAccount
}
enum Currency {
o EURO
o STERLING
o USD
o YEN
o CHF
o CAD
}
enum TransferRequestState {
o PENDING
o PROCESSING
o PRE_PROCESS_COMPLETE
o COMPLETE
o ERROR
}
enum BatchState {
o PENDING_PRE_PROCESS
o READY_TO_SETTLE
o PENDING_POST_PROCESS
o COMPLETE
}

queries.qry

* Queries for Fund Clearing
*/
query TransferRequestsByBankInState {
description: "Select all TransferRequests for a participating bank in a given state"
statement:
SELECT org.clearing.TransferRequest
WHERE (fromBank == _$bank)
}

Permissions.acl:

/**
* Fund clearing access control list.
*/
rule ParticipantsRestrictionOnTransferRequest {
description: "Only allow participants involved with an Asset to interact with it"
participant(p): "org.clearing.BankingParticipant"
operation: ALL
resource(r): "org.clearing.TransferRequest"
condition: (partyWithinTransferRequest(r, p))
action: ALLOW
}
  • partyWithinTransferRequest
  • this checks whether the participant is within the transfer request
  • partyWithinBatchTransferRequest
  • this checks whether the participant is part of the batchTransferRequest or not

clearing.js

/**
* Determine the net transfer between a banking pair, accounting for exchange rates
* @param {TransferRequest[]} transferRequests array of TransferRequest objects
* @param {participantId} participantId string participant identity
* @param {rates[]} rates array of UsdExchangeRate objects
* @return {Double} net amount in USD
*/
function netTransfers(transferRequests, participantId, rates) {
let amount = 0;
// Determine amount in USD
for (let request of transferRequests) {
if (request.toBank.getIdentifier() === participantId) {
if (request.details.currency === 'USD') {
amount += request.details.amount;
} else {
let filteredRate = rates.filter((rate) => { return rate.to === request.details.currency; });
amount += request.details.amount / filteredRate[0].rate;
}
} else {
if (request.details.currency === 'USD') {
amount -= request.details.amount;
} else {
let filteredRate = rates.filter((rate) => { return rate.to === request.details.currency; });
amount -= request.details.amount / filteredRate[0].rate;
}
}
}
return amount;
}

Conclusion

--

--

--

Follow me to keep abreast with the latest technology news, industry insights, and developer trends. Alibaba Cloud website:https://www.alibabacloud.com

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

NFY — Platform Launching

Thirm Protocol Audited!

Blockwhat? Blockchain!

Tezos dApp platform — A Complete Guide

InvArch Network | IP standard for Polkadot.

Leader in BPaas enables industry leaders with Sandbox for metaverse development

What Is Avalanche (AVAX)?

CHANGES TO THE TOKEN ECONOMICS

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Alibaba Cloud

Alibaba Cloud

Follow me to keep abreast with the latest technology news, industry insights, and developer trends. Alibaba Cloud website:https://www.alibabacloud.com

More from Medium

Deploy: StackPath’s web application firewall to protect your Solana RPC

Real-time Visibility into Ukraine Crypto Activity Part I

Hyperledger Iroha: Features and Business Applications — Blockchain Shiksha

Ansible Role to Configure Kubernetes Multi Node Cluster over AWS Cloud