Skip to content

Commit

Permalink
display whitelist as table (#275)
Browse files Browse the repository at this point in the history
* switches output of whitelist to a table as is standard for the cli

also perhapse these columns headers are more understandable

* add flags

* add changeset

* now test tables

* update docs
  • Loading branch information
aaronmgdr authored Jul 24, 2024
1 parent 2c028d0 commit ac736ff
Show file tree
Hide file tree
Showing 5 changed files with 169 additions and 39 deletions.
5 changes: 5 additions & 0 deletions .changeset/nasty-clocks-hear.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@celo/celocli': patch
---

network:whitelist now oututs as a table, typical table formatting flags are now accepted such as --csv
42 changes: 34 additions & 8 deletions docs/command-line-interface/network.md
Original file line number Diff line number Diff line change
Expand Up @@ -114,16 +114,42 @@ List the whitelisted fee currencies

```
USAGE
$ celocli network:whitelist [--gasCurrency <value>] [--globalHelp]
$ celocli network:whitelist [--gasCurrency <value>] [--globalHelp] [--columns <value>
| -x] [--filter <value>] [--no-header | [--csv | --no-truncate]] [--output
csv|json|yaml | | ] [--sort <value>]
FLAGS
--gasCurrency=0x1234567890123456789012345678901234567890 Use a specific gas currency
for transaction fees
(defaults to CELO if no gas
currency is supplied). It
must be a whitelisted token.
--globalHelp View all available global
flags
-x, --extended
show extra columns
--columns=<value>
only show provided columns (comma-separated)
--csv
output is csv format [alias: --output=csv]
--filter=<value>
filter property by partial string matching, ex: name=foo
--gasCurrency=0x1234567890123456789012345678901234567890
Use a specific gas currency for transaction fees (defaults to CELO if no gas
currency is supplied). It must be a whitelisted token.
--globalHelp
View all available global flags
--no-header
hide table header from output
--no-truncate
do not truncate output to fit screen
--output=<option>
output in a more machine friendly format
<options: csv|json|yaml>
--sort=<value>
property to sort by (prepend '-' for descending)
DESCRIPTION
List the whitelisted fee currencies
Expand Down
84 changes: 71 additions & 13 deletions packages/cli/src/commands/network/whitelist-l2.test.ts
Original file line number Diff line number Diff line change
@@ -1,27 +1,85 @@
import { setupL2, testWithAnvil } from '@celo/dev-utils/lib/anvil-test'
import { ux } from '@oclif/core'
import Web3 from 'web3'
import { testLocallyWithWeb3Node } from '../../test-utils/cliUtils'
import { stripAnsiCodesFromNestedArray, testLocallyWithWeb3Node } from '../../test-utils/cliUtils'
import Whitelist from './whitelist'

process.env.NO_SYNCCHECK = 'true'

afterAll(() => {
jest.clearAllMocks()
})

testWithAnvil('network:whitelist cmd', (web3: Web3) => {
test('can print the whitelist', async () => {
const spy = jest.spyOn(console, 'log')

beforeEach(async () => {
await setupL2(web3)
})

const writeMock = jest.spyOn(ux.write, 'stdout')

afterAll(() => {
jest.clearAllMocks()
})

it('can print the whitelist', async () => {
await testLocallyWithWeb3Node(Whitelist, [], web3)

expect(spy.mock.calls[0][0]).toMatchInlineSnapshot(`
"Available currencies:
0x0c6a0fde0A72bA3990870f0F99ED79a821703474 - Celo Euro (Celo Euro) - 18 decimals
0x603931FF5E63d2fd3EEF1513a55fB773d8082195 - Celo Brazilian Real (Celo Brazilian Real) - 18 decimals
0x82398F079D742F9D0Ae71ef8C99E5c68b2eD6705 - Celo Dollar (Celo Dollar) - 18 decimals"
expect(stripAnsiCodesFromNestedArray(writeMock.mock.calls)).toMatchInlineSnapshot(`
[
[
" Name Symbol Whitelisted Address Token Address Decimals Uses Adapter?
",
],
[
" ─────────────────── ─────────────────── ────────────────────────────────────────── ────────────────────────────────────────── ──────── ─────────────
",
],
[
" Celo Euro Celo Euro 0x0c6a0fde0A72bA3990870f0F99ED79a821703474 0x0c6a0fde0A72bA3990870f0F99ED79a821703474 18 false
",
],
[
" Celo Brazilian Real Celo Brazilian Real 0x603931FF5E63d2fd3EEF1513a55fB773d8082195 0x603931FF5E63d2fd3EEF1513a55fB773d8082195 18 false
",
],
[
" Celo Dollar Celo Dollar 0x82398F079D742F9D0Ae71ef8C99E5c68b2eD6705 0x82398F079D742F9D0Ae71ef8C99E5c68b2eD6705 18 false
",
],
]
`)
})
it('modifies output when formating flag is passed', async () => {
await testLocallyWithWeb3Node(Whitelist, ['--output=json'], web3)

expect(writeMock.mock.calls).toMatchInlineSnapshot(`
[
[
"[
{
"name": "Celo Euro",
"symbol": "Celo Euro",
"whitelisted": "0x0c6a0fde0A72bA3990870f0F99ED79a821703474",
"token": "0x0c6a0fde0A72bA3990870f0F99ED79a821703474",
"decimals": "18",
"usesAdapter": "false"
},
{
"name": "Celo Brazilian Real",
"symbol": "Celo Brazilian Real",
"whitelisted": "0x603931FF5E63d2fd3EEF1513a55fB773d8082195",
"token": "0x603931FF5E63d2fd3EEF1513a55fB773d8082195",
"decimals": "18",
"usesAdapter": "false"
},
{
"name": "Celo Dollar",
"symbol": "Celo Dollar",
"whitelisted": "0x82398F079D742F9D0Ae71ef8C99E5c68b2eD6705",
"token": "0x82398F079D742F9D0Ae71ef8C99E5c68b2eD6705",
"decimals": "18",
"usesAdapter": "false"
}
]
",
],
]
`)
})
})
53 changes: 42 additions & 11 deletions packages/cli/src/commands/network/whitelist.test.ts
Original file line number Diff line number Diff line change
@@ -1,27 +1,46 @@
import { FeeCurrencyWhitelistWrapper } from '@celo/contractkit/lib/wrappers/FeeCurrencyWhitelistWrapper'
import { testWithGanache } from '@celo/dev-utils/lib/ganache-test'
import { testLocally } from '../../test-utils/cliUtils'
import { ux } from '@oclif/core'
import { stripAnsiCodesFromNestedArray, testLocally } from '../../test-utils/cliUtils'
import Whitelist from './whitelist'

