|
| 1 | +import { |
| 2 | + ConfirmOptions, |
| 3 | + PublicKey, |
| 4 | + Signer, |
| 5 | + TransactionSignature, |
| 6 | +} from '@solana/web3.js'; |
| 7 | +import { |
| 8 | + Rpc, |
| 9 | + buildAndSignTx, |
| 10 | + sendAndConfirmTx, |
| 11 | + dedupeSigner, |
| 12 | + assertBetaEnabled, |
| 13 | + LIGHT_TOKEN_PROGRAM_ID, |
| 14 | +} from '@lightprotocol/stateless.js'; |
| 15 | +import BN from 'bn.js'; |
| 16 | +import { |
| 17 | + createApproveInterfaceInstructions, |
| 18 | + createRevokeInterfaceInstructions, |
| 19 | +} from '../instructions/approve-interface'; |
| 20 | +import { getAssociatedTokenAddressInterface } from '../get-associated-token-address-interface'; |
| 21 | +import { getMintInterface } from '../get-mint-interface'; |
| 22 | +import { sliceLast } from './slice-last'; |
| 23 | +import type { InterfaceOptions } from './transfer-interface'; |
| 24 | +import { TOKEN_PROGRAM_ID, TOKEN_2022_PROGRAM_ID } from '@solana/spl-token'; |
| 25 | + |
| 26 | +/** |
| 27 | + * Approve a delegate for an associated token account. |
| 28 | + * |
| 29 | + * Supports light-token, SPL, and Token-2022 mints. For light-token mints, |
| 30 | + * loads cold accounts if needed before sending the approve instruction. |
| 31 | + * |
| 32 | + * @remarks For light-token mints, all cold (compressed) balances are loaded |
| 33 | + * into the hot ATA, not just the delegation amount. The `amount` parameter |
| 34 | + * only controls the delegate's spending limit. |
| 35 | + * |
| 36 | + * @param rpc RPC connection |
| 37 | + * @param payer Fee payer (signer) |
| 38 | + * @param tokenAccount ATA address |
| 39 | + * @param mint Mint address |
| 40 | + * @param delegate Delegate to approve |
| 41 | + * @param amount Amount to delegate |
| 42 | + * @param owner Owner of the token account (signer) |
| 43 | + * @param confirmOptions Optional confirm options |
| 44 | + * @param programId Token program ID (default: LIGHT_TOKEN_PROGRAM_ID) |
| 45 | + * @param wrap When true and mint is SPL/T22, wrap into light-token then approve |
| 46 | + * @returns Transaction signature |
| 47 | + */ |
| 48 | +export async function approveInterface( |
| 49 | + rpc: Rpc, |
| 50 | + payer: Signer, |
| 51 | + tokenAccount: PublicKey, |
| 52 | + mint: PublicKey, |
| 53 | + delegate: PublicKey, |
| 54 | + amount: number | bigint | BN, |
| 55 | + owner: Signer, |
| 56 | + confirmOptions?: ConfirmOptions, |
| 57 | + programId: PublicKey = LIGHT_TOKEN_PROGRAM_ID, |
| 58 | + wrap = false, |
| 59 | + options?: InterfaceOptions, |
| 60 | + decimals?: number, |
| 61 | +): Promise<TransactionSignature> { |
| 62 | + assertBetaEnabled(); |
| 63 | + |
| 64 | + const expectedAta = getAssociatedTokenAddressInterface( |
| 65 | + mint, |
| 66 | + owner.publicKey, |
| 67 | + false, |
| 68 | + programId, |
| 69 | + ); |
| 70 | + if (!tokenAccount.equals(expectedAta)) { |
| 71 | + throw new Error( |
| 72 | + `Token account mismatch. Expected ${expectedAta.toBase58()}, got ${tokenAccount.toBase58()}`, |
| 73 | + ); |
| 74 | + } |
| 75 | + |
| 76 | + const isSplOrT22 = |
| 77 | + programId.equals(TOKEN_PROGRAM_ID) || |
| 78 | + programId.equals(TOKEN_2022_PROGRAM_ID); |
| 79 | + const resolvedDecimals = |
| 80 | + decimals ?? |
| 81 | + (isSplOrT22 && !wrap |
| 82 | + ? 0 |
| 83 | + : (await getMintInterface(rpc, mint)).mint.decimals); |
| 84 | + const batches = await createApproveInterfaceInstructions( |
| 85 | + rpc, |
| 86 | + payer.publicKey, |
| 87 | + mint, |
| 88 | + tokenAccount, |
| 89 | + delegate, |
| 90 | + amount, |
| 91 | + owner.publicKey, |
| 92 | + resolvedDecimals, |
| 93 | + programId, |
| 94 | + wrap, |
| 95 | + options, |
| 96 | + ); |
| 97 | + |
| 98 | + const additionalSigners = dedupeSigner(payer, [owner]); |
| 99 | + const { rest: loads, last: approveIxs } = sliceLast(batches); |
| 100 | + |
| 101 | + await Promise.all( |
| 102 | + loads.map(async ixs => { |
| 103 | + const { blockhash } = await rpc.getLatestBlockhash(); |
| 104 | + const tx = buildAndSignTx(ixs, payer, blockhash, additionalSigners); |
| 105 | + return sendAndConfirmTx(rpc, tx, confirmOptions); |
| 106 | + }), |
| 107 | + ); |
| 108 | + |
| 109 | + const { blockhash } = await rpc.getLatestBlockhash(); |
| 110 | + const tx = buildAndSignTx(approveIxs, payer, blockhash, additionalSigners); |
| 111 | + return sendAndConfirmTx(rpc, tx, confirmOptions); |
| 112 | +} |
| 113 | + |
| 114 | +/** |
| 115 | + * Revoke delegation for an associated token account. |
| 116 | + * |
| 117 | + * Supports light-token, SPL, and Token-2022 mints. For light-token mints, |
| 118 | + * loads cold accounts if needed before sending the revoke instruction. |
| 119 | + * |
| 120 | + * @remarks For light-token mints, all cold (compressed) balances are loaded |
| 121 | + * into the hot ATA before the revoke instruction. |
| 122 | + * |
| 123 | + * @param rpc RPC connection |
| 124 | + * @param payer Fee payer (signer) |
| 125 | + * @param tokenAccount ATA address |
| 126 | + * @param mint Mint address |
| 127 | + * @param owner Owner of the token account (signer) |
| 128 | + * @param confirmOptions Optional confirm options |
| 129 | + * @param programId Token program ID (default: LIGHT_TOKEN_PROGRAM_ID) |
| 130 | + * @param wrap When true and mint is SPL/T22, wrap into light-token then revoke |
| 131 | + * @returns Transaction signature |
| 132 | + */ |
| 133 | +export async function revokeInterface( |
| 134 | + rpc: Rpc, |
| 135 | + payer: Signer, |
| 136 | + tokenAccount: PublicKey, |
| 137 | + mint: PublicKey, |
| 138 | + owner: Signer, |
| 139 | + confirmOptions?: ConfirmOptions, |
| 140 | + programId: PublicKey = LIGHT_TOKEN_PROGRAM_ID, |
| 141 | + wrap = false, |
| 142 | + options?: InterfaceOptions, |
| 143 | + decimals?: number, |
| 144 | +): Promise<TransactionSignature> { |
| 145 | + assertBetaEnabled(); |
| 146 | + |
| 147 | + const expectedAta = getAssociatedTokenAddressInterface( |
| 148 | + mint, |
| 149 | + owner.publicKey, |
| 150 | + false, |
| 151 | + programId, |
| 152 | + ); |
| 153 | + if (!tokenAccount.equals(expectedAta)) { |
| 154 | + throw new Error( |
| 155 | + `Token account mismatch. Expected ${expectedAta.toBase58()}, got ${tokenAccount.toBase58()}`, |
| 156 | + ); |
| 157 | + } |
| 158 | + |
| 159 | + const isSplOrT22 = |
| 160 | + programId.equals(TOKEN_PROGRAM_ID) || |
| 161 | + programId.equals(TOKEN_2022_PROGRAM_ID); |
| 162 | + const resolvedDecimals = |
| 163 | + decimals ?? |
| 164 | + (isSplOrT22 && !wrap |
| 165 | + ? 0 |
| 166 | + : (await getMintInterface(rpc, mint)).mint.decimals); |
| 167 | + const batches = await createRevokeInterfaceInstructions( |
| 168 | + rpc, |
| 169 | + payer.publicKey, |
| 170 | + mint, |
| 171 | + tokenAccount, |
| 172 | + owner.publicKey, |
| 173 | + resolvedDecimals, |
| 174 | + programId, |
| 175 | + wrap, |
| 176 | + options, |
| 177 | + ); |
| 178 | + |
| 179 | + const additionalSigners = dedupeSigner(payer, [owner]); |
| 180 | + const { rest: loads, last: revokeIxs } = sliceLast(batches); |
| 181 | + |
| 182 | + await Promise.all( |
| 183 | + loads.map(async ixs => { |
| 184 | + const { blockhash } = await rpc.getLatestBlockhash(); |
| 185 | + const tx = buildAndSignTx(ixs, payer, blockhash, additionalSigners); |
| 186 | + return sendAndConfirmTx(rpc, tx, confirmOptions); |
| 187 | + }), |
| 188 | + ); |
| 189 | + |
| 190 | + const { blockhash } = await rpc.getLatestBlockhash(); |
| 191 | + const tx = buildAndSignTx(revokeIxs, payer, blockhash, additionalSigners); |
| 192 | + return sendAndConfirmTx(rpc, tx, confirmOptions); |
| 193 | +} |
0 commit comments