SmartContracts Class

An abstraction in TypeScript for interacting with SmartContracts, enabling typed methods for reading, writing, and event handling on the blockchain.

Introduction

This documentation describes an abstraction layer for interacting with SmartContracts on the blockchain using TypeScript. The abstraction consists of an abstract class that provides a framework for creating methods to read data from, write data to, and listen to events from SmartContracts.

The abstract class allows you to create customized subclasses that define specific methods and their expected argument types, return types, and event listeners. By implementing this abstraction, you can ensure that interactions with SmartContracts are type-safe and consistent, reducing the potential for errors.

Key features of this abstraction include:

  • Consistent Structure: The abstract class provides a common structure for interacting with SmartContracts, allowing subclasses to focus on specific implementations.

  • Typed Methods: Each method in the subclass has a defined argument type and return type, ensuring accurate and type-safe interactions with the SmartContract.

  • Event Handling: The abstraction includes a way to set up event listeners for SmartContract events, allowing you to react to changes on the blockchain.

This abstraction simplifies SmartContract development by providing a reusable framework that supports reading, writing, and event handling. Developers can use this abstraction to create well-typed subclasses, promoting code reliability and maintainability in blockchain applications.

Abstract SmartContract Class

Provide an overview of the abstract base class and how it supports different SmartContracts. This part sets the context for any contract implementation.

  • Key Methods and Properties: Include typical methods and properties expected in a Smart Contract abstraction.

    • toRead(methodName: string): Allows reading data from a contract method.

    • toWrite(methodName: string): Allows writing data to a contract method.

    • toEvent(eventName: string): Provides a way to listen to contract events.

Usage example

ERC20 (partial)


interface ERC20Read {
  name: () => string;
  totalSupply: () => BigInt;
  balanceOf: (address: string) => BigInt;
  // ...
}

interface ERC20Write {
  approve: (spender: string, amount: BigInt) => void;
  // ...
}

interface ERC20Events {
  Approval: [Address, Address, BigInt];
  // ...
}

export class ERC20 extends SmartContract<ERC20Read, ERC20Write, ERC20Events> {
  constructor(
    protected contractAddress: string,
    provider: ContractRunner,
  ) {
    super(contractAddress, abi /* Provide ABI */, provider);
  }
  
  // Read
  name = this.toRead("name");
  totalSupply = this.toRead("totalSupply");
  balanceOf = this.toRead("balanceOf");
  
  // Write
  approve = this.toWrite("approve");
  
  // Events
  onApprove = this.toEvent("Approval", ([owner, spender, amount]) => ({ owner, spender, amount }));
}

Contract usage

const erc20 = new ERC20("0x...", provider);

await erc20.totalSupply() // BigInt
await erc20.balanceOf("0x...") // BigInt

await erc20.approve("0x...", 1000n) // Send approval

const sub = erc20.onApprove()
    .subscribe(() => console.log("New approval"));
sub.unsubscribe();

Last updated