-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathmint.ts
103 lines (93 loc) · 3.02 KB
/
mint.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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
import { Keypair, Transaction, TransactionInstruction } from '@solana/web3.js';
import { debugLog, Result, Try } from '~/suite-utils';
import { Node } from '~/node';
import { Pubkey } from '~/types/account';
import { Storage } from '~/suite-storage';
import { SplToken } from '~/suite-spl-token';
import { PhantomProvider } from '~/types/phantom';
import { InputTokenMetadata } from '~/types/spl-token';
import { InputNftMetadata } from '~/types/regular-nft';
import { Converter } from '~/converter';
export namespace PhantomSplToken {
/**
* Mint new spl-token
*
* @param {InputNftMetadata} input
* @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 mint = async (
input: InputTokenMetadata,
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 mint = Keypair.generate();
input.royalty = 0;
const sellerFeeBasisPoints = 0;
const tokenStorageMetadata = Storage.toConvertOffchaindata(
input as InputNftMetadata,
input.royalty,
);
let uri!: string;
if (input.filePath && input.storageType) {
const uploaded = await Storage.upload(
tokenStorageMetadata,
input.filePath,
input.storageType,
);
if (uploaded.isErr) {
throw uploaded;
}
uri = uploaded.value;
} else if (input.uri) {
uri = input.uri;
} else {
throw Error(`Must set 'storageType + filePath' or 'uri'`);
}
const isMutable = true;
const datav2 = Converter.TokenMetadata.intoInfra(
input,
uri,
sellerFeeBasisPoints,
);
debugLog('# datav2: ', datav2);
debugLog('# upload content url: ', uri);
const insturctions = await SplToken.createMint(
mint.publicKey,
owner.toPublicKey(),
totalAmount,
mintDecimal,
datav2,
owner.toPublicKey(),
isMutable,
);
insturctions.forEach((inst: TransactionInstruction) =>
transaction.add(inst),
);
transaction.feePayer = owner.toPublicKey();
const blockhashObj = await connection.getLatestBlockhashAndContext();
transaction.recentBlockhash = blockhashObj.value.blockhash;
transaction.partialSign(mint);
const signed = await phantom.signTransaction(transaction);
debugLog(
'# signed, signed.signatures: ',
signed,
signed.signatures.map((sig) => sig.publicKey.toString()),
);
const sig = await connection.sendRawTransaction(signed.serialize());
await Node.confirmedSig(sig);
return mint.publicKey.toString();
});
};
}