In this section of the tutorial, we'll add the ability to hold and transfer custom assets to the basic wallet we built in previous sections. It assumes that you've already completed Create a Basic Wallet and Make XBN Payments
What's a Custom Asset?
Bantu allows anyone to easily issue an asset, and all assets can be held, transferred, and traded just like XBN, the network token. Every asset other than XBN exists on the network in the form of trustlines: an asset holder explicitly agrees to allow a balance of a specific token issued by a specific issuing account by creating a persistent ledger entry tied to the holding account. You can find out more in the guide to creating custom assets.
Each trustline increases the user's base reserve by 0.5 XBN, and in this tutorial, we'll go over how to set up your wallet to create trustlines and manage the base reserve on behalf of a user.
Add Trustlines Button
To enable custom asset handling, we need to modify three files and create one new one. Let’s start with our modifications. First up the ./events/render.tsx file. We need to add a button for creating these new trustlines!
If you look closely you’ll spot the Trust Asset button right below our account-key div. Nothing funky here, just a button that triggers this.trustAsset method which we’ll add in a moment.
Next up, let’s update the ./methods/makePayment.ts file.
This is a big file that was covered in great detail in the Make XBN Payments tutorial, so we’ll just focus on the changes we need to make to support custom asset payments.
let instructions =awaitthis.setPrompt("{Amount} {Asset} {Destination}");instructions =instructions.split(" ");if (!/xlm/gi.test(instructions[1])) instructions[3] =awaitthis.setPrompt(`Who issues the ${instructions[1]} asset?`,"Enter ME to refer to yourself", );
This change allows us to indicate a specific asset code we’d like use to make a payment and triggers an additional prompt to set the issuer for that asset if it’s not the native XBN.
if (/me/gi.test(instructions[3])) instructions[3] =keypair.publicKey();
This is just a nifty little helper shortcut to allow us to use the ME “issuer” to swap with our actual account publicKey. Niceties make the world go ‘round.
The final noteworthy change is a ternary operation that switches our payment asset between the native XBN and a custom asset based off of responses to our prompt. Essentially, if instructions[3] exists — meaning there is an issuer — use that issuer and custom token as the asset for the payment. Otherwise, just use the native Asset.
The final changes are in the wallet.ts itself and tie together all the other updates as well as pull in the new trustAsset method.
We’re allowing the inclusion of several arguments in this function, namely asset, issuer, and pincode. We won’t be making use of them here, but transparently creating trustlines from within other functions will prove useful later.
If we have any of those variables set, we can “preload” our interface a bit, and even bypass user input altogether if a pincode is provided. Again, not something we’ll make use of quite yet, but once we look into depositing and withdrawing assets from an Anchor or accepting incoming payments for which we don’t yet have a trustline this functionality will prove useful.
So there we have it! The ability to accept and pay with custom assets on Bantu!