forked from step-finance/reward-pool
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathutils.js
131 lines (119 loc) · 3.16 KB
/
utils.js
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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
const anchor = require("@project-serum/anchor");
const TokenInstructions = require("@project-serum/serum").TokenInstructions;
const { TOKEN_PROGRAM_ID, Token, MintLayout } = require("@solana/spl-token");
async function initializeProgram(program, provider, authMintPubkey) {
const [ _configPubkey, _nonce] = await anchor.web3.PublicKey.findProgramAddress([Buffer.from("config")], program.programId);
configPubkey = _configPubkey;
let nonce = _nonce;
await program.rpc.initializeProgram(
nonce,
authMintPubkey,
{
accounts: {
config: configPubkey,
payer: provider.wallet.publicKey,
systemProgram: anchor.web3.SystemProgram.programId,
},
}
)
}
async function createMint(provider, decimals) {
const mint = await Token.createMint(
provider.connection,
provider.wallet.payer,
provider.wallet.publicKey,
null,
decimals,
TOKEN_PROGRAM_ID
);
return mint;
}
async function createMintAndVault(provider, vaultOwner, decimals) {
const mint = await createMint(provider);
const vault = await mint.createAccount(vaultOwner ? vaultOwner : provider.wallet.publicKey);
return [mint, vault];
}
async function createMintFromPriv(
mintAccount,
provider,
mintAuthority,
freezeAuthority,
decimals,
programId,
) {
const token = new Token(
provider.connection,
mintAccount.publicKey,
programId,
provider.wallet.payer,
);
// Allocate memory for the account
const balanceNeeded = await Token.getMinBalanceRentForExemptMint(
provider.connection,
);
const transaction = new anchor.web3.Transaction();
transaction.add(
anchor.web3.SystemProgram.createAccount({
fromPubkey: provider.wallet.payer.publicKey,
newAccountPubkey: mintAccount.publicKey,
lamports: balanceNeeded,
space: MintLayout.span,
programId,
}),
);
transaction.add(
Token.createInitMintInstruction(
programId,
mintAccount.publicKey,
decimals,
mintAuthority,
freezeAuthority,
),
);
await provider.send(transaction, [mintAccount]);
return token;
}
async function mintToAccount(
provider,
mint,
destination,
amount
) {
const tx = new anchor.web3.Transaction();
tx.add(
Token.createMintToInstruction(
TOKEN_PROGRAM_ID,
mint,
destination,
provider.wallet.publicKey,
[],
amount
)
);
await provider.send(tx);
}
async function sendLamports(
provider,
destination,
amount
) {
const tx = new anchor.web3.Transaction();
tx.add(
anchor.web3.SystemProgram.transfer(
{
fromPubkey: provider.wallet.publicKey,
lamports: amount,
toPubkey: destination
}
)
);
await provider.send(tx);
}
module.exports = {
mintToAccount,
createMintAndVault,
createMintFromPriv,
createMint,
sendLamports,
initializeProgram,
};