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
#include<stdio.h>#include<stdlib.h>#include<string.h>staticintenvAtoi(constchar* env) {constchar* val =getenv(env);if (val ==NULL) {return0; }returnatoi(val);}intmain(int argc,char*argv[]) {// THIS SAMPLE ONLY SUPPORTS THE "register" FUNCTIONif (argc ==3&&strcmp(argv[1],"register")==0) {// GET THE CURRENT USER'S NAMEconstchar*previousName =getenv("DB_USER_CURRENT");// OR DEFAULT TO "unknown" IF THIS IS THE FIRST CALLif (previousName ==NULL||strlen(previousName)==0) { previousName ="unknown"; }// GET THE TOTAL USER COUNTint totalUserCount =envAtoi("DB_TOTALUSERS");// WRITE PREVIOUS USER NAME TO STDOUTprintf("OUT=prevname: %s\n", previousName);// UPDATE CURRENT USER NAME BY WRITING IT TO DBprintf("DBW=USER_CURRENT=%s\n", argv[2]);// STORE USER NAME UNDER A STORAGE SLOT FOR PERSISTENCE (CURRENT GETS OVERWRITTEN ON EACH CALL)printf("DBW=USER_%d=%s\n", totalUserCount, argv[2]);// INCREMENT THE TOTAL USER COUNTprintf("DBW=TOTALUSERS=%d\n", totalUserCount+1);return0; }if (argc >=2) {fprintf(stderr,"Wrong CMD: %s\n", argv[1]);exit(1); }fprintf(stderr,"Wrong args!\n");exit(1);}
Save the contract
Open a text editor and save above sample contract as main.c file.