-
Notifications
You must be signed in to change notification settings - Fork 8
/
add.ts
59 lines (54 loc) · 1.78 KB
/
add.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
53
54
55
56
57
58
59
import { createMintToCheckedInstruction } from '@solana/spl-token';
import { Result, Try } from '~/suite-utils';
import { Pubkey, Secret } from '~/types/account';
import { TransactionBuilder } from '~/transaction-builder';
import { Account } from '~/account';
import { SplToken as Calculate } from './calculate-amount';
import { MintOptions } from '~/types/spl-token';
import { CommonStructure } from '~/types/transaction-builder';
export namespace SplToken {
/**
* Adding new token to existing token
*
* @param {Pubkey} token
* @param {Pubkey} owner
* @param {Secret[]} ownerOrMultisig
* @param {number} totalAmount
* @param {number} mintDecimal
* @param {Partial<MintOptions>} options
* @return Promise<Result<string, Error>>
*/
export const add = async (
token: Pubkey,
owner: Pubkey,
ownerOrMultisig: Secret[],
totalAmount: number,
mintDecimal: number,
options: Partial<MintOptions> = {},
): Promise<Result<CommonStructure<Pubkey>, Error>> => {
return Try(async () => {
const payer = options.feePayer ? options.feePayer : ownerOrMultisig[0];
const keypairs = ownerOrMultisig.map((s) => s.toKeypair());
const associated = await Account.Associated.makeOrCreateInstruction(
token,
owner,
payer,
);
const inst = createMintToCheckedInstruction(
token.toPublicKey(),
associated.tokenAccount.toPublicKey(),
owner.toPublicKey(),
Calculate.calculateAmount(totalAmount, mintDecimal),
mintDecimal,
keypairs,
);
const instructions = associated.inst ? [associated.inst, inst] : [inst];
return new TransactionBuilder.Common<Pubkey>(
instructions,
keypairs,
payer.toKeypair(),
token,
);
});
};
}