In this section we will create fungible tokens or IOUs and transfert them between accounts.
Creating tokens
Dealing with token codes
Token codes can be three letters or a padded hex string. Here is a convenience function to enable codes longer than three letters. This needs to be done to work with our training app, so you will want to include this in your code. This could easily be done via an import for instance, or placed in the index.ts file directly.
Here is the function code:
functionconvertStringToHexPadded(str:string):string {// Convert string to hexadecimallet hex:string="";for (let i =0; i <str.length; i++) {consthexChar:string=str.charCodeAt(i).toString(16); hex += hexChar; }// Pad with zeros to ensure it's 40 characters longconstpaddedHex:string=hex.padEnd(40,"0");returnpaddedHex.toUpperCase(); // Typically, hex is handled in uppercase}
Enable rippling
For AMMs and the whole process to work, we need to enable rippling from the issuer account. To enable rippling we use the AccountSet transaction with the appropriate flag. Here is the function.
To create an issued token, the receiver first needs to add a trust line to the issuer. It's straightforward, we create a TrustSet transaction and sign it with the receiver account.
Once the trustline is set, we send an amount from the issuer to the receiver that is less than the trust line maximum (500M tokens in this case).
Here is the whole file for createToken.ts
createToken.ts
import { TrustSet, convertStringToHex, TrustSetFlags } from"xrpl";import { Payment } from"xrpl/src/models";asyncfunctioncreateToken({ issuer, receiver, client, tokenCode }:any) {// Create the trust line to send the tokenconsttrustSet:TrustSet= { TransactionType:"TrustSet", Account:receiver.address, LimitAmount: { currency: tokenCode, issuer:issuer.address, value:"500000000",// 500M tokens }, Flags:TrustSetFlags.tfClearNoRipple, };console.log(trustSet);// Receiver opening trust linesconstpreparedTrust=awaitclient.autofill(trustSet);constsignedTrust=receiver.sign(preparedTrust);constresultTrust=awaitclient.submitAndWait(signedTrust.tx_blob);console.log(resultTrust);console.log("Trust line issuance tx result: ",resultTrust.result.hash);// Send the token to the receiverconstsendPayment:Payment= { TransactionType:"Payment", Account:issuer.address, Destination:receiver.address, Amount: { currency: tokenCode, issuer:issuer.address, value:"200000000",// 200M tokens }, };console.log(sendPayment);constpreparedPayment=awaitclient.autofill(sendPayment);constsignedPayment=issuer.sign(preparedPayment);constresultPayment=awaitclient.submitAndWait(signedPayment.tx_blob);console.log(resultPayment);console.log("Transfer issuance tx result: ",resultPayment.result.hash);return;}exportdefault createToken;
We can now call this function from the main function insideindex.ts, remembering to wrap our token currency code with the convertStringToHexPadded function.