Jump to content

How to retrieve the ESDT token identifier when issuing token through erdjs sdk


Ludo
 Share

Recommended Posts

Hi here,
 
I wonder how I can retrieve the ESDT token identifier automatically through the erdjs sdk.
What I mean is that I am issuing some ESDT token using something like :
 

 let tx = new Transaction({
	receiver: new Address("erd1qqqqqqqqqqqqqqqpqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqzllls8a5w6u"),
	gasLimit: new GasLimit(60000000),
	value: Balance.egld(0.05),
	data: new TransactionPayload(
	  "issue" +
	  "@4d534c546f6b656e73" +  // "MSLTokens" hex encoded
	  "@4d534c" +              // "MSL" hex encoded
	  "@0f41dc" +              // 999900 hex encoded (9999*10^2)
	  "@02" // 2 decimals
	),
  });

  tx.setNonce(account.getNonceThenIncrement());
  await signer.sign(tx);
  const transactionHash = await tx.send(network);
  await tx.awaitExecuted(network);

Once the transaction is done, I am trying extract the token identifier that matches that transaction.
I have understand how to retrieve it manually (for example in that transaction, the token identifier is `MSL-b55cd2`, we can found it (encoded in hex) in the data of the linked smartcontract).
But I have failed to retrieve it through the erdjs sdk. I have tried to retrieve the TransactionOnNetwork from the transaction hash, but I haven't found the token identifier inside it.
If some one knows how to do it, it will be a big help !
 
By the way, if you have a good way to encode the TransactionPayload for the esdt token issue transaction, I am also interested !
 
Have a good day !

Link to comment
Share on other sites

Hello !
Of course, this can be automated. You have to also get the results of the transaction. Speaking of API endpoint, this is the URL you're interested in:

https://devnet-gateway.elrond.com/transaction/5c8a46bedf88048768da6de865b296e74604c4badc88ea5e1a8295e7c5e20849?withResults=true

 

In erdjs, you can use ProxyProvider -> getTransaction() method that also receives a withResults boolean param, that has to be set to true.

 

  • Thanks 2
Link to comment
Share on other sites

Thanks a lot !
For those who may want to do the same thing, the following code seems to work (not very robust through, adding some error handling won't hurt) :

export async function getTokenIdentifier(issueTxHash: TransactionHash, network: ProxyProvider): Promise<string> {
  const issueTxOnNework = await network.getTransaction(issueTxHash, undefined, true);
  return issueTxOnNework.getSmartContractResults().getResultingCalls()[0].data.split('@')[1];
}

 

  • Like 1
Link to comment
Share on other sites

To complete, the first times we retrieve the transaction from the network, the resulting calls may not be complete and the token identifier not present (if we retrieve the transaction status just after the transaction `awaitExecuted` call).

Here is some code to handle that

const snooze = ms => new Promise(resolve => setTimeout(resolve, ms));

export async function getTokenIdentifier(issueTxHash: TransactionHash, network: ProxyProvider, nbRetry = 10): Promise<string> {
  const issueTxOnNework = await network.getTransaction(issueTxHash, undefined, true);
  const resultingCalls = issueTxOnNework.getSmartContractResults().getResultingCalls();
  const transferContractResult = resultingCalls.find(
    c => c.data.startsWith('ESDTTransfer@')
  );
  if (!transferContractResult) {
    if (nbRetry <= 0) {
      console.log('token identifier not found', resultingCalls);
      throw new Error('token identifier not found');
    }
    console.log('getTokenIdentifier retry');
    await snooze(1000);
    return getTokenIdentifier(issueTxHash, network, nbRetry-1);
  }
  return transferContractResult.data.split('@')[1];
}

 

Edited by Ludo
Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

 Share

×
×
  • Create New...