Skip to content

Commit

Permalink
Merge pull request #90 from terra-money/feat/arguments
Browse files Browse the repository at this point in the history
feat: refact arguments and add missing ones
  • Loading branch information
emidev98 authored Mar 24, 2023
2 parents 6f46589 + cd537bc commit 762c4a1
Show file tree
Hide file tree
Showing 38 changed files with 271 additions and 372 deletions.
1 change: 1 addition & 0 deletions .eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ module.exports = {
jsx: false, // Allows for the parsing of JSX
},
},
ignorePatterns: ["**/dist"],
extends: [
'plugin:@typescript-eslint/recommended', // Uses the recommended rules from the @typescript-eslint/eslint-plugin
'plugin:prettier/recommended', // Enables eslint-plugin-prettier and eslint-config-prettier. This will display prettier errors as ESLint errors. Make sure this is always the last configuration in the extends array.
Expand Down
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -35,4 +35,5 @@ yarn-error.log
# Files that must be skipped
default.js
.env
voter.json
voter.json
dist/
4 changes: 2 additions & 2 deletions feeder/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 4 additions & 3 deletions feeder/package.json
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
{
"name": "@terra-money/oracle-feeder",
"version": "3.0.1",
"main": "src/index.ts",
"version": "3.0.2",
"main": "dist/index.js",
"license": "Apache-2.0",
"scripts": {
"start": "ts-node src/index.ts"
"start": "ts-node src/index.ts",
"build": "tsc"
},
"dependencies": {
"@terra-money/feather.js": "^1.0.0-beta.4",
Expand Down
40 changes: 18 additions & 22 deletions feeder/src/addKey.ts
Original file line number Diff line number Diff line change
@@ -1,43 +1,39 @@
import * as keystore from './keystore'
import * as promptly from 'promptly'

export async function addKey(filePath: string, coinType: string, keyName: string, prefix: string): Promise<void> {
export async function addKey(args): Promise<void> {
console.log(args)
let password = process.env.PASSWORD || ''
let mnemonic = process.env.MNEMONIC || ''

coinType = process.env.COIN_TYPE ? process.env.COIN_TYPE : coinType
keyName = process.env.KEY_NAME ? process.env.KEY_NAME : keyName

if (password === '') {
password = await promptly.password(`Enter a passphrase to encrypt your key to disk:`, {
replace: `*`,
password = await promptly.password('Enter a passphrase to encrypt your key to disk:', {
replace: '*',
})
const confirm = await promptly.password(`Repeat the passphrase:`, { replace: `*` })

if (password !== confirm) {
console.error(`ERROR: passphrases don't matchPassword confirm failed`)
return
if (password.length < 8) {
throw Error('ERROR: password must be at least 8 characters')
}
}

if (password.length < 8) {
console.error(`ERROR: password must be at least 8 characters`)
return
const confirm = await promptly.password('Repeat the passphrase:', { replace: '*' })

if (password !== confirm) {
throw Error("ERROR: passphrases don't matchPassword confirm failed")
}
}

if (mnemonic === '') {
mnemonic = await promptly.prompt(`Enter your bip39 mnemonic: `)
mnemonic = await promptly.prompt('Enter your bip39 mnemonic: ')
}

if (mnemonic.trim().split(` `).length !== 24) {
console.error(`Error: Mnemonic is not valid.`)
return
if (mnemonic.trim().split(' ').length !== 24) {
throw Error('Error: Mnemonic is not valid.')
}

if (!prefix) {
prefix = await promptly.prompt(`\nEnter the address prefix: `)
if (!args.prefix) {
args.prefix = await promptly.prompt('\nEnter the address prefix: ')
}

await keystore.save(filePath, keyName, password, mnemonic, coinType, prefix)
console.info(`saved!`)
await keystore.save(args, password, mnemonic)
console.info('saved!')
}
142 changes: 71 additions & 71 deletions feeder/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,128 +2,128 @@ import { ArgumentParser } from 'argparse'
import { vote } from './vote'
import { addKey } from './addKey'
import * as packageInfo from '../package.json'
import * as promptly from 'promptly'
import * as dotenv from 'dotenv' // see https://github.com/motdotla/dotenv#how-do-i-use-dotenv-with-import
dotenv.config()

function registerCommands(parser: ArgumentParser): void {
const subparsers = parser.addSubparsers({
title: `commands`,
dest: `subparser_name`,
description: `Available commands`,
title: 'commands',
dest: 'subparser_name',
description: 'Available commands',
})

// Voting command
const voteCommand = subparsers.addParser(`vote`, {
const voteCommand = subparsers.addParser('vote', {
addHelp: true,
description: `Fetch price and broadcast oracle messages`,
description: 'Fetch price and broadcast oracle messages',
})

voteCommand.addArgument(['-l', '--lcd-url'], {
help: 'lcd url address (default: process.env.LCD_URL or ["http://localhost:1317"])',
action: 'append',
help: 'lcd address',
dest: 'lcdUrl',
defaultValue: (process.env.LCD_URL && process.env.LCD_URL.split(',')) || ['http://localhost:1317'],
required: false,
})

voteCommand.addArgument([`-c`, `--chain-id`], {
action: `store`,
help: `chain ID`,
dest: `chainID`,
voteCommand.addArgument(['-c', '--chain-id'], {
help: 'chain id where the vote transactions must be submitted to (default: process.env.CHAIN_ID or "candle-1")',
action: 'store',
dest: 'chainID',
defaultValue: process.env.CHAIN_ID || 'candle-1',
required: false,
})

voteCommand.addArgument([`--validators`], {
action: `append`,
help: `validators address (e.g. terravaloper1...), can have multiple`,
voteCommand.addArgument(['--validators'], {
help: 'validators address(es) (e.g. terravaloper1...)',
action: 'append',
required: false,
defaultValue: process.env.VALIDATORS && process.env.VALIDATORS.split(','),
})

voteCommand.addArgument([`-d`, `--data-source-url`], {
action: `append`,
help: `Append price data source(It can handle multiple sources)`,
dest: `dataSourceUrl`,
voteCommand.addArgument(['-d', '--data-source-url'], {
help: 'Append price(s) data source (default: process.env.DATA_SOURCE_URL or ["http://localhost:8532/latest"])',
action: 'append',
dest: 'dataSourceUrl',
defaultValue: (process.env.DATA_SOURCE_URL && process.env.DATA_SOURCE_URL.split(',')) || [
'http://localhost:8532/latest',
],
required: false,
})

voteCommand.addArgument([`-p`, `--password`], {
action: `store`,
help: `voter password`,
voteCommand.addArgument(['-p', '--password'], {
help: 'key store voter password (default: process.env.PASSWORD)',
action: 'store',
defaultValue: process.env.PASSWORD,
})

voteCommand.addArgument([`-k`, `--key-path`, `--keystore`], {
action: `store`,
help: `key store path to save encrypted key`,
dest: `keyPath`,
required: false,
voteCommand.addArgument(['-k', '--key-path', '--keystore'], {
help: 'key store path where to save encrypted key (default: process.env.KEY_PATH or "voter.json")',
action: 'store',
dest: 'keyPath',
defaultValue: process.env.KEY_PATH || 'voter.json',
})

voteCommand.addArgument([`-n`, `--key-name`], {
help: `name assigned to the generated key`,
dest: `keyName`,
defaultValue: `voter`,
voteCommand.addArgument(['-n', '--key-name'], {
help: 'name to assing to the generated key inside the keystore (default: process.env.KEY_NAME or "voter")',
dest: 'keyName',
defaultValue: process.env.KEY_NAME || 'voter',
})

// Updating Key command
const keyCommand = subparsers.addParser(`add-key`, { addHelp: true })

keyCommand.addArgument([`-n`, `--key-name`], {
help: `name to assigns to the generated key`,
dest: `keyName`,
defaultValue: `voter`,
voteCommand.addArgument(['-a', '--address-prefix'], {
help: 'prefix for the addresses to be generated (default: process.env.ADDR_PREFIX or "candle")',
dest: 'prefix',
defaultValue: process.env.ADDR_PREFIX || 'candle',
})

keyCommand.addArgument([`-t`, `--coin-type`], {
help: `coin type used to derive the public address (https://github.com/bitcoin/bips/blob/master/bip-0044.mediawiki#path-levels)`,
dest: `coinType`,
defaultValue: `118`,
voteCommand.addArgument(['-v', '--voter'], {
help: 'addresses of the wallet to vote on behalf (default: process.env.VOTER or default stored address from keystore)',
dest: 'voter',
defaultValue: process.env.VOTER,
})

keyCommand.addArgument([`-k`, `--key-path`], {
help: `key store path to save encrypted key`,
dest: `keyPath`,
defaultValue: `voter.json`,
// Updating Key command
const addKeyCommand = subparsers.addParser('add-key', { addHelp: true })
addKeyCommand.addArgument(['-n', '--key-name'], {
help: 'name to assing to the generated key inside the keystore (default: process.env.KEY_NAME or "voter")',
dest: 'keyName',
defaultValue: process.env.KEY_NAME || 'voter',
})
addKeyCommand.addArgument(['-t', '--coin-type'], {
help: 'coin type used to derive the public address (default: process.env.COIN_TYPE or "118")',
dest: 'coinType',
defaultValue: process.env.COIN_TYPE || '118',
})
addKeyCommand.addArgument(['-k', '--key-path'], {
help: 'key store path where to save encrypted key (default: process.env.KEY_PATH or "voter.json")',
dest: 'keyPath',
defaultValue: process.env.KEY_PATH || 'voter.json',
})
addKeyCommand.addArgument(['-a', '--address-prefix'], {
help: 'prefix for the addresses to be generated (default: process.env.ADDR_PREFIX or "candle")',
dest: 'prefix',
defaultValue: process.env.ADDR_PREFIX || 'candle',
})
}

async function main(): Promise<void> {
const parser = new ArgumentParser({
version: packageInfo.version,
addHelp: true,
description: `Terra oracle voter`,
description: 'Terra oracle voter',
})

registerCommands(parser)
const args = parser.parseArgs()

args.prefix = args.prefix || process.env.ADDR_PREFIX

if (args.subparser_name === `vote`) {
args.lcdUrl = args.lcdUrl || (process.env.LCD_URL && process.env.LCD_URL.split(',')) || []

args.dataSourceUrl =
args.dataSourceUrl || (process.env.DATA_SOURCE_URL && process.env.DATA_SOURCE_URL.split(',')) || []
args.chainID = args.chainID || process.env.CHAIN_ID || 'candled-testnet-1'
if (args.lcdUrl?.length === 0 || args.dataSourceUrl?.length === 0 || args.chainID === '') {
console.error('Missing --lcd, --chain-id or --data-source-url')
return
}

args.keyPath = args.keyPath || process.env.KEY_PATH || 'voter.json'
args.password = args.password || process.env.PASSWORD || ''
if (args.keyPath === '' || args.password === '') {
console.error('Missing either --key-path or --password')
return
}

// validators is skippable and default value will be extracted from the key
args.validators = args.validators || (process.env.VALIDATORS && process.env.VALIDATORS.split(','))
console.log(args)

args.keyName = process.env.KEY_NAME ? process.env.KEY_NAME : args.keyName
if (args.subparser_name === 'vote') {
args.password = args.password || (await promptly.password(`Enter a passphrase:`, { replace: `*` }))

await vote(args)
} else if (args.subparser_name === `add-key`) {
await addKey(args.keyPath, args.coinType, args.keyName, args.prefix)
}
} else if (args.subparser_name === 'add-key') await addKey(args)
}

main().catch((e) => {
Expand Down
Loading

0 comments on commit 762c4a1

Please sign in to comment.