Skip to content

Commit

Permalink
Create SustainableSupplyChainContract.sol
Browse files Browse the repository at this point in the history
  • Loading branch information
KOSASIH authored Jul 27, 2024
1 parent e5af452 commit 9ebe97e
Showing 1 changed file with 72 additions and 0 deletions.
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;
}
}

0 comments on commit 9ebe97e

Please sign in to comment.