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.
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
| Member | Type | Notes |
|---|---|---|
name | string | Read-only; set at construction |
abi | ReadonlyArray<AbiItem> | Read-only; the ABI you passed in |
Contract.load(name) | static (name: string) => Contract | null | Returns a previously constructed contract, or null |
Methods
encode(functionName, args)
Encodes a function call to a hex data string.
const data = myToken.encode("transfer", [
"0x1234567890123456789012345678901234567890",
1_000_000n,
]);functionName: string— must name afunctionentry in the ABI, orPARAMETER_ERRORis thrown.args: ReadonlyArray<unknown>— the positional arguments.- Returns the
0x-prefixed call data. An encoding failure (wrong arity or types) throwsTX_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.
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.
const evm = acc.wallets[0].evm; // an EVM Address (EvmSigner)
const tx = myToken.call(
evm,
"0xTokenContractAddress…",
"transfer",
["0xRecipient…", 1_000_000n],
);| Parameter | Type | Notes |
|---|---|---|
from | Address | Must be an EVM address (from.vm === "evm"); otherwise UNSUPPORTED_OP |
contractAddress | string | The contract's address, used as the transaction to |
functionName | string | Encoded via encode above |
args | ReadonlyArray<unknown> | The call arguments |
opts | Partial<EvmTxBuildParams> | Optional overrides (see below) |
opts accepts the same fields as EvmTxBuildParams — value, 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.
from is read off a wallet, not constructed directly — acc.wallets[0].evm returns the EvmSigner, which is an Address.
AbiItem
The element type of Contract.abi, re-exported from wative-core.
import type { AbiItem } from "wative-core";| Field | Type | Notes |
|---|---|---|
type | string (optional) | e.g. "function", "event", "constructor" |
name | string (optional) | The entry's name |
inputs | ReadonlyArray<{ name: string; type: string; components?: unknown[] }> (optional) | Input parameters |
outputs | ReadonlyArray<{ name: string; type: string; components?: unknown[] }> (optional) | Output parameters |
stateMutability | string (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.
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.