Skip to content

Commit

Permalink
Modules refactoring to split to multiple ones
Browse files Browse the repository at this point in the history
  • Loading branch information
ochaloup committed Aug 9, 2023
1 parent b36b5c7 commit c239c43
Show file tree
Hide file tree
Showing 49 changed files with 1,435 additions and 939 deletions.
7 changes: 7 additions & 0 deletions .eslintrc.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,12 @@
"extends": "./node_modules/gts/",
"rules": {
"semi": ["error", "never"]
},
"settings": {
"node": {
// Modules are used by jest dev
"allowModules": ["@jest/globals"]
}
}

}
3 changes: 3 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,15 @@
"borsh",
"commitlint",
"devnet",
"divn",
"GontTwDeBduvbW85oHyC8A7GekuT8X1NkZHDDdUWWvsV",
"HOMEPATH",
"Keypair",
"Lamports",
"ledgerhq",
"localnet",
"MR2LqxoSbw831bNy68utpu5n4YqBH3AzDmddkgk9LQv",
"Msol",
"MsolAmount",
"MsolFees",
"muln",
Expand All @@ -25,6 +27,7 @@
"staker",
"SYSVAR",
"typescript-estree",
"unstake",
"unstaked",
"unstaking"
],
Expand Down
7 changes: 7 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -50,3 +50,10 @@ pnpm cli --help
# to test Solana CLI tools and Anchor in version 0.28.0 is required
pnpm test
```

To get the latest version from npm and run it

```bash
npm i @marinade.finance/marinade-ts-cli
npx @marinade.finance/marinade-ts-cli
```
9 changes: 7 additions & 2 deletions jest.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,12 @@

module.exports = {
preset: 'ts-jest',
testEnvironment: 'node',
testTimeout: 90000,
globalSetup: '<rootDir>/test/setup/globalSetup.ts',
setupFilesAfterEnv: ['<rootDir>/test/setup/equalityTesters.ts'],
detectOpenHandles: true,
modulePathIgnorePatterns: ['<rootDir>/dist/'],
testPathIgnorePatterns: ['__tests__/.*.skip.ts', '__tests__/setup/*'],
globalSetup:
'<rootDir>/packages/marinade-ts-cli/__tests__/setup/globalSetup.ts',
setupFilesAfterEnv: ['<rootDir>/packages/jest-utils/src/equalityTesters.ts'],
}
60 changes: 10 additions & 50 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,66 +1,26 @@
{
"name": "@marinade.finance/marinade-ts-cli",
"version": "5.0.0",
"description": "Marinade typescript-based CLI",
"description": "Marinade typescript-based SDK for CLI",
"license": "ISC",
"author": "Marinade.Finance",
"bin": {
"marinade": "./dist/index.js"
},
"main": "src/index",
"repository": {
"type": "git",
"url": "git+https://github.com/marinade-finance/marinade-ts-cli.git"
},
"publishConfig": {
"access": "public"
},
"files": [
"dist"
],
"type": "commonjs",
"scripts": {
"build": "npx tsc -b -verbose",
"marinade": "node ./dist/index.js",
"cli": "ts-node ./src/index.ts",
"marinade": "node ./dist/packages/marinade-ts-cli/src//index.js",
"cli": "ts-node ./packages/marinade-ts-cli/src/index.ts",
"lint:fix": "gts fix",
"lint": "gts lint",
"test": "anchor test",
"_test": "jest $FILE",
"test:no-anchor": "jest --globalSetup='' $FILE"
},
"keywords": [
"solana",
"marinade.finance",
"blockchain",
"staking"
],
"dependencies": {
"@coral-xyz/anchor": "0.28",
"@ledgerhq/errors": "^6.12.6",
"@ledgerhq/hw-app-solana": "^7.0.9",
"@ledgerhq/hw-transport-node-hid-noevents": "^6.27.15",
"@marinade.finance/marinade-ts-sdk": "^5.0.1",
"@solana/spl-governance": "^0.3.27",
"@solana/spl-token": "^0.3.8",
"bn.js": "^5.2.1",
"borsh": "^0.7.0",
"bs58": "^4.0.1",
"commander": "^11.0.0",
"expand-tilde": "^2.0.2",
"pino": "^8.14.1",
"pino-pretty": "^10.0.0"
"test:no-anchor": "jest --globalSetup='' $FILE",
"publish-all": "pnpm build && pnpm publish dist/packages/cli-common && pnpm publish dist/packages/ledger-utils && pnpm publish dist/packages/marinade-ts-cli"
},
"devDependencies": {
"@jest/globals": "^29.5.0",
"@marinade.finance/solana-test-utils": "^0.0.89",
"@types/bn.js": "^5.1.1",
"@types/bs58": "^4.0.1",
"@types/expand-tilde": "^2.0.0",
"@types/jest": "^29.5.2",
"@types/node": "^16.0.0",
"@typescript-eslint/typescript-estree": "^5.59.11",
"gts": "^3.1.1",
"@typescript-eslint/typescript-estree": "^5.59.1",
"@typescript-eslint/eslint-plugin": "^5.59.1",
"@typescript-eslint/parser": "^5.59.1",
"gts": "^5.0.0",
"husky": "^8.0.3",
"jest": "^29.0.5",
"lint-staged": "^13.2.2",
Expand All @@ -69,7 +29,7 @@
"typescript": "5.0.4"
},
"resolutions": {
"@typescript-eslint/typescript-estree": "^5.59.11"
"@typescript-eslint/typescript-estree": "^5.59.1"
},
"engines": {
"node": ">=16.0.0"
Expand Down
40 changes: 40 additions & 0 deletions packages/cli-common/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
{
"name": "@marinade.finance/cli-common",
"version": "1.0.0",
"description": "CLI tooling",
"repository": {
"type": "git",
"url": "[email protected]:marinade-finance/marinade-ts-cli.git"
},
"publishConfig": {
"access": "public"
},
"scripts": {
"test": "echo \"Error: no test specified, go to project root\" && exit 1"
},
"keywords": [
"solana",
"marinade.finance",
"blockchain",
"cli"
],
"author": "Marinade Finance",
"license": "ISC",
"main": "src/index",
"files": [
"src"
],
"devDependencies": {
"pino": "^8.15.0",
"@solana/web3.js": "^1.78.3",
"borsh": "^0.7.0",
"@types/expand-tilde": "^2.0.0",
"expand-tilde": "^2.0.2"
},
"peerDependencies": {
"pino": "^8.15.0",
"@solana/web3.js": "^1.78.3",
"borsh": "^0.7.0",
"expand-tilde": "^2.0.2"
}
}
52 changes: 52 additions & 0 deletions packages/cli-common/src/context.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
import { Logger } from 'pino'
import { Wallet } from './wallet'

let context: Context | undefined

export function getContext(): Context {
if (!context) {
throw new Error('Context not initialized')
}
return context
}

export function setContext(newContext: Context) {
if (context) {
throw new Error(
'Context already initialized, context can be initialized only once'
)
}
context = newContext
}

export abstract class Context {
readonly wallet: Wallet
readonly logger: Logger
readonly skipPreflight: boolean
readonly simulate: boolean
readonly printOnly: boolean
readonly commandName: string

constructor({
wallet,
logger,
skipPreflight,
simulate,
printOnly,
commandName,
}: {
wallet: Wallet
logger: Logger
skipPreflight: boolean
simulate: boolean
printOnly: boolean
commandName: string
}) {
this.commandName = commandName
this.wallet = wallet
this.logger = logger
this.skipPreflight = skipPreflight
this.simulate = simulate
this.printOnly = printOnly
}
}
15 changes: 10 additions & 5 deletions src/utils/error.ts → packages/cli-common/src/error.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
import { format } from 'util'
import { getContext } from '../context'
import { getContext } from './context'
import { Transaction, VersionedTransaction } from '@solana/web3.js'

export interface WithCommandName {
readonly name: string
readonly age: number
}

export class CliCommandError extends Error {
readonly cause?: Error
readonly logs?: string[]
Expand All @@ -22,12 +27,12 @@ export class CliCommandError extends Error {
logs?: string[]
transaction?: Transaction | VersionedTransaction
}) {
const { command } = getContext()
let errorMessage
const { commandName } = getContext()
let errorMessage: string
if (valueName) {
errorMessage = format('%s[%s=%s]: %s', command, valueName, value, msg)
errorMessage = format('%s[%s=%s]: %s', commandName, valueName, value, msg)
} else {
errorMessage = format('%s:%s', command, msg)
errorMessage = format('%s:%s', commandName, msg)
}
super(errorMessage)
this.cause = cause
Expand Down
7 changes: 7 additions & 0 deletions packages/cli-common/src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
export * from './parsers'
export * from './error'
export * from './time'
export * from './context'
export * from './tx'
export * from './txToBase64'
export * from './wallet'
80 changes: 80 additions & 0 deletions packages/cli-common/src/parsers.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
import {
Keypair,
PublicKey,
Commitment,
clusterApiUrl,
Cluster,
} from '@solana/web3.js'
import expandTilde from 'expand-tilde' // eslint-disable-line node/no-extraneous-import
import { readFile } from 'fs/promises'

export async function parsePubkey(pubkeyOrPath: string): Promise<PublicKey> {
try {
return new PublicKey(pubkeyOrPath)
} catch (err) {
const keypair = await parseKeypair(pubkeyOrPath)
return keypair.publicKey
}
}

export async function parsePubkeyOrKeypair(
pubkeyOrPath: string
): Promise<PublicKey | Keypair> {
try {
return new PublicKey(pubkeyOrPath)
} catch (err) {
return await parseKeypair(pubkeyOrPath)
}
}

export async function parseKeypair(path: string): Promise<Keypair> {
return Keypair.fromSecretKey(
new Uint8Array(JSON.parse(await readFile(expandTilde(path), 'utf-8')))
)
}

export function getClusterUrl(url: string): string {
let clusterUrl =
url === 'd'
? 'devnet'
: url === 't'
? 'testnet'
: url === 'm' || url === 'mainnet'
? 'mainnet-beta'
: url === 'l' || url === 'localnet' || url === 'localhost'
? 'http://localhost:8899'
: url

try {
clusterUrl = clusterApiUrl(clusterUrl as Cluster)
} catch (e) {
// ignore
}
return clusterUrl
}

export function parseCommitment(commitment: string): Commitment {
if (commitment === 'processed') {
return 'processed'
} else if (commitment === 'confirmed') {
return 'confirmed'
} else if (commitment === 'finalized') {
return 'finalized'
} else if (commitment === 'recent') {
return 'recent'
} else if (commitment === 'single') {
return 'single'
} else if (commitment === 'singleGossip') {
return 'singleGossip'
} else if (commitment === 'root') {
return 'root'
} else if (commitment === 'max') {
return 'max'
} else {
throw new Error(
'Invalid value of --commitment: ' +
commitment +
'. Permitted values: processed, confirmed, finalized, recent, single, singleGossip, root, max'
)
}
}
2 changes: 1 addition & 1 deletion src/utils/time.ts → packages/cli-common/src/time.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ export const formatDuration = (seconds: number) => {
{ unit: 'h', mod: 24, pad: 2 },
{ unit: 'd' },
]
const parts = []
const parts: string[] = []
let rem = seconds
for (const { unit, mod, pad } of units) {
const amount = mod ? rem % mod : rem
Expand Down
Loading

0 comments on commit c239c43

Please sign in to comment.