In this document, you will learn how to retrieve smart contract events or manage your smart contracts using the CDP SDK.

You can retrieve smart contract events via CDP API.

You can also management your smart contract events via CDP Portal or CDP API.

To learn more about smart contracts options, please follow this document.

What You’ll Learn

  • How to retrieve smart contracts events

Prerequisites

Before you get started, please follow this guide to install CDP SDK.

Smart Contract Events

List Smart Contract Events

To start, create a CDP API key. Then, initialize the CDP SDK by passing your downloaded API key file, and retrieve smart contracts events.

Typescript
import { Coinbase } from "@coinbase/coinbase-sdk";
const coinbase = Coinbase.configureFromJson({ filePath: '~/Downloads/cdp_api_key.json' });

This will allow you to authenticate with the Platform APIs.

// Below is an example of list smart contract events call
SmartContract.listEvents(
  "base-mainnet", // networkId
  "public", // protocolName
  "0x940181a94a35a4569e4529a3cdfb74e38fd98631", // contractAddress
  "AERO", // contractName
  "Transfer", // eventName
  18341291, // fromBlockHeight
  18341292 // toBlockHeight
).then(events => {
    console.log(`Smart Contract Events: `, events);
  })
  .catch(error => {
    console.error('Error:', error);
  });

Smart Contract ABI Management

Authentication

To get started, create a secret API key in the Portal’s API Key section if you don’t already have one. Suppose you store your secret API key at ~/Downloads/cdp_api_key.json Before making any SDK calls, import the necessary package and authenticate using your downloaded API key.

    from cdp import *
    Cdp.configure_from_json("~/Downloads/cdp_api_key.json")

Register A Smart Contract

Below is an example of a smart contract registration call. You can upload the smart contract you’re interested in. Remember to replace the network, contract address, contract name and abi with your own values. Note that attempting to register the same contract again will result in an error.

    smart_contract = SmartContract.register(
    network_id="base-mainnet",
    contract_address="0x0000000071727de22e5e9d8baf0edac6f37da000",
    abi=[
        {
        "inputs": [
            {
            "internalType": "string",
            "name": "_initialMessage",
            "type": "string",
            },
        ],
        "stateMutability": "nonpayable",
        "type": "constructor",
        },
    ],
    contract_name="Test Smart Contract",
    )

List Smart Contracts

Below is an example call that lists all of your smart contracts.

    SmartContract.list()

Update The Smart Contract

Below is an example of a smart contract update call. Remember to replace network, contract address, contract name and abi with your own values. Currently, only the ABI and contract name can be updated. You should perform the update call on an existing Smart Contract object.

   updated_smart_contract = smart_contract.update(
    abi=[
        {
        "inputs": [
            {
            "internalType": "string",
            "name": "_initialMessage",
            "type": "string",
            },
        ],
        "stateMutability": "nonpayable",
        "type": "constructor",
        },
    ],
    contract_name="Test Smart Contract",
    )