Giter VIP home page Giter VIP logo

customer-loyalty-program-hyperledger-fabric-vscode's Introduction

Build Status

Customer Loyalty Program with blockchain

A customer loyalty program allows companies to reward customers who frequently make purchases. Program members are able to earn points on purchases, which can translate into some type of reward such as discount, freebie or special customer treatment. The members work toward a certain amount of points to redeem their reward. These programs can have multiple companies as partners on the program, to cater to a customer base. However, current loyalty program systems are restraint on relations between partners, and with visibility to members. These restraints can be removed by creating the customer loyalty program on a blockchain network.

This blockchain model for a customer loyalty program enhances the value of points to loyalty program members and brings in new value to the partners by creating trusted transactions. Participants in this network have a more level relation among each other and points are in the centric position to connect all participants.

In this code pattern, we will create a customer loyalty program as a blockchain web application using Hyperledger Fabric and Node.js. The application will allow members to register on the network where they will create their account. They will be identified on the network with their account number and will create an access key which they will use to sign in. This access key is used as the card id for the member to make transactions and query records. The member once signed in, can make transactions to earn points and redeem points from the partners on the network. They can view their transactions as part of the blockchain ledger. This code pattern illustrates the use of permissions as part of the network where a member can only view their transactions.

Similarly for the partner, they will register by creating an identity on the network and an access key which will be used to view their records. Partners are allowed to view only transactions they were part of, and thus can keep track of all their transactions where they allocated or redeemed points. The web application shows a basic dashboard for the partner displaying the total points that they have allocated and redeemed to members. As transactions get complex, the partner can perform analysis on their transactions to create informative dashboards.

This code pattern is for developers looking to start building blockchain applications with Hyperledger Fabric and IBM Blockchain extension for VSCode. When the reader has completed this code pattern, they will understand how to:

  • Setup a Hyperledger Fabric network on IBM Blockchain Platform Extension for VSCode.
  • Install and instantiate a smart contract through the IBM Blockchain Platform Extension for VSCode on a local fabric connection.
  • Develop a Node.js web application with the Hyperledger Fabric SDK to interact with the deployed network

Architecture Flow

Note The blockchain network will have multiple members and partners

  1. Member is registered on the network
  2. Member can sign-in to make transactions to earn points, redeem points and view their transactions
  3. Partner is registered on the network
  4. Partner can sign-in to view their transactions and display dashboard

Included Components

  • IBM Blockchain Platform gives you total control of your blockchain network with a user interface that can simplify and accelerate your journey to deploy and manage blockchain components on the IBM Cloud Kubernetes Service.
  • IBM Blockchain Platform Extension for VS Code is designed to assist users in developing, testing, and deploying smart contracts -- including connecting to Hyperledger Fabric environments.

Featured technology

  • Hyperledger Fabric v1.4 is a platform for distributed ledger solutions, underpinned by a modular architecture that delivers high degrees of confidentiality, resiliency, flexibility, and scalability.
  • Node.js is an open source, cross-platform JavaScript run-time environment that executes server-side JavaScript code.
  • Express.js is a minimal and flexible Node.js web application framework that provides a robust set of features for web and mobile applications.
  • Bootstrap Bootstrap is an open source toolkit for developing with HTML, CSS, and JS.

Running the application locally

Follow these steps to set up and run this code pattern.

Prerequisites

You will need to follow the requirements for the IBM Blockchain Platform Extension for VS Code:

Steps

  1. Clone the repo
  2. Package the smart contract
  3. Setup network locally and deploy the smart contract
  4. Run the application

1. Clone the repo

Clone this repository in a folder your choice:

git clone https://github.com/IBM/customer-loyalty-program-hyperledger-fabric-VSCode.git
cd customer-loyalty-program-hyperledger-fabric-VSCode

2. Package the smart contract

We will use the IBM Blockchain Platform Extension for VSCode to package the customerloyalty smart contract.

  • Open Visual Studio code and open the contract folder from this repository that was cloned earlier.

Press the F1 key to see the different VS code options. Choose IBM Blockchain Platform: Package Open Project.

Click the IBM Blockchain Platform extension button on the left. This will show the packaged contracts on top and the blockchain connections on the bottom.

3. Setup network locally and deploy the smart contract

You should see FABRIC ENVIRONMENTS on the left side of the editor. Under this section, you should see Local Fabric. Click it to start the Local Fabric.

