Skip to main content

Getting a Contract ABI

The ABI (Application Binary Interface) defines the methods and events of a smart contract, serving as a blueprint for interacting with it. On OP_NET, creating a JSON ABI is straightforward and allows you to define the structure of your contract's functions, properties, and events.


Creating a Contract JSON ABI

To create a contract ABI, you need to define the functions and events using the appropriate types from the OP_NET library. Below is a breakdown of how to create a JSON ABI for an OP_20 token contract.


Example: OP_20 ABI Definition

Dependencies

Ensure you import the necessary types and helpers for defining your ABI:

import { ABIDataTypes } from "@btc-vision/transaction";
import { BitcoinAbiTypes } from "../../BitcoinAbiTypes.js";
import { BitcoinInterfaceAbi } from "../../interfaces/BitcoinInterfaceAbi.js";
import { OP_NET_ABI } from "./OP_NET_ABI.js";

Event Definitions

Define the contract's events. For OP_20, these include events such as Mint, Transfer, and Approve:

export const OP20Events: BitcoinInterfaceAbi = [
{
name: "Mint",
values: [
{ name: "to", type: ABIDataTypes.ADDRESS },
{ name: "amount", type: ABIDataTypes.UINT256 },
],
type: BitcoinAbiTypes.Event,
},
{
name: "Transfer",
values: [
{ name: "from", type: ABIDataTypes.ADDRESS },
{ name: "to", type: ABIDataTypes.ADDRESS },
{ name: "amount", type: ABIDataTypes.UINT256 },
],
type: BitcoinAbiTypes.Event,
},
{
name: "Approve",
values: [
{ name: "owner", type: ABIDataTypes.ADDRESS },
{ name: "spender", type: ABIDataTypes.ADDRESS },
{ name: "value", type: ABIDataTypes.UINT256 },
],
type: BitcoinAbiTypes.Event,
},
];

Function Definitions

Functions are the primary way to interact with a contract. Each function includes:

  • Inputs: Parameters passed to the function.
  • Outputs: Return values from the function.

For example, defining the balanceOf function:

export const OP_20_ABI: BitcoinInterfaceAbi = [
{
name: "balanceOf",
inputs: [{ name: "account", type: ABIDataTypes.ADDRESS }],
outputs: [{ name: "balance", type: ABIDataTypes.UINT256 }],
type: BitcoinAbiTypes.Function,
},
];

Combining Events and Functions

Combine events, functions, and any additional properties to create the complete ABI:

export const OP_20_ABI: BitcoinInterfaceAbi = [
// Functions
{
name: "transfer",
inputs: [
{ name: "recipient", type: ABIDataTypes.ADDRESS },
{ name: "amount", type: ABIDataTypes.UINT256 },
],
outputs: [{ name: "success", type: ABIDataTypes.BOOL }],
type: BitcoinAbiTypes.Function,
},
{
name: "approve",
inputs: [
{ name: "spender", type: ABIDataTypes.ADDRESS },
{ name: "amount", type: ABIDataTypes.UINT256 },
],
outputs: [{ name: "success", type: ABIDataTypes.BOOL }],
type: BitcoinAbiTypes.Function,
},
// Properties
{
name: "totalSupply",
outputs: [{ name: "totalSupply", type: ABIDataTypes.UINT256 }],
type: BitcoinAbiTypes.Function,
},
// Events
...OP20Events,
// OP_NET Specific
...OP_NET_ABI,
];

ABI Components Overview
  • Functions
    Define contract methods with inputs and outputs, specifying their data types (ABIDataTypes).

  • Events
    Include event definitions for tracking on-chain interactions.

  • Properties
    Add constant values or properties that can be queried from the contract.

  • OP_NET-Specific Extensions Extend with OP_NET-specific functionality by importing OP_NET_ABI.


Best Practices

  • Use ABIDataTypes for parameter and return types.
  • Modularize event and function definitions for easy maintenance.
  • Test the ABI against a deployed contract to ensure correctness.

What’s Next?

Now that you understand the basics, explore the following topics to dive deeper into contract interactions: