Skip to content

Commit

Permalink
rewrite and publish cli (#78)
Browse files Browse the repository at this point in the history
* rewrite and publish cli

* pkg

* fix ci

* fix task

* restore cargo workflow
  • Loading branch information
juliangruber authored Apr 8, 2024
1 parent 554b206 commit 9d61759
Show file tree
Hide file tree
Showing 32 changed files with 6,792 additions and 392 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/ci.yaml → .github/workflows/cargo.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
name: Tests
name: Cargo

on:
workflow_dispatch:
Expand Down
10 changes: 10 additions & 0 deletions .github/workflows/cli.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
name: CI
on: [push]
jobs:
build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: actions/setup-node@v4
- run: cd cli && npm ci
- run: cd cli && npm test
40 changes: 37 additions & 3 deletions cli/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,49 @@

## Authentication

Get a token from https://auth.node.glif.io/, then pass `GLIF_TOKEN=...` with
Get a token from https://auth.node.glif.io/, then pass `GLIF_TOKEN=...` with
every CLI invocation.

## Ledger

Keep your HW ledger device connected and unlocked on the Ethereum app.

## Example
## Installation

```bash
GLIF_TOKEN=abc node bin/current-round-index.js
$ npm install --global @meridian-ie/cli
```

## API

```bash
$ meridian --help
meridian <command>

Commands:
meridian current-round-index Get the current round index
meridian list-transfers List transfers from last 100 blocks
meridian Watch the participant count
participant-count-scheduled-for-transfer scheduled for transfer
meridian ready-for-transfer List participants ready for transfer
meridian release-rewards Release rewards
meridian rewards-scheduled-for Get rewards scheduled for a
<participant> participant
meridian round-reward Get the current round reward
meridian scheduled-for-transfer Get all participants scheduled for
transfer
meridian set-min-balance-for-transfer Set the minimum balance for transfer
<minBalance>
meridian set-next-round-length <blocks> Set the next round length
meridian set-round-reward <reward> Set the round reward
meridian tick Trigger a tick

Options:
-a, --address Contract address (or $MERIDIAN_ADDRESS) [string] [required]
-g, --glif-token GLIF token (or $MERIDIAN_GLIF_TOKEN) [string] [required]
-v, --version Show version number [boolean]
-h, --help Show help [boolean]

Examples:
$ meridian current-round-index Get the current round index
```
3 changes: 0 additions & 3 deletions cli/bin/current-round-index.js

This file was deleted.

20 changes: 0 additions & 20 deletions cli/bin/list-transfers.js

This file was deleted.

130 changes: 130 additions & 0 deletions cli/bin/meridian.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,130 @@
#!/usr/bin/env node

import yargs from 'yargs/yargs'
import { hideBin } from 'yargs/helpers'
import fs from 'node:fs/promises'
import { fileURLToPath } from 'node:url'

import { currentRoundIndex } from '../commands/current-round-index.js'
import { listTransfers } from '../commands/list-transfers.js'
import {
participantCountScheduledForTransfer
} from '../commands/participant-count-scheduled-for-transfer.js'
import { readyForTransfer } from '../commands/ready-for-transfer.js'
import { releaseRewards } from '../commands/release-rewards.js'
import { rewardsScheduledFor } from '../commands/rewards-scheduled-for.js'
import { roundReward } from '../commands/round-reward.js'
import { scheduledForTransfer } from '../commands/scheduled-for-transfer.js'
import {
setMinBalanceForTransfer
} from '../commands/set-min-balance-for-transfer.js'
import { setNextRoundLength } from '../commands/set-next-round-length.js'
import { setRoundReward } from '../commands/set-round-reward.js'
import { tick } from '../commands/tick.js'

const pkg = JSON.parse(
await fs.readFile(
fileURLToPath(new URL('../package.json', import.meta.url)),
'utf8'
)
)

yargs(hideBin(process.argv))
.env('MERIDIAN')
.option('address', {
alias: 'a',
type: 'string',
description: 'Contract address (or $MERIDIAN_ADDRESS)'
})
.option('glif-token', {
alias: 'g',
type: 'string',
description: 'GLIF token (or $MERIDIAN_GLIF_TOKEN)'
})
.demandOption('address')
.demandOption('glif-token')
.command(
'current-round-index',
'Get the current round index',
yargs => yargs,
currentRoundIndex
)
.command(
'list-transfers',
'List transfers from last 100 blocks',
yargs => yargs,
listTransfers
)
.command(
'participant-count-scheduled-for-transfer',
'Watch the participant count scheduled for transfer',
yargs => yargs.option('watch', {
alias: 'w',
type: 'boolean',
default: false
}),
participantCountScheduledForTransfer
)
.command(
'ready-for-transfer',
'List participants ready for transfer',
yargs => yargs,
readyForTransfer
)
.command(
'release-rewards',
'Release rewards',
yargs => yargs,
releaseRewards
)
.command(
'rewards-scheduled-for <participant>',
'Get rewards scheduled for a participant',
yargs => yargs.positional('participant', { type: 'string' }),
rewardsScheduledFor
)
.command(
'round-reward',
'Get the current round reward',
yargs => yargs,
roundReward
)
.command(
'scheduled-for-transfer',
'Get all participants scheduled for transfer',
yargs => yargs,
scheduledForTransfer
)
.command(
'set-min-balance-for-transfer <minBalance>',
'Set the minimum balance for transfer',
yargs => yargs.positional('minBalance', { type: 'string' }),
setMinBalanceForTransfer
)
.command(
'set-next-round-length <blocks>',
'Set the next round length',
yargs => yargs.positional('blocks', { type: 'number' }),
setNextRoundLength
)
.command(
'set-round-reward <reward>',
'Set the round reward',
yargs => yargs.positional('reward', { type: 'string' }),
setRoundReward
)
.command(
'tick',
'Trigger a tick',
yargs => yargs,
tick
)
.demandCommand()
.version(`${pkg.name}: ${pkg.version}`)
.alias('v', 'version')
.alias('h', 'help')
.example([
['$ $0 current-round-index', 'Get the current round index']
])
.help()
.parse()
12 changes: 0 additions & 12 deletions cli/bin/participant-count-scheduled-for-transfer.js

This file was deleted.

13 changes: 0 additions & 13 deletions cli/bin/ready-for-transfer.js

This file was deleted.

21 changes: 0 additions & 21 deletions cli/bin/release-rewards.js

This file was deleted.

6 changes: 0 additions & 6 deletions cli/bin/rewards-scheduled-for.js

This file was deleted.

3 changes: 0 additions & 3 deletions cli/bin/round-reward.js

This file was deleted.

9 changes: 0 additions & 9 deletions cli/bin/scheduled-for-transfer.js

This file was deleted.

9 changes: 0 additions & 9 deletions cli/bin/set-min-balance-for-transfer.js

This file was deleted.

10 changes: 0 additions & 10 deletions cli/bin/set-next-round-length.js

This file was deleted.

11 changes: 0 additions & 11 deletions cli/bin/set-round-reward.js

This file was deleted.

4 changes: 0 additions & 4 deletions cli/bin/tick.js

This file was deleted.

6 changes: 6 additions & 0 deletions cli/commands/current-round-index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import { createContract } from '../index.js'

export const currentRoundIndex = async opts => {
const { contract } = createContract(opts)
console.log((await contract.currentRoundIndex()).toString())
}
23 changes: 23 additions & 0 deletions cli/commands/list-transfers.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import { createContract } from '../index.js'
import { formatEther } from 'ethers'

export const listTransfers = async opts => {
const { contract } = await createContract(opts)
console.error('Warning: Only showing transfers from the last 100 blocks')
const events = await contract.queryFilter(contract.filters.Transfer, -100)
const keys = new Set()
let sum = 0n

for (const event of events) {
const [to, amount] = event.args
const { blockNumber } = event
const key = `${to}:${amount}:${blockNumber}`
if (!keys.has(key)) {
console.log(`- ${to}: ${formatEther(amount)}`)
keys.add(key)
sum += amount
}
}

console.log(`Total: ${formatEther(sum)} / ${sum} attoFIL`)
}
12 changes: 12 additions & 0 deletions cli/commands/participant-count-scheduled-for-transfer.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { createContract } from '../index.js'
import timers from 'node:timers/promises'

export const participantCountScheduledForTransfer = async ({ watch, ...opts }) => {
const { contract } = await createContract(opts)
while (true) {
const transfersLeft = await contract.participantCountScheduledForTransfer()
console.log('Transfers left:', transfersLeft)
if (!watch) break
await timers.setTimeout(10_000)
}
}
16 changes: 16 additions & 0 deletions cli/commands/ready-for-transfer.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import { createContract } from '../index.js'
import { formatEther } from 'ethers'

export const readyForTransfer = async opts => {
const { contract } = await createContract(opts)
for (let i = 0; ; i++) {
try {
const address = await contract.readyForTransfer(i)
const amount = await contract.rewardsScheduledFor(address)
console.log(`${address} FIL ${formatEther(amount)}`)
} catch (err) {
break
}
}
process.exit()
}
Loading

0 comments on commit 9d61759

Please sign in to comment.