-
Notifications
You must be signed in to change notification settings - Fork 0
/
create-token-account.ts
42 lines (32 loc) · 1.36 KB
/
create-token-account.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
import "dotenv/config";
import {
getExplorerLink,
getKeypairFromEnvironment,
} from "@solana-developers/helpers";
import { Connection, PublicKey, clusterApiUrl } from "@solana/web3.js";
const connection = new Connection(clusterApiUrl("devnet"));
const user = getKeypairFromEnvironment("SECRET_KEY");
console.log(
`🔑 Loaded our keypair securely, using an env file! Our public key is: ${user.publicKey.toBase58()}`
);
// token mint account from create-token-mint.ts
const tokenMintAccount = new PublicKey(
"3SYu9RpKyuo4tQxpejkPR1ZSk1uyKN7fyHLxBLJJHUPt"
);
// making an associated token account for address,
// make an ATA on any other wallet in devnet!
// const recipient = new PublicKey("SOMEONE_ELSES_DEVNET_ADDRESS");
const recipient = user.publicKey;
import("@solana/spl-token").then(({ getOrCreateAssociatedTokenAccount }) => {
async function fetchAndLogTokenAccount() {
try {
const tokenAccount = await getOrCreateAssociatedTokenAccount(connection, user, tokenMintAccount, recipient);
console.log(`Token Account: ${tokenAccount.address.toBase58()}`);
const link = await getExplorerLink("address", tokenAccount.address.toBase58(), "devnet");
console.log(`✅ Created token Account: ${link}`);
} catch (error) {
console.error('Failed to fetch or create token account:', error);
}
}
fetchAndLogTokenAccount();
});