-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathadd.ts
70 lines (63 loc) · 2 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
60
61
62
63
64
65
66
67
68
69
70
import {
createMintToCheckedInstruction,
TOKEN_PROGRAM_ID,
} from '@solana/spl-token';
import { Transaction, TransactionInstruction } from '@solana/web3.js';
import { Result, Try } from '~/suite-utils';
import { Node } from '~/node';
import { Pubkey } from '~/types/account';
import { Account } from '~/account';
import { PhantomProvider } from '~/types/phantom';
export namespace PhantomSplToken {
/**
* Adding new token to existing token
*
* @param {Pubkey} token
* @param {Pubkey} owner
* @param {string} cluster
* @param {number} totalAmount
* @param {number} mintDecimal
* @param {Phantom} phantom //phantom wallet object
* @return Promise<Result<string, Error>>
*/
export const add = async (
token: Pubkey,
owner: Pubkey,
cluster: string,
totalAmount: number,
mintDecimal: number,
phantom: PhantomProvider,
): Promise<Result<string, Error>> => {
return Try(async () => {
Node.changeConnection({ cluster });
const connection = Node.getConnection();
const transaction = new Transaction();
const makeInstruction = await Account.Associated.makeOrCreateInstruction(
token,
owner,
);
transaction.add(makeInstruction.inst as TransactionInstruction);
transaction.add(
createMintToCheckedInstruction(
token.toPublicKey(),
makeInstruction.tokenAccount.toPublicKey(),
owner.toPublicKey(),
totalAmount,
mintDecimal,
[],
TOKEN_PROGRAM_ID,
),
);
transaction.feePayer = owner.toPublicKey();
const blockhashObj = await connection.getLatestBlockhashAndContext();
transaction.recentBlockhash = blockhashObj.value.blockhash;
const signed = await phantom.signAllTransactions([transaction]);
// TODO: refactoring
for (const sign of signed) {
const sig = await connection.sendRawTransaction(sign.serialize());
await Node.confirmedSig(sig);
}
return token;
});
};
}