The extension will now provision the Docker containers that will act as nodes in your network. Once the provisioning is finished and the network is up and running, you will see the options to install and instantiate the smart contract, the Channels information, the Nodes and the organization msps under Organizations. You are now ready to install the smart contract.

  • In the FABRIC ENVIRONMENTS section near the bottom, click on Smart Contracts > Installed > + Install. You will see a pop-up similar to the graphic below.

  • Then select the packaged contract: [email protected] Packaged Note The 0.0.1 comes from your package.json line: "version": "0.0.1"

After the install is complete, you should get a message Successfully installed on peer peer0.org1.example.com. You should also see that the contract is listed under Installed under FABRIC ENVIRONMENTS.

  • Under Smart Contracts you will see a section that says Instantiated. Click on + Instantiate under it.

  • The extension will then ask you which contract and version to instantiate โ€” choose [email protected] Installed.

  • The extension will then ask you which function to call on instantiate โ€” type in instantiate

  • Next, it will ask you for the arguments to the function. There are none, so just hit enter.

  • Next, the extension will then ask you do you want to use a provide a private data collection configuration file? - Click on No.

  • Lastly, the extension will then ask you do you want to choose a smart contract endorsement policy. Choose Default (single endorser, any org).

Once instantiation of the contract completes, you should get the message Successfully instantiated smart contract and you should see [email protected] under Instantiated under FABRIC ENVIRONMENTS.

4. Run the application

  • Enroll admin

    • First, navigate to the web-app directory, and install the node dependencies.

      cd web-app/
      npm install
    • Run the enrollAdmin.js script

      node enrollAdmin.js
    • You should see the following in the terminal:

      msg: Successfully enrolled admin user app-admin and imported it into the wallet
  • Run the application server

    • From the web-app directory, start the server.

      npm start

You can find the app running at http://localhost:8000/



Links

License

This code pattern is licensed under the Apache Software License, Version 2. Separate third-party code objects invoked within this code pattern are licensed by their respective providers pursuant to their own separate licenses. Contributions are subject to the Developer Certificate of Origin, Version 1.1 (DCO) and the Apache Software License, Version 2.

Apache Software License (ASL) FAQ

customer-loyalty-program-hyperledger-fabric-vscode's People

Contributors

ash7594 avatar dependabot[bot] avatar ljbennett62 avatar lucianoacsilva avatar praveenhonavar avatar raheelzubairy avatar rgeraldporter avatar sandhya-nayak avatar stevemart avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

customer-loyalty-program-hyperledger-fabric-vscode's Issues

vscode prereq should be 1.36

The pre req for the version of vscode to use should now be 1.36 to work with the latest version of the extension

Application has references to Composer

The application has a few references to Composer remaining:

  • about.html - it does not use the Composer API, but the fabric network API
  • index.html - same as above

Could be confusing to new users.

Unable to Sign In.

Wallet path: /Users/syed/Downloads/hyperledger/customer-loyalty-program-hyperledger-fabric-VSCode/web-app/wallet

Get use points transactions state for member 456789
{ Error: transaction returned with failure: SyntaxError: Unexpected end of JSON input
at self._endorserClient.processProposal (/Users/syed/Downloads/hyperledger/customer-loyalty-program-hyperledger-fabric-VSCode/web-app/node_modules/fabric-client/lib/Peer.js:140:36)
at Object.onReceiveStatus (/Users/syed/Downloads/hyperledger/customer-loyalty-program-hyperledger-fabric-VSCode/web-app/node_modules/grpc/src/client_interceptors.js:1191:9)
at InterceptingListener._callNext (/Users/syed/Downloads/hyperledger/customer-loyalty-program-hyperledger-fabric-VSCode/web-app/node_modules/grpc/src/client_interceptors.js:564:42)
at InterceptingListener.onReceiveStatus (/Users/syed/Downloads/hyperledger/customer-loyalty-program-hyperledger-fabric-VSCode/web-app/node_modules/grpc/src/client_interceptors.js:614:8)
at callback (/Users/syed/Downloads/hyperledger/customer-loyalty-program-hyperledger-fabric-VSCode/web-app/node_modules/grpc/src/client_interceptors.js:841:24)
status: 500,
payload: ,
peer:
{ url: 'grpc://localhost:17051',
name: 'peer0.org1.example.com',
options:
{ name: 'peer0.org1.example.com',
'request-timeout': 300000,
'grpc.max_receive_message_length': -1,
'grpc.max_send_message_length': -1 } },
isProposalResponse: true }
Wallet path: /Users/syed/Downloads/hyperledger/customer-loyalty-program-hyperledger-fabric-VSCode/web-app/wallet

