Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
Aval = parseInt(args[1]);
if (isNaN(Aval)) {
return shim.error('Expecting integer value for asset holding');
}
Bval = parseInt(args[3]);
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');
}
}
}
// 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);
}
}
async Init(stub: ChaincodeStub): Promise {
const logger: LoggerInstance = shim.newLogger('init');
return shim.success();
}
async Invoke(stub) {
console.info('Transaction ID: ' + stub.getTxID());
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 method of name:' + ret.fcn + ' found');
return shim.success();
}
try {
let payload = await method(stub, ret.params);
return shim.success(payload);
} catch (err) {
console.log(err);
return shim.error(err);
}
}
async queryMarbles(stub, args, thisObject) {
if (args.length !== 1) {
return shim.error('Incorrect number of arguments. Expecting queryString');
}
const queryString = args[0];
if (!queryString) {
return shim.error('queryString must not be empty');
}
const queryResults = await thisObject.getQueryResultForQueryString(stub, queryString, thisObject);
return shim.success(queryResults);
}
async echo(stub, args) {
stub.setEvent('echo', Buffer.from('content'));
if (args.length > 0) {
return shim.success(Buffer.from(args[0]));
} else {
return shim.success();
}
}
};
return shim.error(Buffer.from(err.message));
}
if (fcn === 'ThrowErrorShim') {
const err: Error = new Error('Had a problem');
return Shim.error(Buffer.from(err.message));
}
if (fcn === 'SuccessShim') {
return Shim.success();
}
await this.testAll(stub);
if (fcn === 'nopayload') {
return shim.success();
}
if (fcn === 'myReturnCode') {
let rc: number;
rc = ChaincodeStub.RESPONSE_CODE.OK;
rc = shim.RESPONSE_CODE.OK;
rc = ChaincodeStub.RESPONSE_CODE.ERRORTHRESHOLD;
rc = shim.RESPONSE_CODE.ERRORTHRESHOLD;
rc = ChaincodeStub.RESPONSE_CODE.ERROR;
rc = shim.RESPONSE_CODE.ERROR;
rc++;
}
return shim.success(Buffer.from('all good'));
}
try {
await this.container.initLogging(stub);
LOG.entry(method, stub);
let {fcn: fcn, params: params} = stub.getFunctionAndParameters();
let engine = this._createEngine();
let nodeContext = this._createContext(engine, stub);
let payload = await engine.invoke(nodeContext, fcn, params);
if (payload !== null && payload !== undefined) {
LOG.exit(method, payload);
LOG.debug('@PERF ' + method, 'Total (ms) duration for txnID [' + stub.getTxID() + ']: ' + (Date.now() - t0).toFixed(2));
return shim.success(Buffer.from(JSON.stringify(payload)));
}
LOG.exit(method);
LOG.debug('@PERF ' + method, 'Total (ms) duration for txnID [' + stub.getTxID() + ']: ' + (Date.now() - t0).toFixed(2));
return shim.success();
}
catch(err) {
LOG.error(method, err);
LOG.debug('@PERF ' + method, 'Total (ms) duration for txnID [' + stub.getTxID() + ']: ' + (Date.now() - t0).toFixed(2));
return shim.error(err);
}
}
}
async Invoke(stub) {
console.info('Transaction ID: ' + stub.getTxID());
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);
return shim.success(payload);
} catch (err) {
console.log(err);
return shim.error(err);
}
}
async Invoke(stub) {
const method = 'Invoke';
const t0 = Date.now();
try {
await this.container.initLogging(stub);
LOG.entry(method, stub);
let {fcn: fcn, params: params} = stub.getFunctionAndParameters();
let engine = this._createEngine();
let nodeContext = this._createContext(engine, stub);
let payload = await engine.invoke(nodeContext, fcn, params);
if (payload !== null && payload !== undefined) {
LOG.exit(method, payload);
LOG.debug('@PERF ' + method, 'Total (ms) duration for txnID [' + stub.getTxID() + ']: ' + (Date.now() - t0).toFixed(2));
return shim.success(Buffer.from(JSON.stringify(payload)));
}
LOG.exit(method);
LOG.debug('@PERF ' + method, 'Total (ms) duration for txnID [' + stub.getTxID() + ']: ' + (Date.now() - t0).toFixed(2));
return shim.success();
}
catch(err) {
LOG.error(method, err);
LOG.debug('@PERF ' + method, 'Total (ms) duration for txnID [' + stub.getTxID() + ']: ' + (Date.now() - t0).toFixed(2));
return shim.error(err);
}
}
}