@@ -65,26 +65,15 @@ type TokenInfo = {|
65
65
* Number of base 10 digits to the right of the decimal place
66
66
*/
67
67
decimals : number ,
68
-
69
- /**
70
- * Descriptive name of this token
71
- */
72
- name : string ,
73
-
74
- /**
75
- * Symbol for this token
76
- */
77
- symbol : string ,
78
68
| } ;
79
69
80
70
/**
81
71
* @private
82
72
*/
83
73
const TokenInfoLayout = BufferLayout . struct ( [
74
+ BufferLayout . u8 ( 'state' ) ,
84
75
Layout . uint64 ( 'supply' ) ,
85
76
BufferLayout . u8 ( 'decimals' ) ,
86
- Layout . rustString ( 'name' ) ,
87
- Layout . rustString ( 'symbol' ) ,
88
77
] ) ;
89
78
90
79
/**
@@ -126,10 +115,11 @@ type TokenAccountInfo = {|
126
115
* @private
127
116
*/
128
117
const TokenAccountInfoLayout = BufferLayout . struct ( [
118
+ BufferLayout . u8 ( 'state' ) ,
129
119
Layout . publicKey ( 'token' ) ,
130
120
Layout . publicKey ( 'owner' ) ,
131
121
Layout . uint64 ( 'amount' ) ,
132
- BufferLayout . u8 ( 'sourceOption' ) ,
122
+ BufferLayout . nu64 ( 'sourceOption' ) ,
133
123
Layout . publicKey ( 'source' ) ,
134
124
Layout . uint64 ( 'originalAmount' ) ,
135
125
] ) ;
@@ -166,14 +156,38 @@ export class Token {
166
156
Object . assign ( this , { connection, token, programId} ) ;
167
157
}
168
158
159
+ /**
160
+ * Get the minimum balance for the token to be rent exempt
161
+ *
162
+ * @return Number of lamports required
163
+ */
164
+ static async getMinBalanceRentForExemptToken (
165
+ connection : Connection ,
166
+ ) : Promise < number > {
167
+ return await connection . getMinimumBalanceForRentExemption (
168
+ TokenInfoLayout . span ,
169
+ ) ;
170
+ }
171
+
172
+ /**
173
+ * Get the minimum balance for the token account to be rent exempt
174
+ *
175
+ * @return Number of lamports required
176
+ */
177
+ static async getMinBalanceRentForExemptTokenAccount (
178
+ connection : Connection ,
179
+ ) : Promise < number > {
180
+ return await connection . getMinimumBalanceForRentExemption (
181
+ TokenAccountInfoLayout . span ,
182
+ ) ;
183
+ }
184
+
169
185
/**
170
186
* Create a new Token
171
187
*
172
188
* @param connection The connection to use
173
189
* @param owner User account that will own the returned Token Account
174
190
* @param supply Total supply of the new token
175
- * @param name Descriptive name of this token
176
- * @param symbol Symbol for this token
177
191
* @param decimals Location of the decimal place
178
192
* @param programId Optional token programId, uses the system programId by default
179
193
* @return Token object for the newly minted token, Public key of the Token Account holding the total supply of new tokens
@@ -182,8 +196,6 @@ export class Token {
182
196
connection : Connection ,
183
197
owner : Account ,
184
198
supply : TokenAmount ,
185
- name : string ,
186
- symbol : string ,
187
199
decimals : number ,
188
200
programId : PublicKey ,
189
201
) : Promise < TokenAndPublicKey > {
@@ -194,11 +206,9 @@ export class Token {
194
206
let transaction ;
195
207
196
208
const dataLayout = BufferLayout . struct ( [
197
- BufferLayout . u32 ( 'instruction' ) ,
209
+ BufferLayout . u8 ( 'instruction' ) ,
198
210
Layout . uint64 ( 'supply' ) ,
199
- BufferLayout . u8 ( 'decimals' ) ,
200
- Layout . rustString ( 'name' ) ,
201
- Layout . rustString ( 'symbol' ) ,
211
+ BufferLayout . nu64 ( 'decimals' ) ,
202
212
] ) ;
203
213
204
214
let data = Buffer . alloc ( 1024 ) ;
@@ -208,19 +218,21 @@ export class Token {
208
218
instruction : 0 , // NewToken instruction
209
219
supply : supply . toBuffer ( ) ,
210
220
decimals,
211
- name,
212
- symbol,
213
221
} ,
214
222
data ,
215
223
) ;
216
224
data = data . slice ( 0 , encodeLength ) ;
217
225
}
218
226
227
+ const balanceNeeded = await Token . getMinBalanceRentForExemptToken (
228
+ connection ,
229
+ ) ;
230
+
219
231
// Allocate memory for the tokenAccount account
220
232
transaction = SystemProgram . createAccount (
221
233
owner . publicKey ,
222
234
tokenAccount . publicKey ,
223
- 1 ,
235
+ balanceNeeded ,
224
236
1 + data . length ,
225
237
programId ,
226
238
) ;
@@ -268,7 +280,7 @@ export class Token {
268
280
const tokenAccount = new Account ( ) ;
269
281
let transaction ;
270
282
271
- const dataLayout = BufferLayout . struct ( [ BufferLayout . u32 ( 'instruction' ) ] ) ;
283
+ const dataLayout = BufferLayout . struct ( [ BufferLayout . u8 ( 'instruction' ) ] ) ;
272
284
273
285
const data = Buffer . alloc ( dataLayout . span ) ;
274
286
dataLayout . encode (
@@ -278,12 +290,16 @@ export class Token {
278
290
data ,
279
291
) ;
280
292
293
+ const balanceNeeded = await Token . getMinBalanceRentForExemptTokenAccount (
294
+ this . connection ,
295
+ ) ;
296
+
281
297
// Allocate memory for the token
282
298
transaction = SystemProgram . createAccount (
283
299
owner . publicKey ,
284
300
tokenAccount . publicKey ,
285
- 1 ,
286
- 1 + TokenAccountInfoLayout . span ,
301
+ balanceNeeded ,
302
+ TokenAccountInfoLayout . span ,
287
303
this . programId ,
288
304
) ;
289
305
await sendAndConfirmTransaction (
@@ -332,10 +348,10 @@ export class Token {
332
348
333
349
const data = Buffer . from ( accountInfo . data ) ;
334
350
335
- if ( data . readUInt8 ( 0 ) !== 1 ) {
336
- throw new Error ( `Invalid token data` ) ;
351
+ const tokenInfo = TokenInfoLayout . decode ( data ) ;
352
+ if ( tokenInfo . state !== 1 ) {
353
+ throw new Error ( `Invalid token account data` ) ;
337
354
}
338
- const tokenInfo = TokenInfoLayout . decode ( data , 1 ) ;
339
355
tokenInfo . supply = TokenAmount . fromBuffer ( tokenInfo . supply ) ;
340
356
return tokenInfo ;
341
357
}
@@ -352,11 +368,11 @@ export class Token {
352
368
}
353
369
354
370
const data = Buffer . from ( accountInfo . data ) ;
355
- if ( data . readUInt8 ( 0 ) !== 2 ) {
371
+ const tokenAccountInfo = TokenAccountInfoLayout . decode ( data ) ;
372
+
373
+ if ( tokenAccountInfo . state !== 2 ) {
356
374
throw new Error ( `Invalid token account data` ) ;
357
375
}
358
- const tokenAccountInfo = TokenAccountInfoLayout . decode ( data , 1 ) ;
359
-
360
376
tokenAccountInfo . token = new PublicKey ( tokenAccountInfo . token ) ;
361
377
tokenAccountInfo . owner = new PublicKey ( tokenAccountInfo . owner ) ;
362
378
tokenAccountInfo . amount = TokenAmount . fromBuffer ( tokenAccountInfo . amount ) ;
@@ -490,7 +506,7 @@ export class Token {
490
506
}
491
507
492
508
const dataLayout = BufferLayout . struct ( [
493
- BufferLayout . u32 ( 'instruction' ) ,
509
+ BufferLayout . u8 ( 'instruction' ) ,
494
510
Layout . uint64 ( 'amount' ) ,
495
511
] ) ;
496
512
@@ -537,7 +553,7 @@ export class Token {
537
553
amount : number | TokenAmount ,
538
554
) : TransactionInstruction {
539
555
const dataLayout = BufferLayout . struct ( [
540
- BufferLayout . u32 ( 'instruction' ) ,
556
+ BufferLayout . u8 ( 'instruction' ) ,
541
557
Layout . uint64 ( 'amount' ) ,
542
558
] ) ;
543
559
@@ -588,7 +604,7 @@ export class Token {
588
604
account : PublicKey ,
589
605
newOwner : PublicKey ,
590
606
) : TransactionInstruction {
591
- const dataLayout = BufferLayout . struct ( [ BufferLayout . u32 ( 'instruction' ) ] ) ;
607
+ const dataLayout = BufferLayout . struct ( [ BufferLayout . u8 ( 'instruction' ) ] ) ;
592
608
593
609
const data = Buffer . alloc ( dataLayout . span ) ;
594
610
dataLayout . encode (
0 commit comments