This step puts the code from the first two steps into a project of your own,
in five files: a connection helper, the asset, holder approval, the transfers
that test the rules, and a script that runs them in order. An optional
confidential variant at the end adds two more. The output under each block is
from a local Surfpool network.
1. Create the project and install the packages
You need Node.js 20.18 or later. The scripts use top-levelawait and
import.meta.url, so package.json needs "type": "module", which the second
command sets. The examples are tested with @solana/kit 8.4,
@solana-program/token-2022 0.19, and @solana-program/system 0.15.
Terminal
2. Pick a network
The code reads the network from two environment variables and uses devnet when they are unset.- Local: install the Surfpool CLI
and run
surfpool start. Then setRPC_URL=http://127.0.0.1:8899andRPC_SUBSCRIPTIONS_URL=ws://127.0.0.1:8900. This is where the output on these pages comes from. - Devnet: leave both unset. Fund your issuer wallet (step 7 creates its key file) with free devnet SOL from the Solana Faucet to pay fees and account deposits.
3. Add the connection helper
send builds a transaction from a list of instructions, signs it, sends it, and
waits until it’s confirmed. On failure it keeps the signature and message on
the error, so you can look up the transaction and decode its program error.
config.ts
4. Create the asset
Save this ascreate-asset.ts. It’s the code from the previous step.
create-asset.ts
Output of createAsset(issuer)Local test network (Surfpool)
await createAsset(issuer)
Mint: HZum3YzcFTMWDGAQWBYMLDUACtLcxc1pF67doKooqYzY
Extensions: MetadataPointer, PausableConfig, PermanentDelegate, DefaultAccountState, TokenMetadata
Signature: 5AJfoLZSwWu7J568eoPPj9NFoGAzYDAS6uBD7zb4bb6E7dNbDRPYdxX6s8hZEo9qqeTpihA9zg6P9sGneEXURiL4
Fee: 10000 lamports
Confirmed
5. Approve holders
Save this asapprove-holder.ts. openAccount creates a holder’s token
account, which starts frozen. approveHolder thaws it with the freeze authority
and, when you pass an amount, mints LSF to it in the same transaction. Amounts
are in base units: with 6 decimals, 1_000_000_000n is 1000 LSF.
approve-holder.ts
Approving a holder with 1,000 LSF and a second holder with noneLocal test network (Surfpool)
await openAccount(issuer, mint, holder.address)
Opened JAg5yGYw5Be2J54jk9s7ZUxhtU2FXN8txfpjxPctaeqx state: Frozen
await approveHolder(issuer, mint, holder.address, 1_000_000_000n)
Approved JAg5yGYw5Be2J54jk9s7ZUxhtU2FXN8txfpjxPctaeqx state: Initialized balance: 1000 LSF
Signature: 2cTiGJS6oWQFZ8XG4gTXqHz6RiywDvCYRzbprNrPfop8AacwZQCWC4ExL4UNQxK3XYF3LfakaqHPWN9RJQUiQexW
await openAccount(issuer, mint, approved.address)
Opened CpvfwkairbnMrGqdW9wxMSiFgsHxi7666Gu4j7vCdjDv state: Frozen
await approveHolder(issuer, mint, approved.address, 0n)
Approved CpvfwkairbnMrGqdW9wxMSiFgsHxi7666Gu4j7vCdjDv state: Initialized balance: 0 LSF
Signature: 5bjv6TA5KoJ8v61sPZ4Vuf3U8qRoryzTcEGqDShtfGLjxq7zaMn3vxf69QJwbPAxrEFu1tyzPNjw5RLf9ebzRcFa
Opened and approved · 4 transactions
approveHolder only after your KYC check passes. To approve addresses from
an allowlist automatically, use Token ACL. To
withdraw an approval, freeze the account again with the freeze authority.
6. Test the rules
Save this asblocked-transfer.ts. It holds the transfers and controls from
step 1:
transfersends LSF between holders withTransferChecked.sendToUnapprovedsends to a frozen account withskipPreflight, so the failed transaction lands and you can read its error, log, and fee withfailure. The error holdsbigintvalues, whichJSON.stringifycan’t print as they are, so it converts them first.sendToUnapprovedWithPreflightsends the same transfer normally and decodes the rejection withisToken2022Error(…, TOKEN_2022_ERROR__ACCOUNT_FROZEN).pauseandresumeswitch every transfer of the asset off and on.clawBackuses the permanent delegate to move tokens out of a holder’s account into the issuer’s.
blocked-transfer.ts
A permitted transfer, a blocked one, a pause and a permanent-delegate transferLocal test network (Surfpool)
await transfer(holder, mint, approved.address, 100_000_000n)
Transfer 100 LSF: confirmed
Signature: 3m3VeXJvsqmPunuwHRyf3qrXXYApTCv2WABEib1U9M7qrXSXgwJxDopg1L6cfvUQ9NL6aG8bJDj6csHVxDm69oTM
await openAccount(issuer, mint, unapproved.address)
Opened FecRDHyzRTLH3dfdz6ni5pnGq5j883zcSBToSe3PPdbP state: Frozen
await sendToUnapproved(holder, mint, unapproved.address)
Transfer 1 LSF: failed onchain
Failed signature: 57czSUTPw8a1c8yN7n4TMV3CsRGEQSqaa3s7Tuvv4sV3jN5HQaurXZZnCtkFnv4DumVSi7WFMpHVhDYcKdtaxseW
err:
{"InstructionError":[0,{"Custom":17}]}log: Program log: Error: Account is frozen
fee paid anyway: 5000 lamports
await sendToUnapprovedWithPreflight(holder, mint, unapproved.address)
Transfer 1 LSF: rejected: Custom program error: #17 (instruction #1)
accountFrozen: true (no fee: it never landed)
await pause(issuer, mint)
Paused the asset. Signature: 34UgQ3NwajBVsXCGihEi9pTCpFc8CCkWDfgRzQD2KLyfGGwTpAux7hf7e8RTQfFBChZDsdxQwa4Ef5AVSCgWWy67
await transfer(holder, mint, approved.address, 1_000_000n)
Transfer 1 LSF: rejected: Custom program error: #67 (instruction #1)
await resume(issuer, mint)
Resumed the asset. Signature: 2k8BNXskFbwdVxvxihzs72mDDkGkm6GBmgtVXwJAD8X8QXCUcSSXYSENSWbi85uKmUSNEUBwNv63sem2f7vb9weA
await clawBack(issuer, mint, approved.address, 50_000_000n)
Permanent delegate moved 50 LSF from CpvfwkairbnMrGqdW9wxMSiFgsHxi7666Gu4j7vCdjDv to the issuer
Signature: 3ZXvUsuYkbBJMyCr4y1g8iKt8wmsJtt5xhybWW34W6vYShELvj2MMhwZ7UqeAxXVuAFK3Hs76Xb1QBhDJuNXwkmE
100 LSF transfer confirmedFailed: Account is frozen (Custom: 17)Failed: Mint is paused (Custom: 67)
7. Run it
Save this asrun.ts. It calls the functions above in order: create the asset,
give a new holder a little SOL for fees, approve two investors and the issuer’s
own account (so clawBack has somewhere to send tokens), then send to an
approved and an unapproved account (with and without preflight), pause, try a
transfer, resume, claw back 50 LSF, and print every balance.
run.ts
Output of npx tsx run.tsLocal test network (Surfpool)
npx tsx run.ts
Mint: Fser2BLgZx7Ed27Dyb6UiKQvecHWAEThXqVZzFT31hu8
Extensions: MetadataPointer, PausableConfig, PermanentDelegate, DefaultAccountState, TokenMetadata
Signature: 25C6zgPeq6PunC6pcnCWPpZBu7gGkg62Esnpw2TqC4r9meGwXhHaziTjkQDsk5zkMwDXLW5fjqL8GWJJpvp6MZXn
Fee: 10000 lamports
Opened BuJ4KXhmUbw4yi2fd2K3jTE7Smp4f15w7Ezv4Wpkpbwq state: Frozen
Approved BuJ4KXhmUbw4yi2fd2K3jTE7Smp4f15w7Ezv4Wpkpbwq state: Initialized balance: 1000 LSF
Signature: 57hTnE611zzHEXh5t2Dz3iwz7BMNCs3txhkjk6GcNSQwY42DAKzLNb8TMYNwGcraTwaoswwPPXE4CzFx4z3drF6C
Opened BbUYndSSGyYT5gR9LoLjWboZhL63GoyTJm5tDoho7qSS state: Frozen
Approved BbUYndSSGyYT5gR9LoLjWboZhL63GoyTJm5tDoho7qSS state: Initialized balance: 0 LSF
Signature: 5EfHpvbBGZkLx4z3YioPEiu3AHR6pacLt7mzct1TFDN9rwrBUynWNyJbznSeyV8ZNKeW1uSXKR3eEetnF98FmRiz
Opened BuY7wikCSJsP4mDYtCa14b3nXU4NHkPnUdxDn4SGfsUT state: Frozen
Approved BuY7wikCSJsP4mDYtCa14b3nXU4NHkPnUdxDn4SGfsUT state: Initialized balance: 0 LSF
Signature: 2PyJ79R1DvAMPnqkUQKwTYgcVkMaSSbkC1LkD9r9EpYYNMsyjHTiwJo9MQToM8Gv5gQEFnHpwR19Tq7g3e4HoAse
Transfer 100 LSF: confirmed
Signature: 4Sxyq5BBcaNPLBRTJPnGcVXxWQshiJcdkx2UVEop2KoMD8Bd91Vvx111ndbU65Eo7bn3iGPrsERVEqXwjBcCGnFS
Opened JBFb61VX7yHRp3SNR3g2zSFvo4pAKHG6F57qgXZ7dfBz state: Frozen
Transfer 1 LSF: failed onchain
Failed signature: 2uJ6CJ5PsyxZD8cvmgC6YS5KXeS94LPTQUHozG6MhiTVK6VVEqUXS92dRRDjdVYmQ3cX6CJKAXDKfSRZfxU6Qpbi
err:
{"InstructionError":[0,{"Custom":17}]}log: Program log: Error: Account is frozen
fee paid anyway: 5000 lamports
Transfer 1 LSF: rejected: Custom program error: #17 (instruction #1)
accountFrozen: true (no fee: it never landed)
Paused the asset. Signature: Ph8YZfLuwKUxocyPst5fEyziUCtwha3eAAzLggxDsb3Nq8icnX1hAXPmtfkJy2XgcXvW45DfLaZLVvJ8ssz6g9Z
Transfer 1 LSF: rejected: Custom program error: #67 (instruction #1)
Resumed the asset. Signature: 2jv5ueFkaG3LkR1qLJbPLHG9dm9yCSCxRzuD4JRNpTovjFkrvemkUMHa8hFxrBngFAhyJQMpHbk3wsFazj4D9cAq
Permanent delegate moved 50 LSF from BbUYndSSGyYT5gR9LoLjWboZhL63GoyTJm5tDoho7qSS to the issuer
Signature: 3sR7wqUAWtJuiS5bRmDrBYFv8pph6RnHsD8a2XBUrZiF7Mzc66rpoXKDKjnhN1qGARQn9rWqBsR18tNmvUpCs9Ha
Balances: holder 900 LSF | approved 50 LSF | unapproved 0 LSF | issuer 50 LSF
100 LSF transfer confirmedFailed: Account is frozen (Custom: 17)Failed: Mint is paused (Custom: 67)
run.ts signs as the issuer with a key file: the one named in ISSUER_KEYPAIR,
or the Solana CLI default ~/.config/solana/id.json. If you don’t have one
yet, save this as keygen.ts. It writes a new key file in the same format and
prints the issuer’s address:
keygen.ts
Terminal
surfpool start airdrops SOL to
~/.config/solana/id.json at startup. If you created the key file later, or
keep it elsewhere, restart with surfpool start --airdrop <issuer address>.
Then run the script:
Terminal
The full example is in
packages/docs-examples/paths/assets/issue-asset.Enable confidentiality (optional)
Use this variant only if balances must be hidden from the public. It doesn’t change the steps above. Confidential Balances hides token balances and transfer amounts. Addresses stay public, so anyone can still see which accounts hold the asset and which accounts trade with each other. If you set an auditor key, whoever holds it (you, or a regulator you give it to) can decrypt every confidential transfer amount. You turn it on with theConfidentialTransferMint extension when you create
the mint. You can’t add it to an existing mint, so decide before you issue. Set
autoApproveNewAccounts: false, so you approve each holder for confidential
use after your KYC check, as you approve them to hold the asset. Set the
auditor’s ElGamal public key too; the auditor derives it from their own wallet
and sends you only the public key.
The confidential helpers need the ZK SDK, which derives the encryption keys and
generates the proofs (the example is tested with @solana/zk-sdk 0.5):
Terminal
create-confidential-asset.ts:
createConfidentialAssetiscreateAssetwith one more extension.enableConfidentialAccountis the holder’s side. It derives the holder’s encryption keys from one wallet signature and sends a proof, generated on the holder’s machine, that the key is valid.approveConfidentialHolderis your approval. It thaws the account and approves it for confidential use in one transaction.
create-confidential-asset.ts
confidential.ts. Like run.ts, it signs as the issuer with your
key file. It creates a throwaway auditor key and a new holder, gives the holder
a little SOL for fees, creates the asset, then enables and approves the
holder’s account.
confidential.ts
Creating the asset with Confidential Balances, then enabling and approving one holderLocal test network (Surfpool)
npx tsx confidential.ts
Mint: BGNsTFxmE969yPfGMS6GriTFgVYPcDZXqHbuRcLibfJH
Extensions: MetadataPointer, PausableConfig, PermanentDelegate, DefaultAccountState, ConfidentialTransferMint, TokenMetadata
Auto-approve new accounts: false
Auditor ElGamal key: BGAZF9CWgvrByfhk3EE9tgVAyViHqd53NEP1g4sWkMac
Signature: 37wcDe44ozgju3EJWw7mwnPcRmpMN3xgYWmd4WUa1sENqFfRiFmGmpsijNSf7c9rHeaPL7F6jRJNYwG5wHeYcSTX
Holder enabled confidential balances on 9f4CRR8qhtrJqCrwoLSpTBkqfmxA6cBcrevXYy7TLDiU state: Frozen confidential approved: false
Signature: 3CLJNdq8zb9CPLyNM3SzNWRwRAeDYCykPYH4zHB3QWoECvueGGcNyhsGUPhMnjwGKjsEYAkHe1xbXG2bU46zCguT
Issuer approved 9f4CRR8qhtrJqCrwoLSpTBkqfmxA6cBcrevXYy7TLDiU state: Initialized confidential approved: true
Signature: 2mtEayHEcyJhdtb6zGCNqd8cXMa6DyEq7viJ5A36uAqw5W8aFUuThLZa3MzjiakXrP5efChXXJ4q429Fv2jde6Um
All transactions confirmed
Terminal
- Approval. A holder needs both approvals: a thaw from the freeze authority and a confidential approval from the confidential transfer authority. There is no instruction to withdraw a confidential approval. To stop a holder, freeze the account. A frozen account can’t deposit, withdraw, or send or receive a confidential transfer.
- Pause. Pausing also stops confidential deposits, transfers, and withdrawals.
- Permanent delegate. It can still move or burn any account’s public
balance, but not the encrypted balance: confidential transfers, withdrawals,
and
ConfidentialMintBurnburns all need the holder’s signature. To recover tokens from an encrypted balance, the holder has to withdraw them first; freezing the account keeps them where they are. Plan your recovery runbook around this. - Auditor. The confidential transfer authority can change the auditor key later. Transfers made after the change are encrypted to the new key.