forked from solana-labs/solana-program-library
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcreateMintAndTransferTokens.ts
58 lines (49 loc) · 1.99 KB
/
createMintAndTransferTokens.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
import { clusterApiUrl, Connection, Keypair, LAMPORTS_PER_SOL } from '@solana/web3.js';
import { createMint, getOrCreateAssociatedTokenAccount, mintTo, transfer } from '../src'; // @FIXME: replace with @solana/spl-token
(async () => {
// Connect to cluster
const connection = new Connection(clusterApiUrl('devnet'), 'confirmed');
// Generate a new wallet keypair and airdrop SOL
const fromWallet = Keypair.generate();
const fromAirdropSignature = await connection.requestAirdrop(fromWallet.publicKey, LAMPORTS_PER_SOL);
// Wait for airdrop confirmation
await connection.confirmTransaction({
signature: fromAirdropSignature,
...(await connection.getLatestBlockhash()),
});
// Generate a new wallet to receive newly minted token
const toWallet = Keypair.generate();
// Create new token mint
const mint = await createMint(connection, fromWallet, fromWallet.publicKey, null, 9);
// Get the token account of the fromWallet address, and if it does not exist, create it
const fromTokenAccount = await getOrCreateAssociatedTokenAccount(
connection,
fromWallet,
mint,
fromWallet.publicKey,
);
// Get the token account of the toWallet address, and if it does not exist, create it
const toTokenAccount = await getOrCreateAssociatedTokenAccount(connection, fromWallet, mint, toWallet.publicKey);
// Mint 1 new token to the "fromTokenAccount" account we just created
let signature = await mintTo(
connection,
fromWallet,
mint,
fromTokenAccount.address,
fromWallet.publicKey,
1000000000,
[],
);
console.log('mint tx:', signature);
// Transfer the new token to the "toTokenAccount" we just created
signature = await transfer(
connection,
fromWallet,
fromTokenAccount.address,
toTokenAccount.address,
fromWallet.publicKey,
1000000000,
[],
);
console.log('transfer tx:', signature);
})();