|
| 1 | +# SPL Token to CToken Payment Migration |
| 2 | + |
| 3 | +Mirrors SPL Token's API. Same pattern, same flow. |
| 4 | + |
| 5 | +## TL;DR |
| 6 | + |
| 7 | +```typescript |
| 8 | +// SPL Token |
| 9 | +import { transfer, getOrCreateAssociatedTokenAccount } from '@solana/spl-token'; |
| 10 | + |
| 11 | +// CToken |
| 12 | +import { |
| 13 | + transferInterface, |
| 14 | + getOrCreateAtaInterface, |
| 15 | +} from '@lightprotocol/compressed-token'; |
| 16 | +``` |
| 17 | + |
| 18 | +## Action Level |
| 19 | + |
| 20 | +### SPL Token |
| 21 | + |
| 22 | +```typescript |
| 23 | +const recipientAta = await getOrCreateAssociatedTokenAccount( |
| 24 | + connection, |
| 25 | + payer, |
| 26 | + mint, |
| 27 | + recipient, |
| 28 | +); |
| 29 | +await transfer( |
| 30 | + connection, |
| 31 | + payer, |
| 32 | + sourceAta, |
| 33 | + recipientAta.address, |
| 34 | + owner, |
| 35 | + amount, |
| 36 | +); |
| 37 | +``` |
| 38 | + |
| 39 | +### CToken |
| 40 | + |
| 41 | +```typescript |
| 42 | +const recipientAta = await getOrCreateAtaInterface(rpc, payer, mint, recipient); |
| 43 | +await transferInterface( |
| 44 | + rpc, |
| 45 | + payer, |
| 46 | + sourceAta, |
| 47 | + recipientAta.address, |
| 48 | + owner, |
| 49 | + mint, |
| 50 | + amount, |
| 51 | +); |
| 52 | +``` |
| 53 | + |
| 54 | +Same two-step pattern. `transferInterface` auto-loads sender's unified balance (cold + SPL + T22). |
| 55 | + |
| 56 | +--- |
| 57 | + |
| 58 | +## Instruction Level |
| 59 | + |
| 60 | +### SPL Token |
| 61 | + |
| 62 | +```typescript |
| 63 | +import { |
| 64 | + createAssociatedTokenAccountIdempotentInstruction, |
| 65 | + createTransferInstruction, |
| 66 | + getAssociatedTokenAddressSync, |
| 67 | +} from '@solana/spl-token'; |
| 68 | + |
| 69 | +const sourceAta = getAssociatedTokenAddressSync(mint, sender); |
| 70 | +const recipientAta = getAssociatedTokenAddressSync(mint, recipient); |
| 71 | + |
| 72 | +const tx = new Transaction().add( |
| 73 | + createAssociatedTokenAccountIdempotentInstruction( |
| 74 | + payer, |
| 75 | + recipientAta, |
| 76 | + recipient, |
| 77 | + mint, |
| 78 | + ), |
| 79 | + createTransferInstruction(sourceAta, recipientAta, sender, amount), |
| 80 | +); |
| 81 | +``` |
| 82 | + |
| 83 | +### CToken (sender already hot) |
| 84 | + |
| 85 | +```typescript |
| 86 | +import { |
| 87 | + getAtaAddressInterface, |
| 88 | + createAtaInterfaceIdempotentInstruction, |
| 89 | + createTransferInterfaceInstruction, |
| 90 | + CTOKEN_PROGRAM_ID, |
| 91 | +} from '@lightprotocol/compressed-token'; |
| 92 | + |
| 93 | +const sourceAta = getAtaAddressInterface(mint, sender); |
| 94 | +const recipientAta = getAtaAddressInterface(mint, recipient); |
| 95 | + |
| 96 | +const tx = new Transaction().add( |
| 97 | + createAtaInterfaceIdempotentInstruction( |
| 98 | + payer, |
| 99 | + recipientAta, |
| 100 | + recipient, |
| 101 | + mint, |
| 102 | + CTOKEN_PROGRAM_ID, |
| 103 | + ), |
| 104 | + createTransferInterfaceInstruction(sourceAta, recipientAta, sender, amount), |
| 105 | +); |
| 106 | +``` |
| 107 | + |
| 108 | +### CToken (sender may be cold - needs loading) |
| 109 | + |
| 110 | +```typescript |
| 111 | +import { |
| 112 | + createLoadAtaInstructions, |
| 113 | + getAtaAddressInterface, |
| 114 | + createAtaInterfaceIdempotentInstruction, |
| 115 | + createTransferInterfaceInstruction, |
| 116 | + CTOKEN_PROGRAM_ID, |
| 117 | +} from '@lightprotocol/compressed-token'; |
| 118 | + |
| 119 | +// 1. Derive addresses |
| 120 | +const sourceAta = getAtaAddressInterface(mint, sender); |
| 121 | +const recipientAta = getAtaAddressInterface(mint, recipient); |
| 122 | + |
| 123 | +// 2. Build load instructions (empty if already hot) |
| 124 | +const loadIxs = await createLoadAtaInstructions( |
| 125 | + rpc, |
| 126 | + payer, |
| 127 | + sourceAta, |
| 128 | + sender, |
| 129 | + mint, |
| 130 | +); |
| 131 | + |
| 132 | +// 3. Build transaction |
| 133 | +const tx = new Transaction().add( |
| 134 | + ...loadIxs, // Load sender if cold (wrap SPL/T22, decompress) |
| 135 | + createAtaInterfaceIdempotentInstruction( |
| 136 | + payer, |
| 137 | + recipientAta, |
| 138 | + recipient, |
| 139 | + mint, |
| 140 | + CTOKEN_PROGRAM_ID, |
| 141 | + ), |
| 142 | + createTransferInterfaceInstruction(sourceAta, recipientAta, sender, amount), |
| 143 | +); |
| 144 | +``` |
| 145 | + |
| 146 | +### CToken (sender pre-fetched) |
| 147 | + |
| 148 | +```typescript |
| 149 | +import { |
| 150 | + getAtaInterface, |
| 151 | + createLoadAtaInstructionsFromInterface, |
| 152 | + getAtaAddressInterface, |
| 153 | + createAtaInterfaceIdempotentInstruction, |
| 154 | + createTransferInterfaceInstruction, |
| 155 | + CTOKEN_PROGRAM_ID, |
| 156 | +} from '@lightprotocol/compressed-token'; |
| 157 | + |
| 158 | +// 1. Pre-fetch sender's unified balance |
| 159 | +const senderAtaInfo = await getAtaInterface(rpc, sender, mint); |
| 160 | + |
| 161 | +// 2. Build load instructions from interface (empty if already hot) |
| 162 | +const loadIxs = await createLoadAtaInstructionsFromInterface( |
| 163 | + rpc, |
| 164 | + payer, |
| 165 | + senderAtaInfo, |
| 166 | +); |
| 167 | + |
| 168 | +// 3. Derive addresses |
| 169 | +const sourceAta = getAtaAddressInterface(mint, sender); |
| 170 | +const recipientAta = getAtaAddressInterface(mint, recipient); |
| 171 | + |
| 172 | +// 4. Build transaction |
| 173 | +const tx = new Transaction().add( |
| 174 | + ...loadIxs, |
| 175 | + createAtaInterfaceIdempotentInstruction( |
| 176 | + payer, |
| 177 | + recipientAta, |
| 178 | + recipient, |
| 179 | + mint, |
| 180 | + CTOKEN_PROGRAM_ID, |
| 181 | + ), |
| 182 | + createTransferInterfaceInstruction(sourceAta, recipientAta, sender, amount), |
| 183 | +); |
| 184 | +``` |
| 185 | + |
| 186 | +--- |
| 187 | + |
| 188 | +## Instruction Mapping |
| 189 | + |
| 190 | +| SPL Token | CToken | |
| 191 | +| --------------------------------------------------- | ----------------------------------------------------------------- | |
| 192 | +| `getAssociatedTokenAddressSync` | `getAtaAddressInterface` | |
| 193 | +| `createAssociatedTokenAccountIdempotentInstruction` | `createAtaInterfaceIdempotentInstruction` | |
| 194 | +| `createTransferInstruction` | `createTransferInterfaceInstruction` | |
| 195 | +| N/A | `createLoadAtaInstructions` (fetch + build) | |
| 196 | +| N/A | `createLoadAtaInstructionsFromInterface` (build from pre-fetched) | |
| 197 | + |
| 198 | +--- |
| 199 | + |
| 200 | +## Key Differences |
| 201 | + |
| 202 | +| | SPL Token | CToken | |
| 203 | +| ------------------- | ---------------------- | --------------------------------------------- | |
| 204 | +| Recipient ATA | Create before transfer | Create before transfer | |
| 205 | +| Sender balance | Single ATA | Unified (cold + SPL + T22 + hot) | |
| 206 | +| Loading | N/A | `createLoadAtaInstructions` or auto in action | |
| 207 | +| `destination` param | ATA address | ATA address | |
| 208 | + |
| 209 | +--- |
| 210 | + |
| 211 | +## Common Patterns |
| 212 | + |
| 213 | +### Check if loading needed |
| 214 | + |
| 215 | +```typescript |
| 216 | +const ata = await getAtaInterface(rpc, owner, mint); |
| 217 | +if (ata.isCold) { |
| 218 | + // Need to include load instructions |
| 219 | +} |
| 220 | +``` |
| 221 | + |
| 222 | +### Get unified balance |
| 223 | + |
| 224 | +```typescript |
| 225 | +const ata = await getAtaInterface(rpc, owner, mint); |
| 226 | +const totalBalance = ata.parsed.amount; // All sources combined |
| 227 | +``` |
| 228 | + |
| 229 | +### Idempotent recipient ATA |
| 230 | + |
| 231 | +Always safe to include - no-op if exists: |
| 232 | + |
| 233 | +```typescript |
| 234 | +createAtaInterfaceIdempotentInstruction( |
| 235 | + payer, |
| 236 | + recipientAta, |
| 237 | + recipient, |
| 238 | + mint, |
| 239 | + CTOKEN_PROGRAM_ID, |
| 240 | +); |
| 241 | +``` |
0 commit comments