Skip to content

Commit

Permalink
Merge pull request #29 from freespek/igor/cli-ux
Browse files Browse the repository at this point in the history
Add mock CLI commands
  • Loading branch information
konnov authored Apr 18, 2024
2 parents fdb5cc3 + 2b24f08 commit a4a065b
Show file tree
Hide file tree
Showing 4 changed files with 122 additions and 17 deletions.
34 changes: 34 additions & 0 deletions solarkraft/src/fetch.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
/**
* @license
* [Apache-2.0](https://github.com/freespek/solarkraft/blob/main/LICENSE)
*/
/**
* Fetch transactions from the ledger
* @param args the arguments parsed by yargs
*/
export function fetch(args: any) {
console.log(
`*** WARNING: THIS IS A MOCK. NOTHING IS IMPLEMENTED YET. ***\n`
)
console.log(`Target contract: ${args.id}...`)
console.log(`Fetching fresh transactions from: ${args.rpc}...`)
// read the latest height cached from the latest invocation of fetch
const cachedHeight = 1000
// fetch the actual height from the RPC endpoint
const currentHeight = 12345
let startHeight =
args.height < 0 ? currentHeight + args.height : args.height
startHeight = Math.max(startHeight, cachedHeight)
console.log(`| ledger | cached | start |`)
console.log(`| ${currentHeight} | ${cachedHeight} | ${startHeight} |\n`)
// fetch the transactions that involve that target contract
console.log(
`TX b6efc58ab03db82b5b2fa44b69ff89b64e54af5e6e5266dbdd70fc57d2dc583e at 51291021`
)
console.log(
`TX d669f322d1011a3726301535f5451ef4398d40ad150b79845fb9a5fc781092cf at 51291024`
)
console.log(
`TX cf96fc2bb266912f8063c6091a70a680498bbe41e71132814f765186926e4f80 at 51291018`
)
}
25 changes: 25 additions & 0 deletions solarkraft/src/list.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
/**
* @license
* [Apache-2.0](https://github.com/freespek/solarkraft/blob/main/LICENSE)
*/
/**
* List the transactions fetched from the ledger
*/
export function list() {
console.log(
`*** WARNING: THIS IS A MOCK. NOTHING IS IMPLEMENTED YET. ***\n`
)

console.log(`Verified:`)
console.log(
`TX d669f322d1011a3726301535f5451ef4398d40ad150b79845fb9a5fc781092cf at 51291024`
)
console.log(
`TX cf96fc2bb266912f8063c6091a70a680498bbe41e71132814f765186926e4f80 at 51291018`
)
console.log('')
console.log(`Unverified:`)
console.log(
`TX b6efc58ab03db82b5b2fa44b69ff89b64e54af5e6e5266dbdd70fc57d2dc583e at 51291021`
)
}
65 changes: 48 additions & 17 deletions solarkraft/src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,37 +7,68 @@

import yargs from 'yargs'
import { version } from './version.js'
import { fetch } from './fetch.js'
import { verify } from './verify.js'
import { list } from './list.js'

// The default options present in every command
const defaultOpts = (yargs: any) =>
yargs.option('color', {
desc: 'color output',
type: 'boolean',
default: true,
desc: 'color output',
type: 'boolean',
default: true,
})

// transaction extractor
const txExtractorCmd = {
command: ['txs'],
desc: 'extract transactions',
// fetch: transaction extractor
const fetchCmd = {
command: ['fetch'],
desc: 'fetch Soroban transactions from Stellar',
builder: (yargs: any) =>
defaultOpts(yargs)
.option('id', {
desc: 'Contract id',
type: 'string',
default: '123',
defaultOpts(yargs)
.option('id', {
desc: 'Contract id',
type: 'string',
default: '123',
})
.option('rpc', {
desc: 'URL of the Stellar RPC',
type: 'string',
default: 'http://localhost:8000',
})
.option('height', {
desc: 'The height to start with (a negative value -n goes from the latest block - n)',
type: 'number',
default: -10,
}),
handler: fetch,
}

// verify: transaction verifier
const verifyCmd = {
command: ['verify'],
desc: 'verify a previously fetched transaction',
builder: (yargs: any) =>
defaultOpts(yargs).option('txHash', {
desc: 'Transaction hash',
type: 'string',
demandOption: true,
}),
handler: onTxExtractor,
handler: verify,
}

// call the transaction extractor here
function onTxExtractor (args: any) {
console.log(`Mock TX Extractor(id: ${args.id})`)
// list: show the fetched and verified transactions
const listCmd = {
command: ['list'],
desc: 'list the fetched and verified transactions',
builder: (yargs: any) => defaultOpts(yargs),
handler: list,
}

function main() {
return yargs(process.argv.slice(2))
.command(txExtractorCmd)
.command(fetchCmd)
.command(verifyCmd)
.command(listCmd)
.demandCommand(1)
.version(version)
.strict()
Expand Down
15 changes: 15 additions & 0 deletions solarkraft/src/verify.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
/**
* @license
* [Apache-2.0](https://github.com/freespek/solarkraft/blob/main/LICENSE)
*/
/**
* Verify transactions fetched from the ledger
* @param args the arguments parsed by yargs
*/
export function verify(args: any) {
console.log(
`*** WARNING: THIS IS A MOCK. NOTHING IS IMPLEMENTED YET. ***\n`
)
console.log(`Verifying transaction: ${args.txHash}...`)
console.log(`[OK]`)
}

0 comments on commit a4a065b

Please sign in to comment.