Contract ABI Encoding
Go to file
Mark Spanbroek 9ea526814d version 0.6.0
possibly breaking changes in nimcrypto,
which was updated from 0.5.4 to 0.6.0
2023-08-29 12:05:20 +02:00
.github/workflows update nim version 1.6.x in CI 2023-08-29 12:05:20 +02:00
contractabi Add generic decode function 2023-06-29 11:11:02 +02:00
tests Fix test 2023-06-29 11:12:15 +02:00
.editorconfig ABI Encoder extracted from nim-nitro module 2021-11-25 09:33:32 +01:00
.gitignore Add setup files 2022-07-13 09:14:54 +02:00
License.md Add license 2022-01-10 11:17:48 +01:00
Readme.md version 0.6.0 2023-08-29 12:05:20 +02:00
config.nims Add setup files 2022-07-13 09:14:54 +02:00
contractabi.nim Support calculation of solidity function selectors 2022-01-19 09:18:46 +01:00
contractabi.nimble version 0.6.0 2023-08-29 12:05:20 +02:00
nim.cfg ABI Encoder extracted from nim-nitro module 2021-11-25 09:33:32 +01:00
nimble.lock update nimcrypto and questionable 2023-08-29 12:05:20 +02:00

Readme.md

Contract ABI

Implements encoding of parameters according to the Ethereum Contract ABI Specification.

Installation

Use the Nimble package manager to add contractabi to an existing project. Add the following to its .nimble file:

requires "contractabi >= 0.6.0 & < 0.7.0"

Usage

import contractabi

# encode unsigned integers, booleans, enums
AbiEncoder.encode(42'u8)

# encode uint256
import stint
AbiEncoder.encode(42.u256)

# encode byte arrays and sequences
AbiEncoder.encode([1'u8, 2'u8, 3'u8])
AbiEncoder.encode(@[1'u8, 2'u8, 3'u8])

# encode tuples
AbiEncoder.encode( (42'u8, @[1'u8, 2'u8, 3'u8], true) )

# decode values of different types
AbiDecoder.decode(bytes, uint8)
AbiDecoder.decode(bytes, UInt256)
AbiDecoder.decode(bytes, array[3, uint8])
AbiDecoder.decode(bytes, seq[uint8])

# decode tuples
AbiDecoder.decode(bytes, (uint32, bool, seq[byte]) )

# add support for encoding of custom types
import questionable/results

type CustomType = object
  a: uint16
  b: string

func encode(encoder: var AbiEncoder, custom: CustomType) =
  encoder.write( (custom.a, custom.b) )

func decode(decoder: var AbiDecoder, T: type CustomType): ?!T =
  let (a, b) = ?decoder.read( (uint16, string) )
  success CustomType(a: a, b: b)