Skip to content

decodeAbiFunctionRuntime

Definition

Decoded the generated Abi encoded data into decoded native types using the contract ABI specification

This takes in 5 arguments:

  • an allocator used to manage the memory allocations
  • a type that is used as the expected return type of this call.
  • the function struct signature to decode against.
  • the abi encoded hex string.
  • the options used for decoding (Checkout the options here: DecodeOptions)
You must call deinit() after to free any allocated memory.
const std = @import("std");
const decoder = @import("zabi").decoder;
const Function = @import("zabi").abi.Function;
 
const abi_function: Function = .{
  .type = .function, 
  .name = "Foo", 
  .inputs = &.{
      .{ .type = .{ .bool = {} }, .name = "foo"}, 
      .{ .type = .{ .string = {} }, .name = "bar" } 
    },
  .stateMutability = .nonpayable, 
  .inputs = &.{} 
};
 
const ReturnType = std.meta.Tuple(&[_]type{bool , []const u8});
const encoded = "65c9c0c100000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000040000000000000000000000000000000000000000000000000000000000000000866697a7a62757a7a000000000000000000000000000000000000000000000000"
 
const decoded = try decoder.decodeAbiFunctionRuntime(std.testing.allocator, ReturnType, abi_function, encoded, .{});
defer decoded.deinit();
 
// Result
// .{true, "fizzbuzz"}

Returns

The return value is expected to be a tuple of types used for encoding. Compilation will fail or runtime errors will happen if the incorrect type is passed to the decoded function.

  • Type: AbiSignatureDecodeRuntime(T)

decodeAbiFunction

Definition

Decoded the generated Abi encoded data into decoded native types using the contract ABI specification This expects that the function struct is comptime know. With this we don't need to know the expected return type since zabi can infer the return type from the struct signature.

This takes in 5 arguments:

  • an allocator used to manage the memory allocations
  • the function struct signature to decode against.
  • the abi encoded hex string.
  • the options used for decoding (Checkout the options here: DecodeOptions)
You must call deinit() after to free any allocated memory.
const std = @import("std");
const decoder = @import("zabi").decoder;
const Function = @import("zabi").abi.Function;
 
const abi_function: Function = .{
  .type = .function, 
  .name = "Foo", 
  .inputs = &.{
      .{ .type = .{ .bool = {} }, .name = "foo"}, 
      .{ .type = .{ .string = {} }, .name = "bar" } 
    },
  .stateMutability = .nonpayable, 
  .inputs = &.{} 
};
 
const encoded = "65c9c0c100000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000040000000000000000000000000000000000000000000000000000000000000000866697a7a62757a7a000000000000000000000000000000000000000000000000"
 
const decoded = try decoder.decodeAbiFunction(std.testing.allocator, abi_function, encoded, .{});
defer decoded.deinit();
 
// Result
// .{true, "fizzbuzz"}

Returns

  • Type: AbiSignatureDecode(function.inputs)

Outputs

Zabi also supports decoding function outputs for both comptime or runtime know signatures. They behaive in the same manner as the above the difference is instead of using the function inputs it uses it's outputs.