forked from rsksmart/rif-relay
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
added "gsn registry" command-line tool
list all ids: `gsn registry --list` latest version for id: `gsn registry --id` all versions for id: `gsn registry --id <id> --history` add version `gsn registry --id <id> --ver <ver> --add <value> ` cancel version `gsn registry --id <id> --ver <ver> --cancel `
- Loading branch information
1 parent
4e8df88
commit 505f921
Showing
12 changed files
with
226 additions
and
18 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,118 @@ | ||
import CommandsLogic from '../CommandsLogic' | ||
import { configureGSN } from '../../relayclient/GSNConfigurator' | ||
import DateFormatter from 'date-format' | ||
import { | ||
getMnemonic, | ||
getNetworkUrl, | ||
getRegistryAddress, | ||
gsnCommander | ||
} from '../utils' | ||
import { VersionInfo, VersionRegistry } from '../../common/VersionRegistry' | ||
|
||
function error (s: string): never { | ||
console.error(s) | ||
process.exit(1) | ||
} | ||
|
||
function parseTime (t: string): number { | ||
const m = t.match(/^\s*([\d.]+)\s*([smhdw]?)/i) | ||
if (m == null) error('invalid --delay parameter: must be number with sec/min/hour/day suffix') | ||
const n = parseFloat(m[1]) | ||
switch (m[2].toLowerCase()) { | ||
case 'm': | ||
return n * 60 | ||
case 'h': | ||
return n * 3600 | ||
case 'd': | ||
return n * 3600 * 24 | ||
case 'w': | ||
return n * 3600 * 24 * 7 | ||
default: // either 'sec' or nothing | ||
return n | ||
} | ||
} | ||
|
||
const commander = gsnCommander(['n', 'f', 'm', 'g']) | ||
.option('--registry <address>', 'versionRegistry') | ||
.option('-i, --id <string>', 'id to edit/change') | ||
.option('--list', 'list all registered ids') | ||
.option('-d, --delay <string>', 'view latest version that is at least that old (sec/min/hour/day)', '0') | ||
.option('-h, --history', 'show all version history') | ||
.option('-V, --ver <string>', 'new version to add/cancel') | ||
.option('-d, --date', 'show date info of versions') | ||
.option('-a, --add <string>', 'add this version value. if not set, show current value') | ||
.option('-C, --cancel', 'cancel the given version') | ||
.option('-r, --reason <string>', 'cancel reason') | ||
.parse(process.argv) | ||
|
||
function formatVersion (id: string, versionInfo: VersionInfo, showDate = false): string { | ||
const dateInfo = showDate ? `[${DateFormatter('yyyy-MM-dd hh:mm', new Date(versionInfo.time * 1000))}] ` : '' | ||
return `${id} @ ${versionInfo.version} = ${dateInfo} ${versionInfo.value} ${versionInfo.canceled ? `- CANCELED ${versionInfo.cancelReason}` : ''}`.trim() | ||
} | ||
|
||
(async () => { | ||
const nodeURL = getNetworkUrl(commander.network) | ||
|
||
const mnemonic = getMnemonic(commander.mnemonic) | ||
const logic = new CommandsLogic(nodeURL, configureGSN({}), mnemonic) | ||
const provider = (logic as any).web3.currentProvider | ||
const versionRegistryAddress = getRegistryAddress(commander.registry) ?? error('must specify --registry') | ||
console.log('Using registry at address: ', versionRegistryAddress) | ||
const versionRegistry = new VersionRegistry(provider, versionRegistryAddress) | ||
if (!await versionRegistry.isValid()) { | ||
error(`Not a valid registry address: ${versionRegistryAddress}`) | ||
} | ||
|
||
if (commander.list != null) { | ||
const ids = await versionRegistry.listIds() | ||
console.log('All registered IDs:') | ||
ids.forEach(id => console.log('-', id)) | ||
return | ||
} | ||
|
||
const id: string = commander.id ?? error('must specify --id') | ||
const add = commander.add as (string | undefined) | ||
const cancel = commander.cancel | ||
|
||
const version: string | undefined = commander.ver | ||
if (add == null && cancel == null) { | ||
// view mode | ||
|
||
if (version != null) { | ||
error('cannot specify --ver without --add or --cancel') | ||
} | ||
const showDate = commander.date | ||
if (commander.history != null) { | ||
if (commander.delay !== '0') error('cannot specify --delay and --history') | ||
console.log((await versionRegistry.getAllVersions(id)).map(v => formatVersion(id, v, showDate))) | ||
} else { | ||
const delayPeriod = parseTime(commander.delay) | ||
console.log(formatVersion(id, await versionRegistry.getVersion(id, delayPeriod), showDate)) | ||
} | ||
} else { | ||
if ((add == null) === (cancel == null)) error('must specify --add or --cancel, but not both') | ||
const from = commander.from ?? await logic.findWealthyAccount() | ||
const sendOptions = { | ||
gasPrice: commander.gasPrice, | ||
from | ||
} | ||
if (version == null) { | ||
error('--add/--cancel commands require both --id and --ver') | ||
} | ||
if (add != null) { | ||
await versionRegistry.addVersion(id, version, add, sendOptions) | ||
console.log(`== Added version ${id} @ ${version}`) | ||
} else { | ||
const reason = commander.reason ?? '' | ||
await versionRegistry.cancelVersion(id, version, reason, sendOptions) | ||
console.log(`== Canceled version ${id} @ ${version}`) | ||
} | ||
} | ||
})() | ||
.then(() => process.exit(0)) | ||
.catch( | ||
reason => { | ||
console.error(reason) | ||
process.exit(1) | ||
} | ||
) |
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
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 |
---|---|---|
|
@@ -30,6 +30,7 @@ | |
"truffle-contracts", | ||
"openzeppelin__test-helpers", | ||
"ganache-core", | ||
"date-format", | ||
"chai-bn" | ||
] | ||
}, | ||
|
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,3 @@ | ||
declare module 'date-format' { | ||
export default function asString(format: string, date: Date): string | ||
} |
Oops, something went wrong.