Skip to content

Commit

Permalink
Cleanup payer and remove use of instanceof (solana-labs#176)
Browse files Browse the repository at this point in the history
Co-authored-by: Justin Starry <justin@solana.com>
  • Loading branch information
jackcmay and jstarry authored Jul 31, 2020
1 parent 662a733 commit 302855f
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 40 deletions.
6 changes: 3 additions & 3 deletions token-swap/js/cli/token-swap-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ async function GetPrograms(connection: Connection): Promise<[PublicKey, PublicKe
try {
const config = await store.load('config.json');
console.log('Using pre-loaded Token and Token-swap programs');
console.log(' Note: To reload programs remove client/util/sore/config.json');
console.log(' Note: To reload programs remove client/util/store/config.json');
tokenProgramId = new PublicKey(config.tokenProgramId);
tokenSwapProgramId = new PublicKey(config.tokenSwapProgramId);
} catch (err) {
Expand All @@ -107,8 +107,8 @@ export async function loadPrograms(): Promise<void> {
export async function createTokenSwap(): Promise<void> {
const connection = await getConnection();
const [tokenProgramId, tokenSwapProgramId] = await GetPrograms(connection);
const payer = await Token.getAccount(connection);
owner = await Token.getAccount(connection);
const payer = await newAccountWithLamports(connection, 100000000000 /* wag */);
owner = await newAccountWithLamports(connection, 100000000000 /* wag */);
const tokenSwapAccount = new Account();
authority = await PublicKey.createProgramAddress(
[tokenSwapAccount.publicKey.toString().substring(0, 32)],
Expand Down
3 changes: 3 additions & 0 deletions token-swap/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

66 changes: 32 additions & 34 deletions token/js/client/token.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ import {
} from '@solana/web3.js';
import type {Connection, TransactionSignature} from '@solana/web3.js';

import {newAccountWithLamports} from '../client/util/new-account-with-lamports';
import * as Layout from './layout';
import {sendAndConfirmTransaction} from './util/send-and-confirm-transaction';

Expand Down Expand Up @@ -53,6 +52,10 @@ export class u64 extends BN {
}
}

function isAccount(accountOrPublicKey: any): boolean {
return 'publicKey' in accountOrPublicKey;
}

/**
* Information about the mint
*/
Expand Down Expand Up @@ -364,11 +367,6 @@ export class Token {
return [token, initialAccountPublicKey];
}

// Create payer here to avoid cross-node_modules issues with `instanceof`
static async getAccount(connection: Connection): Promise<Account> {
return await newAccountWithLamports(connection, 100000000000 /* wag */);
}

/**
* Create and initializes a new account.
*
Expand Down Expand Up @@ -602,13 +600,13 @@ export class Token {
async transfer(
source: PublicKey,
destination: PublicKey,
authority: Account | PublicKey,
authority: any,
multiSigners: Array<Account>,
amount: number | u64,
): Promise<?TransactionSignature> {
let ownerPublicKey;
let signers;
if (authority instanceof Account) {
if (isAccount(authority)) {
ownerPublicKey = authority.publicKey;
signers = [authority];
} else {
Expand Down Expand Up @@ -645,13 +643,13 @@ export class Token {
async approve(
account: PublicKey,
delegate: PublicKey,
owner: Account | PublicKey,
owner: any,
multiSigners: Array<Account>,
amount: number | u64,
): Promise<void> {
let ownerPublicKey;
let signers;
if (owner instanceof Account) {
if (isAccount(owner)) {
ownerPublicKey = owner.publicKey;
signers = [owner];
} else {
Expand Down Expand Up @@ -685,12 +683,12 @@ export class Token {
*/
async revoke(
account: PublicKey,
owner: Account | PublicKey,
owner: any,
multiSigners: Array<Account>,
): Promise<void> {
let ownerPublicKey;
let signers;
if (owner instanceof Account) {
if (isAccount(owner)) {
ownerPublicKey = owner.publicKey;
signers = [owner];
} else {
Expand Down Expand Up @@ -724,12 +722,12 @@ export class Token {
async setOwner(
owned: PublicKey,
newOwner: PublicKey,
owner: Account | PublicKey,
owner: any,
multiSigners: Array<Account>,
): Promise<void> {
let ownerPublicKey;
let signers;
if (owner instanceof Account) {
if (isAccount(owner)) {
ownerPublicKey = owner.publicKey;
signers = [owner];
} else {
Expand Down Expand Up @@ -764,13 +762,13 @@ export class Token {
*/
async mintTo(
dest: PublicKey,
authority: Account | PublicKey,
authority: any,
multiSigners: Array<Account>,
amount: number,
): Promise<void> {
let ownerPublicKey;
let signers;
if (authority instanceof Account) {
if (isAccount(authority)) {
ownerPublicKey = authority.publicKey;
signers = [authority];
} else {
Expand Down Expand Up @@ -805,13 +803,13 @@ export class Token {
*/
async burn(
account: PublicKey,
authority: Account | PublicKey,
authority: any,
multiSigners: Array<Account>,
amount: number,
): Promise<void> {
let ownerPublicKey;
let signers;
if (authority instanceof Account) {
if (isAccount(authority)) {
ownerPublicKey = authority.publicKey;
signers = [authority];
} else {
Expand Down Expand Up @@ -845,12 +843,12 @@ export class Token {
async closeAccount(
account: PublicKey,
dest: PublicKey,
owner: Account | PublicKey,
owner: any,
multiSigners: Array<Account>,
): Promise<void> {
let ownerPublicKey;
let signers;
if (owner instanceof Account) {
if (isAccount(owner)) {
ownerPublicKey = owner.publicKey;
signers = [owner];
} else {
Expand Down Expand Up @@ -887,7 +885,7 @@ export class Token {
programId: PublicKey,
source: PublicKey,
destination: PublicKey,
authority: Account | PublicKey,
authority: any,
multiSigners: Array<Account>,
amount: number | u64,
): TransactionInstruction {
Expand All @@ -909,7 +907,7 @@ export class Token {
{pubkey: source, isSigner: false, isWritable: true},
{pubkey: destination, isSigner: false, isWritable: true},
];
if (authority instanceof Account) {
if (isAccount(authority)) {
keys.push({
pubkey: authority.publicKey,
isSigner: true,
Expand Down Expand Up @@ -945,7 +943,7 @@ export class Token {
programId: PublicKey,
account: PublicKey,
delegate: PublicKey,
owner: Account | PublicKey,
owner: any,
multiSigners: Array<Account>,
amount: number | u64,
): TransactionInstruction {
Expand All @@ -967,7 +965,7 @@ export class Token {
{pubkey: account, isSigner: false, isWritable: true},
{pubkey: delegate, isSigner: false, isWritable: false},
];
if (owner instanceof Account) {
if (isAccount(owner)) {
keys.push({pubkey: owner.publicKey, isSigner: true, isWritable: false});
} else {
keys.push({pubkey: owner, isSigner: false, isWritable: false});
Expand Down Expand Up @@ -999,7 +997,7 @@ export class Token {
static createRevokeInstruction(
programId: PublicKey,
account: PublicKey,
owner: Account | PublicKey,
owner: any,
multiSigners: Array<Account>,
): TransactionInstruction {
const dataLayout = BufferLayout.struct([BufferLayout.u8('instruction')]);
Expand All @@ -1013,7 +1011,7 @@ export class Token {
);

let keys = [{pubkey: account, isSigner: false, isWritable: true}];
if (owner instanceof Account) {
if (isAccount(owner)) {
keys.push({pubkey: owner.publicKey, isSigner: true, isWritable: false});
} else {
keys.push({pubkey: owner, isSigner: false, isWritable: false});
Expand Down Expand Up @@ -1045,7 +1043,7 @@ export class Token {
programId: PublicKey,
owned: PublicKey,
newOwner: PublicKey,
owner: Account | PublicKey,
owner: any,
multiSigners: Array<Account>,
): TransactionInstruction {
const dataLayout = BufferLayout.struct([BufferLayout.u8('instruction')]);
Expand All @@ -1062,7 +1060,7 @@ export class Token {
{pubkey: owned, isSigner: false, isWritable: true},
{pubkey: newOwner, isSigner: false, isWritable: false},
];
if (owner instanceof Account) {
if (isAccount(owner)) {
keys.push({pubkey: owner.publicKey, isSigner: true, isWritable: false});
} else {
keys.push({pubkey: owner, isSigner: false, isWritable: false});
Expand Down Expand Up @@ -1095,7 +1093,7 @@ export class Token {
programId: PublicKey,
mint: PublicKey,
dest: PublicKey,
authority: Account | PublicKey,
authority: any,
multiSigners: Array<Account>,
amount: number,
): TransactionInstruction {
Expand All @@ -1117,7 +1115,7 @@ export class Token {
{pubkey: mint, isSigner: false, isWritable: true},
{pubkey: dest, isSigner: false, isWritable: true},
];
if (authority instanceof Account) {
if (isAccount(authority)) {
keys.push({
pubkey: authority.publicKey,
isSigner: true,
Expand Down Expand Up @@ -1152,7 +1150,7 @@ export class Token {
static createBurnInstruction(
programId: PublicKey,
account: PublicKey,
authority: Account | PublicKey,
authority: any,
multiSigners: Array<Account>,
amount: number,
): TransactionInstruction {
Expand All @@ -1171,7 +1169,7 @@ export class Token {
);

let keys = [{pubkey: account, isSigner: false, isWritable: true}];
if (authority instanceof Account) {
if (isAccount(authority)) {
keys.push({
pubkey: authority.publicKey,
isSigner: true,
Expand Down Expand Up @@ -1206,7 +1204,7 @@ export class Token {
programId: PublicKey,
account: PublicKey,
dest: PublicKey,
owner: Account | PublicKey,
owner: any,
multiSigners: Array<Account>,
): TransactionInstruction {
const dataLayout = BufferLayout.struct([BufferLayout.u8('instruction')]);
Expand All @@ -1222,7 +1220,7 @@ export class Token {
{pubkey: account, isSigner: false, isWritable: true},
{pubkey: dest, isSigner: false, isWritable: true},
];
if (owner instanceof Account) {
if (isAccount(owner)) {
keys.push({pubkey: owner.publicKey, isSigner: true, isWritable: false});
} else {
keys.push({pubkey: owner, isSigner: false, isWritable: false});
Expand Down
6 changes: 3 additions & 3 deletions token/js/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 302855f

Please sign in to comment.