fabric-samples/full-stack-asset-transfer-guide/applications/ping-chaincode/src/app.ts
jkneubuh a299e18e26
Moves the Full Stack Asset Transfer Development Guide to fabric-samples (#852)
* Import Full Stack Asset Transfer Guide at commit fb554befdbbeff9e69159b54fce0b811603f29c7

Signed-off-by: Josh Kneubuhl <jkneubuh@us.ibm.com>

* Update the workshop with a new WORKSHOP_PATH under fabric-samples

Signed-off-by: Josh Kneubuhl <jkneubuh@us.ibm.com>

* Update the workshop with a new WORKSHOP_PATH under fabric-samples

Signed-off-by: Josh Kneubuhl <jkneubuh@us.ibm.com>

* missed a .git ignored directory on add

Signed-off-by: Josh Kneubuhl <jkneubuh@us.ibm.com>

* Updates to run the workshop on the Apple M1

Signed-off-by: Josh Kneubuhl <jkneubuh@us.ibm.com>

* Workaround for https://github.com/eslint/eslint/issues/15299 in the contract tslinter

Signed-off-by: Josh Kneubuhl <jkneubuh@us.ibm.com>

* Build an arch-specific CC images on M1

Signed-off-by: Josh Kneubuhl <jkneubuh@us.ibm.com>

* empty commit - force a build

Signed-off-by: Josh Kneubuhl <jkneubuh@us.ibm.com>

* revert an accidental commit that was building the top-level asset-transfer as arm64

Signed-off-by: Josh Kneubuhl <jkneubuh@us.ibm.com>

Signed-off-by: Josh Kneubuhl <jkneubuh@us.ibm.com>
2022-11-10 10:40:27 -05:00

98 lines
3.2 KiB
TypeScript

/*
* Copyright IBM Corp. All Rights Reserved.
*
* SPDX-License-Identifier: Apache-2.0
*/
import { connect, Contract, Identity, Signer, signers } from '@hyperledger/fabric-gateway';
import * as path from 'path';
import { TextDecoder } from 'util';
import { ConnectionHelper } from './fabric-connection-profile';
import JSONIDAdapter from './jsonid-adapter';
import { dump } from 'js-yaml';
import {config} from 'dotenv';
config({path:'app.env'});
import * as env from 'env-var'
const channelName = env.get('CHANNEL_NAME').default('mychannel').asString();
const chaincodeName = env.get('CHAINCODE_NAME').default('conga-nft-contract').asString();
const connectionProfile = env.get('CONN_PROFILE_FILE').required().asString();
const identityFile = env.get('ID_FILE').required().asString()
const identityDir = env.get('ID_DIR').required().asString()
const mspID = env.get('MSPID').required().asString()
const tls = env.get('TLS_ENABLED').default("false").asBool();
const utf8Decoder = new TextDecoder();
async function main(): Promise<void> {
const cp = await ConnectionHelper.loadProfile(connectionProfile);
// The gRPC client connection should be shared by all Gateway connections to this endpoint.
const client = await ConnectionHelper.newGrpcConnection(cp,tls);
console.log("Created GRPC Connection")
const jsonAdapter: JSONIDAdapter = new JSONIDAdapter(path.resolve(identityDir),mspID);
const identity = await jsonAdapter.getIdentity(identityFile);
const signer = await jsonAdapter.getSigner(identityFile);
console.log("Loaded Identity")
const gateway = connect({
client,
identity,
signer,
// Default timeouts for different gRPC calls
evaluateOptions: () => {
return { deadline: Date.now() + 5000 }; // 5 seconds
},
endorseOptions: () => {
return { deadline: Date.now() + 15000 }; // 15 seconds
},
submitOptions: () => {
return { deadline: Date.now() + 5000 }; // 5 seconds
},
commitStatusOptions: () => {
return { deadline: Date.now() + 60000 }; // 1 minute
},
});
try {
// Get a network instance representing the channel where the smart contract is deployed.
const network = gateway.getNetwork(channelName);
// Get the smart contract from the network.
const contract = network.getContract(chaincodeName);
// Return all the current assets on the ledger.
await ping(contract);
} finally {
gateway.close();
client.close();
}
}
main().catch(error => {
console.error('******** FAILED to run the application:', error);
process.exitCode = 1;
});
/**
* Evaluate a transaction to query ledger state.
*/
async function ping(contract: Contract): Promise<void> {
console.log('\n--> Evaluate Transaction: Get Contract Metdata from : org.hyperledger.fabric:GetMetadata');
const resultBytes = await contract.evaluateTransaction('org.hyperledger.fabric:GetMetadata');
const resultJson = utf8Decoder.decode(resultBytes);
const result = JSON.parse(resultJson);
console.log('*** Result:');
console.log(dump(result));
}