fabric-samples/auction-dutch/application-javascript/revealBid.js
nikhil550 1cd71fd26a
Add dutch auction sample with auditor (#426)
Signed-off-by: NIKHIL E GUPTA <ngupta@symbridge.com>
2021-05-24 22:01:54 +02:00

100 lines
3.5 KiB
JavaScript

/*
* Copyright IBM Corp. All Rights Reserved.
*
* SPDX-License-Identifier: Apache-2.0
*/
'use strict';
const { Gateway, Wallets } = require('fabric-network');
const path = require('path');
const { buildCCPOrg1, buildCCPOrg2, buildWallet, prettyJSONString } = require('../../test-application/javascript/AppUtil.js');
const myChannel = 'mychannel';
const myChaincodeName = 'auction';
async function addBid (ccp, wallet, user, auctionID, bidID) {
try {
const gateway = new Gateway();
// connect using Discovery enabled
await gateway.connect(ccp,
{ wallet: wallet, identity: user, discovery: { enabled: true, asLocalhost: true } });
const network = await gateway.getNetwork(myChannel);
const contract = network.getContract(myChaincodeName);
console.log('\n--> Evaluate Transaction: read your bid');
const bidString = await contract.evaluateTransaction('QueryBid', auctionID, bidID);
const bidJSON = JSON.parse(bidString);
// console.log('\n--> Evaluate Transaction: query the auction you want to join');
const auctionString = await contract.evaluateTransaction('QueryAuction', auctionID);
// console.log('*** Result: Bid: ' + prettyJSONString(auctionString.toString()));
const auctionJSON = JSON.parse(auctionString);
const bidData = { objectType: 'bid', quantity: parseInt(bidJSON.quantity), price: parseInt(bidJSON.price), org: bidJSON.org, buyer: bidJSON.buyer };
console.log('*** Result: Bid: ' + JSON.stringify(bidData, null, 2));
const statefulTxn = contract.createTransaction('RevealBid');
const tmapData = Buffer.from(JSON.stringify(bidData));
statefulTxn.setTransient({
bid: tmapData
});
if (auctionJSON.organizations.length === 2) {
statefulTxn.setEndorsingOrganizations(auctionJSON.organizations[0], auctionJSON.organizations[1]);
} else {
statefulTxn.setEndorsingOrganizations(auctionJSON.organizations[0]);
}
await statefulTxn.submit(auctionID, bidID);
console.log('\n--> Evaluate Transaction: query the auction to see that our bid was added');
const result = await contract.evaluateTransaction('QueryAuction', auctionID);
console.log('*** Result: Auction: ' + prettyJSONString(result.toString()));
gateway.disconnect();
} catch (error) {
console.error(`******** FAILED to submit bid: ${error}`);
process.exit(1);
}
}
async function main () {
try {
if (process.argv[2] === undefined || process.argv[3] === undefined ||
process.argv[4] === undefined || process.argv[5] === undefined) {
console.log('Usage: node revealBid.js org userID auctionID bidID');
process.exit(1);
}
const org = process.argv[2];
const user = process.argv[3];
const auctionID = process.argv[4];
const bidID = process.argv[5];
if (org === 'Org1' || org === 'org1') {
const ccp = buildCCPOrg1();
const walletPath = path.join(__dirname, 'wallet/org1');
const wallet = await buildWallet(Wallets, walletPath);
await addBid(ccp, wallet, user, auctionID, bidID);
} else if (org === 'Org2' || org === 'org2') {
const ccp = buildCCPOrg2();
const walletPath = path.join(__dirname, 'wallet/org2');
const wallet = await buildWallet(Wallets, walletPath);
await addBid(ccp, wallet, user, auctionID, bidID);
} else {
console.log('Usage: node revealBid.js org userID auctionID bidID');
console.log('Org must be Org1 or Org2');
}
} catch (error) {
console.error(`******** FAILED to run the application: ${error}`);
if (error.stack) {
console.error(error.stack);
}
process.exit(1);
}
}
main();