Writing the utilities

We'll have to create two nodejs apps called Transfer1.js and Transfer2.js:

  1. First, we'll write Transfer1. To do this, we need to import the Stellar SDK from the node modules, create the server object, and extract the public and private keys for the issuing account, as well as the two receiving accounts from their respective secret keys, as shown in the following code:
const StellarSdk = require('stellar-sdk');

const server = new StellarSdk.Server('http://127.0.0.1:8000', {allowHttp: true});

var issuingKeys = StellarSdk.Keypair
.fromSecret('SAEEE4UUP3DRYTEFHNFKCVB4ZCQT2W2KPFW7FLE6VLE7QABAAZATFZFD');

var receivingKeys1 = StellarSdk.Keypair
.fromSecret('SDSQ5MJALF7VWDFEFETPGGWJK2UEQ5HU6HJBKMT5M5YDJ3WYKMC5RC3O');

var receivingKeys2 = StellarSdk.Keypair
.fromSecret('SB6HTLWBKVY6KOGKFZE2EKH3ZFSIYHYXJOORGKIOHSMPHBCX4SS4PU6G');

var USD = new StellarSdk.Asset('USD', 'GAIHBCB57M2SDFQYUMANDBHW4YYMD3FJVK2OGHRKKCNF2HBZIRBKRX6E');
  1. Let's look at our transaction. We fetch the base fee and the account sequence number for the issuing account using the following code:
server.fetchBaseFee()
.then(function(fee){
console.log("Fee is",fee);
server.loadAccount(issuingKeys.publicKey())
.then(function(account){
  1. Our transaction source account will be the issuing account that distributes the newly created asset:
var transaction = new StellarSdk.TransactionBuilder(account,{ fee,networkPassphrase:'Standalone Network ; February 2017'})
.addOperation(StellarSdk.Operation.payment({
destination: receivingKeys1.publicKey(),
asset: USD,
amount: '1000'
})).setTimeout(100)
.build();

Asset transfer is a payment operation in Stellar. We need to provide the destination Stellar account (public key of receivingKeys1), the asset to be transferred (USD), and the amount to be transferred (1,000).

The transaction time is set to 100 seconds. We call build() to build and return the transaction object.

  1. After the transaction object has been returned, we sign and submit the transaction. As shown in the following code, the transaction is signed using the issuing account's key pair since the source is the issuing account:
transaction.sign(issuingKeys);
return server.submitTransaction(transaction);
  1. As shown in the following code, add a response block to log any errors and notify transaction success to the requestor by printing a message on the console:
.then(function(response,error){
if (response)
{
console.log("Response",response);
}
else
{
console.log("Error",error);
}})
});

That brings us to the end of Transfer1. Repeat the same steps for Transfer2, but replace the receiving account with receivingKeys2. Now, Transfer1.js will transfer the assets to the first receiving account, while Transfer2 will transfer the assets to the second.

Now, let's run these utilities.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.191.223.123