Skip to content

(Just tracking) DevRel to publish code snippet to lookup core contract address using Registry.sol #30

@arthurgousset

Description

@arthurgousset

@therealharpaljadeja shared a great insight.

He loves using viem + @celo/abis, but when he wants to interact with core contracts, he ends up hard-coding the core contract address.

That's not strictly necessary, because the address can be fetched from Registry.sol.
In ContractKit, this is abstracted from the developer, whereas with viem this has to be done manually.

There are various solutions and ideas to make this easier:

  • v1 (easiest): publish a code snippet that achieves this (so devs can copy/paste and get started)
  • v2 (more involved): publish a viem plugin, NPM package, or other form of abstraction

Here is a little code snippet I used in celo-org/epochs to lookup core contract addresses in various places:

// utils.ts
import { createPublicClient, http, getContract } from "viem";
import { celo } from "viem/chains";
import { registryABI } from "@celo/abis";

// ...

export async function getCoreContractAddress(
    contractName: string
): Promise<Address> {
    const registryContract = getContract({
        address: "0x000000000000000000000000000000000000ce10",
        abi: registryABI,
        publicClient,
    });
    return await registryContract.read.getAddressForStringOrDie([contractName]);
}

Source: utils.ts

// carbonOffsetDistributions.ts
import { getCoreContractAddress, publicClient } from "./utils";
import { goldTokenABI } from "@celo/abis";

// ...

async function getCarbonOffsetDistribution(epochNumber: bigint) {
    const filter = await publicClient.createContractEventFilter({
        address: await getCoreContractAddress("GoldToken"), // CELO ERC20 contract address
        abi: goldTokenABI,
        eventName: "Transfer",
        // ...
    });
    // ...
}

Source: carbonOffsetDistributions.ts

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions