From a1259e2deb3ae2331210a488bd10358d983d112a Mon Sep 17 00:00:00 2001 From: Jack May Date: Tue, 30 Jun 2020 16:37:55 -0700 Subject: [PATCH] Prep token for impending token-swap merge (#69) --- token/js/cli/token-test.js | 211 +++++++++++++++++------------------ token/js/client/token.js | 219 ++++++++++++++++++------------------- token/js/package-lock.json | 6 +- 3 files changed, 218 insertions(+), 218 deletions(-) diff --git a/token/js/cli/token-test.js b/token/js/cli/token-test.js index a6575dd911f2d1..8fc13dcc5267e9 100644 --- a/token/js/cli/token-test.js +++ b/token/js/cli/token-test.js @@ -16,8 +16,11 @@ let programId: PublicKey; let testToken: Token; // Initial owner of the token supply -let initialOwner; -let initialOwnerTokenAccount: PublicKey; +let tokenOwner: Account; + +// Initial token account +let testAccountOwner: Account; +let testAccount: PublicKey; function assert(condition, message) { if (!condition) { @@ -54,10 +57,9 @@ async function getConnection(): Promise { return connection; } -export async function loadTokenProgram(): Promise { +export async function loadTokenProgram(path: string): Promise { const NUM_RETRIES = 500; /* allow some number of retries */ - const data = await fs.readFile( - '../target/bpfel-unknown-unknown/release/spl_token.so', + const data = await fs.readFile(path ); const connection = await getConnection(); const {feeCalculator} = await connection.getRecentBlockhash(); @@ -71,34 +73,36 @@ export async function loadTokenProgram(): Promise { console.log('Loading Token program...'); await BpfLoader.load(connection, from, program_account, data); programId = program_account.publicKey; + return programId; } export async function createNewToken(): Promise { const connection = await getConnection(); - const balanceNeeded = - (await Token.getMinBalanceRentForExemptToken(connection)) + - (await Token.getMinBalanceRentForExemptAccount(connection)); - initialOwner = await newAccountWithLamports(connection, balanceNeeded); - [testToken, initialOwnerTokenAccount] = await Token.createNewToken( + const payer = await newAccountWithLamports(connection, 100000000000 /* wag */); + tokenOwner = new Account(); + testAccountOwner = new Account(); + [testToken, testAccount] = await Token.createNewToken( connection, - initialOwner, + payer, + tokenOwner.publicKey, + testAccountOwner.publicKey, new TokenAmount(10000), 2, programId, false, ); - const TokenInfo = await testToken.TokenInfo(); - assert(TokenInfo.supply.toNumber() == 10000); - assert(TokenInfo.decimals == 2); - assert(TokenInfo.owner == null); - - const TokenAccountInfo = await testToken.TokenAccountInfo(initialOwnerTokenAccount); - assert(TokenAccountInfo.token.equals(testToken.token)); - assert(TokenAccountInfo.owner.equals(initialOwner.publicKey)); - assert(TokenAccountInfo.amount.toNumber() == 10000); - assert(TokenAccountInfo.source == null); - assert(TokenAccountInfo.originalAmount.toNumber() == 0); + const tokenInfo = await testToken.getTokenInfo(); + assert(tokenInfo.supply.toNumber() == 10000); + assert(tokenInfo.decimals == 2); + assert(tokenInfo.owner == null); + + const accountInfo = await testToken.getAccountInfo(testAccount); + assert(accountInfo.token.equals(testToken.publicKey)); + assert(accountInfo.owner.equals(testAccountOwner.publicKey)); + assert(accountInfo.amount.toNumber() == 10000); + assert(accountInfo.source == null); + assert(accountInfo.originalAmount.toNumber() == 0); } export async function createNewAccount(): Promise { @@ -107,12 +111,12 @@ export async function createNewAccount(): Promise { connection, ); const destOwner = await newAccountWithLamports(connection, balanceNeeded); - const dest = await testToken.newAccount(destOwner); - const TokenAccountInfo = await testToken.TokenAccountInfo(dest); - assert(TokenAccountInfo.token.equals(testToken.token)); - assert(TokenAccountInfo.owner.equals(destOwner.publicKey)); - assert(TokenAccountInfo.amount.toNumber() == 0); - assert(TokenAccountInfo.source == null); + const dest = await testToken.newAccount(destOwner.publicKey); + const accountInfo = await testToken.getAccountInfo(dest); + assert(accountInfo.token.equals(testToken.publicKey)); + assert(accountInfo.owner.equals(destOwner.publicKey)); + assert(accountInfo.amount.toNumber() == 0); + assert(accountInfo.source == null); } export async function transfer(): Promise { @@ -121,12 +125,12 @@ export async function transfer(): Promise { connection, ); const destOwner = await newAccountWithLamports(connection, balanceNeeded); - const dest = await testToken.newAccount(destOwner); + const dest = await testToken.newAccount(destOwner.publicKey); - await testToken.transfer(initialOwner, initialOwnerTokenAccount, dest, 123); + await testToken.transfer(testAccountOwner, testAccount, dest, 123); await sleep(500); - let destTokenAccountInfo = await testToken.TokenAccountInfo(dest); + let destTokenAccountInfo = await testToken.getAccountInfo(dest); assert(destTokenAccountInfo.amount.toNumber() == 123); } @@ -142,34 +146,34 @@ export async function approveRevoke(): Promise { ); const delegateOwner = await newAccountWithLamports(connection, balanceNeeded); const delegate = await testToken.newAccount( - delegateOwner, - initialOwnerTokenAccount, + delegateOwner.publicKey, + testAccount, ); await testToken.approve( - initialOwner, - initialOwnerTokenAccount, + testAccountOwner, + testAccount, delegate, 456, ); - let delegateTokenAccountInfo = await testToken.TokenAccountInfo(delegate); - assert(delegateTokenAccountInfo.amount.toNumber() == 456); - assert(delegateTokenAccountInfo.originalAmount.toNumber() == 456); - if (delegateTokenAccountInfo.source === null) { + let delegateAccountInfo = await testToken.getAccountInfo(delegate); + assert(delegateAccountInfo.amount.toNumber() == 456); + assert(delegateAccountInfo.originalAmount.toNumber() == 456); + if (delegateAccountInfo.source === null) { throw new Error('source should not be null'); } else { - assert(delegateTokenAccountInfo.source.equals(initialOwnerTokenAccount)); + assert(delegateAccountInfo.source.equals(testAccount)); } - await testToken.revoke(initialOwner, initialOwnerTokenAccount, delegate); - delegateTokenAccountInfo = await testToken.TokenAccountInfo(delegate); - assert(delegateTokenAccountInfo.amount.toNumber() == 0); - assert(delegateTokenAccountInfo.originalAmount.toNumber() == 0); - if (delegateTokenAccountInfo.source === null) { + await testToken.revoke(testAccountOwner, testAccount, delegate); + delegateAccountInfo = await testToken.getAccountInfo(delegate); + assert(delegateAccountInfo.amount.toNumber() == 0); + assert(delegateAccountInfo.originalAmount.toNumber() == 0); + if (delegateAccountInfo.source === null) { throw new Error('source should not be null'); } else { - assert(delegateTokenAccountInfo.source.equals(initialOwnerTokenAccount)); + assert(delegateAccountInfo.source.equals(testAccount)); } } @@ -178,9 +182,9 @@ export async function invalidApprove(): Promise { const balanceNeeded = (await Token.getMinBalanceRentForExemptAccount(connection)) * 3; const owner = await newAccountWithLamports(connection, balanceNeeded); - const account1 = await testToken.newAccount(owner); - const account1Delegate = await testToken.newAccount(owner, account1); - const account2 = await testToken.newAccount(owner); + const account1 = await testToken.newAccount(owner.publicKey); + const account1Delegate = await testToken.newAccount(owner.publicKey, account1); + const account2 = await testToken.newAccount(owner.publicKey); // account2 is not a delegate account of account1 assert(didThrow(testToken.approve, [owner, account1, account2, 123])); @@ -193,34 +197,34 @@ export async function failOnApproveOverspend(): Promise { const balanceNeeded = (await Token.getMinBalanceRentForExemptAccount(connection)) * 3; const owner = await newAccountWithLamports(connection, balanceNeeded); - const account1 = await testToken.newAccount(owner); - const account1Delegate = await testToken.newAccount(owner, account1); - const account2 = await testToken.newAccount(owner); + const account1 = await testToken.newAccount(owner.publicKey); + const account1Delegate = await testToken.newAccount(owner.publicKey, account1); + const account2 = await testToken.newAccount(owner.publicKey); await testToken.transfer( - initialOwner, - initialOwnerTokenAccount, + testAccountOwner, + testAccount, account1, 10, ); await testToken.approve(owner, account1, account1Delegate, 2); - let delegateTokenAccountInfo = await testToken.TokenAccountInfo(account1Delegate); - assert(delegateTokenAccountInfo.amount.toNumber() == 2); - assert(delegateTokenAccountInfo.originalAmount.toNumber() == 2); + let delegateAccountInfo = await testToken.getAccountInfo(account1Delegate); + assert(delegateAccountInfo.amount.toNumber() == 2); + assert(delegateAccountInfo.originalAmount.toNumber() == 2); await testToken.transfer(owner, account1Delegate, account2, 1); - delegateTokenAccountInfo = await testToken.TokenAccountInfo(account1Delegate); - assert(delegateTokenAccountInfo.amount.toNumber() == 1); - assert(delegateTokenAccountInfo.originalAmount.toNumber() == 2); + delegateAccountInfo = await testToken.getAccountInfo(account1Delegate); + assert(delegateAccountInfo.amount.toNumber() == 1); + assert(delegateAccountInfo.originalAmount.toNumber() == 2); await testToken.transfer(owner, account1Delegate, account2, 1); - delegateTokenAccountInfo = await testToken.TokenAccountInfo(account1Delegate); - assert(delegateTokenAccountInfo.amount.toNumber() == 0); - assert(delegateTokenAccountInfo.originalAmount.toNumber() == 2); + delegateAccountInfo = await testToken.getAccountInfo(account1Delegate); + assert(delegateAccountInfo.amount.toNumber() == 0); + assert(delegateAccountInfo.originalAmount.toNumber() == 2); assert(didThrow(testToken.transfer, [owner, account1Delegate, account2, 1])); } @@ -232,7 +236,7 @@ export async function setOwner(): Promise { ); const owner = await newAccountWithLamports(connection, balanceNeeded); const newOwner = await newAccountWithLamports(connection, balanceNeeded); - const owned = await testToken.newAccount(owner); + const owned = await testToken.newAccount(owner.publicKey); await testToken.setOwner(owner, owned, newOwner.publicKey); assert(didThrow(testToken.setOwner, [owner, owned, newOwner.publicKey])); @@ -241,15 +245,14 @@ export async function setOwner(): Promise { export async function mintTo(): Promise { const connection = await getConnection(); - const balanceNeeded = - (await Token.getMinBalanceRentForExemptToken(connection)) + - (await Token.getMinBalanceRentForExemptAccount(connection)) * 2; - - const owner = await newAccountWithLamports(connection, balanceNeeded); - + const payer = await newAccountWithLamports(connection, 100000000000 /* wag */); + const tokenOwner = new Account(); + const testAccountOwner = new Account(); const [mintableToken, initialAccount] = await Token.createNewToken( connection, - owner, + payer, + tokenOwner.publicKey, + testAccountOwner.publicKey, new TokenAmount(10000), 2, programId, @@ -257,56 +260,56 @@ export async function mintTo(): Promise { ); { - const TokenInfo = await mintableToken.TokenInfo(); - assert(TokenInfo.supply.toNumber() == 10000); - assert(TokenInfo.decimals == 2); - if (TokenInfo.owner === null) { + const tokenInfo = await mintableToken.getTokenInfo(); + assert(tokenInfo.supply.toNumber() == 10000); + assert(tokenInfo.decimals == 2); + if (tokenInfo.owner === null) { throw new Error('owner should not be null'); } else { - assert(TokenInfo.owner.equals(owner.publicKey)); + assert(tokenInfo.owner.equals(tokenOwner.publicKey)); } - const TokenAccountInfo = await mintableToken.TokenAccountInfo(initialAccount); - assert(TokenAccountInfo.token.equals(mintableToken.token)); - assert(TokenAccountInfo.owner.equals(owner.publicKey)); - assert(TokenAccountInfo.amount.toNumber() == 10000); - assert(TokenAccountInfo.source == null); - assert(TokenAccountInfo.originalAmount.toNumber() == 0); + const accountInfo = await mintableToken.getAccountInfo(initialAccount); + assert(accountInfo.token.equals(mintableToken.publicKey)); + assert(accountInfo.owner.equals(testAccountOwner.publicKey)); + assert(accountInfo.amount.toNumber() == 10000); + assert(accountInfo.source == null); + assert(accountInfo.originalAmount.toNumber() == 0); } - const dest = await mintableToken.newAccount(owner); - await mintableToken.mintTo(owner, mintableToken.token, dest, 42); + const dest = await mintableToken.newAccount(testAccountOwner.publicKey); + await mintableToken.mintTo(tokenOwner, dest, 42); { - const TokenInfo = await mintableToken.TokenInfo(); - assert(TokenInfo.supply.toNumber() == 10042); - assert(TokenInfo.decimals == 2); - if (TokenInfo.owner === null) { + const tokenInfo = await mintableToken.getTokenInfo(); + assert(tokenInfo.supply.toNumber() == 10042); + assert(tokenInfo.decimals == 2); + if (tokenInfo.owner === null) { throw new Error('owner should not be null'); } else { - assert(TokenInfo.owner.equals(owner.publicKey)); + assert(tokenInfo.owner.equals(tokenOwner.publicKey)); } - const TokenAccountInfo = await mintableToken.TokenAccountInfo(dest); - assert(TokenAccountInfo.token.equals(mintableToken.token)); - assert(TokenAccountInfo.owner.equals(owner.publicKey)); - assert(TokenAccountInfo.amount.toNumber() == 42); - assert(TokenAccountInfo.source == null); - assert(TokenAccountInfo.originalAmount.toNumber() == 0); + const accountInfo = await mintableToken.getAccountInfo(dest); + assert(accountInfo.token.equals(mintableToken.publicKey)); + assert(accountInfo.owner.equals(testAccountOwner.publicKey)); + assert(accountInfo.amount.toNumber() == 42); + assert(accountInfo.source == null); + assert(accountInfo.originalAmount.toNumber() == 0); } } export async function burn(): Promise { - let TokenInfo = await testToken.TokenInfo(); - const supply = TokenInfo.supply.toNumber(); - let TokenAccountInfo = await testToken.TokenAccountInfo(initialOwnerTokenAccount); - const amount = TokenAccountInfo.amount.toNumber(); + let tokenInfo = await testToken.getTokenInfo(); + const supply = tokenInfo.supply.toNumber(); + let accountInfo = await testToken.getAccountInfo(testAccount); + const amount = accountInfo.amount.toNumber(); - await testToken.burn(initialOwner, initialOwnerTokenAccount, 1); + await testToken.burn(testAccountOwner, testAccount, 1); await sleep(500); - TokenInfo = await testToken.TokenInfo(); - assert(TokenInfo.supply.toNumber() == supply - 1); - TokenAccountInfo = await testToken.TokenAccountInfo(initialOwnerTokenAccount); - assert(TokenAccountInfo.amount.toNumber() == amount - 1); + tokenInfo = await testToken.getTokenInfo(); + assert(tokenInfo.supply.toNumber() == supply - 1); + accountInfo = await testToken.getAccountInfo(testAccount); + assert(accountInfo.amount.toNumber() == amount - 1); } diff --git a/token/js/client/token.js b/token/js/client/token.js index 9e3b4f5019048b..1a5ef9d99c0b06 100644 --- a/token/js/client/token.js +++ b/token/js/client/token.js @@ -72,7 +72,7 @@ type TokenInfo = {| owner: null | PublicKey, |}; -const TokenLayout = BufferLayout.struct([ +const TokenInfoLayout = BufferLayout.struct([ BufferLayout.u8('state'), Layout.uint64('supply'), BufferLayout.nu64('decimals'), @@ -84,7 +84,7 @@ const TokenLayout = BufferLayout.struct([ /** * Information about an account */ -type TokenAccountInfo = {| +type AccountInfo = {| /** * The kind of token this account holds */ @@ -119,7 +119,7 @@ type TokenAccountInfo = {| /** * @private */ -const TokenAccountInfoLayout = BufferLayout.struct([ +const AccountInfoLayout = BufferLayout.struct([ BufferLayout.u8('state'), Layout.publicKey('token'), Layout.publicKey('owner'), @@ -144,7 +144,7 @@ export class Token { /** * The public key identifying this token */ - token: PublicKey; + publicKey: PublicKey; /** * Program Identifier for the Token program @@ -164,8 +164,8 @@ export class Token { * @param programId Optional token programId, uses the system programId by default * @param payer Payer of fees */ - constructor(connection: Connection, token: PublicKey, programId: PublicKey, payer: Account) { - Object.assign(this, {connection, token, programId, payer}); + constructor(connection: Connection, publicKey: PublicKey, programId: PublicKey, payer: Account) { + Object.assign(this, {connection, publicKey, programId, payer}); } /** @@ -177,7 +177,7 @@ export class Token { connection: Connection, ): Promise { return await connection.getMinimumBalanceForRentExemption( - TokenLayout.span, + TokenInfoLayout.span, ); } @@ -190,7 +190,7 @@ export class Token { connection: Connection, ): Promise { return await connection.getMinimumBalanceForRentExemption( - TokenAccountInfoLayout.span, + AccountInfoLayout.span, ); } @@ -206,48 +206,28 @@ export class Token { */ static async createNewToken( connection: Connection, - owner: Account, + payer: Account, + tokenOwner: PublicKey, + accountOwner: PublicKey, supply: TokenAmount, decimals: number, programId: PublicKey, is_owned: boolean = false, ): Promise { + let transaction; const tokenAccount = new Account(); - const payer = await newAccountWithLamports(connection, 10000000 /* wag */); const token = new Token(connection, tokenAccount.publicKey, programId, payer); - const initialAccountPublicKey = await token.newAccount(owner, null); - - let transaction; - - const commandDataLayout = BufferLayout.struct([ - BufferLayout.u8('instruction'), - Layout.uint64('supply'), - BufferLayout.nu64('decimals'), - ]); - - let data = Buffer.alloc(1024); - { - const encodeLength = commandDataLayout.encode( - { - instruction: 0, // NewToken instruction - supply: supply.toBuffer(), - decimals, - }, - data, - ); - data = data.slice(0, encodeLength); - } + const initialAccountPublicKey = await token.newAccount(accountOwner, null); + // Allocate memory for the account const balanceNeeded = await Token.getMinBalanceRentForExemptToken( connection, ); - - // Allocate memory for the account transaction = SystemProgram.createAccount({ - fromPubkey: owner.publicKey, + fromPubkey: payer.publicKey, newAccountPubkey: tokenAccount.publicKey, lamports: balanceNeeded, - space: TokenLayout.span, + space: TokenInfoLayout.span, programId, }); await sendAndConfirmTransaction( @@ -255,16 +235,35 @@ export class Token { connection, transaction, payer, - owner, tokenAccount, ); + // Create the token let keys = [ {pubkey: tokenAccount.publicKey, isSigner: true, isWritable: false}, - {pubkey: initialAccountPublicKey, isSigner: false, isWritable: true}, ]; + if (supply.toNumber() != 0) { + keys.push({pubkey: initialAccountPublicKey, isSigner: false, isWritable: true}); + } if (is_owned) { - keys.push({pubkey: owner.publicKey, isSigner: true, isWritable: false}); + keys.push({pubkey: tokenOwner, isSigner: false, isWritable: false}); + } + const commandDataLayout = BufferLayout.struct([ + BufferLayout.u8('instruction'), + Layout.uint64('supply'), + BufferLayout.nu64('decimals'), + ]); + let data = Buffer.alloc(1024); + { + const encodeLength = commandDataLayout.encode( + { + instruction: 0, // NewToken instruction + supply: supply.toBuffer(), + decimals, + }, + data, + ); + data = data.slice(0, encodeLength); } transaction = new Transaction().add({ @@ -277,13 +276,17 @@ export class Token { connection, transaction, payer, - owner, tokenAccount, ); return [token, initialAccountPublicKey]; } + // Create payer here to avoid cross-node_modules issues with `instanceof` + static async getAccount(connection: Connection): Promise { + return await newAccountWithLamports(connection, 100000000000 /* wag */); + } + /** * Create a new and empty account. * @@ -295,32 +298,22 @@ export class Token { * @return Public key of the new empty account */ async newAccount( - owner: Account, + owner: PublicKey, source: null | PublicKey = null, ): Promise { const tokenAccount = new Account(); let transaction; - const dataLayout = BufferLayout.struct([BufferLayout.u8('instruction')]); - - const data = Buffer.alloc(dataLayout.span); - dataLayout.encode( - { - instruction: 1, // NewAccount instruction - }, - data, - ); - + // Allocate memory for the token const balanceNeeded = await Token.getMinBalanceRentForExemptAccount( this.connection, ); - // Allocate memory for the token transaction = SystemProgram.createAccount({ - fromPubkey: owner.publicKey, + fromPubkey: this.payer.publicKey, newAccountPubkey: tokenAccount.publicKey, lamports: balanceNeeded, - space: TokenAccountInfoLayout.span, + space: AccountInfoLayout.span, programId: this.programId, }); await sendAndConfirmTransaction( @@ -328,30 +321,36 @@ export class Token { this.connection, transaction, this.payer, - owner, tokenAccount, ); - // Initialize the account + // create the new account const keys = [ {pubkey: tokenAccount.publicKey, isSigner: true, isWritable: true}, - {pubkey: owner.publicKey, isSigner: false, isWritable: false}, - {pubkey: this.token, isSigner: false, isWritable: false}, + {pubkey: owner, isSigner: false, isWritable: false}, + {pubkey: this.publicKey, isSigner: false, isWritable: false}, ]; if (source) { keys.push({pubkey: source, isSigner: false, isWritable: false}); } + const dataLayout = BufferLayout.struct([BufferLayout.u8('instruction')]); + const data = Buffer.alloc(dataLayout.span); + dataLayout.encode( + { + instruction: 1, // NewAccount instruction + }, + data, + ); transaction = new Transaction().add({ keys, programId: this.programId, data, }); await sendAndConfirmTransaction( - 'init account', + 'new account', this.connection, transaction, this.payer, - owner, tokenAccount, ); @@ -361,30 +360,30 @@ export class Token { /** * Retrieve token information */ - async TokenInfo(): Promise { - const TokenAccountInfo = await this.connection.getAccountInfo(this.token); - if (TokenAccountInfo === null) { + async getTokenInfo(): Promise { + const accountInfo = await this.connection.getAccountInfo(this.publicKey); + if (accountInfo === null) { throw new Error('Failed to find token info account'); } - if (!TokenAccountInfo.owner.equals(this.programId)) { + if (!accountInfo.owner.equals(this.programId)) { throw new Error( - `Invalid token owner: ${JSON.stringify(TokenAccountInfo.owner)}`, + `Invalid token owner: ${JSON.stringify(accountInfo.owner)}`, ); } - const data = Buffer.from(TokenAccountInfo.data); + const data = Buffer.from(accountInfo.data); - const TokenInfo = TokenLayout.decode(data); - if (TokenInfo.state !== 1) { + const tokenInfo = TokenInfoLayout.decode(data); + if (tokenInfo.state !== 1) { throw new Error(`Invalid account data`); } - TokenInfo.supply = TokenAmount.fromBuffer(TokenInfo.supply); - if (TokenInfo.option === 0) { - TokenInfo.owner = null; + tokenInfo.supply = TokenAmount.fromBuffer(tokenInfo.supply); + if (tokenInfo.option === 0) { + tokenInfo.owner = null; } else { - TokenInfo.owner = new PublicKey(TokenInfo.owner); + tokenInfo.owner = new PublicKey(tokenInfo.owner); } - return TokenInfo; + return tokenInfo; } /** @@ -392,42 +391,42 @@ export class Token { * * @param account Public key of the account */ - async TokenAccountInfo(account: PublicKey): Promise { - const TokenAccountInfo = await this.connection.getAccountInfo(account); - if (TokenAccountInfo === null) { + async getAccountInfo(account: PublicKey): Promise { + const accountInfo = await this.connection.getAccountInfo(account); + if (accountInfo === null) { throw new Error('Failed to find account'); } - if (!TokenAccountInfo.owner.equals(this.programId)) { + if (!accountInfo.owner.equals(this.programId)) { throw new Error(`Invalid account owner`); } - const data = Buffer.from(TokenAccountInfo.data); - const tokenTokenAccountInfo = TokenAccountInfoLayout.decode(data); + const data = Buffer.from(accountInfo.data); + const tokenAccountInfo = AccountInfoLayout.decode(data); - if (tokenTokenAccountInfo.state !== 2) { + if (tokenAccountInfo.state !== 2) { throw new Error(`Invalid account data`); } - tokenTokenAccountInfo.token = new PublicKey(tokenTokenAccountInfo.token); - tokenTokenAccountInfo.owner = new PublicKey(tokenTokenAccountInfo.owner); - tokenTokenAccountInfo.amount = TokenAmount.fromBuffer(tokenTokenAccountInfo.amount); - if (tokenTokenAccountInfo.option === 0) { - tokenTokenAccountInfo.source = null; - tokenTokenAccountInfo.originalAmount = new TokenAmount(); + tokenAccountInfo.token = new PublicKey(tokenAccountInfo.token); + tokenAccountInfo.owner = new PublicKey(tokenAccountInfo.owner); + tokenAccountInfo.amount = TokenAmount.fromBuffer(tokenAccountInfo.amount); + if (tokenAccountInfo.option === 0) { + tokenAccountInfo.source = null; + tokenAccountInfo.originalAmount = new TokenAmount(); } else { - tokenTokenAccountInfo.source = new PublicKey(tokenTokenAccountInfo.source); - tokenTokenAccountInfo.originalAmount = TokenAmount.fromBuffer( - tokenTokenAccountInfo.originalAmount, + tokenAccountInfo.source = new PublicKey(tokenAccountInfo.source); + tokenAccountInfo.originalAmount = TokenAmount.fromBuffer( + tokenAccountInfo.originalAmount, ); } - if (!tokenTokenAccountInfo.token.equals(this.token)) { + if (!tokenAccountInfo.token.equals(this.publicKey)) { throw new Error( `Invalid account token: ${JSON.stringify( - tokenTokenAccountInfo.token, - )} !== ${JSON.stringify(this.token)}`, + tokenAccountInfo.token, + )} !== ${JSON.stringify(this.publicKey)}`, ); } - return tokenTokenAccountInfo; + return tokenAccountInfo; } /** @@ -533,14 +532,13 @@ export class Token { */ async mintTo( owner: Account, - token: PublicKey, dest: PublicKey, amount: number, ): Promise { await sendAndConfirmTransaction( 'mintTo', this.connection, - new Transaction().add(this.mintToInstruction(owner, token, dest, amount)), + new Transaction().add(this.mintToInstruction(owner.publicKey, dest, amount)), this.payer, owner, ); @@ -561,7 +559,7 @@ export class Token { await sendAndConfirmTransaction( 'burn', this.connection, - new Transaction().add(await this.burnInstruction(owner, account, amount)), + new Transaction().add(await this.burnInstruction(owner.publicKey, account, amount)), this.payer, owner, ); @@ -581,8 +579,8 @@ export class Token { destination: PublicKey, amount: number | TokenAmount, ): Promise { - const TokenAccountInfo = await this.TokenAccountInfo(source); - if (!owner.equals(TokenAccountInfo.owner)) { + const accountInfo = await this.getAccountInfo(source); + if (!owner.equals(accountInfo.owner)) { throw new Error('Account owner mismatch'); } @@ -605,9 +603,9 @@ export class Token { {pubkey: source, isSigner: false, isWritable: true}, {pubkey: destination, isSigner: false, isWritable: true}, ]; - if (TokenAccountInfo.source) { + if (accountInfo.source) { keys.push({ - pubkey: TokenAccountInfo.source, + pubkey: accountInfo.source, isSigner: false, isWritable: true, }); @@ -712,11 +710,10 @@ export class Token { * @param token Public key of the token * @param owner Owner of the token * @param dest Public key of the account to mint to - * @param amount ammount to mint + * @param amount amount to mint */ mintToInstruction( - owner: Account, - token: PublicKey, + owner: PublicKey, dest: PublicKey, amount: number, ): TransactionInstruction { @@ -736,8 +733,8 @@ export class Token { return new TransactionInstruction({ keys: [ - {pubkey: owner.publicKey, isSigner: true, isWritable: false}, - {pubkey: token, isSigner: false, isWritable: true}, + {pubkey: owner, isSigner: true, isWritable: false}, + {pubkey: this.publicKey, isSigner: false, isWritable: true}, {pubkey: dest, isSigner: false, isWritable: true}, ], programId: this.programId, @@ -753,11 +750,11 @@ export class Token { * @param amount ammount to burn */ async burnInstruction( - owner: Account, + owner: PublicKey, account: PublicKey, amount: number, ): Promise { - const TokenAccountInfo = await this.TokenAccountInfo(account); + const accountInfo = await this.getAccountInfo(account); const dataLayout = BufferLayout.struct([ BufferLayout.u8('instruction'), @@ -774,13 +771,13 @@ export class Token { ); const keys = [ - {pubkey: owner.publicKey, isSigner: true, isWritable: false}, + {pubkey: owner, isSigner: true, isWritable: false}, {pubkey: account, isSigner: false, isWritable: true}, - {pubkey: this.token, isSigner: false, isWritable: true}, + {pubkey: this.publicKey, isSigner: false, isWritable: true}, ]; - if (TokenAccountInfo.source) { + if (accountInfo.source) { keys.push({ - pubkey: TokenAccountInfo.source, + pubkey: accountInfo.source, isSigner: false, isWritable: true, }); diff --git a/token/js/package-lock.json b/token/js/package-lock.json index 3c5aa0cee946d5..5375950cb7bb47 100644 --- a/token/js/package-lock.json +++ b/token/js/package-lock.json @@ -9424,9 +9424,9 @@ }, "dependencies": { "@babel/runtime": { - "version": "7.10.3", - "resolved": "https://registry.npmjs.org/@babel/runtime/-/runtime-7.10.3.tgz", - "integrity": "sha512-RzGO0RLSdokm9Ipe/YD+7ww8X2Ro79qiXZF3HU9ljrM+qnJmH1Vqth+hbiQZy761LnMJTMitHDuKVYTk3k4dLw==", + "version": "7.10.4", + "resolved": "https://registry.npmjs.org/@babel/runtime/-/runtime-7.10.4.tgz", + "integrity": "sha512-UpTN5yUJr9b4EX2CnGNWIvER7Ab83ibv0pcvvHc4UOdrBI5jb8bj+32cCwPX6xu0mt2daFNjYhoi+X7beH0RSw==", "requires": { "regenerator-runtime": "^0.13.4" }