Skip to content

ABI: Provide 1st class arithmetic support for numeric types #181

@jasonpaulos

Description

@jasonpaulos

Problem

The ergonomics for arithmetic operations involving numeric ABI types is lacking.

  • Numeric ABI types do not expose a 1st class way to perform arithmetic (e.g. add 2 abi.Uint16s via +).
  • Defining numeric constants is verbose due to the API's create-and-set requirement.

Here's an example of how arithmetic looks today with unsigned integers <= 64 bits:

a = abi.Uint16()
b = abi.Uint16()
c = abi.Uint16()
program = Seq(
    a.set(100),
    b.set(100),
    c.set(a.get() + b.get()))

Here's a motivational example of how arithmetic and defining numeric constants can look:

a = abi.Uint16()
b = abi.Uint16()
program = Seq(
    a.set(100),
    b.set(a + abi.Uint16Constant(200)) // No .get() and more concise constant definition 
)

Solution

Proposal by @jasonpaulos:
Allow number ABI types to perform math using Python's built-in operators. We will need to introduce a new mini-AST for each numeric type. For example, the addition of two abi.Uint16s would need to produce something like a abi.Uint16BinaryExpr. And the abi.Uint16.set method should accept an abi.Uint16BinaryExpr as a possible value, in order to make the expression usable.

For safety, mixing numeric type should probably not be allowed without explicit casting. This issue should also consider implementing a way to cast types, or creating a new issue for it.

Finally, a useful feature might be to allow immediately-creatable numeric constants without having to define a full abi.Type -- something like abi.Uint16Constant that extends abi.Uint16 and is only instantiable from a compile-time constant int, for each numeric type.

Dependencies

#154

Urgency

TBD

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions