Skip to content

Commit

Permalink
feat: add burn token action (#72)
Browse files Browse the repository at this point in the history
* feat: add burn token action

* refactor: fix lint issues

* feat: add burn token to public api

* feat: add close instruction to remove empty account

* test: update timeout for the failing test

* test: increase timeouts for particular tests
  • Loading branch information
kurpav authored Nov 23, 2021
1 parent 02e7cb3 commit 3bd2381
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 5 deletions.
58 changes: 58 additions & 0 deletions src/actions/burnToken.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
import { PublicKey } from '@solana/web3.js';
import { Wallet } from '../wallet';
import { Connection } from '../Connection';
import { sendTransaction } from './transactions';
import { Transaction } from '../Transaction';
import { Token, TOKEN_PROGRAM_ID, u64 } from '@solana/spl-token';

interface IBurnTokenParams {
connection: Connection;
wallet: Wallet;
token: PublicKey;
mint: PublicKey;
amount: number | u64;
owner?: PublicKey;
// close token account after
close?: boolean;
}

interface IBurnTokenResponse {
txId: string;
}

export const burnToken = async ({
connection,
wallet,
token,
mint,
amount,
owner,
close = true,
}: IBurnTokenParams): Promise<IBurnTokenResponse> => {
const tx = new Transaction({ feePayer: wallet.publicKey }).add(
Token.createBurnInstruction(
TOKEN_PROGRAM_ID,
mint,
token,
owner ?? wallet.publicKey,
[],
amount,
),
);

if (close) {
tx.add(
Token.createCloseAccountInstruction(
TOKEN_PROGRAM_ID,
token,
wallet.publicKey,
owner ?? wallet.publicKey,
[],
),
);
}

const txId = await sendTransaction({ connection, wallet, txs: [tx] });

return { txId };
};
1 change: 1 addition & 0 deletions src/actions/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,4 @@ export * from './placeBid';
export * from './redeemBid';
export * from './claimBid';
export * from './instantSale';
export * from './burnToken';
4 changes: 1 addition & 3 deletions test/actions/createMetadataAndME.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@ import { createMasterEdition } from '../../src/actions/createMasterEdition';
import BN from 'bn.js';
import { uri } from './shared';

jest.setTimeout(150000); //this one takes particularly long

// NOTE: testing the two together because latter effectively requires former
describe('creatomg metadata and master edition PDAs', () => {
const connection = new Connection('devnet');
Expand Down Expand Up @@ -74,5 +72,5 @@ describe('creatomg metadata and master edition PDAs', () => {
const editionInfo = await Account.getInfo(connection, edition);
const deserializedEditionData = new MasterEdition(edition, editionInfo).data;
expect(deserializedEditionData.maxSupply.toString(10)).toEqual('100');
});
}, 150000);
});
4 changes: 2 additions & 2 deletions test/metadata.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ describe('Metadata', () => {
});

expect(metadata[0].data.key).toBe(MetadataKey.MetadataV1);
});
}, 10000);
});

describe('Master edition', () => {
Expand All @@ -32,6 +32,6 @@ describe('Metadata', () => {
const editions = await masterEdition.getEditions(connection);

expect(editions[0].data.parent).toEqual(MASTER_EDITION_PUBKEY.toString());
});
}, 10000);
});
});

0 comments on commit 3bd2381

Please sign in to comment.