Published 8 July 2019

Examples: JS: Deploy Smart contract

In order to deploy a Smart Contract, wallet’s public and private keys, and public address of transfer destination have to be shared to the class CreditsUtils

// gets integer and fraction parts of the tokens amount for transfer and reward amount (the transfer this amount equals 0.9 by default)

deploySmartContract(feeValue, smCode) {

   var tran = this.createTransactionWithSmartContract(feeValue, smCode);

   if(tran === null)

       return null;

   var tranFlow = this.client().TransactionFlow(tran);

   console.log(tranFlow);

   return tranFlow;

}


// method to create a transaction, form its signature and to sign the transaction

createTransactionWithSmartContract(feeValue, smCode) {

   if(smCode == '')

       smCode =

       "import com.credits.scapi.annotations.*; import com.credits.scapi.v0.*; public class MySmartContract extends SmartContract { public MySmartContract() {} public String hello() { return \"Hello\"; } }";

   var tran = new Transaction();

   let res = this.client().WalletTransactionsCountGet(this.publicKeyByte);

   if (res.status.code === 0) {

       tran.id = res.lastTransactionInnerId + 1;

   }

   else {

       return null;

   }

   tran.source = this.publicKeyByte;

   tran.target = this.targetKeyByte;

   tran.amount = new Amount({integral: 0, fraction: 0});

   let F = this.fee(feeValue);

   let FE = this.numbToBits(F.exp);

   while (FE.length < 5){

       FE = "0" + FE;

   }

   let FM = this.numbToBits(F.man);

   while (FM.length < 10) {

       FM = "0" + FM;

   }

   tran.fee = new AmountCommission({

       commission: this.bitsToNumb("0" + FE + FM)

   });

   tran.currency = 1;

   let target = tran.source;

   target = this.concatTypedArrays(target, this.numbToByte(tran.id, 6));

   let byteCode = this.client().SmartContractCompile(smCode);

   if (byteCode.status.code === 0) {

       for (let i in byteCode.byteCodeObjects) {

           target = this.concatTypedArrays(target, this.convertCharToByte(byteCode.byteCodeObjects[i].byteCode));

       }

   }

   else {

       //ResObj.Message = ByteCode.status.message;

       //return ResObj;

       return byteCode.status.message;

   }

   tran.target = this.blake2s(target);

   let PerStr = this.numbToByte(tran.id, 6);

   PerStr = this.concatTypedArrays(PerStr, tran.source);

   PerStr = this.concatTypedArrays(PerStr, tran.target);

   PerStr = this.concatTypedArrays(PerStr, this.numbToByte(tran.amount.integral, 4));

   PerStr = this.concatTypedArrays(PerStr, this.numbToByte(tran.amount.fraction, 8));

   PerStr = this.concatTypedArrays(PerStr, this.numbToByte(tran.fee.commission, 2));

   PerStr = this.concatTypedArrays(PerStr, new Uint8Array([1]));

   PerStr = this.concatTypedArrays(PerStr, new Uint8Array([1]));

   let UserField = new Uint8Array();

   tran.smartContract = new SmartContractInvocation();

   UserField = this.concatTypedArrays(UserField, new Uint8Array([11, 0, 1]));

   UserField = this.concatTypedArrays(UserField, new Uint8Array(4));

   UserField = this.concatTypedArrays(UserField, new Uint8Array([15, 0, 2, 12]));

   UserField = this.concatTypedArrays(UserField, new Uint8Array(4));

   UserField = this.concatTypedArrays(UserField, new Uint8Array([15, 0, 3, 11, 0, 0, 0, 0]));

   tran.smartContract.forgetNewState = false;

   UserField = this.concatTypedArrays(UserField, new Uint8Array([2, 0, 4, 0]));

   if (smCode !== undefined) {

       UserField = this.concatTypedArrays(UserField, new Uint8Array([12, 0, 5, 11, 0, 1]));

       tran.smartContract.smartContractDeploy = new SmartContractDeploy({

           sourceCode: smCode

       });

       UserField = this.concatTypedArrays(UserField, this.numbToByte(smCode.length, 4).reverse());

       UserField = this.concatTypedArrays(UserField, this.convertCharToByte(smCode));

       UserField = this.concatTypedArrays(UserField, new Uint8Array([15, 0, 2, 12]));

       let ByteCode = this.client().SmartContractCompile(smCode);

       if (ByteCode.status.code === 0) {

           tran.smartContract.smartContractDeploy.byteCodeObjects = [];

           UserField = this.concatTypedArrays(UserField, this.numbToByte(ByteCode.byteCodeObjects.length, 4).reverse());

           for (let i in ByteCode.byteCodeObjects) {

               let val = ByteCode.byteCodeObjects[i];

               UserField = this.concatTypedArrays(UserField, new Uint8Array([11, 0, 1]));

               UserField = this.concatTypedArrays(UserField, this.numbToByte(val.name.length, 4).reverse());

               UserField = this.concatTypedArrays(UserField, this.convertCharToByte(val.name));

               UserField = this.concatTypedArrays(UserField, new Uint8Array([11, 0, 2]));

               UserField = this.concatTypedArrays(UserField, this.numbToByte(val.byteCode.length, 4).reverse());

               UserField = this.concatTypedArrays(UserField, this.convertCharToByte(val.byteCode));

               tran.smartContract.smartContractDeploy.byteCodeObjects.push(new ByteCodeObject({

                   name: val.name,

                   byteCode: val.byteCode

               }));

               UserField = this.concatTypedArrays(UserField, new Uint8Array(1));

           }

       }

       else {

           //ResObj.Message = ByteCode.Status.Message;

           //return ResObj;

           return ByteCode.Status.Message;

       }

       UserField = this.concatTypedArrays(UserField, new Uint8Array([11, 0, 3, 0, 0, 0, 0, 8, 0, 4, 0, 0, 0, 0, 0]));

   }

   UserField = this.concatTypedArrays(UserField, new Uint8Array(1));

   PerStr = this.concatTypedArrays(PerStr, this.numbToByte(UserField.length, 4));

   PerStr = this.concatTypedArrays(PerStr, UserField);

   var ArHex = "0123456789ABCDEF";

   var Hex = "";

   for (let j = 0; j < PerStr.length; j++) {

       Hex += ArHex[Math.floor(PerStr[j] / 16)];

       Hex += ArHex[Math.floor(PerStr[j] % 16)];

   }

   console.log(Hex);

   tran.signature = nacl.sign.detached(PerStr, this.privateKeyByte);

   return tran;

}

Call example

function run()

{

   var pubkey = "your public address";

   var prvkey = "your private address";

   var trgKey = "your target address";

   var url = "http://127.0.0.1:8081";

   var client = new CreditsUtils(pubkey, prvkey, trgKey, url);

   client.deploySmartContract(0.9, '');

}

Votes 0, average rating 0

Contents