Get earn points transactions state for member 456789
{ Error: transaction returned with failure: SyntaxError: Unexpected end of JSON input
at self._endorserClient.processProposal (/Users/syed/Downloads/hyperledger/customer-loyalty-program-hyperledger-fabric-VSCode/web-app/node_modules/fabric-client/lib/Peer.js:140:36)
at Object.onReceiveStatus (/Users/syed/Downloads/hyperledger/customer-loyalty-program-hyperledger-fabric-VSCode/web-app/node_modules/grpc/src/client_interceptors.js:1191:9)
at InterceptingListener._callNext (/Users/syed/Downloads/hyperledger/customer-loyalty-program-hyperledger-fabric-VSCode/web-app/node_modules/grpc/src/client_interceptors.js:564:42)
at InterceptingListener.onReceiveStatus (/Users/syed/Downloads/hyperledger/customer-loyalty-program-hyperledger-fabric-VSCode/web-app/node_modules/grpc/src/client_interceptors.js:614:8)
at callback (/Users/syed/Downloads/hyperledger/customer-loyalty-program-hyperledger-fabric-VSCode/web-app/node_modules/grpc/src/client_interceptors.js:841:24)
status: 500,
payload: ,
peer:
{ url: 'grpc://localhost:17051',
name: 'peer0.org1.example.com',
options:
{ name: 'peer0.org1.example.com',
'request-timeout': 300000,
'grpc.max_receive_message_length': -1,
'grpc.max_send_message_length': -1 } },
isProposalResponse: true }
(node:53834) UnhandledPromiseRejectionWarning: Error [ERR_HTTP_HEADERS_SENT]: Cannot set headers after they are sent to the client
at ServerResponse.setHeader (_http_outgoing.js:470:11)
at ServerResponse.header (/Users/syed/Downloads/hyperledger/customer-loyalty-program-hyperledger-fabric-VSCode/web-app/node_modules/express/lib/response.js:767:10)
at ServerResponse.send (/Users/syed/Downloads/hyperledger/customer-loyalty-program-hyperledger-fabric-VSCode/web-app/node_modules/express/lib/response.js:170:12)
at ServerResponse.json (/Users/syed/Downloads/hyperledger/customer-loyalty-program-hyperledger-fabric-VSCode/web-app/node_modules/express/lib/response.js:267:15)
at network.earnPointsTransactionsInfo.then (/Users/syed/Downloads/hyperledger/customer-loyalty-program-hyperledger-fabric-VSCode/web-app/app.js:279:21)
(node:53834) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). (rejection id: 10)

timestamp undefined

I implemented the fabric locally and I did not see any errors.

My member made purchases and redeemed points but the transaction data is incorrect.
For example:

Points Earned Transactions

timeStamp: undefined
partner: Villanova01
member: 123456
points: 30
transactionName: undefined
transactionID: undefined


timeStamp: undefined
partner: Villanova01
member: 123456
points: 80
transactionName: undefined
transactionID: undefined

and

Points Redeemed Transactions

timeStamp: undefined
partner: Villanova01
member: 123456
points: 50
transactionName: undefined
transactionID: undefined

Appreciate suggestions on how to fix.

Error while Member Registration

I am getting the following error

Using param - firstname: aaa lastname: bb email: [email protected] phonenumber: 1234567890 accountNumber: 100001 cardId: ac1
Valid Entries
Wallet path: /Users/abc/Desktop/BlockChain/customer-loyalty-program-hyperledger-fabric-VSCode/web-app/wallet
Error: fabric-ca request register failed with errors [[{"code":19,"message":"CA 'ca.org1.example.com' does not exist"}]]
at IncomingMessage.response.on (/Users/avnirai/Desktop/BlockChain/customer-loyalty-program-hyperledger-fabric-VSCode/web-app/node_modules/fabric-client/node_modules/fabric-ca-client/lib/FabricCAClient.js:327:22)
at IncomingMessage.emit (events.js:203:15)
at endReadableNT (_stream_readable.js:1129:12)
at process._tickCallback (internal/process/next_tick.js:63:19)

Unexpected token o in JSON at position 1

Hi, when I tried to create a new member and using it to sign in, however the browser throws this error:

