-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcomparison-web3js.ts
128 lines (107 loc) · 4.01 KB
/
comparison-web3js.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
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
import {
airdropFactory,
appendTransactionMessageInstructions,
createSolanaRpc,
createSolanaRpcSubscriptions,
createTransactionMessage,
generateKeyPairSigner,
getSignatureFromTransaction,
lamports,
pipe,
sendAndConfirmTransactionFactory,
setTransactionMessageFeePayerSigner,
setTransactionMessageLifetimeUsingBlockhash,
signTransactionMessageWithSigners,
some,
} from "@solana/kit";
import { getCreateAccountInstruction } from "@solana-program/system";
import {
getInitializeMintInstruction,
getMintSize,
TOKEN_2022_PROGRAM_ADDRESS,
extension,
getInitializeMetadataPointerInstruction,
getInitializeTokenMetadataInstruction,
tokenMetadataField,
getUpdateTokenMetadataFieldInstruction,
} from "@solana-program/token-2022";
const rpc = createSolanaRpc("http://127.0.0.1:8899");
const rpcSubscriptions = createSolanaRpcSubscriptions("ws://127.0.0.1:8900");
const feePayer = await generateKeyPairSigner();
console.log(feePayer.address);
const mint = await generateKeyPairSigner();
await airdropFactory({ rpc, rpcSubscriptions })({
recipientAddress: feePayer.address,
lamports: lamports(1_000_000_000n),
commitment: "confirmed",
});
const balance = await rpc.getBalance(feePayer.address).send();
console.log("balance:", balance.value);
const metadataPointerExtension = extension("MetadataPointer", {
authority: some(feePayer.address),
metadataAddress: some(mint.address),
});
const tokenMetadataExtension = extension("TokenMetadata", {
updateAuthority: some(feePayer.address),
mint: mint.address,
name: "OPOS",
symbol: "OPOS",
uri: "https://raw.githubusercontent.com/solana-developers/opos-asset/main/assets/DeveloperPortal/metadata.json",
additionalMetadata: new Map<string, string>([["description", "Only Possible On Solana"]]),
});
const spaceWithoutMetadata = BigInt(getMintSize([metadataPointerExtension]));
const spaceWithMetadata = BigInt(getMintSize([metadataPointerExtension, tokenMetadataExtension]));
const rent = await rpc.getMinimumBalanceForRentExemption(spaceWithMetadata).send();
const createAccountInstruction = getCreateAccountInstruction({
payer: feePayer,
newAccount: mint,
lamports: rent,
space: spaceWithoutMetadata,
programAddress: TOKEN_2022_PROGRAM_ADDRESS,
});
const initializeMetadataPointerInstruction = getInitializeMetadataPointerInstruction({
mint: mint.address,
authority: feePayer.address,
metadataAddress: mint.address,
});
const initializeMintInstruction = getInitializeMintInstruction({
mint: mint.address,
decimals: 2,
mintAuthority: feePayer.address,
});
const initializeTokenMetadataInstruction = getInitializeTokenMetadataInstruction({
metadata: mint.address,
updateAuthority: feePayer.address,
mint: mint.address,
mintAuthority: feePayer,
name: tokenMetadataExtension.name,
symbol: tokenMetadataExtension.symbol,
uri: tokenMetadataExtension.uri,
});
const updateTokenMetadataInstruction = getUpdateTokenMetadataFieldInstruction({
metadata: mint.address,
updateAuthority: feePayer,
field: tokenMetadataField("Key", ["description"]),
value: "Only Possible On Solana",
});
const instructions = [
createAccountInstruction,
initializeMetadataPointerInstruction,
initializeMintInstruction,
initializeTokenMetadataInstruction,
updateTokenMetadataInstruction,
];
const { value: latestBlockhash } = await rpc.getLatestBlockhash().send();
const transactionMessage = pipe(
createTransactionMessage({ version: 0 }),
(tx) => setTransactionMessageFeePayerSigner(feePayer, tx),
(tx) => setTransactionMessageLifetimeUsingBlockhash(latestBlockhash, tx),
(tx) => appendTransactionMessageInstructions(instructions, tx),
);
const signedTransaction = await signTransactionMessageWithSigners(transactionMessage);
const transactionSignature = getSignatureFromTransaction(signedTransaction);
await sendAndConfirmTransactionFactory({ rpc, rpcSubscriptions })(signedTransaction, {
commitment: "confirmed",
skipPreflight: true,
});
console.log("Transaction Signature:", `https://explorer.solana.com/tx/${transactionSignature}?cluster=custom`);