-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathburn.ts
52 lines (49 loc) · 1.58 KB
/
burn.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
import {
createBurnCheckedInstruction,
getAssociatedTokenAddressSync,
} from '@solana/spl-token';
import { Pubkey, Secret } from '~/types/account';
import { TransactionBuilder } from '~/transaction-builder';
import { Result, Try } from '~/suite-utils';
import { SplToken as Calculate } from './calculate-amount';
import { BurnOptions } from '~/types/spl-token';
import { CommonStructure } from '~/types/transaction-builder';
export namespace SplToken {
/**
* Burn existing token
*
* @param {Pubkey} mint
* @param {Pubkey} owner
* @param {Secret[]} ownerOrMultisig
* @param {number} burnAmount
* @param {number} tokenDecimals
* @param {Partial<BurnOptions>} options
* @return Result<CommonStructure, Error>>
*/
export const burn = (
mint: Pubkey,
owner: Pubkey,
ownerOrMultisig: Secret[],
burnAmount: number,
tokenDecimals: number,
options: Partial<BurnOptions> = {},
): Result<CommonStructure, Error> => {
return Try(() => {
const tokenAccount = getAssociatedTokenAddressSync(
mint.toPublicKey(),
owner.toPublicKey(),
);
const payer = options.feePayer ? options.feePayer : ownerOrMultisig[0];
const keypairs = ownerOrMultisig.map((s) => s.toKeypair());
const inst = createBurnCheckedInstruction(
tokenAccount,
mint.toPublicKey(),
owner.toPublicKey(),
Calculate.calculateAmount(burnAmount, tokenDecimals),
tokenDecimals,
keypairs,
);
return new TransactionBuilder.Common([inst], keypairs, payer.toKeypair());
});
};
}