Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
// This is a node-js version of example_02.go
const shim = require('fabric-shim');
// An log4js logger instance
const logger = shim.newLogger('example_cc1');
// The logger level can also be set by environment variable 'CORE_CHAINCODE_LOGGING_SHIM'
// to CRITICAL, ERROR, WARNING, DEBUG
logger.level = 'info';
const Chaincode = class {
async Init(stub) {
logger.info('########### example_cc1 Init ###########');
// test the transient map support with chaincode instantiation
return this.testTransient(stub);
}
async Invoke(stub) {
logger.info('########### example_cc1 Invoke ###########');
const ret = stub.getFunctionAndParameters();
const fcn = ret.fcn;
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
// This is a node-js version of example_02.go
const shim = require('fabric-shim');
// An log4js logger instance
const logger = shim.newLogger('example_cc0');
// The logger level can also be set by environment variable 'CORE_CHAINCODE_LOGGING_SHIM'
// to CRITICAL, ERROR, WARNING, DEBUG
logger.level = 'info';
const Chaincode = class {
async Init(stub) {
logger.info('########### example_cc0 Init ###########');
const ret = stub.getFunctionAndParameters();
let A, B; // Entities
let Aval, Bval; // Asset holdings
const args = ret.params;
if (args.length === 4) {
A = args[0];
B = args[2];
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');
}
}
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);
}
}