Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add burn token action #72

Merged
merged 6 commits into from
Nov 23, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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);
austbot marked this conversation as resolved.
Show resolved Hide resolved
});
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);
});
});