diff --git a/contractabi/address.nim b/contractabi/address.nim new file mode 100644 index 0000000..30fb6f6 --- /dev/null +++ b/contractabi/address.nim @@ -0,0 +1,26 @@ +import pkg/stew/byteutils +import pkg/questionable +import pkg/upraises + +push: {.upraises: [].} + +type + Address* = distinct array[20, byte] + +func init*(_: type Address, bytes: array[20, byte]): Address = + Address(bytes) + +func init*(_: type Address, hex: string): ?Address = + try: + let bytes = array[20, byte].fromHex(hex) + some Address.init(bytes) + except ValueError: + none Address + +func toArray*(address: Address): array[20, byte] = + array[20, byte](address) + +func `$`*(address: Address): string = + "0x" & toHex(address.toArray) + +func `==`*(a, b: Address): bool {.borrow.} diff --git a/contractabi/decoding.nim b/contractabi/decoding.nim index f4123b3..ba4375c 100644 --- a/contractabi/decoding.nim +++ b/contractabi/decoding.nim @@ -5,6 +5,10 @@ import pkg/questionable/results import pkg/upraises import ./encoding import ./integers +import ./address + +export stint +export address push: {.upraises:[].} @@ -114,6 +118,11 @@ func decode(decoder: var AbiDecoder, T: type enum): ?!T = else: failure "invalid enum value" +func decode(decoder: var AbiDecoder, T: type Address): ?!T = + var bytes: array[20, byte] + bytes[0..<20] =(?decoder.read(20))[0..<20] + success T.init(bytes) + func decode[I](decoder: var AbiDecoder, T: type array[I, byte]): ?!T = var arr: T arr[0..