Application Binary Inteface (ABI) Coder For Ethereum & Co.

abicoder - "lite" application binary interface (abi) encoding / decoding machinery / helper for Ethereum & Co. (blockchain) contracts with zero-dependencies for easy (re)use

Usage

require 'abicode'

#####
#  try ABI.encode

##  Encoding simple types
ABI.encode( [ 'uint256', 'string' ],
               [ 1234, 'Hello World' ]).hexdigest
#=> "00000000000000000000000000000000000000000000000000000000000004d2"+
#   "0000000000000000000000000000000000000000000000000000000000000040"+
#   "000000000000000000000000000000000000000000000000000000000000000b"+
#   "48656c6c6f20576f726c64000000000000000000000000000000000000000000"

## Encoding with arrays types
ABI.encode([ 'uint256[]', 'string' ],
              [ [1234, 5678] , 'Hello World' ]).hexdigest
#=> "0000000000000000000000000000000000000000000000000000000000000040"+
#   "00000000000000000000000000000000000000000000000000000000000000a0"+
#   "0000000000000000000000000000000000000000000000000000000000000002"+
#   "00000000000000000000000000000000000000000000000000000000000004d2"+
#   "000000000000000000000000000000000000000000000000000000000000162e"+
#   "000000000000000000000000000000000000000000000000000000000000000b"+
#   "48656c6c6f20576f726c64000000000000000000000000000000000000000000"


#####
#  try ABI.decode

##  Decoding simple types
data = hex'00000000000000000000000000000000000000000000000000000000000004d2'+
          '0000000000000000000000000000000000000000000000000000000000000040'+
          '000000000000000000000000000000000000000000000000000000000000000b'+
          '48656c6c6f20576f726c64000000000000000000000000000000000000000000'
ABI.decode([ 'uint256', 'string' ], data)
#=>  [1234, "Hello World"]


##  Decoding with arrays types
data = hex'0000000000000000000000000000000000000000000000000000000000000040'+
          '00000000000000000000000000000000000000000000000000000000000000a0'+
          '0000000000000000000000000000000000000000000000000000000000000002'+
          '00000000000000000000000000000000000000000000000000000000000004d2'+
          '000000000000000000000000000000000000000000000000000000000000162e'+
          '000000000000000000000000000000000000000000000000000000000000000b'+
          '48656c6c6f20576f726c64000000000000000000000000000000000000000000'
ABI.decode([ 'uint256[]', 'string' ], data )
#=>  [[1234, 5678], "Hello World"]


## Decoding complex structs (also known as tuples)
data = hex'00000000000000000000000000000000000000000000000000000000000004d2'+
          '0000000000000000000000000000000000000000000000000000000000000040'+
          '000000000000000000000000000000000000000000000000000000000000162e'+
          '0000000000000000000000000000000000000000000000000000000000000040'+
          '000000000000000000000000000000000000000000000000000000000000000b'+
          '48656c6c6f20576f726c64000000000000000000000000000000000000000000'
ABI.decode([ 'uint256', '(uint256,string)'], data)
#=> [1234, [5678, "Hello World"]]

License

The scripts are dedicated to the public domain. Use it as you please with no restrictions whatsoever.

Questions? Comments?

Post them on the D.I.Y. Punk (Pixel) Art reddit. Thanks.