Writing the utility

Let's write a Nodejs utility in order to create new assets and extend trustlines to the receiving accounts, as follows:

  1. Create a new nodejs app called CreateTrustline.js.
  2. Start by importing the stellar-sdk from node-modules and creating a new server object pointed at localhost:8000 (Horizon instance), as follows:
const StellarSdk = require('stellar-sdk');
const server = new StellarSdk.Server('http://127.0.0.1:8000', {allowHttp: true});
  1. Next, we'll use the accounts we generated in the previous section. Let the first account be our issuing account, which issues assets. The second and third accounts will be the receiving accounts, which trade the assets on the exchange. Use the secret key of the three accounts to extract their key pair, as follows:
var issuingKeys = StellarSdk.Keypair.fromSecret('SDHH7CAELIMPNZPRFEBJHSFP24B7UEAAJVW2PMR5AZP5OESHH435DZFC');

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

var receivingKeys2 = StellarSdk.Keypair.fromSecret('SDOE7ICIYRSH74VWUTO52T24BDZBYHRYSYAPS5V73Z37WLYQNA6B4PP4');
  1. The issuingKeys variable is the key pair for the issuing account. receivingKeys1 and receivingKeys2 are for the two receiving accounts.
  2. Next, we create the asset object. We create a new asset object for US Dollar and UK Pound, as well as for Euro, like this:
var USD = new StellarSdk.Asset('USD', 'GBUM3XRJKUVEQA4UF63CUCS3P72C5AZTRYI2VKELS7T7DVCCLV3DODNE');
var GBP = new StellarSdk.Asset('GBP', 'GBUM3XRJKUVEQA4UF63CUCS3P72C5AZTRYI2VKELS7T7DVCCLV3DODNE');
var EUR = new StellarSdk.Asset('EUR', 'GBUM3XRJKUVEQA4UF63CUCS3P72C5AZTRYI2VKELS7T7DVCCLV3DODNE');

Remember the public key for your issuing account that we saved when we created it? Paste it here and add a symbol that we'll use to refer the asset. Use StellarSdk.Asset to create a new asset with these details.

  1. Before we can transfer the assets, the receiving accounts need to extend a trustline for these assets. This is a transaction that's fired by the receiving account to itself. It indicates the asset that will be held by the account and the maximum volume that can be held. 

Let's write the transaction that will extend the trustline limit. We start by fetching the base network fee and the sequence number for the account. This is done using the server object we created earlier, as follows:

server.fetchBaseFee()
.then(function(fee){
console.log("Fee is",fee);

server.loadAccount(receivingKeys1.publicKey())
.then(function(account){

Since the transaction is to be fired from receivingKeys1, we fetch the current sequence for this account.

 

  1. Let's take a look at our extend trustline transaction, shown here:
var transaction = new StellarSdk.TransactionBuilder(account, { fee, networkPassphrase:'Standalone Network ; February 2017'}
.addOperation(StellarSdk.Operation.changeTrust({
asset: USD,
limit: '1000000',
source: receivingKeys1.publicKey()
}))
.addOperation(StellarSdk.Operation.changeTrust({
asset: GBP,
limit: '1000000',
source: receivingKeys1.publicKey()
}))
.addOperation(StellarSdk.Operation.changeTrust({
asset: EUR,
limit: '1000000',
source: receivingKeys1.publicKey()
})).setTimeout(100)
.build();

Let's try to understand this transaction. We start by passing the account sequence number, fee, and the network passphrase, like this: 

var transaction = new StellarSdk.TransactionBuilder(account, { fee, networkPassphrase:'Standalone Network ; February 2017'}

Next, we add the ChangeTrust operation, as follows:

.addOperation(StellarSdk.Operation.changeTrust({
asset: USD,
limit: '1000000',
source: receivingKeys1.publicKey()
}))

The operation indicates that we need to extend a trustline for the asset USD, with a limit of 1 million USD. Since the transaction is to "self", the source of the transactions is the public key of receivingKeys1.

We also add the same operation for GBP and EUR, as follows:

.addOperation(StellarSdk.Operation.changeTrust({
asset: GBP,
limit: '1000000',
source: receivingKeys1.publicKey()
}))
.addOperation(StellarSdk.Operation.changeTrust({
asset: EUR,
limit: '1000000',
source: receivingKeys1.publicKey()
}))

Lastly, we add a transaction timeout after 100 seconds and invoke build() to build the transaction object, like this:

.setTimeout(100)
.build();
  1. After building the transactions, we sign it using the public key for receivingKeys1 and submit it to the Stellar network for processing. Add a catch block for catching any errors, as follows:
transaction.sign(receivingKeys1);

return server.submitTransaction(transaction);

})}).catch(function(error) {
console.error('Error!', error);
});
  1. Next, we repeat the same for receivingKeys2—that is, the second trading account we created—as follows:
server.fetchBaseFee()
.then(function(fee){
console.log("Fee is",fee);

server.loadAccount(receivingKeys2.publicKey())
.then(function(account){

var transaction = new StellarSdk.TransactionBuilder(account, { fee, networkPassphrase:'Standalone Network ; February 2017'})
.addOperation(StellarSdk.Operation.changeTrust({
asset: USD,
limit: '1000000',
source: receivingKeys2.publicKey()
}))
.addOperation(StellarSdk.Operation.changeTrust({
asset: GBP,
limit: '1000000',
source: receivingKeys2.publicKey()
}))
.addOperation(StellarSdk.Operation.changeTrust({
asset: EUR,
limit: '1000000',
source: receivingKeys2.publicKey()
})).setTimeout(100)
.build();

transaction.sign(receivingKeys2);

return server.submitTransaction(transaction);

})}).catch(function(error) {
console.error('Error!', error);
});

That brings us to the end of our CreateTrustline utility. Now, let's run this utility.

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

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