Writing the utilities

All right. Now, let's write the Node.js utilities, to transfer the assets from the issuing account to the accounts we'll use for trading, as follows:

  1. Create two nodejs apps called Transfer1.js and Transfer2.js
  2. Let's write Transfer1. We import the StellarSDK from node_modules, create the server object, and extract the public and private keys for the issuing account and the two receiving accounts from their respective secret keys, as follows:
const StellarSdk = require('stellar-sdk');

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

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

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

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

var USD = new StellarSdk.Asset('USD', 'GBUM3XRJKUVEQA4UF63CUCS3P72C5AZTRYI2VKELS7T7DVCCLV3DODNE');
var GBP = new StellarSdk.Asset('GBP', 'GBUM3XRJKUVEQA4UF63CUCS3P72C5AZTRYI2VKELS7T7DVCCLV3DODNE');
var EUR = new StellarSdk.Asset('EUR', 'GBUM3XRJKUVEQA4UF63CUCS3P72C5AZTRYI2VKELS7T7DVCCLV3DODNE');
  1. Lastly, we create the three asset objects. Now, let's look at our transaction.
  2. Fetch the base fee and the account sequence number for the issuing account, like this:
server.fetchBaseFee()
.then(function(fee){
console.log("Fee is",fee);
server.loadAccount(issuingKeys.publicKey())
.then(function(account){
  1. Our transaction source account this time will be the issuing account, which distributes the newly created asset, as follows:
var transaction = new StellarSdk.TransactionBuilder(account,{ fee,networkPassphrase:'Standalone Network ; February 2017'})
.addOperation(StellarSdk.Operation.payment({
destination: receivingKeys1.publicKey(),
asset: USD,
amount: '1000'
}))

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

  1. We repeat the same for the other two assets as well, like this:
.addOperation(StellarSdk.Operation.payment({
destination: receivingKeys1.publicKey(),
asset: GBP,
amount: '1000'
}))
.addOperation(StellarSdk.Operation.payment({
destination: receivingKeys1.publicKey(),
asset: EUR,
amount: '1000'
})).setTimeout(100)
.build();
  1. The transaction timeout is set at 100 seconds. We call build() to build and return the transaction object.
  2. After the transaction object is returned, we sign and submit the transaction. The transaction is signed using the issuing account's key pair since the source is the issuing account, as follows:
transaction.sign(issuingKeys);
return server.submitTransaction(transaction);
  1. Add a response block for logging any errors and notifying transaction success, like this:
.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, except replace the receiving account with receivingKeys2. Thus, Transfer1.js will transfer the assets to the first receiving account, and Transfer2.js 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.222.148.124