@@ -65,26 +65,15 @@ type TokenInfo = {|
6565 * Number of base 10 digits to the right of the decimal place
6666 */
6767 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 ,
7868| } ;
7969
8070/**
8171 * @private
8272 */
8373const TokenInfoLayout = BufferLayout . struct ( [
74+ BufferLayout . u8 ( 'state' ) ,
8475 Layout . uint64 ( 'supply' ) ,
8576 BufferLayout . u8 ( 'decimals' ) ,
86- Layout . rustString ( 'name' ) ,
87- Layout . rustString ( 'symbol' ) ,
8877] ) ;
8978
9079/**
@@ -126,10 +115,11 @@ type TokenAccountInfo = {|
126115 * @private
127116 */
128117const TokenAccountInfoLayout = BufferLayout . struct ( [
118+ BufferLayout . u8 ( 'state' ) ,
129119 Layout . publicKey ( 'token' ) ,
130120 Layout . publicKey ( 'owner' ) ,
131121 Layout . uint64 ( 'amount' ) ,
132- BufferLayout . u8 ( 'sourceOption' ) ,
122+ BufferLayout . nu64 ( 'sourceOption' ) ,
133123 Layout . publicKey ( 'source' ) ,
134124 Layout . uint64 ( 'originalAmount' ) ,
135125] ) ;
@@ -166,14 +156,38 @@ export class Token {
166156 Object . assign ( this , { connection, token, programId} ) ;
167157 }
168158
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+
169185 /**
170186 * Create a new Token
171187 *
172188 * @param connection The connection to use
173189 * @param owner User account that will own the returned Token Account
174190 * @param supply Total supply of the new token
175- * @param name Descriptive name of this token
176- * @param symbol Symbol for this token
177191 * @param decimals Location of the decimal place
178192 * @param programId Optional token programId, uses the system programId by default
179193 * @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 {
182196 connection : Connection ,
183197 owner : Account ,
184198 supply : TokenAmount ,
185- name : string ,
186- symbol : string ,
187199 decimals : number ,
188200 programId : PublicKey ,
189201 ) : Promise < TokenAndPublicKey > {
@@ -194,11 +206,9 @@ export class Token {
194206 let transaction ;
195207
196208 const dataLayout = BufferLayout . struct ( [
197- BufferLayout . u32 ( 'instruction' ) ,
209+ BufferLayout . u8 ( 'instruction' ) ,
198210 Layout . uint64 ( 'supply' ) ,
199- BufferLayout . u8 ( 'decimals' ) ,
200- Layout . rustString ( 'name' ) ,
201- Layout . rustString ( 'symbol' ) ,
211+ BufferLayout . nu64 ( 'decimals' ) ,
202212 ] ) ;
203213
204214 let data = Buffer . alloc ( 1024 ) ;
@@ -208,19 +218,21 @@ export class Token {
208218 instruction : 0 , // NewToken instruction
209219 supply : supply . toBuffer ( ) ,
210220 decimals,
211- name,
212- symbol,
213221 } ,
214222 data ,
215223 ) ;
216224 data = data . slice ( 0 , encodeLength ) ;
217225 }
218226
227+ const balanceNeeded = await Token . getMinBalanceRentForExemptToken (
228+ connection ,
229+ ) ;
230+
219231 // Allocate memory for the tokenAccount account
220232 transaction = SystemProgram . createAccount (
221233 owner . publicKey ,
222234 tokenAccount . publicKey ,
223- 1 ,
235+ balanceNeeded ,
224236 1 + data . length ,
225237 programId ,
226238 ) ;
@@ -268,7 +280,7 @@ export class Token {
268280 const tokenAccount = new Account ( ) ;
269281 let transaction ;
270282
271- const dataLayout = BufferLayout . struct ( [ BufferLayout . u32 ( 'instruction' ) ] ) ;
283+ const dataLayout = BufferLayout . struct ( [ BufferLayout . u8 ( 'instruction' ) ] ) ;
272284
273285 const data = Buffer . alloc ( dataLayout . span ) ;
274286 dataLayout . encode (
@@ -278,12 +290,16 @@ export class Token {
278290 data ,
279291 ) ;
280292
293+ const balanceNeeded = await Token . getMinBalanceRentForExemptTokenAccount (
294+ this . connection ,
295+ ) ;
296+
281297 // Allocate memory for the token
282298 transaction = SystemProgram . createAccount (
283299 owner . publicKey ,
284300 tokenAccount . publicKey ,
285- 1 ,
286- 1 + TokenAccountInfoLayout . span ,
301+ balanceNeeded ,
302+ TokenAccountInfoLayout . span ,
287303 this . programId ,
288304 ) ;
289305 await sendAndConfirmTransaction (
@@ -332,10 +348,10 @@ export class Token {
332348
333349 const data = Buffer . from ( accountInfo . data ) ;
334350
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` ) ;
337354 }
338- const tokenInfo = TokenInfoLayout . decode ( data , 1 ) ;
339355 tokenInfo . supply = TokenAmount . fromBuffer ( tokenInfo . supply ) ;
340356 return tokenInfo ;
341357 }
@@ -352,11 +368,11 @@ export class Token {
352368 }
353369
354370 const data = Buffer . from ( accountInfo . data ) ;
355- if ( data . readUInt8 ( 0 ) !== 2 ) {
371+ const tokenAccountInfo = TokenAccountInfoLayout . decode ( data ) ;
372+
373+ if ( tokenAccountInfo . state !== 2 ) {
356374 throw new Error ( `Invalid token account data` ) ;
357375 }
358- const tokenAccountInfo = TokenAccountInfoLayout . decode ( data , 1 ) ;
359-
360376 tokenAccountInfo . token = new PublicKey ( tokenAccountInfo . token ) ;
361377 tokenAccountInfo . owner = new PublicKey ( tokenAccountInfo . owner ) ;
362378 tokenAccountInfo . amount = TokenAmount . fromBuffer ( tokenAccountInfo . amount ) ;
@@ -490,7 +506,7 @@ export class Token {
490506 }
491507
492508 const dataLayout = BufferLayout . struct ( [
493- BufferLayout . u32 ( 'instruction' ) ,
509+ BufferLayout . u8 ( 'instruction' ) ,
494510 Layout . uint64 ( 'amount' ) ,
495511 ] ) ;
496512
@@ -537,7 +553,7 @@ export class Token {
537553 amount : number | TokenAmount ,
538554 ) : TransactionInstruction {
539555 const dataLayout = BufferLayout . struct ( [
540- BufferLayout . u32 ( 'instruction' ) ,
556+ BufferLayout . u8 ( 'instruction' ) ,
541557 Layout . uint64 ( 'amount' ) ,
542558 ] ) ;
543559
@@ -588,7 +604,7 @@ export class Token {
588604 account : PublicKey ,
589605 newOwner : PublicKey ,
590606 ) : TransactionInstruction {
591- const dataLayout = BufferLayout . struct ( [ BufferLayout . u32 ( 'instruction' ) ] ) ;
607+ const dataLayout = BufferLayout . struct ( [ BufferLayout . u8 ( 'instruction' ) ] ) ;
592608
593609 const data = Buffer . alloc ( dataLayout . span ) ;
594610 dataLayout . encode (
0 commit comments