Published 8 July 2019

Examples: JS: Send transaction

 

In order to receive or transfer coins to the specified address, it is necessary to transfer the public key of the wallet, the private key of the wallet and the public address of the transfer destination to the client class

takes the whole and fractional part of the number of coins to transfer and the amount of reward, by default, this amount for the transfer is 0.9 CS

class CreditsUtils {

   constructor(publicKey, privateKey, targetKey, url) {

       this.publicKey = publicKey;

       this.privateKey = privateKey;

       this.targetKey = targetKey;

       this.url = url;

       this.publicKeyByte = from_b58(this.publicKey);

       this.privateKeyByte = from_b58(this.privateKey);

       this.targetKeyByte = from_b58(this.targetKey);

   }

   walletGetBalance() {

 var balance = this.client().WalletBalanceGet(this.publicKeyByte).balance;

       return balance;

   }

transferCoins(amountVal, feeValue) {

   var tran = this.createTransaction(amountVal, feeValue);

   if(tran === null)

       return null;

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

   console.log(tranFlow);

   return tranFlow;

}

   client() {

       var transport = new Thrift.Transport(this.url);

       var protocol = new Thrift.Protocol(transport);

       return new APIClient(protocol);

   }

}

Usage

function run()

{

   var pubkey = "your public key";

   var prvkey = "your private key";

   var trgKey = "your target key";

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

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

   var tranFlow = client.transferCoins(1, 0.9);

   if(tranFlow === null) {

       document.getElementById('tr-st-id').innerText = "Error of creating a transaction";

   }

   else {

       document.getElementById('tr-st-id').innerText = tranFlow.status.message;

   }

}

 

Votes 0, average rating 0

Contents