Manage Trust | Stellar Docs (2024)

For an account to hold and trade assets other than XLM, it must establish a trustline with the issuing account of that particular asset. Each trustline increases the account’s base reserve by 0.5 XLM, which means the account will have to hold more XLM in its minimum balance.

User experience

First, we’ll have the user create a trustline for an asset by navigating to the Assets page, selecting an asset, and clicking the “Add Asset” button.

info

An asset is displayed as an asset code and issuer address. Learn more in our Assets section.

Manage Trust | Stellar Docs (1)

This triggers a modal form for the user to confirm the transaction with their pincode. Once confirmed, a transaction containing the changeTrust operation is signed and submitted to the network, and a trustline is established between the user's account and the issuing account for the asset.

The changeTrust operation can also be used to modify or remove trustlines.

info

Every transaction must contain a sequence number that is used to identify and verify the order of transactions with the account. A transaction’s sequence number must always increase by one. In BasicPay, fetching and incrementing the sequence number is handled automatically by the transaction builder.

Trustlines hold the balances for all of their associated assets (except XLM, which are held at the account level), and you can display the user’s various balances in your application.

Manage Trust | Stellar Docs (2)

See it in action here: https://basicpay.pages.dev/dashboard/assets

Code implementation

The trustlines an account holds will be necessary to view in several parts of the BasicPay application. First, we'll discuss how we manage different trustlines for the account.

The /dashboard/assets page

The /dashboard/assets page allows the user to manage the Stellar assets their account carries trustlines to. On this page, they can select from several pre-suggested or highly ranked assets, or they could specify their own asset to trust using an asset code and issuer public key. They can also remove trustlines that already exist on their account.

The layout of the page is quite similar to our contacts page. It has a table displaying the existing trustlines and a section where you can add new ones. The key difference is that the contacts store is held in the browser's localStorage, whereas an account's balances are held on the blockchain. So, we will be querying the network to get that information. For more information about how we query this information from the Stellar network, check out the fetchAccountBalances() function in this querying data section.

/src/routes/dashboard/assets/+page.svelte

<script>
// `export let data` allows us to pull in any parent load data for use here.
/** @type {import('./$types').PageData} */
export let data;

// This is where our _reactive_ array of balances is declared. The query
// actually takes place in `/src/routes/dashboard/+layout.js`, and is
// inherited here.
$: balances = data.balances ?? [];

// We import things from external packages that will be needed
import { Trash2Icon } from "svelte-feather-icons";

// We import any Svelte components we will need
import ConfirmationModal from "$lib/components/ConfirmationModal.svelte";
import TruncatedKey from "$lib/components/TruncatedKey.svelte";

// We import any stores we will need to read and/or write
import { walletStore } from "$lib/stores/walletStore";
import { invalidateAll } from "$app/navigation";

// We import some of our `$lib` functions
import { submit } from "$lib/stellar/horizonQueries";
import { createChangeTrustTransaction } from "$lib/stellar/transactions";
import { fetchAssets } from "$lib/utils/stellarExpert";

// The `open` Svelte context is used to open the confirmation modal
import { getContext } from "svelte";
const { open } = getContext("simple-modal");

// Define some component variables that will be used throughout the page
let addAsset = "";
let customAssetCode = "";
let customAssetIssuer = "";
let changeTrustXDR = "";
let changeTrustNetwork = "";
$: asset =
addAsset !== "custom"
? addAsset
: `${customAssetCode}:${customAssetIssuer}`;

// Takes an action after the pincode has been confirmed by the user.
const onConfirm = async (pincode) => {
// Use the walletStore to sign the transaction
let signedTransaction = await walletStore.sign({
transactionXDR: changeTrustXDR,
network: changeTrustNetwork,
pincode: pincode,
});
// Submit the transaction to the Stellar network
await submit(signedTransaction);
// `invalidateAll` will tell SvelteKit that it should re-run any `load`
// functions. Since we have a new (or newly deleted) trustline, this
// results in re-querying the network to get updated account balances.
invalidateAll();
};

// Builds and presents to the user for confirmation a Stellar transaction that
// will add/modify/remove a trustline on their account. This function is
// called when the user clicks the "add" or "delete" trustline buttons.
const previewChangeTrustTransaction = async (
addingAsset = true,
removeAsset = undefined,
) => {
// Generate the transaction, expecting back the XDR string
let { transaction, network_passphrase } =
await createChangeTrustTransaction({
source: data.publicKey,
asset: removeAsset ?? asset,
limit: addingAsset ? undefined : "0",
});

// Set the component variables to hold the transaction details
changeTrustXDR = transaction;
changeTrustNetwork = network_passphrase;

// Open the confirmation modal for the user to confirm or reject the
// transaction. We provide our customized `onConfirm` function, but we
// have no need to customize and pass an `onReject` function.
open(ConfirmationModal, {
transactionXDR: changeTrustXDR,
transactionNetwork: changeTrustNetwork,
onConfirm: onConfirm,
});
};
</script>

<!-- HTML has been omitted from this tutorial. Please check the source file -->

Source: https://github.com/stellar/basic-payment-app/blob/main/src/routes/dashboard/assets/+page.svelte

