Take the custodian side of a gated token: derive the custody address, get it allowlisted, receive the asset, observe a freeze, and reconcile at a stated commitment level.
The issuing tutorial builds
a gated token from the issuer’s side. This one takes the custodian side, where
a custody system holds the asset for a client and has to handle what the issuer
does with its controls. It continues with the mint from
gate the holders,
so $MINT and $LIST should already be exported, on the same network.
Custodian-side and issuer-side stepsThe custodian-side steps read the custody account with spl-token, which is
part of the Solana CLI install. To read it from code as well, use the
derive-ata and read-account scripts in the Kit, Web3.js v3, or Rust project
from the issuing tutorial. The spl-token commands use $RPC_URL when it is
set and devnet otherwise. The issuer-side steps run the issuing tutorial’s
scripts and are marked where they appear: in a real integration another party
runs them.
1
Create the custody address
A custodian holds the asset at an address it controls. Here a key file stands
in for whatever your key-management backend produces; see
choosing a backend for
what that is in production. The custodian signs nothing in this tutorial, so it
needs no SOL.
Terminal
solana-keygen new --no-bip39-passphrase --silent \ --outfile ~/.config/solana/demo-custodian.jsonexport CUSTODIAN=$(solana-keygen pubkey ~/.config/solana/demo-custodian.json)
2
Derive the token account address before it exists
The asset does not sit at your wallet address. It sits in an associated token
account derived from that wallet and the mint. Two addresses are involved:
The wallet is what the issuer allowlists. That is the onboarding
decision.
The associated token account holds the balance, is what the issuer
freezes, and is what your reconciliation reads.
The associated token address is deterministic, so you can compute it before
the account exists and set up monitoring and reconciliation ahead of the first
transfer. Save the script as derive-ata.ts (in Rust, src/bin/derive-ata.rs)
and run it with $CUSTODIAN.
// npx tsx derive-ata.ts <wallet>import { address } from "@solana/kit";import { findAssociatedTokenPda, TOKEN_2022_PROGRAM_ADDRESS,} from "@solana-program/token-2022";import { arg, env } from "./common";const owner = address(arg(1, "the custody wallet"));// Deterministic: the address exists before the account does, so monitoring// and reconciliation can be wired up ahead of the first transfer.const [tokenAccount] = await findAssociatedTokenPda({ mint: address(env("MINT")), owner, tokenProgram: TOKEN_2022_PROGRAM_ADDRESS,});console.log("Wallet address:", owner);console.log("Associated token address:", tokenAccount);
Deriving the custody token account before it exists
// npx tsx derive-ata.ts <wallet>import { findAssociatedTokenPda, TOKEN_2022_PROGRAM_ADDRESS,} from "@solana-program/token-2022";import { addressOf, arg, env } from "./common";const owner = addressOf(arg(1, "the custody wallet"));// Deterministic: the address exists before the account does, so monitoring// and reconciliation can be wired up ahead of the first transfer.const [tokenAccount] = await findAssociatedTokenPda({ mint: addressOf(env("MINT")), owner, tokenProgram: TOKEN_2022_PROGRAM_ADDRESS,});console.log("Wallet address:", owner);console.log("Associated token address:", tokenAccount);
Deriving the custody token account before it exists
// cargo run --release --bin derive-ata -- <wallet>use anyhow::Result;use gated_token::{arg_pubkey, ata, env};fn main() -> Result<()> { let owner = arg_pubkey(1, "the custody wallet")?; // Deterministic: the address exists before the account does, so // monitoring and reconciliation can be wired up ahead of the first transfer. println!("Wallet address: {owner}"); println!("Associated token address: {}", ata(&env("MINT")?, &owner)); Ok(())}
Deriving the custody token account before it exists
Export the Associated token address it prints as CUSTODY_ATA; the reads
below use it:
Terminal
export CUSTODY_ATA=<the associated token address>
If your platform rotates wallets per client or per deposit, each wallet needs allowlisting before it
can receive (see
custody integration).
3
Get the address onto the allowlist
Issuer side. You supply the wallet address, not the token account, and the
issuer records the onboarding decision with allowlist-add:
npx tsx allowlist-add.ts $CUSTODIAN
cargo run --release --bin allowlist-add -- $CUSTODIAN
4
Receive the asset
Issuer side again: a mint of 500 to the custody address. The token account is
created and thawed in the same transaction, because the wallet is on the list.
npx tsx mint.ts $CUSTODIAN 500
cargo run --release --bin mint -- $CUSTODIAN 500
5
Confirm what you hold
Back on the custodian side. Read the token account directly with spl-token:
Reading the custody account after the issuer mints 500
Output
SPL Token Account Address: EqM2po44bk4JqjMeyynvaGd7aFWquBgLmnyVgMHyt1GC Program: TokenzQdBNbLqP5VEhdkAS6EPFLC1PHnBqCXEpPxuEb Balance: 500 Decimals: 6 Mint: 8ComqBcog3Aig4hYN9PcCQHcK4rCCUhv7QoxUfrVVB66 Owner: 6tUvc1Jco5iA8c1JKpjtLFwLvLSXTCfjtoYnnTazH7yH State: Initialized Delegation: (not set) Close authority: (not set)Extensions: Immutable owner* This token has a permanent delegate! This means the mint may withdraw all funds from this account at any time. If this was not adequately disclosed to you, you may be dealing with a malicious mint.
To read it from code, use the read-account script from
operate the token,
run with $CUSTODY_ATA:
Kit
Web3.js v3
Rust
Kit
// npx tsx read-account.ts <token account> [commitment]import { address, type Commitment } from "@solana/kit";import { AccountState, fetchToken } from "@solana-program/token-2022";import { arg, rpc } from "./common";const tokenAccount = address(arg(1, "the token account"));// Read at a stated commitment level. Use "finalized" for anything that feeds// a book of record; "confirmed" is enough for a display.const commitment = (process.argv[3] ?? "confirmed") as Commitment;const { data } = await fetchToken(rpc, tokenAccount, { commitment });console.log("Mint:", data.mint);console.log("Owner:", data.owner);console.log("Balance (base units):", data.amount.toString());console.log("State:", AccountState[data.state]);
Reading the custody account after the issuer mints 500
// npx tsx read-account.ts <token account> [commitment]import { type Commitment, PublicKey } from "@solana/web3.js";import { AccountState, getTokenDecoder } from "@solana-program/token-2022";import { arg, connection } from "./common";const tokenAccount = new PublicKey(arg(1, "the token account"));// Read at a stated commitment level. Use "finalized" for anything that feeds// a book of record; "confirmed" is enough for a display.const commitment = (process.argv[3] ?? "confirmed") as Commitment;const info = await connection.getAccountInfo(tokenAccount, commitment);const data = getTokenDecoder().decode(info!.data);console.log("Mint:", data.mint);console.log("Owner:", data.owner);console.log("Balance (base units):", data.amount.toString());console.log("State:", AccountState[data.state]);
Reading the custody account after the issuer mints 500
// cargo run --release --bin read-account -- <token account> [commitment]use anyhow::{anyhow, Result};use gated_token::{arg, arg_pubkey, rpc_with};use solana_commitment_config::CommitmentConfig;use spl_token_2022_interface::{ extension::StateWithExtensions, state::{Account, AccountState},};#[tokio::main]async fn main() -> Result<()> { let token_account = arg_pubkey(1, "the token account")?; // Read at a stated commitment level. Use "finalized" for anything that // feeds a book of record; "confirmed" is enough for a display. let commitment = match arg(2, "the commitment").as_deref() { Ok("finalized") => CommitmentConfig::finalized(), _ => CommitmentConfig::confirmed(), }; let rpc = rpc_with(commitment); let account = rpc .get_account_with_commitment(&token_account, commitment) .await? .value .ok_or_else(|| anyhow!("{token_account} not found at {commitment:?}"))?; let state = StateWithExtensions::<Account>::unpack(&account.data)?.base; println!("Mint: {}", state.mint); println!("Owner: {}", state.owner); println!("Balance (base units): {}", state.amount); let label = if state.state == AccountState::Frozen { "Frozen" } else { "Initialized" }; println!("State: {label}"); Ok(())}
Reading the custody account after the issuer mints 500
Read two fields. Balance is the position. spl-token applies the mint’s
six decimals and shows Balance: 500; the scripts print the raw amount in base
units, 500000000. State is the field a custody system must handle:
Initialized means the account is thawed and usable, and Frozen means it is
not. If a custody system does not show this field, a failed withdrawal cannot
be explained from the account state.The spl-token display output also warns that the mint has a permanent
delegate, which can move the balance out of the account at any time. A
custodian needs that disclosed by the issuer, and a category for such movements
in its reconciliation.
6
Observe a freeze
The issuer can freeze an account you hold without your involvement, so
detection has to come from monitoring the account rather than from a failed
withdrawal. Issuer side:
Reading the custody account after the issuer freezes it
Output
SPL Token Account Address: EqM2po44bk4JqjMeyynvaGd7aFWquBgLmnyVgMHyt1GC Program: TokenzQdBNbLqP5VEhdkAS6EPFLC1PHnBqCXEpPxuEb Balance: 500 Decimals: 6 Mint: 8ComqBcog3Aig4hYN9PcCQHcK4rCCUhv7QoxUfrVVB66 Owner: 6tUvc1Jco5iA8c1JKpjtLFwLvLSXTCfjtoYnnTazH7yH State: Frozen Delegation: (not set) Close authority: (not set)Extensions: Immutable owner* This token has a permanent delegate! This means the mint may withdraw all funds from this account at any time. If this was not adequately disclosed to you, you may be dealing with a malicious mint.
From code, run read-account with $CUSTODY_ATA again:
Kit
Web3.js v3
Rust
Kit
// npx tsx read-account.ts <token account> [commitment]import { address, type Commitment } from "@solana/kit";import { AccountState, fetchToken } from "@solana-program/token-2022";import { arg, rpc } from "./common";const tokenAccount = address(arg(1, "the token account"));// Read at a stated commitment level. Use "finalized" for anything that feeds// a book of record; "confirmed" is enough for a display.const commitment = (process.argv[3] ?? "confirmed") as Commitment;const { data } = await fetchToken(rpc, tokenAccount, { commitment });console.log("Mint:", data.mint);console.log("Owner:", data.owner);console.log("Balance (base units):", data.amount.toString());console.log("State:", AccountState[data.state]);
Reading the custody account after the issuer freezes it
// npx tsx read-account.ts <token account> [commitment]import { type Commitment, PublicKey } from "@solana/web3.js";import { AccountState, getTokenDecoder } from "@solana-program/token-2022";import { arg, connection } from "./common";const tokenAccount = new PublicKey(arg(1, "the token account"));// Read at a stated commitment level. Use "finalized" for anything that feeds// a book of record; "confirmed" is enough for a display.const commitment = (process.argv[3] ?? "confirmed") as Commitment;const info = await connection.getAccountInfo(tokenAccount, commitment);const data = getTokenDecoder().decode(info!.data);console.log("Mint:", data.mint);console.log("Owner:", data.owner);console.log("Balance (base units):", data.amount.toString());console.log("State:", AccountState[data.state]);
Reading the custody account after the issuer freezes it
// cargo run --release --bin read-account -- <token account> [commitment]use anyhow::{anyhow, Result};use gated_token::{arg, arg_pubkey, rpc_with};use solana_commitment_config::CommitmentConfig;use spl_token_2022_interface::{ extension::StateWithExtensions, state::{Account, AccountState},};#[tokio::main]async fn main() -> Result<()> { let token_account = arg_pubkey(1, "the token account")?; // Read at a stated commitment level. Use "finalized" for anything that // feeds a book of record; "confirmed" is enough for a display. let commitment = match arg(2, "the commitment").as_deref() { Ok("finalized") => CommitmentConfig::finalized(), _ => CommitmentConfig::confirmed(), }; let rpc = rpc_with(commitment); let account = rpc .get_account_with_commitment(&token_account, commitment) .await? .value .ok_or_else(|| anyhow!("{token_account} not found at {commitment:?}"))?; let state = StateWithExtensions::<Account>::unpack(&account.data)?.base; println!("Mint: {}", state.mint); println!("Owner: {}", state.owner); println!("Balance (base units): {}", state.amount); let label = if state.state == AccountState::Frozen { "Frozen" } else { "Initialized" }; println!("State: {label}"); Ok(())}
Reading the custody account after the issuer freezes it
State is now Frozen. The balance is unchanged: the assets are still
there but cannot move. Report a frozen position as unavailable, not as zero.Ask the issuer to restore it before you continue:
npx tsx thaw.ts $CUSTODIAN
cargo run --release --bin thaw -- $CUSTODIAN
7
Reconcile at a stated commitment level
Reconciliation against a book of record needs a defined point in time, not
“now.” spl-token has no --commitment flag: it reads at the commitment set
in the Solana CLI config, confirmed by default. For a finalized read, keep
a separate config file and pass it with -C.The config file replaces your settings rather than adding to them, so it has
to name the cluster as well as the commitment. Without json_rpc_url, the read
goes to mainnet, where $CUSTODY_ATA does not exist. The command below
writes $RPC_URL into it when it is set, and devnet otherwise.
Reading the custody account at finalized commitment
Output
SPL Token Account Address: EqM2po44bk4JqjMeyynvaGd7aFWquBgLmnyVgMHyt1GC Program: TokenzQdBNbLqP5VEhdkAS6EPFLC1PHnBqCXEpPxuEb Balance: 500 Decimals: 6 Mint: 8ComqBcog3Aig4hYN9PcCQHcK4rCCUhv7QoxUfrVVB66 Owner: 6tUvc1Jco5iA8c1JKpjtLFwLvLSXTCfjtoYnnTazH7yH State: Initialized Delegation: (not set) Close authority: (not set)Extensions: Immutable owner* This token has a permanent delegate! This means the mint may withdraw all funds from this account at any time. If this was not adequately disclosed to you, you may be dealing with a malicious mint.
The scripts take the commitment level as their second argument, so from code
run read-account with $CUSTODY_ATA finalized:
Kit
Web3.js v3
Rust
Kit
// npx tsx read-account.ts <token account> [commitment]import { address, type Commitment } from "@solana/kit";import { AccountState, fetchToken } from "@solana-program/token-2022";import { arg, rpc } from "./common";const tokenAccount = address(arg(1, "the token account"));// Read at a stated commitment level. Use "finalized" for anything that feeds// a book of record; "confirmed" is enough for a display.const commitment = (process.argv[3] ?? "confirmed") as Commitment;const { data } = await fetchToken(rpc, tokenAccount, { commitment });console.log("Mint:", data.mint);console.log("Owner:", data.owner);console.log("Balance (base units):", data.amount.toString());console.log("State:", AccountState[data.state]);
Reading the custody account at finalized commitment
// npx tsx read-account.ts <token account> [commitment]import { type Commitment, PublicKey } from "@solana/web3.js";import { AccountState, getTokenDecoder } from "@solana-program/token-2022";import { arg, connection } from "./common";const tokenAccount = new PublicKey(arg(1, "the token account"));// Read at a stated commitment level. Use "finalized" for anything that feeds// a book of record; "confirmed" is enough for a display.const commitment = (process.argv[3] ?? "confirmed") as Commitment;const info = await connection.getAccountInfo(tokenAccount, commitment);const data = getTokenDecoder().decode(info!.data);console.log("Mint:", data.mint);console.log("Owner:", data.owner);console.log("Balance (base units):", data.amount.toString());console.log("State:", AccountState[data.state]);
Reading the custody account at finalized commitment
// cargo run --release --bin read-account -- <token account> [commitment]use anyhow::{anyhow, Result};use gated_token::{arg, arg_pubkey, rpc_with};use solana_commitment_config::CommitmentConfig;use spl_token_2022_interface::{ extension::StateWithExtensions, state::{Account, AccountState},};#[tokio::main]async fn main() -> Result<()> { let token_account = arg_pubkey(1, "the token account")?; // Read at a stated commitment level. Use "finalized" for anything that // feeds a book of record; "confirmed" is enough for a display. let commitment = match arg(2, "the commitment").as_deref() { Ok("finalized") => CommitmentConfig::finalized(), _ => CommitmentConfig::confirmed(), }; let rpc = rpc_with(commitment); let account = rpc .get_account_with_commitment(&token_account, commitment) .await? .value .ok_or_else(|| anyhow!("{token_account} not found at {commitment:?}"))?; let state = StateWithExtensions::<Account>::unpack(&account.data)?.base; println!("Mint: {}", state.mint); println!("Owner: {}", state.owner); println!("Balance (base units): {}", state.amount); let label = if state.state == AccountState::Frozen { "Frozen" } else { "Initialized" }; println!("State: {label}"); Ok(())}
Reading the custody account at finalized commitment
Use a separate config file, not solana config set --commitment finalized,
for a reconciliation job, so other commands keep their own commitment.solana account returns the raw account: its lamports, owner program, and
data bytes, base64-encoded, with the balance inside the data:
Everything above is a read or a single transaction. A production integration
also needs continuous detection: subscribe to the accounts you hold and to the
mint, where a pause is recorded, so a freeze, a pause, or a permanent-delegate
transfer raises an alert before a client instruction fails on it.
Custody integration
lists what a custodian’s systems have to support.