Skip to content

Commit

Permalink
Merge pull request #2 from Lbqds/allow_decode_constant
Browse files Browse the repository at this point in the history
allow decode constant & refine repl
  • Loading branch information
Lbqds authored Apr 28, 2020
2 parents 5a589a1 + 77d549b commit 7e2cb85
Show file tree
Hide file tree
Showing 2 changed files with 90 additions and 5 deletions.
61 changes: 61 additions & 0 deletions codegen/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
## repl

repl used for convenience encode and decode, download abi-codegen-0.2.0 from [release page](https://github.com/Lbqds/eth-abi/releases)

usage:

```
$ ./abi-codegen-0.2.0 -i
```

start repl mode, and only have 3 command now:

```
>>load absolute-path-abi-file
```

```
>>encode selector [params]
```

```
>>decode selector params
```

for example([Token](https://github.com/Lbqds/eth-abi/tree/master/examples/src/main/scala/examples/token)):

```
>>load /absolute-path/Token.abi
```

output:

```
load completed
```

```
>>encode approve 0x0C256268f13CACeF89e15c7512c5fCf3f6775680 100
```

output:

```
0000000000000000000000000c256268f13cacef89e15c7512c5fcf3f67756800000000000000000000000000000000000000000000000000000000000000064
```

```
decode totalSupply 0000000000000000000000000000000000000000000000000000000000000064
```

output:

```
(0000000000000000000000000000000000000000000000000000000000000064)
```

ctrl-d to quit

thanks to [ammonite-terminal](https://github.com/lihaoyi/Ammonite/tree/master/terminal/src), you can use ctrl-r/h/b/f/a/e etc.

TODO: support tuple, event
34 changes: 29 additions & 5 deletions codegen/src/main/scala/codegen/Repl.scala
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ private[codegen] object Repl {
override def execute(): String = {
val abiDef =
if (selector == ctorSelector) throw new InvalidCommandException("ctor have outputs")
else defs.filter(_.isFunction).find(_.name.get == selector).get
else defs.filter(d => d.isFunction || d.isConstant).find(_.name.get == selector).get
if (abiDef.outputs.get.exists(_.isTupleTpe)) throw new InvalidCommandException("tuple type unsupported now")
val types = abiDef.outputs.get.map(_.tpe.toString)
def helper[T]: Option[T] = None
Expand Down Expand Up @@ -165,35 +165,59 @@ private[codegen] object Repl {

def execute(raw: String): Unit = {
try {
history = history :+ raw
parseCmd(raw) match {
case cmd: Load => ctx = cmd.execute()
case cmd: EncodeArg => println(cmd.copy(defs = ctx).execute())
case cmd: DecodeRet => println(cmd.copy(defs = ctx).execute())
case cmd: Command => cmd.execute()
}
history = history :+ raw
} catch {
case NonFatal(exp) => println(exp)
case e: Throwable => throw e
}
}

// TODO: remove unnecessary filters
val historyFilter = new HistoryFilter(() => history.toVector, fansi.Attrs.Empty)
val selection = GUILikeFilters.SelectionFilter(indent = 4)
val filters = Filter.merge(
UndoFilter(),
ReadlineFilters.CutPasteFilter(),
new HistoryFilter(() => history.toVector, fansi.Attrs.Empty),
GUILikeFilters.SelectionFilter(indent = 4),
historyFilter,
selection,
BasicFilters.tabFilter(4),
GUILikeFilters.altFilter,
GUILikeFilters.fnFilter,
ReadlineFilters.navFilter,
BasicFilters.all)
val reader = new InputStreamReader(System.in)
val writer = new OutputStreamWriter(System.out)

val transformer = (buffer: Vector[Char], cursor: Int) => {
// underline all non-blank lines

def hl(b: Vector[Char]): Vector[Char] = b.flatMap{
case ' ' => " "
case '\n' => "\n"
case c => Console.UNDERLINED + c + Console.RESET
}
// and highlight the selection
val ansiBuffer = fansi.Str(hl(buffer))
val (newBuffer, cursorOffset) = GUILikeFilters.SelectionFilter.mangleBuffer(
selection, ansiBuffer, cursor, fansi.Reversed.On
)
val newNewBuffer = HistoryFilter.mangleBuffer(
historyFilter, newBuffer, cursor,
fansi.Color.Green
)

(newNewBuffer, cursorOffset)
}

@tailrec
def loop(): Unit = {
Terminal.readLine(">>", reader, writer, filters) match {
Terminal.readLine(">>", reader, writer, filters, transformer) match {
case Some(line) => execute(line); loop()
case _ => println("\nBye!"); saveHistory(history)
}
Expand Down

0 comments on commit 7e2cb85

Please sign in to comment.