Module

Contract — 10 credits

Call any smart contract function. Use read() for view/pure functions (no gas, no transaction). Use write() for state-changing functions that require a transaction. Both cost 10 credits.

read()
POST 10 credits API Key or JWT

Call a read-only (view or pure) contract function. No transaction is created; the result is returned immediately from the node.

Parameters

NameTypeRequiredDescription
$contractAddressstringYesSmart contract address
$functionNamestringYesFunction name to call (e.g. "balanceOf")
$argsarrayNoPositional arguments to pass to the function
$abiarrayNoABI fragment(s) for the function. Can be minimal — just the function entry.
$network?stringNoNetwork name. Defaults to WEB3_DEFAULT_NETWORK

Example — call balanceOf on any ERC-20

$result = Web3Api::contract()->read(
    contractAddress: '0x2791Bca1f2de4661ED88A30C99A7a9449Aa84174',
    functionName:    'balanceOf',
    args:            ['0xd8dA6BF26964aF9D7eEd9e03E53415D37aA96045'],
    abi:             [[
        'name'            => 'balanceOf',
        'type'            => 'function',
        'stateMutability' => 'view',
        'inputs'          => [['name' => 'account', 'type' => 'address']],
        'outputs'         => [['name' => '', 'type' => 'uint256']],
    ]],
    network:         'polygon',
);

echo $result['result'];  // "100000000" — raw units (divide by 10^6 for USDC)

Example — call a custom contract function

$result = Web3Api::contract()->read(
    contractAddress: '0xMY_CONTRACT',
    functionName:    'getOwner',
    args:            [],
    abi:             [[
        'name'            => 'getOwner',
        'type'            => 'function',
        'stateMutability' => 'view',
        'inputs'          => [],
        'outputs'         => [['name' => '', 'type' => 'address']],
    ]],
);
// $result['result'] => "0xOWNER_ADDRESS"

Response

[
  'result' => '1000000000000000000',  // raw return value as string
]
write()
POST 10 credits JWT

Execute a state-changing contract function. Sends a signed transaction to the network. Requires the sender's private key to sign the transaction.

Parameters

NameTypeRequiredDescription
$contractAddressstringYesSmart contract address
$functionNamestringYesFunction name to call (e.g. "mint", "transfer")
$argsarrayYesPositional arguments for the function
$abiarrayYesABI fragment(s) for the function
$fromAddressstringYesCaller's wallet address
$privateKey?stringNoCaller's private key. Pass null to use the stored managed wallet
$gas?intNoGas limit (auto-estimated if null)
$network?stringNoNetwork name. Defaults to WEB3_DEFAULT_NETWORK

Example — mint an NFT

$mintAbi = [[
    'name'            => 'mint',
    'type'            => 'function',
    'stateMutability' => 'nonpayable',
    'inputs'          => [
        ['name' => 'to',      'type' => 'address'],
        ['name' => 'tokenId', 'type' => 'uint256'],
    ],
    'outputs'         => [],
]];

$tx = Web3Api::contract()->write(
    contractAddress: '0xMY_NFT_CONTRACT',
    functionName:    'mint',
    args:            ['0xRECIPIENT_ADDRESS', 101],
    abi:             $mintAbi,
    fromAddress:     '0xCONTRACT_OWNER',
    privateKey:      '0xOWNER_PRIVATE_KEY',
    gas:             150000,
    network:         'polygon',
);

Response

[
  'tx_hash' => '0xabcdef1234567890...',
  'status'  => 'pending',
]