Skip to content

Commit

Permalink
Fix lint check in CI (solana-labs#519)
Browse files Browse the repository at this point in the history
* Fix lint check in CI

* `npm run lint` now checks that code is properly formatted, instead of
  running the formatter
* Add extra commands to run the linting, include ts definitions file
* Update flow-bin, fix type errors that come up

* Clarify lint vs lint:fix as requested
  • Loading branch information
joncinque authored Sep 23, 2020
1 parent e8bb772 commit 51c4dc6
Show file tree
Hide file tree
Showing 13 changed files with 179 additions and 129 deletions.
2 changes: 1 addition & 1 deletion token-swap/js/client/layout.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ export const uint64 = (property: string = 'uint64'): Object => {
/**
* Layout for a Rust String type
*/
export const rustString = (property: string = 'string') => {
export const rustString = (property: string = 'string'): Object => {
const rsl = BufferLayout.struct(
[
BufferLayout.u32('length'),
Expand Down
70 changes: 44 additions & 26 deletions token-swap/js/client/token-swap.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,17 @@
import assert from 'assert';
import BN from 'bn.js';
import * as BufferLayout from 'buffer-layout';
import type { Connection, TransactionSignature } from '@solana/web3.js';
import { Account, PublicKey, SystemProgram, Transaction, TransactionInstruction, } from '@solana/web3.js';
import type {Connection, TransactionSignature} from '@solana/web3.js';
import {
Account,
PublicKey,
SystemProgram,
Transaction,
TransactionInstruction,
} from '@solana/web3.js';

import * as Layout from './layout';
import { sendAndConfirmTransaction } from './util/send-and-confirm-transaction';
import {sendAndConfirmTransaction} from './util/send-and-confirm-transaction';

/**
* Some amount of tokens
Expand All @@ -18,7 +24,7 @@ export class Numberu64 extends BN {
/**
* Convert to Buffer representation
*/
toBuffer(): Buffer {
toBuffer(): typeof Buffer {
const a = super.toArray().reverse();
const b = Buffer.from(a);
if (b.length === 8) {
Expand All @@ -34,7 +40,7 @@ export class Numberu64 extends BN {
/**
* Construct a Numberu64 from Buffer representation
*/
static fromBuffer(buffer: Buffer): Numberu64 {
static fromBuffer(buffer: typeof Buffer): Numberu64 {
assert(buffer.length === 8, `Invalid buffer length: ${buffer.length}`);
return new BN(
[...buffer]
Expand Down Expand Up @@ -89,15 +95,17 @@ type TokenSwapInfo = {|
/**
* @private
*/
export const TokenSwapLayout = BufferLayout.struct([
BufferLayout.u8('isInitialized'),
BufferLayout.u8('nonce'),
Layout.publicKey('tokenAccountA'),
Layout.publicKey('tokenAccountB'),
Layout.publicKey('tokenPool'),
Layout.uint64('feesNumerator'),
Layout.uint64('feesDenominator'),
]);
export const TokenSwapLayout: typeof BufferLayout.Structure = BufferLayout.struct(
[
BufferLayout.u8('isInitialized'),
BufferLayout.u8('nonce'),
Layout.publicKey('tokenAccountA'),
Layout.publicKey('tokenAccountB'),
Layout.publicKey('tokenPool'),
Layout.uint64('feesNumerator'),
Layout.uint64('feesDenominator'),
],
);

/**
* An ERC20-like Token
Expand Down Expand Up @@ -153,7 +161,6 @@ export class TokenSwap {
);
}


static createInitSwapInstruction(
tokenSwapAccount: Account,
authority: PublicKey,
Expand All @@ -165,16 +172,16 @@ export class TokenSwap {
tokenProgramId: PublicKey,
swapProgramId: PublicKey,
feeNumerator: number,
feeDenominator: number
):TransactionInstruction {
feeDenominator: number,
): TransactionInstruction {
const keys = [
{ pubkey: tokenSwapAccount.publicKey, isSigner: false, isWritable: true },
{ pubkey: authority, isSigner: false, isWritable: false },
{ pubkey: tokenAccountA, isSigner: false, isWritable: false },
{ pubkey: tokenAccountB, isSigner: false, isWritable: false },
{ pubkey: tokenPool, isSigner: false, isWritable: true },
{ pubkey: tokenAccountPool, isSigner: false, isWritable: true },
{ pubkey: tokenProgramId, isSigner: false, isWritable: false },
{pubkey: tokenSwapAccount.publicKey, isSigner: false, isWritable: true},
{pubkey: authority, isSigner: false, isWritable: false},
{pubkey: tokenAccountA, isSigner: false, isWritable: false},
{pubkey: tokenAccountB, isSigner: false, isWritable: false},
{pubkey: tokenPool, isSigner: false, isWritable: true},
{pubkey: tokenAccountPool, isSigner: false, isWritable: true},
{pubkey: tokenProgramId, isSigner: false, isWritable: false},
];
const commandDataLayout = BufferLayout.struct([
BufferLayout.u8('instruction'),
Expand Down Expand Up @@ -257,7 +264,19 @@ export class TokenSwap {
}),
);

const instruction = TokenSwap.createInitSwapInstruction(tokenSwapAccount, authority, nonce, tokenAccountA, tokenAccountB, tokenPool, tokenAccountPool, tokenProgramId, swapProgramId, feeNumerator, feeDenominator);
const instruction = TokenSwap.createInitSwapInstruction(
tokenSwapAccount,
authority,
nonce,
tokenAccountA,
tokenAccountB,
tokenPool,
tokenAccountPool,
tokenProgramId,
swapProgramId,
feeNumerator,
feeDenominator,
);

transaction.add(instruction);
await sendAndConfirmTransaction(
Expand All @@ -271,7 +290,6 @@ export class TokenSwap {
return tokenSwap;
}


/**
* Retrieve tokenSwap information
*/
Expand Down
10 changes: 6 additions & 4 deletions token-swap/js/client/util/store.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,18 +9,20 @@ import fs from 'mz/fs';
import mkdirp from 'mkdirp-promise';

export class Store {
dir = path.join(__dirname, 'store');
static getDir(): string {
return path.join(__dirname, 'store');
}

async load(uri: string): Promise<Object> {
const filename = path.join(this.dir, uri);
const filename = path.join(Store.getDir(), uri);
const data = await fs.readFile(filename, 'utf8');
const config = JSON.parse(data);
return config;
}

async save(uri: string, config: Object): Promise<void> {
await mkdirp(this.dir);
const filename = path.join(this.dir, uri);
await mkdirp(Store.getDir());
const filename = path.join(Store.getDir(), uri);
await fs.writeFile(filename, JSON.stringify(config), 'utf8');
}
}
51 changes: 31 additions & 20 deletions token-swap/js/module.d.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,13 @@
declare module '@solana/spl-token-swap' {
import { Buffer } from 'buffer';
import { Layout } from 'buffer-layout';
import { PublicKey, TransactionInstruction, TransactionSignature, Connection, Account } from "@solana/web3.js";
import {Buffer} from 'buffer';
import {Layout} from 'buffer-layout';
import {
PublicKey,
TransactionInstruction,
TransactionSignature,
Connection,
Account,
} from '@solana/web3.js';
import BN from 'bn.js';

// === client/token-swap.js ===
Expand All @@ -11,19 +17,24 @@ declare module '@solana/spl-token-swap' {
}

export type TokenSwapInfo = {
nonce: number,
tokenAccountA: PublicKey,
tokenAccountB: PublicKey,
tokenPool: PublicKey,
feesNumerator: Numberu64,
feesDenominator: Numberu64,
feeRatio: number,
nonce: number;
tokenAccountA: PublicKey;
tokenAccountB: PublicKey;
tokenPool: PublicKey;
feesNumerator: Numberu64;
feesDenominator: Numberu64;
feeRatio: number;
};

export const TokenSwapLayout: Layout;

export class TokenSwap {
constructor(connection: Connection, tokenSwap: PublicKey, programId: PublicKey, payer: Account);
constructor(
connection: Connection,
tokenSwap: PublicKey,
programId: PublicKey,
payer: Account,
);

static getMinBalanceRentForExemptTokenSwap(
connection: Connection,
Expand All @@ -40,7 +51,7 @@ declare module '@solana/spl-token-swap' {
tokenProgramId: PublicKey,
swapProgramId: PublicKey,
feeNumerator: number,
feeDenominator: number
feeDenominator: number,
): TransactionInstruction;

static createTokenSwap(
Expand All @@ -57,9 +68,9 @@ declare module '@solana/spl-token-swap' {
feeNumerator: number,
feeDenominator: number,
swapProgramId: PublicKey,
): Promise<TokenSwap>
): Promise<TokenSwap>;

getInfo(): Promise<TokenSwapInfo>
getInfo(): Promise<TokenSwapInfo>;
swap(
authority: PublicKey,
source: PublicKey,
Expand All @@ -68,7 +79,7 @@ declare module '@solana/spl-token-swap' {
destination: PublicKey,
tokenProgramId: PublicKey,
amount: number | Numberu64,
): Promise<TransactionSignature>
): Promise<TransactionSignature>;

static swapInstruction(
tokenSwap: PublicKey,
Expand All @@ -80,7 +91,7 @@ declare module '@solana/spl-token-swap' {
swapProgramId: PublicKey,
tokenProgramId: PublicKey,
amount: number | Numberu64,
): TransactionInstruction
): TransactionInstruction;

deposit(
authority: PublicKey,
Expand All @@ -92,7 +103,7 @@ declare module '@solana/spl-token-swap' {
poolAccount: PublicKey,
tokenProgramId: PublicKey,
amount: number | Numberu64,
): Promise<TransactionSignature>
): Promise<TransactionSignature>;

static depositInstruction(
tokenSwap: PublicKey,
Expand All @@ -106,7 +117,7 @@ declare module '@solana/spl-token-swap' {
swapProgramId: PublicKey,
tokenProgramId: PublicKey,
amount: number | Numberu64,
): TransactionInstruction
): TransactionInstruction;

withdraw(
authority: PublicKey,
Expand All @@ -118,7 +129,7 @@ declare module '@solana/spl-token-swap' {
userAccountB: PublicKey,
tokenProgramId: PublicKey,
amount: number | Numberu64,
): Promise<TransactionSignature>
): Promise<TransactionSignature>;

static withdrawInstruction(
tokenSwap: PublicKey,
Expand All @@ -132,6 +143,6 @@ declare module '@solana/spl-token-swap' {
swapProgramId: PublicKey,
tokenProgramId: PublicKey,
amount: number | Numberu64,
): TransactionInstruction
): TransactionInstruction;
}
}
2 changes: 1 addition & 1 deletion token-swap/js/module.flow.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ declare module '@solana/spl-token-swap' {
tokenAccountPool: PublicKey,
tokenProgramId: PublicKey,
feeNumerator: number,
feeDenominator: number
feeDenominator: number,
): TransactionInstruction;

static createTokenSwap(
Expand Down
6 changes: 3 additions & 3 deletions token-swap/js/package-lock.json

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

7 changes: 4 additions & 3 deletions token-swap/js/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
"build": "rollup -c",
"start": "babel-node --ignore node_modules cli/main.js",
"lint": "npm run pretty && eslint .",
"lint:fix": "npm run lint -- --fix",
"lint:fix": "npm run pretty:fix && eslint . --fix",
"flow": "flow",
"flow:watch": "watch 'flow' . --wait=1 --ignoreDirectoryPattern=/doc/",
"lint:watch": "watch 'npm run lint:fix' . --wait=1",
Expand All @@ -40,7 +40,8 @@
"localnet:up": "rm client/util/store/config.json; set -x; solana-localnet down; set -e; solana-localnet up",
"localnet:down": "solana-localnet down",
"localnet:logs": "solana-localnet logs -f",
"pretty": "prettier --write '{,cli*/**/}*.js'"
"pretty": "prettier --check '{,cli*/**/}*.[jt]s'",
"pretty:fix": "prettier --write '{,cli*/**/}*.[jt]s'"
},
"keywords": [],
"dependencies": {
Expand All @@ -65,7 +66,7 @@
"babel-eslint": "^10.1.0",
"eslint": "^7.9.0",
"eslint-plugin-import": "^2.22.0",
"flow-bin": "0.131.0",
"flow-bin": "0.134.0",
"flow-typed": "^3.2.0",
"mz": "^2.7.0",
"prettier": "^2.1.2",
Expand Down
2 changes: 1 addition & 1 deletion token/js/client/layout.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ export const uint64 = (property: string = 'uint64'): Object => {
/**
* Layout for a Rust String type
*/
export const rustString = (property: string = 'string') => {
export const rustString = (property: string = 'string'): Object => {
const rsl = BufferLayout.struct(
[
BufferLayout.u32('length'),
Expand Down
Loading

0 comments on commit 51c4dc6

Please sign in to comment.