process.env.NO_SYNCCHECK = 'true'

// Lots of commands, sometimes times out
jest.setTimeout(15000)

const spy = jest.spyOn(console, 'log')
const writeMock = jest.spyOn(ux.write, 'stdout')

beforeEach(() => {
spy.mockClear()
writeMock.mockClear()
})

testWithGanache('network:whitelist cmd', () => {
test('can print the whitelist', async () => {
await testLocally(Whitelist, [])
expect(spy.mock.calls[0][0]).toMatchInlineSnapshot(`
"Available currencies:
0x5315e44798395d4a952530d131249fE00f554565 - Celo Dollar (cUSD) - 18 decimals
0x965D352283a3C8A016b9BBbC9bf6306665d495E7 - Celo Brazilian Real (cREAL) - 18 decimals
0xdD66C23e07b4D6925b6089b5Fe6fc9E62941aFE8 - Celo Euro (cEUR) - 18 decimals"
expect(stripAnsiCodesFromNestedArray(writeMock.mock.calls)).toMatchInlineSnapshot(`
[
[
" Name Symbol Whitelisted Address Token Address Decimals Uses Adapter?
",
],
[
" ─────────────────── ────── ────────────────────────────────────────── ────────────────────────────────────────── ──────── ─────────────
",
],
[
" Celo Dollar cUSD 0x5315e44798395d4a952530d131249fE00f554565 0x5315e44798395d4a952530d131249fE00f554565 18 false
",
],
[
" Celo Brazilian Real cREAL 0x965D352283a3C8A016b9BBbC9bf6306665d495E7 0x965D352283a3C8A016b9BBbC9bf6306665d495E7 18 false
",
],
[
" Celo Euro cEUR 0xdD66C23e07b4D6925b6089b5Fe6fc9E62941aFE8 0xdD66C23e07b4D6925b6089b5Fe6fc9E62941aFE8 18 false
",
],
]
`)
})

Expand All @@ -42,9 +61,21 @@ testWithGanache('network:whitelist cmd', () => {

await testLocally(Whitelist, [])

expect(spy.mock.calls[0][0]).toMatchInlineSnapshot(`
"Available currencies:
0x123 - mock token (MCK) (adapted token: 0x456) - 69 decimals"
expect(stripAnsiCodesFromNestedArray(writeMock.mock.calls)).toMatchInlineSnapshot(`
[
[
" Name Symbol Whitelisted Address Token Address Decimals Uses Adapter?
",
],
[
" ────────── ────── ─────────────────── ───────────── ──────── ─────────────
",
],
[
" mock token MCK 0x123 0x456 69 true
",
],
]
`)

mock.mockClear()
Expand Down
24 changes: 17 additions & 7 deletions packages/cli/src/commands/network/whitelist.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { ux } from '@oclif/core'
import { BaseCommand } from '../../base'
import { getFeeCurrencyContractWrapper } from '../../utils/fee-currency'

Expand All @@ -6,6 +7,7 @@ export default class Whitelist extends BaseCommand {

static flags = {
...BaseCommand.flags,
...(ux.table.flags() as object),
}

static args = {}
Expand All @@ -14,16 +16,24 @@ export default class Whitelist extends BaseCommand {

async run() {
const kit = await this.getKit()
const { flags } = await this.parse(Whitelist)

const feeCurrencyContract = await getFeeCurrencyContractWrapper(kit, await this.isCel2())
const validFeeCurrencies = await feeCurrencyContract.getAddresses()

const pairs = (await feeCurrencyContract.getFeeCurrencyInformation(validFeeCurrencies)).map(
({ name, symbol, address, adaptedToken, decimals }) =>
`${address} - ${name || 'unknown name'} (${symbol || 'N/A'})${
adaptedToken ? ` (adapted token: ${adaptedToken})` : ''
} - ${decimals} decimals`
const pairs = await feeCurrencyContract.getFeeCurrencyInformation(validFeeCurrencies)

ux.table(
pairs.map((token) => token),
{
name: { get: (token) => token.name },
symbol: { get: (token) => token.symbol },
whitelisted: { get: (token) => token.address, header: 'Whitelisted Address' },
token: { get: (token) => token.adaptedToken || token.address, header: 'Token Address' },
decimals: { get: (token) => token.decimals },
usesAdapter: { get: (token) => !!token.adaptedToken, header: 'Uses Adapter?' },
},
flags
)
// if we use ux.table for this instead then people could pass --csv or --json to get the data how they need it
console.log(`Available currencies:\n${pairs.join('\n')}`)
}
}

0 comments on commit ac736ff

Please sign in to comment.