This sample will show you how to prompt for a token transfer. We will first generate a request for the end user. Once validated, you will get notified about transaction's status.
Prerequisites:
Step 1: Import the following modules
Let's continue building on the index.js from the previous example (Environment Set-up) and add the following modules:
In order to get the fees from our service, you need to proceed as below
constxactFees=awaitclient.getXactFeesTransfer();
Step 3: Initializing Token Transfer
When initializing a token transfer, a request will be sent to the end user
/* Update the fields with your informations */constfromAccountId='';consttoAccountId='';consttokenToTransfer='';awaitclient.transfer({fromAccountId, toAccountId, tokenId: tokenToTransfer});
Step 4: Listen for the Token Transfer
Finally get notified when a new token association has been done successfully !
/* Subscribe to new Token Transfer */client.transferValidation().subscribe(token => {console.log('Transfer Token', token);});
Code Check ✅
Your index.js file should look like this:
index.js
const { Client } =require("@xact-wallet-sdk/client");require("dotenv").config();asyncfunctionassociate() {//Grab your api key from your .env fileconstapiKey=process.env.API_KEY;// If we weren't able to grab it, we should throw a new errorif (apiKey ==null) {thrownewError("Environment variables API_KEY must be present"); }/* Create a new instance of Client */constclient=newClient({apiKey});/* Init the connexion */awaitclient.initConnexion();/* Update the fields with your informations */constfromAccountId='';consttoAccountId='';consttokenToTransfer='';/* Request for transfer */awaitclient.transfer({fromAccountId, toAccountId, tokenId});/* Listen for transaction success */client.transferValidation().subscribe(token => {console.log('New transfer token', token); });}associate();