EncodeErrors
Set of errors while perfoming abi encoding.
AbiEncoder.Errors || error{NoSpaceLeft}AbiEncodedValues
Runtime value representation for abi encoding.
Properties
union(enum) {
bool: bool
uint: u256
int: i256
address: Address
fixed_bytes: []u8
string: []const u8
bytes: []const u8
fixed_array: []const AbiEncodedValues
dynamic_array: []const AbiEncodedValues
tuple: []const AbiEncodedValues
}IsDynamic
Checks if the given values is a dynamic abi value.
Signature
pub fn isDynamic(self: @This()) boolPreEncodedStructure
The encoded values inner structure representation.
Properties
struct {
type: ParameterType
encoded: []const u8
}Deinit
Signature
pub fn deinit(self: @This(), allocator: Allocator) voidEncodeAbiFunction
Encode an Solidity Function type with the signature and the values encoded.
The signature is calculated by hashing the formated string generated from the Function signature.
Signature
pub fn encodeAbiFunction(
comptime func: Function,
allocator: Allocator,
values: AbiParametersToPrimative(func.inputs),
) (AbiEncoder.Errors || error{NoSpaceLeft})![]u8EncodeAbiFunctionOutputs
Encode an Solidity Function type with the signature and the values encoded.
This is will use the func outputs values as the parameters.
Signature
pub fn encodeAbiFunctionOutputs(
comptime func: Function,
allocator: Allocator,
values: AbiParametersToPrimative(func.outputs),
) AbiEncoder.Errors![]u8EncodeAbiError
Encode an Solidity Error type with the signature and the values encoded.
The signature is calculated by hashing the formated string generated from the Error signature.
Signature
pub fn encodeAbiError(
comptime err: Error,
allocator: Allocator,
values: AbiParametersToPrimative(err.inputs),
) (AbiEncoder.Errors || error{NoSpaceLeft})![]u8EncodeAbiConstructor
Encode an Solidity Constructor type with the signature and the values encoded.
Signature
pub fn encodeAbiConstructor(
comptime constructor: Constructor,
allocator: Allocator,
values: AbiParametersToPrimative(constructor.inputs),
) AbiEncoder.Errors![]u8EncodeAbiParameters
Encodes the values based on the specification
The values types are checked at comptime based on the provided params.
Signature
pub fn encodeAbiParameters(
comptime params: []const AbiParameter,
allocator: Allocator,
values: AbiParametersToPrimative(params),
) AbiEncoder.Errors![]u8EncodeAbiParametersValues
Encodes the values based on the specification
Use this if for some reason you don't know the Abi at comptime.
It's recommended to use encodeAbiParameters whenever possible but this is provided as a fallback
you cannot use it.
Signature
pub fn encodeAbiParametersValues(
allocator: Allocator,
values: []const AbiEncodedValues,
) (AbiEncoder.Errors || error{InvalidType})![]u8EncodeAbiParametersFromReflection
Encodes the values based on the specification
This will use zig's ability to provide compile time reflection based on the values provided.
The values must be a tuple struct. Otherwise it will trigger a compile error.
By default this provides more support for a greater range on zig types that can be used for encoding. Bellow you will find the list of all supported types and what will they be encoded as.
- Zig
bool-> Will be encoded like a boolean value - Zig
?T-> Only encodes if the value is not null. - Zig
int,comptime_int-> Will be encoded based on the signedness of the integer. - Zig
[N]u8-> Only support max size of 32.[20]u8will be encoded as address types and all other as bytes1..32. This is the main limitation because abi encoding of bytes1..32 follows little endian and for address follows big endian. - Zig
enum-> The tagname of the enum encoded as a string/bytes value. - Zig
*T-> will encoded the child type. If the child type is anarrayit will encode as string/bytes. - Zig
[]const u8,[]u8-> Will encode according the string/bytes specification. - Zig
[]const T-> Will encode as a dynamic array - Zig
[N]T-> Will encode as a dynamic value if the child type is of a dynamic type. - Zig
struct-> Will encode as a dynamic value if the child type is of a dynamic type.
All other types are currently not supported.
Signature
pub fn encodeAbiParametersFromReflection(
allocator: Allocator,
values: anytype,
) AbiEncoder.Errors![]u8EncodePacked
Encode values based on solidity's encodePacked.
Solidity types are infered from zig ones since it closely follows them.
Supported zig types:
- Zig
bool-> Will be encoded like a boolean value - Zig
?T-> Only encodes if the value is not null. - Zig
int,comptime_int-> Will be encoded based on the signedness of the integer. - Zig
[N]u8-> Only support max size of 32.[20]u8will be encoded as address types and all other as bytes1..32. This is the main limitation because abi encoding of bytes1..32 follows little endian and for address follows big endian. - Zig
enum,enum_literal,error_set-> The tagname of the enum or the error_set names will be encoded as a string/bytes value. - Zig
*T-> will encoded the child type. If the child type is anarrayit will encode as string/bytes. - Zig
[]const u8,[]u8-> Will encode according the string/bytes specification. - Zig
[]const T-> Will encode as a dynamic array - Zig
[N]T-> Will encode as a dynamic value if the child type is of a dynamic type. - Zig
struct-> Will encode as a dynamic value if the child type is of a dynamic type.
All other types are currently not supported.
If the value provided is either a []const T, [N]T, []T, or tuple,
the child values will be 32 bit padded.
Signature
pub fn encodePacked(
allocator: Allocator,
value: anytype,
) EncodePacked.Errors![]u8AbiEncoder
The abi encoding structure used to encoded values with the abi encoding specification
You can initialize this structure like this:
var encoder: AbiEncoder = .empty;
try encoder.encodeAbiParameters(params, allocator, .{69, 420});
defer allocator.free(allocator);Properties
struct {
/// Essentially a `stack` of encoded values that will need to be analysed
/// in the `encodePointers` step to re-arrange the location in the encoded slice based on
/// if they are dynamic or static types.
pre_encoded: ArrayListUnmanaged(PreEncodedStructure)
/// Stream of encoded values that should show up at the top of the encoded slice.
heads: ArrayListUnmanaged(u8)
/// Stream of encoded values that should show up at the end of the encoded slice.
tails: ArrayListUnmanaged(u8)
/// Used to calculated the initial pointer when facing `dynamic` types.
/// Also used to know the memory size of the `heads` stream.
heads_size: u32
/// Only used to know the memory size of the `tails` stream.
tails_size: u32
}Self
@This()Errors
Set of possible error when running this encoder.
error{ DivisionByZero, Overflow } || Allocator.Errorempty
Sets the initial state of the encoder.
.{
.pre_encoded = .empty,
.heads = .empty,
.tails = .empty,
.heads_size = 0,
.tails_size = 0,
}EncodeAbiParametersFromReflection
Encodes the values based on the specification
Uses compile time reflection to determine the behaviour. Please check encodeAbiParametersFromReflection for more details.
Signature
pub fn encodeAbiParametersFromReflection(
self: *Self,
allocator: Allocator,
values: anytype,
) Errors![]u8EncodeAbiParametersValues
Encodes the values based on the specification
Uses the AbiEncodedValues type to determine the correct behaviour.
Signature
pub fn encodeAbiParametersValues(
self: *Self,
allocator: Allocator,
values: []const AbiEncodedValues,
) Errors![]u8EncodeAbiParameters
Encodes the values based on the specification
The values types are checked at comptime based on the provided params.
Signature
pub fn encodeAbiParameters(
self: *Self,
comptime params: []const AbiParameter,
allocator: Allocator,
values: AbiParametersToPrimative(params),
) Errors![]u8EncodePointers
Re-arranges the inner stack based on if the value that it's dealing with is either dynamic or now.
Places those values in the heads or tails streams based on that.
Signature
pub fn encodePointers(
self: *Self,
allocator: Allocator,
) Allocator.Error![]u8PreEncodeAbiParameters
Encodes the values and places them on the inner stack.
Signature
pub fn preEncodeAbiParameters(
self: *Self,
comptime params: []const AbiParameter,
allocator: Allocator,
values: AbiParametersToPrimative(params),
) Errors!voidPreEncodeAbiParameter
Encodes a single value and places them on the inner stack.
Signature
pub fn preEncodeAbiParameter(
self: *Self,
comptime param: AbiParameter,
allocator: Allocator,
value: AbiParameterToPrimative(param),
) Errors!voidPreEncodeRuntimeValues
Pre encodes the parameter values according to the specification and places it on pre_encoded arraylist.
Signature
pub fn preEncodeRuntimeValues(
self: *Self,
allocator: Allocator,
values: []const AbiEncodedValues,
) (Errors || error{InvalidType})!voidPreEncodeRuntimeValue
Pre encodes the parameter value according to the specification and places it on pre_encoded arraylist.
This methods and some runtime checks to see if the parameter are valid like preEncodeAbiParameter that instead uses
comptime to get the exact expected types.
Signature
pub fn preEncodeRuntimeValue(
self: *Self,
allocator: Allocator,
value: AbiEncodedValues,
) (Errors || error{InvalidType})!voidPreEncodeValuesFromReflection
This will use zig's ability to provide compile time reflection based on the values provided.
The values must be a tuple struct. Otherwise it will trigger a compile error.
Signature
pub fn preEncodeValuesFromReflection(
self: *Self,
allocator: Allocator,
values: anytype,
) Errors!voidPreEncodeReflection
This will use zig's ability to provide compile time reflection based on the value provided.
Signature
pub fn preEncodeReflection(
self: *Self,
allocator: Allocator,
value: anytype,
) Errors!voidSelf
@This()Errors
Set of possible error when running this encoder.
error{ DivisionByZero, Overflow } || Allocator.Errorempty
Sets the initial state of the encoder.
.{
.pre_encoded = .empty,
.heads = .empty,
.tails = .empty,
.heads_size = 0,
.tails_size = 0,
}EncodePacked
Similar to AbiEncoder but used for packed encoding.
Properties
struct {
/// Changes the encoder behaviour based on the type of the parameter.
param_type: ParameterType
/// List that is used to write the encoded values too.
list: ArrayList(u8)
}Errors
Set of possible error when running this encoder.
error{ DivisionByZero, Overflow } || Allocator.ErrorInit
Sets the initial state of the encoder.
Signature
pub fn init(
allocator: Allocator,
param_type: ParameterType,
) EncodePackedEncodePacked
Abi encodes the values. If the values are dynamic all of the child values will be encoded as 32 sized values with the expection of []u8 slices.
Signature
pub fn encodePacked(
self: *EncodePacked,
value: anytype,
) Errors![]u8EncodePackedValue
Handles the encoding based on the value type and writes them to the list.
Signature
pub fn encodePackedValue(
self: *EncodePacked,
value: anytype,
) Errors!voidChangeParameterType
Used to change the type of value it's dealing with.
Signature
pub fn changeParameterType(
self: *EncodePacked,
param_type: ParameterType,
) voidErrors
Set of possible error when running this encoder.
error{ DivisionByZero, Overflow } || Allocator.ErrorEncodeBoolean
Encodes a boolean value according to the abi encoding specification.
Signature
pub fn encodeBoolean(boolean: bool) [32]u8EncodeNumber
Encodes a integer value according to the abi encoding specification.
Signature
pub fn encodeNumber(comptime T: type, number: T) [32]u8EncodeAddress
Encodes an solidity address value according to the abi encoding specification.
Signature
pub fn encodeAddress(address: Address) [32]u8EncodeFixedBytes
Encodes an bytes1..32 value according to the abi encoding specification.
Signature
pub fn encodeFixedBytes(
comptime size: usize,
payload: [size]u8,
) [32]u8EncodeString
Encodes an solidity string or bytes value according to the abi encoding specification.
Signature
pub fn encodeString(
allocator: Allocator,
payload: []const u8,
) (error{ DivisionByZero, Overflow } || Allocator.Error)![]u8IsDynamicType
Checks if a given parameter is a dynamic abi type.
Signature
pub inline fn isDynamicType(comptime param: AbiParameter) bool