Last active
July 9, 2023 11:40
-
-
Save horeaporutiu/a75704ceb85b2be032d81e1498fd0905 to your computer and use it in GitHub Desktop.
a Hyperleger Smart Contract that is able to retrieve the history of updates for a certain key
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
'use strict'; | |
const { Contract } = require('fabric-contract-api'); | |
class MyContract extends Contract { | |
//add a member along with their email, name, address, and number | |
async addMember(ctx, email, name, address, phoneNumber) { | |
let member = { | |
name: name, | |
address: address, | |
number: phoneNumber, | |
email: email | |
}; | |
await ctx.stub.putState(email, Buffer.from(JSON.stringify(member))); | |
return JSON.stringify(member); | |
} | |
// look up data by key | |
async query(ctx, key) { | |
console.info('querying for key: ' + key); | |
let returnAsBytes = await ctx.stub.getState(key); | |
let result = JSON.parse(returnAsBytes); | |
return JSON.stringify(result); | |
} | |
async retrieveHistory(ctx, key) { | |
console.info('getting history for key: ' + key); | |
let iterator = await ctx.stub.getHistoryForKey(key); | |
let result = []; | |
let res = await iterator.next(); | |
while (!res.done) { | |
if (res.value) { | |
console.info(`found state update with value: ${res.value.value.toString('utf8')}`); | |
const obj = JSON.parse(res.value.value.toString('utf8')); | |
result.push(obj); | |
} | |
res = await iterator.next(); | |
} | |
await iterator.close(); | |
return result; | |
} | |
} | |
module.exports = MyContract; |
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
{ | |
"name": "jan22Test", | |
"version": "0.0.9", | |
"description": "My Smart Contract", | |
"main": "index.js", | |
"engines": { | |
"node": ">=8", | |
"npm": ">=5" | |
}, | |
"scripts": { | |
"lint": "eslint .", | |
"pretest": "npm run lint", | |
"test": "nyc mocha --recursive", | |
"start": "fabric-chaincode-node start" | |
}, | |
"engineStrict": true, | |
"author": "John Doe", | |
"license": "Apache-2.0", | |
"dependencies": { | |
"fabric-contract-api": "1.4.0", | |
"fabric-shim": "1.4.0" | |
}, | |
"devDependencies": { | |
"chai": "^4.2.0", | |
"eslint": "^5.9.0", | |
"mocha": "^5.2.0", | |
"nyc": "^13.1.0", | |
"sinon": "^7.1.1", | |
"sinon-chai": "^3.3.0" | |
}, | |
"nyc": { | |
"exclude": [ | |
"coverage/**", | |
"test/**" | |
], | |
"reporter": [ | |
"text-summary", | |
"html" | |
], | |
"all": true, | |
"check-coverage": true, | |
"statements": 100, | |
"branches": 100, | |
"functions": 100, | |
"lines": 100 | |
} | |
} |
Hi, using ctx.stub.getHistoryForKey(ctx, key) i am not being able to get transaction id. On changing owner it gives output as :
[
{
"color":"black",
"make":"Tesla",
"model":"S",
"owner":"Adriana",
"docType":"car"
},
{
"color":"black",
"docType":"car",
"make":"Tesla",
"model":"S",
"owner":"Sanjiv Ranjit"
}
]
How shall i retrieve transaction id as well?
Regards,
Sanjiv Ranjit
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Oh. sorry. I think history includes the latest and old deals. But I was wrong. History is just old deals. There is no latest.
This is code worked good. thank you @horeaporutiu