1

I am writing a smart contract with the help of hyperledger composer, and I need to access the transaction history of an asset to know it's state whether It's approved by authorities, and when the asset is approved, a transaction is made, now need to access the transaction history for that asset inside the logic.js in my smart contract.

Thanks in advance.

Rohit Khatri
  • 1,980
  • 3
  • 25
  • 45

1 Answers1

1

Here's how you can access the native API and using that you can access Historian records for a particular asset

const id = transaction.assetId;
const nativeSupport = transaction.nativeSupport;

const nativeKey = getNativeAPI().createCompositeKey('Asset:systest.transactions.SimpleStringAsset', [id]);
const iterator = await getNativeAPI().getHistoryForKey(nativeKey);
let results = [];
let res = {done : false};
while (!res.done) {
    res = await iterator.next();

    if (res && res.value && res.value.value) {
        let val = res.value.value.toString('utf8');
        if (val.length > 0) {
            results.push(JSON.parse(val));
        }
    }
    if (res && res.done) {
        try {
            iterator.close();
        }
        catch (err) {
        }
    }
}

For more information, please check out this url: https://hyperledger.github.io/composer/latest/reference/js_scripts

Daisy Saxena
  • 160
  • 5