Setup: Using the transferAsset function

Before you can run you gotta walk, let's get some initial funds in our test accounts and see the benefits of the transferAsset function, as opposed to using pure trades

This step isn't really required if you are just wanting to see how orders work (your users will hopefully be the ones to bring their own assets!) Sadly my test exchange has no real users so I am going to give my fake ones some assets from the admin account.

Using a similar script to that in the previous tutorial 'Creating a User and Wallet', I have added two users accountA@gmail.com and accountB@gmail.com. These two accounts are what we will be using to create a sell and buy order. Account A will be loaded with 100 XHT and account B with 20 USDT, both receiving them from the Admin account.

Something really cool about transferAsset is that by using it you can avoid fees entirely! It allows transferring assets within your exchange and avoiding network fees.

Transfer Assets Script

Remember to check the comments for an explanation of each aspect of the code.

//using the index file, get access to the functions we will be using throughout
//Note: make sure the index file is pointed in the correct relative folder to this
//script
const Network = require('./index');

const network = new Network({
    apiUrl: 'https://api.hollaex.network',
    apiKey: '<YOUR_API_KEY>',
    apiSecret: '<YOUR_API__SECRET_KEY>',
    activation_code: '<YOUR_ACTIVATION_CODE>',
});

//main function for adding user and their address
(async () => {
	try {
		//initialise the network
		const init = await network.init();

		let userList = await network.getUsers();

		//finding the three accounts IDs we want to use
		let accountB = await network.getUser(userList.data[0].id);
		let accountA = await network.getUser(userList.data[1].id);
		let accountAdmin = await network.getUser(userList.data[2].id);

		//view the details of the three ids we will use
		console.log(accountA);
		console.log(accountB);
		console.log(accountAdmin);

		//transfer assets between accounts on the network (fee free!)
		//first parameter is the sender ID, second is the receiver ID
		//third parameter is what we want to transfer, and fouth is amount
		//so here we are sending 100 XHT from the admin to account A
		await network.transferAsset(accountAdmin.id, accountA.id, 'xht', 100);

		//and here, 100 XHT from admin to account B
		await network.transferAsset(accountAdmin.id, accountB.id, 'usdt', 20);

		//refresh the accounts to show the new balances
		accountB = await network.getUser(userList.data[0].id);
		accountA = await network.getUser(userList.data[1].id);
		accountAdmin = await network.getUser(userList.data[2].id);

		//lets have a look at the new balances of accounts
		console.log(accountA);
		console.log(accountB);
		console.log(accountAdmin);
	} catch (err) {
		console.log(err);
	}
})();

Transfer Assets Script: The Output

So when we run the above Javascript file in the terminal with node <FILE_NAME>, this is what the terminal will return to us (this output has been commented, as well as edited for readibility. Sections removed are marked with '~~'):

//initial state of account A before transfer (line 26 & 31)
{ id: 2275,
  email: 'accounta@gmail.com',
  balance:
   { 
    //~~all balances in account A currently at zero, as freshly made~~
   },

  wallet:
   [ { 
    //~~wallet addresses details (eth & btc)~~
   } ] }
   
//initial state of account B before transfer (line 27 && 32)
{ id: 2276,
  email: 'accountb@gmail.com',
  balance:
   { 
    //~~all balances in account A currently at zero, as freshly made~~ 
  },
  wallet:
   [ { 
    //~~wallet addresses details (eth & btc)~~
    } ] }

//initial state of admin account before transfer, note assets existing within its 
//wallets (line 28 && 33)
{ id: 2235,
  email: 'admin@gmail.com',
  balance:
   { btc_balance: 0.003,
     btc_available: 0.003,
     usdt_balance: 136, //note USDT at 136
     usdt_available: 136,
     xht_balance: 150, //note XHT at 150
     xht_available: 150,
     xrp_balance: 200,
     xrp_available: 200 },


  wallet:
   [ {     
    //~~wallet addresses details (eth & btc)~~
    } ] }

//at this point the transferAsset function fires, sending XHT to account A, 
//and USDT to account B, both from the admin account (line 40 & 43)

//get the new balance of account A (line 47 & 51)
{ id: 2275,
  email: 'accounta@gmail.com',
  balance:
   { btc_balance: 0,
     btc_available: 0,
     usdt_balance: 0,
     usdt_available: 0,
     xht_balance: 100, // +100 XHT here
     xht_available: 100,
     xrp_balance: 0,
     xrp_available: 0 },
  wallet:
   [ { 
    //~~wallet addresses details (eth & btc)~~
     } ] }

//get the new balance of account B (line 46 & 52)
{ id: 2276,
  email: 'accountb@gmail.com',
  balance:
   { btc_balance: 0,
     btc_available: 0,
     usdt_balance: 20, // +20 USDT here
     usdt_available: 20,
     xht_balance: 0,
     xht_available: 0,
     xrp_balance: 0,
     xrp_available: 0 },
  wallet:
   [ {     
    //~~wallet addresses details (eth & btc)~~
     } ] }

//finally get the new balance of the admin (line 48 & 53)
{ id: 2235,
  email: 'hollaexprotutorial+1@gmail.com',
  balance:
   { btc_balance: 0.003,
     btc_available: 0.003,
     usdt_balance: 116, // -20 USDT here
     usdt_available: 116,
     xht_balance: 50, // -100 XHT here
     xht_available: 50,
     xrp_balance: 200,
     xrp_available: 200 },
  wallet:
   [ {     
    //~~wallet addresses details (eth & btc)~~
    } ] }

With Account A and B now having some assets to play around with, let's move on to letting them trade.

Last updated