|
| 1 | +import * as assert from "assert"; |
| 2 | +import BN from "bn.js"; |
| 3 | +import { |
| 4 | + setupTestEnvironment, |
| 5 | + createTestMintWithTokens, |
| 6 | + getAta, |
| 7 | + waitForIndexer, |
| 8 | + Rpc, |
| 9 | + web3, |
| 10 | + anchor, |
| 11 | +} from "./shared"; |
| 12 | + |
| 13 | +describe("approve", () => { |
| 14 | + let rpc: Rpc; |
| 15 | + let payer: web3.Keypair; |
| 16 | + let provider: anchor.AnchorProvider; |
| 17 | + |
| 18 | + before(async () => { |
| 19 | + const env = await setupTestEnvironment(); |
| 20 | + rpc = env.rpc; |
| 21 | + payer = env.payer; |
| 22 | + provider = env.provider; |
| 23 | + }); |
| 24 | + |
| 25 | + it("approves a delegate via anchor program", async () => { |
| 26 | + const tokenAmount = 1_000_000; |
| 27 | + const approveAmount = new BN(500_000); |
| 28 | + |
| 29 | + // 1. Setup: Create mint with tokens |
| 30 | + const { mint } = await createTestMintWithTokens( |
| 31 | + rpc, |
| 32 | + payer, |
| 33 | + payer.publicKey, |
| 34 | + tokenAmount |
| 35 | + ); |
| 36 | + |
| 37 | + // 2. Get the token account (ATA) |
| 38 | + const tokenAccount = getAta(payer.publicKey, mint); |
| 39 | + |
| 40 | + // 3. Create a delegate keypair |
| 41 | + const delegate = web3.Keypair.generate(); |
| 42 | + |
| 43 | + // 4. Get the anchor program |
| 44 | + const program = anchor.workspace.LightTokenAnchorApprove; |
| 45 | + |
| 46 | + // 5. Call the anchor program's approve instruction |
| 47 | + const tx = await program.methods |
| 48 | + .approve(approveAmount) |
| 49 | + .accounts({ |
| 50 | + tokenAccount: tokenAccount, |
| 51 | + delegate: delegate.publicKey, |
| 52 | + owner: payer.publicKey, |
| 53 | + systemProgram: web3.SystemProgram.programId, |
| 54 | + }) |
| 55 | + .signers([payer]) |
| 56 | + .rpc({ commitment: "confirmed" }); |
| 57 | + |
| 58 | + console.log("Approve transaction:", tx); |
| 59 | + |
| 60 | + // 6. Wait for indexer to process |
| 61 | + await waitForIndexer(); |
| 62 | + |
| 63 | + // 7. Verify delegation by checking delegate's access |
| 64 | + const delegatedAccounts = await rpc.getCompressedTokenAccountsByDelegate( |
| 65 | + delegate.publicKey, |
| 66 | + { mint } |
| 67 | + ); |
| 68 | + |
| 69 | + assert.ok( |
| 70 | + delegatedAccounts.items.length > 0, |
| 71 | + "Delegate should have access to delegated accounts" |
| 72 | + ); |
| 73 | + |
| 74 | + console.log(`Successfully approved ${approveAmount.toString()} tokens to delegate ${delegate.publicKey.toBase58()}`); |
| 75 | + }); |
| 76 | + |
| 77 | + it("approves full balance to delegate", async () => { |
| 78 | + const tokenAmount = 750_000; |
| 79 | + const approveAmount = new BN(tokenAmount); |
| 80 | + |
| 81 | + // Setup |
| 82 | + const { mint } = await createTestMintWithTokens( |
| 83 | + rpc, |
| 84 | + payer, |
| 85 | + payer.publicKey, |
| 86 | + tokenAmount |
| 87 | + ); |
| 88 | + |
| 89 | + const tokenAccount = getAta(payer.publicKey, mint); |
| 90 | + const delegate = web3.Keypair.generate(); |
| 91 | + const program = anchor.workspace.LightTokenAnchorApprove; |
| 92 | + |
| 93 | + // Approve full balance |
| 94 | + await program.methods |
| 95 | + .approve(approveAmount) |
| 96 | + .accounts({ |
| 97 | + tokenAccount: tokenAccount, |
| 98 | + delegate: delegate.publicKey, |
| 99 | + owner: payer.publicKey, |
| 100 | + systemProgram: web3.SystemProgram.programId, |
| 101 | + }) |
| 102 | + .signers([payer]) |
| 103 | + .rpc({ commitment: "confirmed" }); |
| 104 | + |
| 105 | + await waitForIndexer(); |
| 106 | + |
| 107 | + // Verify |
| 108 | + const delegatedAccounts = await rpc.getCompressedTokenAccountsByDelegate( |
| 109 | + delegate.publicKey, |
| 110 | + { mint } |
| 111 | + ); |
| 112 | + |
| 113 | + assert.ok( |
| 114 | + delegatedAccounts.items.length > 0, |
| 115 | + "Delegate should have access to full balance" |
| 116 | + ); |
| 117 | + |
| 118 | + console.log("Successfully approved full balance to delegate"); |
| 119 | + }); |
| 120 | +}); |
0 commit comments