auction-bid.js注释

/*
 * 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 bid(ccp,wallet,user,orgMSP,auctionID,price) {
    try {
        

        //(1)创建一个新的网关并连接到我们的对等(peer)节点
        const gateway = new Gateway();
        //connect using Discovery enabled
         
        await gateway.connect(ccp,
            { wallet: wallet, identity: user, discovery: { enabled: true, asLocalhost: true } });

       //(2)之前使用createChannel命令生成了默认的mychannel通道,并在上面部署了链码,因此我们需要先连接到mychannel的通道上
        const network = await gateway.getNetwork(myChannel);
        //连接到我们之前部署到mychannel通道上的auction合约
        const contract = network.getContract(myChaincodeName);

        console.log('\n--> Evaluate Transaction: get your client ID');
        //查询语句
        let bidder = await contract.evaluateTransaction('GetSubmittingClientIdentity');
        console.log('*** Result:  Bidder ID is ' + bidder.toString());

        //定义一个biddata数据 包含私人信息
        let bidData = { objectType: 'bid', price: parseInt(price), org: orgMSP, bidder: bidder.toString()};
       //给合约函数起别名
        let statefulTxn = contract.createTransaction('Bid');
        statefulTxn.setEndorsingOrganizations(orgMSP);//设置背书节点?
        let tmapData = Buffer.from(JSON.stringify(bidData));
        statefulTxn.setTransient({
            bid: tmapData
        });

        //定义交易事件的id 
        let bidID = statefulTxn.getTransactionId();
        
        //调用合约函数
        console.log('\n--> Submit Transaction: Create the bid that is stored in your organization\'s private data collection');
        await statefulTxn.submit(auctionID);
        console.log('*** Result: committed');
        console.log('*** Result ***SAVE THIS VALUE*** BidID: ' + bidID.toString());

        console.log('\n--> Evaluate Transaction: read the bid that was just created');
        //
        let result = await contract.evaluateTransaction('QueryBid',auctionID,bidID);
        console.log('*** Result:  Bid: ' + prettyJSONString(result.toString()));

        gateway.disconnect();
    } catch (error) {
        console.error(`******** FAILED to submit bid: ${error}`);
        if (error.stack) {
            console.error(error.stack);
        }
        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 bid.js org userID auctionID price');
            process.exit(1);
        }

        const org = process.argv[2];
        const user = process.argv[3];
        const auctionID = process.argv[4];
        const price = process.argv[5];

        if (org === 'Org1' || org === 'org1') {

            const orgMSP = 'Org1MSP';
            const ccp = buildCCPOrg1();
            const walletPath = path.join(__dirname, 'wallet/org1');
            const wallet = await buildWallet(Wallets, walletPath);
            await bid(ccp,wallet,user,orgMSP,auctionID,price);
        }
        else if (org === 'Org2' || org === 'org2') {

            const orgMSP = 'Org2MSP';
            const ccp = buildCCPOrg2();
            const walletPath = path.join(__dirname, 'wallet/org2');
            const wallet = await buildWallet(Wallets, walletPath);
            await bid(ccp,wallet,user,orgMSP,auctionID,price);
        }  else {
            console.log('Usage: node bid.js org userID auctionID price');
            console.log('Org must be Org1 or Org2');
        }
    } catch (error) {
        console.error(`******** FAILED to run the application: ${error}`);
        process.exit(1);
    }
}

main();

(1)(2)转载使用nodejs的SDK执行更新操作 - 小韦云

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值