|
| 1 | +import chai, { expect } from 'chai'; |
| 2 | +import chaiAsPromised from 'chai-as-promised'; |
| 3 | +chai.use(chaiAsPromised); |
| 4 | + |
| 5 | +import type { Connection, PublicKey, Signer } from '@solana/web3.js'; |
| 6 | +import { sendAndConfirmTransaction, Keypair, SystemProgram, Transaction } from '@solana/web3.js'; |
| 7 | +import { |
| 8 | + createAccount, |
| 9 | + createInitializeMintInstruction, |
| 10 | + mintTo, |
| 11 | + getMintLen, |
| 12 | + ExtensionType, |
| 13 | + createInitializePermanentDelegateInstruction, |
| 14 | + burn, |
| 15 | + transferChecked, |
| 16 | +} from '../../src'; |
| 17 | +import { TEST_PROGRAM_ID, newAccountWithLamports, getConnection } from '../common'; |
| 18 | + |
| 19 | +const TEST_TOKEN_DECIMALS = 0; |
| 20 | +const EXTENSIONS = [ExtensionType.PermanentDelegate]; |
| 21 | +describe('permanentDelegate', () => { |
| 22 | + let connection: Connection; |
| 23 | + let payer: Signer; |
| 24 | + let mint: PublicKey; |
| 25 | + let mintAuthority: Keypair; |
| 26 | + let permanentDelegate: Keypair; |
| 27 | + let account: PublicKey; |
| 28 | + let destination: PublicKey; |
| 29 | + before(async () => { |
| 30 | + connection = await getConnection(); |
| 31 | + payer = await newAccountWithLamports(connection, 1000000000); |
| 32 | + mintAuthority = Keypair.generate(); |
| 33 | + permanentDelegate = Keypair.generate(); |
| 34 | + }); |
| 35 | + beforeEach(async () => { |
| 36 | + const mintKeypair = Keypair.generate(); |
| 37 | + mint = mintKeypair.publicKey; |
| 38 | + const mintLen = getMintLen(EXTENSIONS); |
| 39 | + const lamports = await connection.getMinimumBalanceForRentExemption(mintLen); |
| 40 | + const transaction = new Transaction().add( |
| 41 | + SystemProgram.createAccount({ |
| 42 | + fromPubkey: payer.publicKey, |
| 43 | + newAccountPubkey: mint, |
| 44 | + space: mintLen, |
| 45 | + lamports, |
| 46 | + programId: TEST_PROGRAM_ID, |
| 47 | + }), |
| 48 | + createInitializePermanentDelegateInstruction(mint, permanentDelegate.publicKey, TEST_PROGRAM_ID), |
| 49 | + createInitializeMintInstruction(mint, TEST_TOKEN_DECIMALS, mintAuthority.publicKey, null, TEST_PROGRAM_ID) |
| 50 | + ); |
| 51 | + |
| 52 | + await sendAndConfirmTransaction(connection, transaction, [payer, mintKeypair], undefined); |
| 53 | + }); |
| 54 | + it('burn tokens ', async () => { |
| 55 | + const owner = Keypair.generate(); |
| 56 | + account = await createAccount(connection, payer, mint, owner.publicKey, undefined, undefined, TEST_PROGRAM_ID); |
| 57 | + await mintTo(connection, payer, mint, account, mintAuthority, 5, [], undefined, TEST_PROGRAM_ID); |
| 58 | + await burn(connection, payer, account, mint, permanentDelegate, 2, undefined, undefined, TEST_PROGRAM_ID); |
| 59 | + const info = await connection.getTokenAccountBalance(account); |
| 60 | + expect(info).to.not.be.null; |
| 61 | + if (info !== null) { |
| 62 | + expect(info.value.uiAmount).to.eql(3); |
| 63 | + } |
| 64 | + }); |
| 65 | + it('transfer tokens', async () => { |
| 66 | + const owner1 = Keypair.generate(); |
| 67 | + const owner2 = Keypair.generate(); |
| 68 | + destination = await createAccount( |
| 69 | + connection, |
| 70 | + payer, |
| 71 | + mint, |
| 72 | + owner2.publicKey, |
| 73 | + undefined, |
| 74 | + undefined, |
| 75 | + TEST_PROGRAM_ID |
| 76 | + ); |
| 77 | + account = await createAccount(connection, payer, mint, owner1.publicKey, undefined, undefined, TEST_PROGRAM_ID); |
| 78 | + await mintTo(connection, payer, mint, account, mintAuthority, 5, [], undefined, TEST_PROGRAM_ID); |
| 79 | + await transferChecked( |
| 80 | + connection, |
| 81 | + payer, |
| 82 | + account, |
| 83 | + mint, |
| 84 | + destination, |
| 85 | + permanentDelegate, |
| 86 | + 2, |
| 87 | + 0, |
| 88 | + undefined, |
| 89 | + undefined, |
| 90 | + TEST_PROGRAM_ID |
| 91 | + ); |
| 92 | + const source_info = await connection.getTokenAccountBalance(account); |
| 93 | + const destination_info = await connection.getTokenAccountBalance(destination); |
| 94 | + expect(source_info).to.not.be.null; |
| 95 | + expect(destination_info).to.not.be.null; |
| 96 | + if (source_info !== null) { |
| 97 | + expect(source_info.value.uiAmount).to.eql(3); |
| 98 | + } |
| 99 | + if (destination_info !== null) { |
| 100 | + expect(destination_info.value.uiAmount).to.eql(2); |
| 101 | + } |
| 102 | + }); |
| 103 | +}); |
0 commit comments