-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #23 from urgetolearn/main
added connection-profile to invoke the chaincode
- Loading branch information
Showing
5 changed files
with
134 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
{ | ||
"name": "fabric-network", | ||
"version": "1.0.0", | ||
"client": { | ||
"organization": "Org1", | ||
"connection": { | ||
"timeout": { | ||
"peer": "300" | ||
} | ||
} | ||
}, | ||
"channels": { | ||
"mychannel": { | ||
"orderers": [ | ||
{ | ||
"url": "grpcs://localhost:7050" | ||
} | ||
], | ||
"peers": { | ||
"peer0.org1.example.com": { | ||
"url": "grpcs://localhost:7051" | ||
} | ||
} | ||
} | ||
}, | ||
"organizations": { | ||
"Org1": { | ||
"mspid": "Org1MSP", | ||
"peerNames": ["peer0.org1.example.com"], | ||
"certificateAuthorities": ["ca.org1.example.com"] | ||
} | ||
}, | ||
"orderers": [ | ||
{ | ||
"url": "grpcs://localhost:7050" | ||
} | ||
], | ||
"peers": { | ||
"peer0.org1.example.com": { | ||
"url": "grpcs://localhost:7051" | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
const { Gateway, Wallets } = require('fabric-network'); | ||
const FabricCAServices = require('fabric-ca-client'); | ||
const path = require('path'); | ||
const fs = require('fs'); | ||
const { log } = require('console'); | ||
|
||
async function invokeChaincode(functionName, args, context) { | ||
try { | ||
|
||
const ccpPath = path.resolve(__dirname, 'connection-profile.json'); | ||
const ccp = JSON.parse(fs.readFileSync(ccpPath, 'utf8')); | ||
const wallet = await Wallets.newFileSystemWallet(path.join(process.cwd(), 'wallet')); | ||
const gateway = new Gateway(); | ||
await gateway.connect(ccp, { | ||
wallet, | ||
identity: 'admin', | ||
discovery: { enabled: true, asLocalhost: true } | ||
}); | ||
|
||
|
||
const network = await gateway.getNetwork('mychannel'); | ||
const contract = network.getContract('mychaincode'); | ||
|
||
const result = await contract.submitTransaction('CreateAsset', 'asset1', '100'); | ||
console.log(`Chaincode invoked successfully. Result: ${result.toString()}`); | ||
|
||
|
||
await gateway.disconnect(); | ||
|
||
return result.toString(); | ||
} catch (error) { | ||
console.error(`Failed to invoke chaincode: ${error.message}`); | ||
throw new Error(`Failed to invoke chaincode: ${error.message}`); | ||
} | ||
} | ||
|
||
|
||
invokeChaincode(); |