SPL Associate Token Account & Merge

Multiple accounts for a single SPL token

This is the code where we create accounts for our SPL token:

// step: 1 caculate balance
const balanceNeeded = await Token.getMinBalanceRentForExemptAccount(
this._connection,
);

// step2: create createAccount transaction instrument
const createAccountTrxi = SystemProgram.createAccount({
fromPubkey: account.publicKey,
newAccountPubkey: splAccount.publicKey,
lamports: balanceNeeded,
space: 165,
programId: new PublicKey(TOKEN_PROGRAM),
})

// step3: ceate init account transaction instrucment
const initAccountTrx = Token.createInitAccountInstruction(
new PublicKey(TOKEN_PROGRAM),
new PublicKey(mint),
splAccount.publicKey,
account.publicKey,
)
// step4: create transaction
const transaction = new Transaction()
transaction.add(createAccountTrxi)
transaction.add(initAccountTrx)
  • createAccountTrxi: Create an Account with size of 165 bytes
  • initAccountTrx: Initialize the Account
/// Account data.
#[repr(C)]
#[derive(Clone, Copy, Debug, Default, PartialEq)]
pub struct Account {
/// The mint associated with this account
pub mint: Pubkey,
/// The owner of this account.
pub owner: Pubkey,
/// The amount of tokens this account holds.
pub amount: u64,
/// If `delegate` is `Some` then `delegated_amount` represents
/// the amount authorized by the delegate
pub delegate: COption<Pubkey>,
/// The account's state
pub state: AccountState,
/// If is_some, this is a native token, and the value logs the rent-exempt reserve. An Account
/// is required to be rent-exempt, so the value is used by the Processor to ensure that wrapped
/// SOL accounts do not drop below this threshold.
pub is_native: COption<u64>,
/// The amount delegated
pub delegated_amount: u64,
/// Optional authority to close the account.
pub close_authority: COption<Pubkey>,
}

Associate account

Multiple accounts are hard to manage and cause chaos. Users easily lost track of their assets and it also brings inconvenience for wallet developers. In order the optimize it, Solana has brought up a solution in the latest Wallet Integration Guide to encourage the use of associate program, meaning you can get all the SPL deposit justing using the owner address(SOL address). Woot! That’s just what we want need right?

/**
* Find a valid program address
*
* Valid program addresses must fall off the ed25519 curve. This function
* iterates a nonce until it finds one that when combined with the seeds
* results in a valid program address.
*/
static async findProgramAddress(
seeds: Array<Buffer | Uint8Array>,
programId: PublicKey,
): Promise<PublicKeyNonce>
/**
* Get the address for the associated token account
*
* @param associatedProgramId SPL Associated Token program account
* @param programId SPL Token program account
* @param mint Token mint account
* @param owner Owner of the new account
* @return Public key of the associated token account
*/
static async getAssociatedTokenAddress(
associatedProgramId: PublicKey,
programId: PublicKey,
mint: PublicKey,
owner: PublicKey,
): Promise<PublicKey> {
return (
await PublicKey.findProgramAddress(
[owner.toBuffer(), programId.toBuffer(), mint.toBuffer()],
associatedProgramId,
)
)[0];
}

Merge

To help our users to manage their assets all in one address, we create a merge function to help transfer the token assets in other addresses to associate address. If you have balance more that 0 for a token in other addresses othan the associate address, we will show this Merge button to combine the assets for you.

Conclusion

The Associate token program solves the problem caused by multiple deposit addresses for a single token assets, generally the whole deposit address concept for different tokens is hidden. The token management and transfer are very intuitive, especially for the users who has been used to the Ethereum operations. Solana experience is getting better and better, looking forward to the bright future of it!

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store