The sample contract serves as a demonstration of general QVM logic.
It is capable of:
registering users into the database
storing the last registered user's name in the database
echoing the previous user's name to STDOUT
incrementing the total registered user count
storing above counter in the database
Sample code
functionmain(args) {// THIS SAMPLE ONLY SUPPORTS THE "register" FUNCTIONif (args &&args.length===2&& args[0] ==="register") {// GET THE CURRENT USER'S NAME OR DEFAULT TO "unknown" IF THIS IS THE FIRST CALLconstprevious_name=process.env.DB_USER_CURRENT||"unknown";// GET THE TOTAL USER COUNTconsttotal_user_count=parseInt(process.env.DB_TOTALUSERS||"0");// WRITE PREVIOUS USER NAME TO STDOUTprocess.stdout.write("OUT=prevname: "+ previous_name);process.stdout.write("\n");// UPDATE CURRENT USER NAME BY WRITING IT TO DBprocess.stdout.write("DBW=USER_CURRENT="+ args[1]);process.stdout.write("\n");// STORE USER NAME UNDER A STORAGE SLOT FOR PERSISTENCE (CURRENT GETS OVERWRITTEN ON EACH CALL)process.stdout.write("DBW=USER_"+ total_user_count +"="+ args[1]);process.stdout.write("\n");// INCREMENT THE TOTAL USER COUNTprocess.stdout.write("DBW=TOTALUSERS="+ (total_user_count +1));process.stdout.write("\n");// EXECUTION SUCCESSFULprocess.exit(0); }// EXECUTION FAILED, WRONG COMMANDif (args.length>=1) {process.stderr.write("Wrong CMD: "+ args[0]);process.stderr.write("\n");process.exit(1); }// EXECUTION FAILED, WRONG ARGSprocess.stderr.write("Wrong args!");process.stderr.write("\n");process.exit(1);}main(process.argv.slice(2));
Save the contract
Open a text editor and save above sample contract as main.js file.