Screenshot from 2019-07-26 00-39-26

And the Node console gives:

app running on port: 8000
memberData using param -  accountNumber: 111111 cardId: test
Wallet path: /home/lee/customer-loyalty-program-hyperledger-fabric-VSCode/web-app/wallet

Get member state 
2019-07-25T16:39:14.079Z - info: [TransactionEventHandler]: _strategySuccess: strategy success for transaction "bbf5b13f637a82ca8ad61cf6dc45bebe426a1a7e5e3792ec4ce7e5b345a345fb"
SyntaxError: Unexpected token o in JSON at position 1
    at JSON.parse (<anonymous>)
    at Object.memberData (/home/lee/customer-loyalty-program-hyperledger-fabric-VSCode/web-app/network/network.js:371:21)
    at <anonymous>
    at process._tickCallback (internal/process/next_tick.js:188:7)
Wallet path: /home/lee/customer-loyalty-program-hyperledger-fabric-VSCode/web-app/wallet

Get use points transactions state for member 111111
SyntaxError: Unexpected end of JSON input
    at JSON.parse (<anonymous>)
    at Object.usePointsTransactionsInfo (/home/lee/customer-loyalty-program-hyperledger-fabric-VSCode/web-app/network/network.js:539:36)
    at <anonymous>
(node:32527) UnhandledPromiseRejectionWarning: Unhandled promise rejection (rejection id: 2): Error: Can't set headers after they are sent.
(node:32527) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.

I'm using nodejs version 8.9.4 if that helps.

Getting error while Submit EarnPoints transaction

Hi @ALL,

i got the following error message when i want to "Earn Points through purchases":

Submit EarnPoints transaction.
2023-02-22T16:22:05.943Z - error: [Transaction]: Error: No valid responses from any peers. Errors:
    peer=peer0.org1.example.com:7051, status=500, message=Cannot read properties of undefined (reading 'low')
    peer=peer0.org2.example.com:9051, status=500, message=Cannot read properties of undefined (reading 'low')
