Datasource

export declare namespace dataSource {
  function create(name: string, params: Array<string>): void;
  function createWithContext(name: string, params: Array<string>, context: DataSourceContext): void;
 
  function address(): Address;
  function network(): string;
  function context(): DataSourceContext;
}
export namespace dataSource {
  export function stringParam(): string {
    return String.UTF8.decode(dataSource.address().buffer);
  }
}
 
export class DataSourceContext extends Entity {}
 
export class DataSourceTemplate {
  
  static create(name: string, params: Array<string>): void {
    dataSource.create(name, params);
  }
 
  static createWithContext(name: string, params: Array<string>, context: DataSourceContext): void {
    dataSource.createWithContext(name, params, context);
  }
}

The Data Source class provides metadata and utilities to interact with contracts and events in your subgraph. It includes methods like address(), network(), and context() to retrieve important contract-related data and configurations.

Fields and Use Cases

address()

  • Description: The contract address from which the event or call originated. This is equivalent to event.address.
  • Use Case: Allows tracking and indexing interactions specific to a contract in the subgraph.

network()

  • Description: The network on which the contract is deployed (e.g., mainnet, rinkeby).
  • Use Case: Enables subgraph queries to differentiate and track contract activity across different networks.

context()

  • Description: Extra configuration passed as key-value pairs when instantiating a template. Supports various data types such as Bool, String, Int, BigInt, BigDecimal, and Bytes.
  • Use Case: Stores additional metadata or runtime parameters for dynamic contract interactions or indexing strategies.

Data Source Context

The Data Source Context class provides helpers to dynamically set and get values for configuration or metadata stored as key-value pairs. Below is a reference for its available methods:

Methods and Use Cases

setString(key: string, value: string)

  • Return Type: void
  • Description: Helper to dynamically set string fields in the context.
  • Use Case: Adds string metadata dynamically during subgraph indexing.

getString(key: string)

  • Return Type: string
  • Description: Helper to dynamically retrieve string fields from the context.
  • Use Case: Access string metadata stored during subgraph indexing.

setI32(key: string, value: i32)

  • Return Type: void
  • Description: Helper to dynamically set 32-bit integer fields in the context.
  • Use Case: Adds numerical metadata dynamically during subgraph indexing.

getI32(key: string)

  • Return Type: i32
  • Description: Helper to dynamically retrieve 32-bit integer fields from the context.
  • Use Case: Access numerical metadata stored during subgraph indexing.

setBigInt(key: string, value: BigInt)

  • Return Type: void
  • Description: Helper to dynamically set BigInt fields in the context.
  • Use Case: Adds large numerical metadata dynamically during subgraph indexing.

getBigInt(key: string)

  • Return Type: BigInt
  • Description: Helper to dynamically retrieve BigInt fields from the context.
  • Use Case: Access large numerical metadata stored during subgraph indexing.

setBytes(key: string, value: Bytes)

  • Return Type: void
  • Description: Helper to dynamically set Bytes fields in the context.
  • Use Case: Adds byte metadata dynamically during subgraph indexing.

getBytes(key: string)

  • Return Type: Bytes
  • Description: Helper to dynamically retrieve Bytes fields from the context.
  • Use Case: Access byte metadata stored during subgraph indexing.

setBoolean(key: string, value: bool)

  • Return Type: void
  • Description: Helper to dynamically set boolean fields in the context.
  • Use Case: Adds boolean metadata dynamically during subgraph indexing.

getBoolean(key: string)

  • Return Type: boolean
  • Description: Helper to dynamically retrieve boolean fields from the context.
  • Use Case: Access boolean metadata stored during subgraph indexing.

setBigDecimal(key, value: BigDecimal)

  • Return Type: void
  • Description: Helper to dynamically set BigDecimal fields in the context.
  • Use Case: Adds decimal metadata dynamically during subgraph indexing.

getBigDecimal(key: string)

  • Return Type: BigDecimal
  • Description: Helper to dynamically retrieve BigDecimal fields from the context.
  • Use Case: Access decimal metadata stored during subgraph indexing.