Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
if (args.length !== 3) {
return shim.error('Incorrect number of arguments. Expecting 4, function followed by 2 names and 1 value');
}
const A = args[0];
const B = args[1];
try {
const Avalbytes = await stub.getState(A);
if (!Avalbytes) {
return shim.error('Entity A not found');
}
Aval = Avalbytes.toString();
Aval = parseInt(Aval);
} catch (e) {
return shim.error('Failed to get state A');
}
try {
const Bvalbytes = await stub.getState(B);
if (!Bvalbytes) {
return shim.error('Entity B not found');
}
Bval = Bvalbytes.toString();
Bval = parseInt(Bval);
} catch (e) {
return shim.error('Failed to get state B');
}
// Perform the execution
const X = parseInt(args[2]);
if (isNaN(X)) {
return shim.error('Invalid transaction amount, expecting a integer value');
}
try {
const Bvalbytes = await stub.getState(B);
if (!Bvalbytes) {
return shim.error('Entity B not found');
}
Bval = Bvalbytes.toString();
Bval = parseInt(Bval);
} catch (e) {
return shim.error('Failed to get state B');
}
// Perform the execution
const X = parseInt(args[2]);
if (isNaN(X)) {
return shim.error('Invalid transaction amount, expecting a integer value');
}
Aval = Aval - X;
Bval = Bval + X;
logger.info(`Aval = ${Aval}, Bval = ${Bval}`);
// Write the state back to the ledger
try {
await stub.putState(A, Buffer.from(Aval.toString()));
await stub.putState(B, Buffer.from(Bval.toString()));
logger.info(' example_cc0 - move succeed');
return shim.success(Buffer.from('move succeed'));
} catch (e) {
return shim.error(e);
}
}
// Perform the execution
const X = parseInt(args[2]);
if (isNaN(X)) {
return shim.error('Invalid transaction amount, expecting a integer value');
}
Aval = Aval - X;
Bval = Bval + X + 10;
logger.info(`Aval = ${Aval}, Bval = ${Bval}`);
// Write the state back to the ledger
try {
await stub.putState(A, Buffer.from(Aval.toString()));
await stub.putState(B, Buffer.from(Bval.toString()));
return shim.success(Buffer.from('move succeed'));
} catch (e) {
return shim.error(e);
}
}
if (args.length !== 1) {
return shim.error('Incorrect number of arguments. Expecting name of the person to query');
}
const A = args[0];
let Aval;
// Get the state from the ledger
try {
const Avalbytes = await stub.getState(A);
if (!Avalbytes) {
return shim.error('Entity A not found');
}
Aval = Avalbytes.toString();
} catch (e) {
return shim.error('Failed to get state A');
}
const jsonResp = {
Name: A,
Amount: Aval
};
logger.info('Query Response:%s\n', JSON.stringify(jsonResp));
return shim.success(Buffer.from(Aval.toString()));
}
async delete(stub, args) {
if (args.length !== 1) {
return shim.error('Incorrect number of arguments. Expecting name of the marble to delete');
}
const marbleName = args[0];
if (!marbleName) {
return shim.error('marble name must not be empty');
}
// to maintain the color~name index, we need to read the marble first and get its color
const valAsBytes = await stub.getState(marbleName); // get the marble from chaincode state
let jsonResp = {};
if (!valAsBytes) {
jsonResp.error = 'marble does not exist: ' + marbleName;
return shim.error(jsonResp);
}
let marbleJSON = {};
try {
marbleJSON = JSON.parse(valAsBytes.toString());
} catch (err) {
if (isNaN(Bval)) {
return shim.error('Expecting integer value for asset holding');
}
logger.info(`Aval = ${Aval}, Bval = ${Bval}`);
try {
// Write the state to the ledger
await stub.putState(A, Buffer.from(Aval.toString()));
await stub.putState(B, Buffer.from(Bval.toString()));
return shim.success();
} catch (e) {
return shim.error(e);
}
} else {
return shim.error('init expects 4 args');
}
}
console.info(util.format('Args: %j', stub.getArgs()));
let ret = stub.getFunctionAndParameters();
console.info(ret);
let method = this[ret.fcn];
if (!method) {
console.log('no function of name:' + ret.fcn + ' found');
throw new Error('Received unknown function ' + ret.fcn + ' invocation');
}
try {
let payload = await method(stub, ret.params, this);
return shim.success(payload);
} catch (err) {
console.log(err);
return shim.error(err);
}
}
async testTransient(stub) {
let tm;
try {
tm = stub.getTransient();
} catch (e) {
logger.error('Did not find expected transient map in the proposal');
return shim.error(Buffer.from('{"Error":"Did not find expected transient map in the proposal}'));
}
const v = tm.get('test');
if (!v) {
logger.error('Did not find expected key "test" in the transient map of the proposal');
return shim.error(Buffer.from('{"Error":"Did not find expected key "test" in the transient map of the proposal}'));
}
return shim.success(v);
}
async returnError(stub, args) {
return shim.error(new Error(args[0] || 'returnError: chaincode error response'));
}
return tslib_1.__generator(this, function (_a) {
switch (_a.label) {
case 0:
_a.trys.push([0, 2, , 3]);
return [4, _super.prototype.Init.call(this, stub)];
case 1: return [2, _a.sent()];
case 2:
e_1 = _a.sent();
err = new convector_core_1.ChaincodeInitializationError(e_1);
return [2, fabric_shim_1.error(Buffer.from(JSON.stringify(new fabric_chaincode_utils_1.ChaincodeError(err.toString()))))];
case 3: return [2];
}
});
});