at newEndorsementError ([..]/customer-loyalty-program-hyperledger-fabric/web-app/node_modules/fabric-network/lib/transaction.js:74:12)
    at getResponsePayload ([..]/customer-loyalty-program-hyperledger-fabric/web-app/node_modules/fabric-network/lib/transaction.js:41:23)
    at Transaction.submit ([..]/customer-loyalty-program-hyperledger-fabric/web-app/node_modules/fabric-network/lib/transaction.js:255:28)
    at processTicksAndRejections (node:internal/process/task_queues:96:5)
    at async Object.earnPointsTransaction ([..]/customer-loyalty-program-hyperledger-fabric/web-app/network/network.js:304:40) {
  responses: [
    {
      version: 0,
      timestamp: null,
      response: [Object],
      payload: <Buffer 0a 20 26 c9 ae 9b 52 c5 7d 7b 91 09 a6 97 80 c0 ab 2f f9 cc f8 b6 a8 8d fc df 1e d0 53 3b 53 a6 1e a1 12 ae 01 0a 61 12 39 0a 0a 5f 6c 69 66 65 63 79 ... 161 more bytes>,
      endorsement: null,
      connection: [Object],
      peer: 'peer0.org1.example.com:7051'
    },
    {
      version: 0,
      timestamp: null,
      response: [Object],
      payload: <Buffer 0a 20 26 c9 ae 9b 52 c5 7d 7b 91 09 a6 97 80 c0 ab 2f f9 cc f8 b6 a8 8d fc df 1e d0 53 3b 53 a6 1e a1 12 ae 01 0a 61 12 39 0a 0a 5f 6c 69 66 65 63 79 ... 161 more bytes>,
      endorsement: null,
      connection: [Object],
      peer: 'peer0.org2.example.com:9051'
    }
  ],
  errors: []

Last error comes from this line in the network.js network.js:304:40

const earnPointsResponse = await contract.submitTransaction('EarnPoints', JSON.stringify(earnPoints));

Anybody a hint what the problem can be or how i can fix this?

Error while running enrollAdmin.js

I am getting this error when running enrollAdmin.js. Is there an alternative way to enroll??

2020-02-17T07:00:35.557Z - error: [FabricCAClientService.js]: Failed to enroll admin, error:%o message=Enrollment failed with errors [undefined], stack=Error: Enrollment failed with errors [undefined]
at IncomingMessage.response.on (/Users/a667911/Developer/Loyalty/customer-loyalty-program-hyperledger-fabric-VSCode/web-app/node_modules/fabric-ca-client/lib/FabricCAClient.js:470:22)
at emitNone (events.js:110:20)
at IncomingMessage.emit (events.js:207:7)
at endReadableNT (_stream_readable.js:1059:12)
at _combinedTickCallback (internal/process/next_tick.js:138:11)
at process._tickCallback (internal/process/next_tick.js:180:9)
error Error: Enrollment failed with errors [undefined]
Failed to enroll admin user admin: ${error}

Getting Error while adding new Member during registration

Hi Ashutosh,

I followed same steps as followed by you but i am stuck at step of adding new member registration and partner registration. While creating new member for the first time i am getting below error :

fabric-ca request register failed with errors [[{"code":19,"message":"CA 'ca.example.com' does not exist"}]]
fabric-ca request register failed with errors [[{"code":19,"message":"CA 'ca.example.com' does not exist"}]]

Please find attached snapshot for the same.

Thanks

Screenshot from 2019-05-16 11-01-53

Refer below for terminal logs

app running on port: 8000
Using param - firstname: raj lastname: singh email: [email protected] phonenumber: 5555555555 accountNumber: 100000 cardId: ac1
Valid Entries
Wallet path: /home/raaz/hyperledger/customer-loyalty-program-hyperledger-fabric-VSCode/web-app/wallet
Error: fabric-ca request register failed with errors [[{"code":19,"message":"CA 'ca.example.com' does not exist"}]]
at IncomingMessage.response.on (/home/raaz/hyperledger/customer-loyalty-program-hyperledger-fabric-VSCode/web-app/node_modules/fabric-ca-client/lib/FabricCAClient.js:327:22)
at emitNone (events.js:111:20)
at IncomingMessage.emit (events.js:208:7)
at endReadableNT (_stream_readable.js:1064:12)
at _combinedTickCallback (internal/process/next_tick.js:139:11)
at process._tickCallback (internal/process/next_tick.js:181:9)
Using param - name: [email protected] partnerId: part1 cardId: ac1
Company name must be letters only
Using param - name: ibm partnerId: part1 cardId: ac1
Valid Entries
Wallet path: /home/raaz/hyperledger/customer-loyalty-program-hyperledger-fabric-VSCode/web-app/wallet
Error: fabric-ca request register failed with errors [[{"code":19,"message":"CA 'ca.example.com' does not exist"}]]
at IncomingMessage.response.on (/home/raaz/hyperledger/customer-loyalty-program-hyperledger-fabric-VSCode/web-app/node_modules/fabric-ca-client/lib/FabricCAClient.js:327:22)
at emitNone (events.js:111:20)
at IncomingMessage.emit (events.js:208:7)
at endReadableNT (_stream_readable.js:1064:12)
at _combinedTickCallback (internal/process/next_tick.js:139:11)
at process._tickCallback (internal/process/next_tick.js:181:9)

Unable to see options for install and instantiate for smart contract

VS Code version:- Version: 1.60.2
IBM Blockchain Platform version:- 2.0.4

There is no option for install and instantiate for smart contract at IBM Blockchain platform after packaging. Please see the attached screenshot. Instead of install and instantiate option there is a Deploy smart contract option is there. But deployment failed with error :-

Failed to install on peer Org1 Peer with reason: Error: Could not install smart contact received error: failed with status:500 ::failed to invoke backing implementation of 'InstallChaincode': could not build chaincode: external builder failed: external builder failed to build: external builder 'node' failed: exit status 1 [10/1/2021 12:43:58 PM] [ERROR] Failed to deploy smart contract, Error: Package was not installed. No packageId was returned

Screenshot 2021-10-01 at 12 48 13 PM

I failed to find what's wrong with this, any help?

Readme specifies wrong version of VSCode

The current readme specifies VSCode version 1.26 or greater, but there is an actual requirement of VS Code version 1.35.1 or greater for the IBM Blockchain Platform extension.

If someone picks up an older version of VSCode, they won't be able to install the 1.0.x release of IBM Blockchain Platform

Unable to package the Loyalty Contract

When I attempt to package the Loyalty contract into a package I get the following error message "Failed to determine workspace language type, supported languages are JavaScript, etc."

I have the contracts folder open in the file explorer, when I press F1 and select IBM Blockchain Platform: Package Open Project" command.

Appreciate any guidance you can provide.

Toby

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.