From 9ebe97e60edf40c8a357eea906d67270c905adc5 Mon Sep 17 00:00:00 2001 From: KOSASIH Date: Sat, 27 Jul 2024 14:29:59 +0700 Subject: [PATCH] Create SustainableSupplyChainContract.sol --- .../SustainableSupplyChainContract.sol | 72 +++++++++++++++++++ 1 file changed, 72 insertions(+) create mode 100644 sidra_chain_integration/contracts/SustainableSupplyChainContract.sol diff --git a/sidra_chain_integration/contracts/SustainableSupplyChainContract.sol b/sidra_chain_integration/contracts/SustainableSupplyChainContract.sol new file mode 100644 index 000000000..61756d471 --- /dev/null +++ b/sidra_chain_integration/contracts/SustainableSupplyChainContract.sol @@ -0,0 +1,72 @@ +const { ChaincodeStub } = require('fabric-shim'); +const { Chaincode } = require('fabric-contract-api'); + +class SustainableSupplyChainContract extends Chaincode { + async Init(stub) { + // Initialize the contract with a set of sustainability standards + await stub.putState('sustainabilityStandards', JSON.stringify([ + { name: 'Organic', description: 'Produced without the use of synthetic pesticides or fertilizers' }, + { name: 'Recyclable', description: 'Made from recyclable materials' }, + ])); + } + + async Invoke(stub) { + let ret = stub.getFunctionAndParameters(); + console.log(ret); + let method = this[ret.fcn]; + if (!method) { + console.log('No method of name:' + ret.fcn + ' found'); + throw new Error('Invalid function name'); + } + try { + let payload = await method(stub, ret.params); + return shim.success(payload); + } catch (err) { + console.log(err); + return shim.error(err); + } + } + + async verifySustainability(stub, args) { + // Verify that the product meets the sustainability standards + let product = args[0]; + let sustainabilityStandards = await stub.getState('sustainabilityStandards'); + let standards = JSON.parse(sustainabilityStandards); + + for (let standard of standards) { + if (product[standard.name]) { + // Product meets the sustainability standard + return true; + } + } + return false; + } + + async trackProduct(stub, args) { + // Track the product's origin, production process, and transportation + let product = args[0]; + let origin = args[1]; + let productionProcess = args[2]; + let transportation = args[3]; + + // Update the product's tracking information + product.origin = origin; + product.productionProcess = productionProcess; + product.transportation = transportation; + + // Store the updated product information + await stub.putState('product', JSON.stringify(product)); + } + + async queryProduct(stub, args) { + // Query the product's tracking information + let product = args[0]; + + // Retrieve the product's tracking information + let productInfo = await stub.getState('product'); + let info = JSON.parse(productInfo); + + // Return the product's tracking information + return info; + } +}