01XRPL Basics
Trustline
A trustline on the XRP Ledger is a connection between two accounts that allows one account to hold and transfer a specific issued currency from another account. It establishes a credit limit for the currency, enabling the holder to receive and send the currency up to the specified limit. Trustlines are essential for managing and transferring issued currencies on the XRP Ledger.
Start coding
Create a new file or edit index.ts
import {
AccountSet,
AccountSetAsfFlags,
Client,
Payment,
TrustSet,
} from "xrpl";
import {
convertCurrencyCode,
getExplorerType,
isTxnSuccessful,
} from "./helpers";
const TOKEN = "IRON_MAN";
// Connect the client to the network
const client = new Client("wss://s.altnet.rippletest.net:51233");
const main = async () => {
console.log("lets get started...");
await client.connect();
const { wallet: issuerWallet } = await client.fundWallet();
const { wallet: userWallet } = await client.fundWallet();
// Print the two wallets so you can get the public address starting with "r..."
console.log({ issuerWallet, userWallet });
console.log();
};
main();
Now create a new file helpers.ts
import { TxResponse, convertStringToHex } from "xrpl";
/**
* Helper to convert the token name to the correct hexadecimal.
* https://xrpl.org/currency-formats.html#currency-codes
*
* @param {string} tokenName The token name.
* @returns The token name or the hexadecimal version of it for the XRPL.
*/
export const convertCurrencyCode = (tokenName: string) => {
if (tokenName.length <= 3) return tokenName;
return convertStringToHex(tokenName).toUpperCase().padEnd(40, "0");
};
export const isTxnSuccessful = (result: TxResponse) => {
const { meta } = result.result;
if (typeof meta === "string" || !meta) return false;
return meta.TransactionResult === "tesSUCCESS";
};
export const getExplorerType = (url: string) => {
if (url.includes("altnet") || url.includes("testnet")) {
return "testnet";
}
return "devnet";
};
Running this to ensure everything works should log a few things to the console. If you are running this from your node environment, don't forget to runnpm i xrpl to add it to your project.
Enable rippling
Rippling is a feature in the XRP Ledger that allows issued currencies to flow through multiple accounts without requiring a direct trustline between the sender and receiver. This enables seamless transfers of the same currency across different accounts, provided they all trust the same issuer.
First, we will allow rippling on the issuer account,
console.log(
"Sending the AccountSet to enable Rippling on the issuer account..."
);
const accountSet: AccountSet = {
Account: issuerWallet.address,
TransactionType: "AccountSet",
SetFlag: AccountSetAsfFlags.asfDefaultRipple,
};
console.log(accountSet);
and then submit the AccountSet transaction to the ledger
const accountSetResponse = await client.submitAndWait(accountSet, {
// Add necessary fields such as Fee or LastLedgerSequence
autofill: true,
// Define the wallet which contains the private key to sign the AccountSet transaction
wallet: issuerWallet,
});
console.log(accountSetResponse);
console.log();
// Check the result (optional but recommended), if it is not successful, stop the function
if (!isTxnSuccessful(accountSetResponse)) {
await client.disconnect();
return;
}
Create the trustline
First we will create trustline from the user account to the issuer account. your user account will trust the issuer account and accept to hold and receive "IRON_MAN".
console.log("Sending the TrustSet from the user to the issuer account...");
const trustset: TrustSet = {
Account: userWallet.address,
TransactionType: "TrustSet",
LimitAmount: {
issuer: issuerWallet.address,
// https://xrpl.org/currency-formats.html#currency-codes
// Tokens with more than 3 characters must be encoded
currency: convertCurrencyCode(TOKEN),
// The maximum amount (not in drops) userAccount accepts to hold/receive
value: "1000000", // One million IRON_MAN
},
};
Then we will submit the trustset transaction to the ledger
const trustsetResponse = await client.submitAndWait(trustset, {
// Add necessary fields such as Fee or LastLedgerSequence
autofill: true,
// Define the wallet which contains the private key to sign the Trustset transaction
wallet: userWallet,
});
console.log(trustsetResponse);
console.log();
// Check the result (optional but recommended), if it is not successful, stop the function
if (!isTxnSuccessful(trustsetResponse)) {
await client.disconnect();
return;
}
Send token
We will send 1000 IRON_MAN token from the issuer to the user wallet
const payment: Payment = {
Account: issuerWallet.address,
TransactionType: "Payment",
Destination: userWallet.address,
Amount: {
issuer: issuerWallet.address,
currency: convertCurrencyCode(TOKEN),
value: "1000",
},
};
and then submit the payment transaction to the ledger
const paymentResponse = await client.submitAndWait(payment, {
// Add necessary fields such as Fee or LastLedgerSequence
autofill: true,
// Define the wallet which contains the private key to sign the payment transaction
wallet: issuerWallet,
});
console.log(paymentResponse);
console.log();
console.log(
`Issuer details: https://${getExplorerType(client.url)}.xrpl.org/accounts/${
issuerWallet.address
}`
);
console.log(
`User details: https://${getExplorerType(client.url)}.xrpl.org/accounts/${
userWallet.address
}`
);
await client.disconnect();
At this point, the address receives 1000 IRON_MAN tokens, increasing their balance on the trustline with the issuer. The trustline remains active, with a limit of 1,000,000 IRON_MAN, allowing for further transactions. The issuer's balance decreases by 1000 IRON_MAN, and the transaction is recorded on the XRP Ledger, visible in both accounts' transaction histories.
Extra credits
Freeze the trustline
You can freeze a trust line, you will need to use the tfSetFreeze to do so Learn more here