Encode Abi Constructor
Definition
Generates Abi encoded data using the contract ABI specification by a given set of inputs and the associated values.
Zabi supports encoding AbiConstructor
that are either comptime know or runtime know. It is advised that if you know the specification you are working with is comptime know to use encodeAbiFunctionComptime
.
Usage
encodeAbiConstructor
takes in 3 parameters.
- an allocator used to perform any sort of memory allocations.
- a ABI Constructor specification.
- a tuple of values that the type corresponds to the given set of parameters.
All memory will be managed by a ArenaAllocator
. You must free the memory after this call.
const std = @import("std");
const encoder = @import("zabi").encoder;
const abi_constructor: Constructor = .{.type = .constructor, .name = "Foo", .inputs = &.{.{.type = .{ .bool = {} }, .name = "foo"}, .{ .type = .{ .string = {} }, .name = "bar" } }, .stateMutability = .nonpayable };
const encoded = try encoder.encodeAbiConstructor(std.testing.allocator, abi_parameters, .{true, "fizzbuzz"})
defer std.testing.allocator.free(encoded);
// Result
// 00000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000040000000000000000000000000000000000000000000000000000000000000000866697a7a62757a7a000000000000000000000000000000000000000000000000
This same example of usage could be used with encodeAbiConstructorComptime
because here the Abi Parameter is comptime know.
const std = @import("std");
const encoder = @import("zabi").encoder;
const abi_constructor: Constructor = .{.type = .constructor, .name = "Foo", .inputs = &.{.{.type = .{ .bool = {} }, .name = "foo"}, .{ .type = .{ .string = {} }, .name = "bar" } }, .stateMutability = .nonpayable };
const encoded = try encoder.encodeAbiConstructorComptime(std.testing.allocator, abi_parameters, .{true, "fizzbuzz"})
defer std.testing.allocator.free(encoded);
// Result
// 00000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000040000000000000000000000000000000000000000000000000000000000000000866697a7a62757a7a000000000000000000000000000000000000000000000000
The main benifit of using this is that we can infer which is the expected type of the values
to encode. This leads to better help from the compiler on what are the expected types instead of having it solely rely on type reflection
You could also use the encode
method that the Abi Parameter type has.
const std = @import("std");
const encoder = @import("zabi").encoder;
const abi_constructor: Constructor = .{.type = .constructor, .name = "Foo", .inputs = &.{.{.type = .{ .bool = {} }, .name = "foo"}, .{ .type = .{ .string = {} }, .name = "bar" } }, .stateMutability = .nonpayable };
const encoded = try abi_constructor.encode(std.testing.allocator, .{true, "fizzbuzz"})
// Result
// 65c9c0c100000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000040000000000000000000000000000000000000000000000000000000000000000866697a7a62757a7a000000000000000000000000000000000000000000000000
In this example you will not need to pass in the params
arguments and it will use self
for this.
Returns
Type: []u8
The hex encoded string of the encoded abi function.