1
- import { PROGRAM_ID as TOKEN_METADATA_PROGRAM_ID } from "@metaplex-foundation/mpl-token-metadata" ;
1
+ import {
2
+ PROGRAM_ID as TOKEN_METADATA_PROGRAM_ID
3
+ } from '@metaplex-foundation/mpl-token-metadata' ;
2
4
import {
3
- Connection ,
4
- Keypair ,
5
- PublicKey ,
6
- SystemProgram ,
7
- SYSVAR_RENT_PUBKEY ,
8
- TransactionInstruction ,
9
- Transaction ,
10
- sendAndConfirmTransaction ,
11
- } from "@solana/web3.js" ;
12
- import { TOKEN_PROGRAM_ID } from "@solana/spl-token" ;
5
+ Connection ,
6
+ Keypair ,
7
+ PublicKey ,
8
+ SystemProgram ,
9
+ SYSVAR_RENT_PUBKEY ,
10
+ TransactionInstruction ,
11
+ Transaction ,
12
+ sendAndConfirmTransaction ,
13
+ } from '@solana/web3.js' ;
14
+ import {
15
+ TOKEN_PROGRAM_ID ,
16
+ } from '@solana/spl-token' ;
13
17
import * as borsh from "borsh" ;
14
18
import { Buffer } from "buffer" ;
15
19
20
+
16
21
function createKeypairFromFile ( path : string ) : Keypair {
17
- return Keypair . fromSecretKey (
18
- Buffer . from ( JSON . parse ( require ( "fs" ) . readFileSync ( path , "utf-8" ) ) )
19
- ) ;
20
- }
22
+ return Keypair . fromSecretKey (
23
+ Buffer . from ( JSON . parse ( require ( 'fs' ) . readFileSync ( path , "utf-8" ) ) )
24
+ )
25
+ } ;
26
+
21
27
22
28
class Assignable {
23
- constructor ( properties ) {
24
- Object . keys ( properties ) . map ( ( key ) => {
25
- return ( this [ key ] = properties [ key ] ) ;
26
- } ) ;
27
- }
28
- }
29
+ constructor ( properties ) {
30
+ Object . keys ( properties ) . map ( ( key ) => {
31
+ return ( this [ key ] = properties [ key ] ) ;
32
+ } ) ;
33
+ } ;
34
+ } ;
29
35
30
36
class CreateTokenArgs extends Assignable {
31
- toBuffer ( ) {
32
- return Buffer . from ( borsh . serialize ( CreateTokenArgsSchema , this ) ) ;
33
- }
34
- }
37
+ toBuffer ( ) {
38
+ return Buffer . from ( borsh . serialize ( CreateTokenArgsSchema , this ) ) ;
39
+ }
40
+ } ;
35
41
const CreateTokenArgsSchema = new Map ( [
36
- [
37
- CreateTokenArgs ,
38
- {
39
- kind : "struct" ,
40
- fields : [
41
- [ "token_title" , "string" ] ,
42
- [ "token_symbol" , "string" ] ,
43
- [ "token_uri" , "string" ] ,
44
- [ "token_decimals" , "u8" ] ,
45
- ] ,
46
- } ,
47
- ] ,
42
+ [
43
+ CreateTokenArgs , {
44
+ kind : 'struct' ,
45
+ fields : [
46
+ [ 'token_title' , 'string' ] ,
47
+ [ 'token_symbol' , 'string' ] ,
48
+ [ 'token_uri' , 'string' ] ,
49
+ [ 'token_decimals' , 'u8' ] ,
50
+ ]
51
+ }
52
+ ]
48
53
] ) ;
49
54
50
- describe ( "Create Tokens!" , async ( ) => {
51
- const connection = new Connection ( `http://localhost:8899` , "confirmed" ) ;
52
- // const connection = new Connection(`https://api.devnet.solana.com/`, 'confirmed');
53
- const payer = createKeypairFromFile (
54
- require ( "os" ) . homedir ( ) + "/.config/solana/id.json"
55
- ) ;
56
- const program = createKeypairFromFile (
57
- "./program/target/deploy/program-keypair.json"
58
- ) ;
59
-
60
- it ( "Create an SPL Token!" , async ( ) => {
61
- const mintKeypair : Keypair = Keypair . generate ( ) ;
62
-
63
- const metadataAddress = PublicKey . findProgramAddressSync (
64
- [
65
- Buffer . from ( "metadata" ) ,
66
- TOKEN_METADATA_PROGRAM_ID . toBuffer ( ) ,
67
- mintKeypair . publicKey . toBuffer ( ) ,
68
- ] ,
69
- TOKEN_METADATA_PROGRAM_ID
70
- ) [ 0 ] ;
71
-
72
- // SPL Token default = 9 decimals
73
- //
74
- const instructionData = new CreateTokenArgs ( {
75
- token_title : "Solana Gold" ,
76
- token_symbol : "GOLDSOL" ,
77
- token_uri :
78
- "https://raw.githubusercontent.com/solana-developers/program-examples/new-examples/tokens/tokens/.assets/spl-token.json" ,
79
- token_decimals : 9 ,
80
- } ) ;
81
55
82
- let ix = new TransactionInstruction ( {
83
- keys : [
84
- { pubkey : mintKeypair . publicKey , isSigner : true , isWritable : true } , // Mint account
85
- { pubkey : payer . publicKey , isSigner : false , isWritable : true } , // Mint authority account
86
- { pubkey : metadataAddress , isSigner : false , isWritable : true } , // Metadata account
87
- { pubkey : payer . publicKey , isSigner : true , isWritable : true } , // Payer
88
- { pubkey : SYSVAR_RENT_PUBKEY , isSigner : false , isWritable : false } , // Rent account
89
- { pubkey : SystemProgram . programId , isSigner : false , isWritable : false } , // System program
90
- { pubkey : TOKEN_PROGRAM_ID , isSigner : false , isWritable : false } , // Token program
91
- {
92
- pubkey : TOKEN_METADATA_PROGRAM_ID ,
93
- isSigner : false ,
94
- isWritable : false ,
95
- } , // Token metadata program
96
- ] ,
97
- programId : program . publicKey ,
98
- data : instructionData . toBuffer ( ) ,
99
- } ) ;
100
-
101
- const sx = await sendAndConfirmTransaction (
102
- connection ,
103
- new Transaction ( ) . add ( ix ) ,
104
- [ payer , mintKeypair ]
105
- ) ;
106
-
107
- console . log ( "Success!" ) ;
108
- console . log ( ` Mint Address: ${ mintKeypair . publicKey } ` ) ;
109
- console . log ( ` Tx Signature: ${ sx } ` ) ;
110
- } ) ;
56
+ describe ( "Create Tokens!" , async ( ) => {
111
57
112
- it ( "Create an NFT!" , async ( ) => {
113
- const mintKeypair : Keypair = Keypair . generate ( ) ;
114
-
115
- const metadataAddress = PublicKey . findProgramAddressSync (
116
- [
117
- Buffer . from ( "metadata" ) ,
118
- TOKEN_METADATA_PROGRAM_ID . toBuffer ( ) ,
119
- mintKeypair . publicKey . toBuffer ( ) ,
120
- ] ,
121
- TOKEN_METADATA_PROGRAM_ID
122
- ) [ 0 ] ;
123
-
124
- // NFT default = 0 decimals
125
- //
126
- const instructionData = new CreateTokenArgs ( {
127
- token_title : "Homer NFT" ,
128
- token_symbol : "HOMR" ,
129
- token_uri :
130
- "https://raw.githubusercontent.com/solana-developers/program-examples/new-examples/tokens/tokens/.assets/nft.json" ,
131
- token_decimals : 9 ,
58
+ // const connection = new Connection(`http://localhost:8899`, 'confirmed');
59
+ const connection = new Connection ( `https://api.devnet.solana.com/` , 'confirmed' ) ;
60
+ const payer = createKeypairFromFile ( require ( 'os' ) . homedir ( ) + '/.config/solana/id.json' ) ;
61
+ const program = createKeypairFromFile ( './program/target/deploy/program-keypair.json' ) ;
62
+
63
+ it ( "Create an SPL Token!" , async ( ) => {
64
+
65
+ const mintKeypair : Keypair = Keypair . generate ( ) ;
66
+
67
+ const metadataAddress = ( PublicKey . findProgramAddressSync (
68
+ [
69
+ Buffer . from ( "metadata" ) ,
70
+ TOKEN_METADATA_PROGRAM_ID . toBuffer ( ) ,
71
+ mintKeypair . publicKey . toBuffer ( ) ,
72
+ ] ,
73
+ TOKEN_METADATA_PROGRAM_ID
74
+ ) ) [ 0 ] ;
75
+
76
+ // SPL Token default = 9 decimals
77
+ //
78
+ const instructionData = new CreateTokenArgs ( {
79
+ token_title : "Solana Gold" ,
80
+ token_symbol : "GOLDSOL" ,
81
+ token_uri : "https://raw.githubusercontent.com/solana-developers/program-examples/new-examples/tokens/tokens/.assets/spl-token.json" ,
82
+ token_decimals : 9 ,
83
+ } ) ;
84
+
85
+ let ix = new TransactionInstruction ( {
86
+ keys : [
87
+ { pubkey : mintKeypair . publicKey , isSigner : true , isWritable : true } , // Mint account
88
+ { pubkey : payer . publicKey , isSigner : false , isWritable : true } , // Mint authority account
89
+ { pubkey : metadataAddress , isSigner : false , isWritable : true } , // Metadata account
90
+ { pubkey : payer . publicKey , isSigner : true , isWritable : true } , // Payer
91
+ { pubkey : SYSVAR_RENT_PUBKEY , isSigner : false , isWritable : false } , // Rent account
92
+ { pubkey : SystemProgram . programId , isSigner : false , isWritable : false } , // System program
93
+ { pubkey : TOKEN_PROGRAM_ID , isSigner : false , isWritable : false } , // Token program
94
+ { pubkey : TOKEN_METADATA_PROGRAM_ID , isSigner : false , isWritable : false } , // Token metadata program
95
+ ] ,
96
+ programId : program . publicKey ,
97
+ data : instructionData . toBuffer ( ) ,
98
+ } ) ;
99
+
100
+ const sx = await sendAndConfirmTransaction (
101
+ connection ,
102
+ new Transaction ( ) . add ( ix ) ,
103
+ [ payer , mintKeypair ]
104
+ ) ;
105
+
106
+ console . log ( "Success!" ) ;
107
+ console . log ( ` Mint Address: ${ mintKeypair . publicKey } ` ) ;
108
+ console . log ( ` Tx Signature: ${ sx } ` ) ;
132
109
} ) ;
133
110
134
- let ix = new TransactionInstruction ( {
135
- keys : [
136
- { pubkey : mintKeypair . publicKey , isSigner : true , isWritable : true } , // Mint account
137
- { pubkey : payer . publicKey , isSigner : false , isWritable : true } , // Mint authority account
138
- { pubkey : metadataAddress , isSigner : false , isWritable : true } , // Metadata account
139
- { pubkey : payer . publicKey , isSigner : true , isWritable : true } , // Payer
140
- { pubkey : SYSVAR_RENT_PUBKEY , isSigner : false , isWritable : false } , // Rent account
141
- { pubkey : SystemProgram . programId , isSigner : false , isWritable : false } , // System program
142
- { pubkey : TOKEN_PROGRAM_ID , isSigner : false , isWritable : false } , // Token program
143
- {
144
- pubkey : TOKEN_METADATA_PROGRAM_ID ,
145
- isSigner : false ,
146
- isWritable : false ,
147
- } , // Token metadata program
148
- ] ,
149
- programId : program . publicKey ,
150
- data : instructionData . toBuffer ( ) ,
111
+ it ( "Create an NFT!" , async ( ) => {
112
+
113
+ const mintKeypair : Keypair = Keypair . generate ( ) ;
114
+
115
+ const metadataAddress = ( PublicKey . findProgramAddressSync (
116
+ [
117
+ Buffer . from ( "metadata" ) ,
118
+ TOKEN_METADATA_PROGRAM_ID . toBuffer ( ) ,
119
+ mintKeypair . publicKey . toBuffer ( ) ,
120
+ ] ,
121
+ TOKEN_METADATA_PROGRAM_ID
122
+ ) ) [ 0 ] ;
123
+
124
+ // NFT default = 0 decimals
125
+ //
126
+ const instructionData = new CreateTokenArgs ( {
127
+ token_title : "Homer NFT" ,
128
+ token_symbol : "HOMR" ,
129
+ token_uri : "https://raw.githubusercontent.com/solana-developers/program-examples/new-examples/tokens/tokens/.assets/nft.json" ,
130
+ token_decimals : 9 ,
131
+ } ) ;
132
+
133
+ let ix = new TransactionInstruction ( {
134
+ keys : [
135
+ { pubkey : mintKeypair . publicKey , isSigner : true , isWritable : true } , // Mint account
136
+ { pubkey : payer . publicKey , isSigner : false , isWritable : true } , // Mint authority account
137
+ { pubkey : metadataAddress , isSigner : false , isWritable : true } , // Metadata account
138
+ { pubkey : payer . publicKey , isSigner : true , isWritable : true } , // Payer
139
+ { pubkey : SYSVAR_RENT_PUBKEY , isSigner : false , isWritable : false } , // Rent account
140
+ { pubkey : SystemProgram . programId , isSigner : false , isWritable : false } , // System program
141
+ { pubkey : TOKEN_PROGRAM_ID , isSigner : false , isWritable : false } , // Token program
142
+ { pubkey : TOKEN_METADATA_PROGRAM_ID , isSigner : false , isWritable : false } , // Token metadata program
143
+ ] ,
144
+ programId : program . publicKey ,
145
+ data : instructionData . toBuffer ( ) ,
146
+ } ) ;
147
+
148
+ const sx = await sendAndConfirmTransaction (
149
+ connection ,
150
+ new Transaction ( ) . add ( ix ) ,
151
+ [ payer , mintKeypair ]
152
+ ) ;
153
+
154
+ console . log ( "Success!" ) ;
155
+ console . log ( ` Mint Address: ${ mintKeypair . publicKey } ` ) ;
156
+ console . log ( ` Tx Signature: ${ sx } ` ) ;
151
157
} ) ;
152
-
153
- const sx = await sendAndConfirmTransaction (
154
- connection ,
155
- new Transaction ( ) . add ( ix ) ,
156
- [ payer , mintKeypair ]
157
- ) ;
158
-
159
- console . log ( "Success!" ) ;
160
- console . log ( ` Mint Address: ${ mintKeypair . publicKey } ` ) ;
161
- console . log ( ` Tx Signature: ${ sx } ` ) ;
162
158
} ) ;
163
- } ) ;
159
+
0 commit comments