-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
85 lines (71 loc) · 2.09 KB
/
index.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
const web3 = require('@solana/web3.js')
const splToken = require('@solana/spl-token')
async function main() {
// connect to cluster
const connection = new web3.Connection(
web3.clusterApiUrl('devnet'),
'confirmed'
)
// generate a new wallet keypair
const fromWallet = web3.Keypair.generate()
// airdrop 1 SOL
const fromAirdropSignature = await connection.requestAirdrop(
fromWallet.publicKey,
web3.LAMPORTS_PER_SOL
)
// wait for airdrop confirmation
await connection.confirmTransaction(fromAirdropSignature)
// create new token mint
const mint = await splToken.Token.createMint(
connection,
fromWallet,
fromWallet.publicKey,
null,
9,
splToken.TOKEN_PROGRAM_ID
)
// get the token amount of the fromWallet solana address, if it does not exist, create it
const fromTokenAccount = await mint.getOrCreateAssociatedAccountInfo(
fromWallet.publicKey
)
// generate a new wallet to receive newly minted token
const toWallet = web3.Keypair.generate()
// get the token account of the toWallet solana address, if it does not exist, create it
const toTokenAccount = await mint.getOrCreateAssociatedAccountInfo(
toWallet.publicKey
)
// minting a new token to the fromTokenAccount we just returned / created
await mint.mintTo(
fromTokenAccount.address, // who it goes to
fromWallet.publicKey, // minting authority
[], // multisig
1000000000 // how many
)
await mint.setAuthority(
mint.publicKey,
null,
"MintTokens",
fromWallet.publicKey,
[]
)
// add token transfer instructions to transaction
const transaction = new web3.Transaction().add(
splToken.Token.createTransferInstruction(
splToken.TOKEN_PROGRAM_ID,
fromTokenAccount.address,
toTokenAccount.address,
fromWallet.publicKey,
[],
1
)
)
// sign transaction, broardcast, and confirm
const signature = await web3.sendAndConfirmTransaction(
connection,
transaction,
[fromWallet],
{commitment: 'confirmed'}
)
console.log(signature)
}
main()