-
Notifications
You must be signed in to change notification settings - Fork 17
/
mint_utils.ts
84 lines (75 loc) · 1.88 KB
/
mint_utils.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
import * as splToken from "@solana/spl-token";
import { PublicKey, Connection, Keypair } from "@solana/web3.js";
export interface TokenData {
mint: PublicKey;
startingPrice: number;
nbDecimals: number;
priceOracle: Keypair | undefined;
}
export class MintUtils {
private conn: Connection;
private authority: Keypair;
constructor(conn: Connection, authority: Keypair) {
this.conn = conn;
this.authority = authority;
}
async createMint(nb_decimals = 6): Promise<PublicKey> {
const kp = Keypair.generate();
return await splToken.createMint(
this.conn,
this.authority,
this.authority.publicKey,
this.authority.publicKey,
nb_decimals,
kp
);
}
public async createMints(nbMints: number): Promise<PublicKey[]> {
return await Promise.all(
Array.from(Array(nbMints).keys()).map((_) => {
return this.createMint();
})
);
}
public async createNewToken(nbDecimals = 6, startingPrice = 1_000_000) {
const mint = await this.createMint(nbDecimals);
const tokenData: TokenData = {
mint: mint,
startingPrice: startingPrice,
nbDecimals: nbDecimals,
priceOracle: undefined,
};
return tokenData;
}
public async createTokenAccount(
mint: PublicKey,
payer: Keypair,
owner: PublicKey
) {
const account = Keypair.generate();
return splToken.createAccount(this.conn, payer, mint, owner, account);
}
public async getOrCreateTokenAccount(
mint: PublicKey,
payer: Keypair,
owner: PublicKey
) {
return await splToken.getOrCreateAssociatedTokenAccount(
this.conn,
payer,
mint,
owner,
false
);
}
public async mintTo(mint: PublicKey, tokenAccount: PublicKey) {
await splToken.mintTo(
this.conn,
this.authority,
mint,
tokenAccount,
this.authority,
1000000000000
);
}
}