-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Modules refactoring to split to multiple ones
- Loading branch information
Showing
49 changed files
with
1,435 additions
and
939 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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' | ||
) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.