thegraph.com How to update data source programmatically to track new contract?

628 Views Asked by At

Based on the doc, it seems that we need to specify the address of the contract we want to track. How could I track the new contract? eg. After deploying a factory contract, we also need to track all the new contract created by the factory.

Understanding that you could do this manually or writing a Cron task to check new contract deployment and script a re-deployment, is there any way to do this programmatically within thegraph.com? eg. How does uniswap track new trading pair after someone adding them (new contract address to track I suppose)?

pragma solidity >=0.5.0;

interface IUniswapV2Factory {
  event PairCreated(address indexed token0, address indexed token1, address pair, uint);

  function getPair(address tokenA, address tokenB) external view returns (address pair);
  function allPairs(uint) external view returns (address pair);
  function allPairsLength() external view returns (uint);

  function feeTo() external view returns (address);
  function feeToSetter() external view returns (address);

  function createPair(address tokenA, address tokenB) external returns (address pair);
}

It's straight forward to track the factory contract. But after createPair, how could I track the newly created contract address pair? Is it possible to do this within thegraph.com? Or do I have to do it the hard way?

Answer: https://thegraph.com/docs/define-a-subgraph#data-source-templates

3

There are 3 best solutions below

0
On
3
On

Usually, factory contract emits an event when it deploys a new contract. Just track this event. However the question lacks the source code of the contracts so it is hard to say.

0
On

You can't use dataSources dynamically. You must create a templates on your subgraph.yaml file for the child contract. If you checked the Uniswap subgraph, the factory contract is a dataSources, but the Pair (the child contract) used templates.

It is explained in the graph documentation.