-
-
Notifications
You must be signed in to change notification settings - Fork 41
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Create SustainableSupplyChainContract.sol
- Loading branch information
Showing
1 changed file
with
72 additions
and
0 deletions.
There are no files selected for viewing
72 changes: 72 additions & 0 deletions
72
sidra_chain_integration/contracts/SustainableSupplyChainContract.sol
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,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; | ||
} | ||
} |