eliza icon indicating copy to clipboard operation
eliza copied to clipboard

feat: add readContract / invokeContract functionality to Coinbase plugin

Open monilpat opened this issue 1 year ago • 0 comments

Features

Contract Interactions

  • Introduced readContractAction for reading data from deployed smart contracts
  • Added ReadContractContent interface and ReadContractSchema for input validation
  • Integrated readContractTemplate to guide users in extracting necessary details for read operations
  • Enhanced existing contract interaction capabilities in tokenContractPlugin with invokeContractAction now relying on networkId, amount as a string, and required assetId

Technical Details

New Types

interface ReadContractContent {
    contractAddress: `0x${string}`;
    method: string;
    networkId: string; // Network now identified by networkId (e.g. "ethereum-mainnet")
    args?: Record<string, any>;
    abi?: any[];
}
  • ReadContractSchema added for validating read operations:
    • Validates contractAddress, method, networkId, args, and optional abi

Updated Invocation Schema

  • network replaced by networkId to align with Coinbase SDK requirements
  • amount now handled as a string to support large values without precision loss
  • assetId is now required for payable contract methods

Templates

  • readContractTemplate: Guides extracting details (contractAddress, method, networkId, args, abi) for reading contract data
  • tokenContractTemplate & contractInvocationTemplate: Enhanced for clarity and alignment with updated schema fields

Actions

  • readContractAction: Reads data (view/pure methods) from deployed smart contracts
  • invokeContractAction: Invokes contract methods; updated to use networkId, amount as a string, and a required assetId
  • deployTokenContractAction: Deploys token contracts as before, now compatible with the updated schemas

Testing

Contract Reading

  • Tested reading ERC20 balances and contract state variables
  • Validated network support via networkId
  • Ensured error handling for invalid contracts, addresses, and unsupported networks

Invocation and Deployment

  • Confirmed invocation succeeds with the updated networkId approach
  • Verified that amount as a string and mandatory assetId don't break existing flows

Reading contract: Screenshot 2024-12-08 at 2 36 12 PM

Invoking contract:

https://drive.google.com/file/d/16VtpCI7q8J4opco9jQO3XQdwZMb0u-Yz/view?usp=sharing

Risks

  • Low: Reading contract data is read-only, minimal risk
  • Medium: Contract invocation involves real transactions
    • Mitigation: Added balance checks, improved error handling, and validation through ReadContractSchema

Documentation

  • Documentation updated to reflect new readContractAction, networkId field, and schema changes
  • Added examples showcasing how to read contract data and invoke contract methods with the new parameters

Deploy Notes

  • No database changes required
  • Compatible with existing configurations
  • Requires adjustments to references of network to networkId in configurations and calls
  • Ensure Coinbase API credentials are properly set before reading or invoking contracts

monilpat avatar Dec 08 '24 21:12 monilpat