Skip to content

Review (wip) #2

Description

@mcortesi

Agustin,

As promised, I began reading the code, so I can give you a proper review.

First, let me say, awesome work!
I haven't yet used decorators and I like how you used them... well in a sense.

Now to the review...

  1. I think that even though the @inject and the Method is a nice dynamic way of adding behaviour, you are loosing type safety everywhere.

You could do something like:

type Input<A> = (val: A) => any
type OutputFormatter<X> = (val: any) => X

function newMethod<A1, X>(opts: {
  name: string
  inputs: [Input<A1>]
  output: OutputFormatter<X>
}): (rm: RequestManager) => (a1: A1) => Promise<X>
function newMethod<A1, A2, X>(opts: {
  name: string
  inputs: [Input<A1>, Input<A2>]
  output: OutputFormatter<X>
}): (rm: RequestManager) => (a1: A1, a2: A2) => Promise<X>
function newMethod<A1, A2, A3, X>(opts: {
  name: string
  inputs: [Input<A1>, Input<A2>, Input<A3>]
  output: OutputFormatter<X>
}): (rm: RequestManager) => (a1: A1, a2: A2, a3: A3) => Promise<X>
function newMethod<A1, A2, A3, A4, X>(opts: {
  name: string
  inputs: [Input<A1>, Input<A2>, Input<A3>, Input<A4>]
  output: OutputFormatter<X>
}): (rm: RequestManager) => (a1: A1, a2: A2, a3: A3, a4: A4) => Promise<X>
function newMethod(opts: {
  name: string
  inputs: Input<any>[]
  output: OutputFormatter<any>
}): (rm: RequestManager) => (...args: any[]) => Promise<any> {
  return (rm: RequestManager) => async (...args) => {
    const payload = {
      method: opts.name,
      params: opts.inputs.map((format, i) => format(args[i]))
    }
    const result = await rm.sendAsync(payload)
    return opts.output(result)
  }
}

export const eth_getBalance = newMethod({
  name: 'eth_getBalance',
  inputs: [formatters.inputAddressFormatter, formatters.inputDefaultBlockNumberFormatter],
  output: formatters.outputBigNumberFormatter
})

This way eth_getBalance returns a typed function at it will fail at compile time.
I've just done this, but i also saw that inputAddressFormatter isn't typed either; but it could.

There a few places where parameters or return values are any, maybe because everything is a work in progress.

For now this is my 2 cents, happy to help if you want code labor.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions