Chain HelpersContract (EVM)

Contract (EVM)

The Contract helper and its AbiItem type for encoding and decoding EVM contract calls, plus the ready-made ERC20 artifact re-exported under wative-core/artifacts/evm.

Contract wraps an EVM ABI and turns a function name plus arguments into the hex data an EVM transaction carries. It encodes calls, decodes return values, and can build a complete EvmTransaction in one step. The class is on the main wative-core entry; the ready-made ERC20 instance is on wative-core/artifacts/evm.

Constructing a Contract

Pass a name and an ABI array. The name is used for lookups and error messages; the ABI is stored by reference.

contract.ts
import { Contract } from "wative-core";

const myToken = new Contract("MyToken", myAbi);

The constructor throws PARAMETER_ERROR if name is not a non-empty string or abi is not an array. Every constructed Contract is also registered by name, so Contract.load(name) returns it later.

Identity and lookup

MemberTypeNotes
namestringRead-only; set at construction
abiReadonlyArray<AbiItem>Read-only; the ABI you passed in
Contract.load(name)static (name: string) => Contract | nullReturns a previously constructed contract, or null

Methods

encode(functionName, args)

Encodes a function call to a hex data string.

encode.ts
const data = myToken.encode("transfer", [
  "0x1234567890123456789012345678901234567890",
  1_000_000n,
]);
  • functionName: string — must name a function entry in the ABI, or PARAMETER_ERROR is thrown.
  • args: ReadonlyArray<unknown> — the positional arguments.
  • Returns the 0x-prefixed call data. An encoding failure (wrong arity or types) throws TX_BUILD_FAILED.

The matching fragment is selected from the ABI, so functionName is unambiguous even when the ABI carries overloads.

decode(functionName, encoded)

Decodes a hex-encoded return value by function name.

decode.ts
const [balance] = myToken.decode("balanceOf", returnedHex);
  • Returns a ReadonlyArray<unknown> of the decoded outputs, or [] when the function declares no outputs.
  • Integer values are returned as decimal strings, not bigint — the shape web3 1.x produced — and nested tuples/arrays are walked so the conversion reaches every level.
  • A decode failure throws TX_BUILD_FAILED.

call(from, contractAddress, functionName, args, opts?)

Builds a complete EvmTransaction for a contract call in one step, binding the address so the transaction can sign, send, and simulate.

call.ts
const evm = acc.wallets[0].evm; // an EVM Address (EvmSigner)

const tx = myToken.call(
  evm,
  "0xTokenContractAddress…",
  "transfer",
  ["0xRecipient…", 1_000_000n],
);
ParameterTypeNotes
fromAddressMust be an EVM address (from.vm === "evm"); otherwise UNSUPPORTED_OP
contractAddressstringThe contract's address, used as the transaction to
functionNamestringEncoded via encode above
argsReadonlyArray<unknown>The call arguments
optsPartial<EvmTxBuildParams>Optional overrides (see below)

opts accepts the same fields as EvmTxBuildParamsvalue, type, nonce, gasPrice, maxFeePerGas, maxPriorityFeePerGas, gasLimit, accessList, and rpcUrl for a per-transaction endpoint override. chainId defaults to from.network.chainId when omitted. The returned EvmTransaction is bound to from, so any gas/fee/nonce field you leave out is auto-filled at sign time exactly as a transaction built off the address would be.

AbiItem

The element type of Contract.abi, re-exported from wative-core.

import type { AbiItem } from "wative-core";
FieldTypeNotes
typestring (optional)e.g. "function", "event", "constructor"
namestring (optional)The entry's name
inputsReadonlyArray<{ name: string; type: string; components?: unknown[] }> (optional)Input parameters
outputsReadonlyArray<{ name: string; type: string; components?: unknown[] }> (optional)Output parameters
stateMutabilitystring (optional)e.g. "view", "nonpayable", "payable"

The ERC20 artifact

ERC20 is a ready-made Contract built from the standard ERC-20 ABI, exposed under the wative-core/artifacts/evm subpath so its ABI JSON is only bundled when you import it.

erc20.ts
import { ERC20 } from "wative-core/artifacts/evm";

// Encode transfer(address,uint256) call data.
const data = ERC20.encode("transfer", [recipient, 1_000_000n]);

// Or build a full transaction from an EVM address.
const tx = ERC20.call(evm, usdcAddress, "transfer", [recipient, 1_000_000n]);

Because ERC20 is a Contract, every method above (encode, decode, call) and the abi / name fields work on it directly.

Last updated on