Skip to main content
@metalntwx/metal-ts is the TypeScript library for submitting transactions to Metal. It works with viem and adds support for choosing which MIP20 token pays the transaction fee. The chosen token is recorded in Metal’s 0x7f transaction envelope. The SDK prepares this envelope, requests the account’s signature, and broadcasts the signed transaction. The package also provides a standalone transaction codec and ERC-3009 authorization helpers.

Installation

viem is an optional peer dependency. Install it for the wallet helpers, or import @metalntwx/metal-ts/codec when using another signer. Call initMetalCodec() before using the codec directly. The high-level transaction functions load the codec automatically. ERC-3009 helpers require no initialization.

Choose an API

Build, sign, and send transactions

writeMip20Contract

Build, sign, and broadcast a contract call, paying gas in a MIP20 token. The returned hash confirms broadcast; the helper does not wait for a receipt or finality. Verify the successful receipt and finalized block before crediting a payment.
In the browser, build walletClient from the injected provider (custom(window.ethereum)); the same call covers a Node private-key signer as shown above.

signMip20Contract

Build, sign, and encode a contract call without broadcasting it. It accepts the same arguments as writeMip20Contract and returns:
txHash is computed from the signed 0x7f envelope before submission. Call send() to broadcast raw. Persist txHash and raw before broadcasting so you can check the same transaction if the RPC response is lost. Both signing and writing fetch the account’s pending nonce. Signing does not reserve it: serialize preparation for an account, or coordinate nonces through the codec when preparing several transactions before broadcasting.

sendMip20Call

Use this lower-level helper when you already have ABI-encoded calldata.
Gas estimation: estimateGas is called with 20% headroom + 10,000. On failure it falls back to gasFallback. When authorizationList is non-empty, gasFallback is the minimum, because pre-authorization estimation cannot see the newly installed account code. Nonce: fetched from getTransactionCount with blockTag: "pending", so in-flight transactions are counted. Returns: the transaction hash after broadcast. Await publicClient.waitForTransactionReceipt({ hash }) for execution, check the receipt’s status, then verify finality.

Error handling

sendMip20Call and writeMip20Contract throw on a missing wallet account, an invalid signature yParity, or an eth_sendRawTransaction rejection (for example, insufficient gas-token balance when the account can’t cover gas).

Codec API

The codec implements the same MIP20 transaction encoding used by the chain. It has no wallet-library dependency, so indexers, edge runtimes, and applications using another signer can import it directly.

initMetalCodec

Loads the WASM module. Repeated calls reuse the loaded instance. Pass wasmSource to load WASM from a non-default location.

Use the codec without viem

The following example signs with ethers v5 and submits the encoded envelope through JSON-RPC.

Mip20Tx

value is always 0 and is injected by the codec; it is not on this shape because mempool admission rejects value != 0. authorizationList carries optional EIP-7702 SetCode authorizations. Omit it for ordinary transfers. Include a signed authorization when an account opts into or updates a smart-account implementation; the codec binds the canonical RLP hash of the list into the transaction signature.

buildMip20TypedDataMessage

Produces the EIP-712 typed data a wallet’s signTypedData consumes.
The MIP20Tx typehash has 10 fields. value: 0n and authorizationListHash are injected here to match the chain’s canonical typehash; authorizationListHash is computed inside WASM; never hand-roll it. When tx.to is null the helper substitutes 0x0000…0000, and all address fields are lowercased so viem’s signTypedData accepts them regardless of source casing.

encodeSignedEnvelope

signatureBytes(sigHex) (from the package root) marshals a viem hex signature into the 65-byte layout this expects.

ERC-3009 authorizations

ERC-3009 lets a token holder sign a transfer authorization for another account to submit. The package provides wallet-independent typed-data builders and viem signing helpers. The pure helpers are exported from @metalntwx/metal-ts/erc3009: The viem helpers are exported from @metalntwx/metal-ts/erc3009-viem:

Export reference

Package entrypoints

Other MIP20 exports

Agent packages

@metalntwx/metal-pay adds x402 payments on Metal to a buyer or a seller: the exact scheme, spending-limit mandates, signed orders and pay-per-call clearing. @metalntwx/metal-agent is the CLI and MCP server that holds an agent’s key and pays with it. Both are documented in the Agents section.