The createChangeTrustTransaction function

In the above page, we've made use of the createChangeTrustTransaction function. This function can be used to add, delete, or modify trustlines on a Stellar account.

/src/lib/stellar/transactions.js

import {
TransactionBuilder,
Networks,
Server,
Operation,
Asset,
} from "stellar-sdk";
import { error } from "@sveltejs/kit";

// We are setting a very high maximum fee, which increases our transaction's
// chance of being included in the ledger. We're making this a `const` so we can
// change it on one place as and when recommendations and/or best practices
// evolve. Current recommended fee is `100_000` stroops.
const maxFeePerOperation = "100000";
const horizonUrl = "https://horizon-testnet.stellar.org";
const networkPassphrase = Networks.TESTNET;
const standardTimebounds = 300; // 5 minutes for the user to review/sign/submit

// Constructs and returns a Stellar transaction that will create or modify a
// trustline on an account.
export async function createChangeTrustTransaction({ source, asset, limit }) {
// We start by converting the asset provided in string format into a Stellar
// Asset() object
let trustAsset = new Asset(asset.split(":")[0], asset.split(":")[1]);

// Next, we setup our transaction by loading the source account from the
// network, and initializing the TransactionBuilder.
let server = new Server(horizonUrl);
let sourceAccount = await server.loadAccount(source);

// Chaning everything together from the `transaction` declaration means we
// don't have to assign anything to `builtTransaction` later on. Either
// method will have the same results.
let transaction = new TransactionBuilder(sourceAccount, {
networkPassphrase: networkPassphrase,
fee: maxFeePerOperation,
})
// Add a single `changeTrust` operation (this controls whether we are
// adding, removing, or modifying the account's trustline)
.addOperation(
Operation.changeTrust({
asset: trustAsset,
limit: limit?.toString(),
}),
)
// Before the transaction can be signed, it requires timebounds
.setTimeout(standardTimebounds)
// It also must be "built"
.build();

return {
transaction: transaction.toXDR(),
network_passphrase: networkPassphrase,
};
}
Manage Trust | Stellar Docs (2024)
Top Articles
Master credit card payments: Step-by-step guide for businesses | Stripe
What are the Benefits of Homemade Food? | Mealawe
Swimgs Yuzzle Wuzzle Yups Wits Sadie Plant Tune 3 Tabs Winnie The Pooh Halloween Bob The Builder Christmas Autumns Cow Dog Pig Tim Cook’s Birthday Buff Work It Out Wombats Pineview Playtime Chronicles Day Of The Dead The Alpha Baa Baa Twinkle
Washu Parking
Form V/Legends
How Much Does Dr Pol Charge To Deliver A Calf
Nyu Paralegal Program
Blairsville Online Yard Sale
27 Places With The Absolute Best Pizza In NYC
State Of Illinois Comptroller Salary Database
Find The Eagle Hunter High To The East
Guardians Of The Galaxy Vol 3 Full Movie 123Movies
Shemal Cartoon
OpenXR support for IL-2 and DCS for Windows Mixed Reality VR headsets
Https E24 Ultipro Com
Shreveport Active 911
Tcgplayer Store
Nashville Predators Wiki
Jbf Wichita Falls
Is A Daytona Faster Than A Scat Pack
eHerkenning (eID) | KPN Zakelijk
Daytonaskipthegames
Poe Str Stacking
Loslaten met de Sedona methode
T Mobile Rival Crossword Clue
Reser Funeral Home Obituaries
Weldmotor Vehicle.com
Harrison 911 Cad Log
Mchoul Funeral Home Of Fishkill Inc. Services
Publix Coral Way And 147
Deleted app while troubleshooting recent outage, can I get my devices back?
Kelsey Mcewen Photos
The Bold And The Beautiful Recaps Soap Central
Dmitri Wartranslated
Smith And Wesson Nra Instructor Discount
The Best Restaurants in Dublin - The MICHELIN Guide
Busted Newspaper Campbell County KY Arrests
What Is A K 56 Pink Pill?
Casamba Mobile Login
manhattan cars & trucks - by owner - craigslist
Directions To Cvs Pharmacy
Dr Mayy Deadrick Paradise Valley
Ehome America Coupon Code
Graduation Requirements
Westport gun shops close after confusion over governor's 'essential' business list
Research Tome Neltharus
Cryptoquote Solver For Today
Noelleleyva Leaks
Costco Gas Price Fort Lauderdale
Secondary Math 2 Module 3 Answers
Cheryl Mchenry Retirement
Latest Posts
Article information

Author: Barbera Armstrong

Last Updated:

Views: 5824

Rating: 4.9 / 5 (79 voted)

Reviews: 86% of readers found this page helpful

Author information

Name: Barbera Armstrong

Birthday: 1992-09-12

Address: Suite 993 99852 Daugherty Causeway, Ritchiehaven, VT 49630

Phone: +5026838435397

Job: National Engineer

Hobby: Listening to music, Board games, Photography, Ice skating, LARPing, Kite flying, Rugby

Introduction: My name is Barbera Armstrong, I am a lovely, delightful, cooperative, funny, enchanting, vivacious, tender person who loves writing and wants to share my knowledge and understanding with you.