Code Monkey home page Code Monkey logo

apollo's Introduction

Apollo

Query, transform, filter and save EVM based chaindata using a simple schema

apollo-diagram drawio

Documentation

For detailed documentation, visit apollo.chainbound.io.

Installation

go install github.com/chainbound/apollo

Usage

Setting up

First, generate the config directory and files:

apollo init

This will generate the configuration files (config.yml and schema.hcl) and put it into your configuration directory, which will either be $XDG_CONFIG_HOME/apollo or $HOME/.config/apollo. This is the directory in which you have to configure apollo, and it's also the directory where apollo will try to find the specified contract ABIs.

$HOME/.config/apollo/config.yml will be configured with some standard chains and public RPC APIs. These will not do for most queries, and we recommend either using your own node, or getting one with a node provider like Alchemy or Chainstack.

Schema

$HOME/.config/apollo/schema.hcl is configured with a default schema (below) that you can try out, but for a more in depth explanation visit the schema documentation or check out some examples.

start_time = format_date("02-01-2006 15:04", "25-05-2022 12:00")
end_time = now

variables = {
  b = upper("eth_buy")
  s = upper("eth_sell")
}

// query defines the name of your query -> name of your output files and SQL tables
query usdc_eth_swaps {
  // Each query can have a different chain
  chain = "arbitrum"

  contract {
    address = "0x905dfCD5649217c42684f23958568e533C711Aa3"
    abi = "unipair.abi.json"
    // Listen for events
    event Swap {
      // The outputs we're interested in, same way as with methods.
      outputs = ["amount1In", "amount0Out", "amount0In", "amount1Out"]
    }

    // "transform" blocks are at the contract-level
    transform {
      usdc_sold = parse_decimals(amount1In, 6)
      eth_sold = parse_decimals(amount0In, 18)

      usdc_bought = parse_decimals(amount1Out, 6)
      eth_bought = parse_decimals(amount0Out, 18)

      buy = amount0Out != 0
    }
  }

  filter = [
    eth_bought != 0
  ]

  // Besides the normal context, the "save" block for events provides an additional
  // variable "tx_hash". "save" blocks are at the query-level and have access to variables
  // defined in the "transform" block
  save {
    timestamp = timestamp
    block = blocknumber
    contract = contract_address
    tx_hash = tx_hash

    // Example: we want to calculate the price of the swap.
    // We have to make sure we don't divide by 0, so we use the ternary operator.
    swap_price = eth_bought != 0 ? (usdc_sold / eth_bought) : (usdc_bought / eth_sold)
    direction = buy ? b : s
    size_in_udsc = eth_bought != 0 ? usdc_sold : usdc_bought
  }
}

Running

Important: running apollo with the default parameters will send out a lot of requests, and your node provider might rate limit you. Please check the rate limiting section in the documentation. You can set the --rate-limit option to something low like 20 to start.

Realtime mode

After defining the schema, run

apollo --realtime --stdout

In the case of events, this will listen for events in real-time and save them in your output option. In the case of methods, you will have to define one of the interval parameters, and apollo will run that query at every interval.

Historical mode

After defining the schema with start, end and interval parameters, just run

apollo --stdout

The default mode is historical mode.

Output

There are 3 output options:

  • stdout: this will just print the results to your terminal.
  • csv: this will save your output into a csv file. The name of your file will be the name of your query. The other columns will be made up of what's defined in the save block.
  • db: this will save your output into a Postgres SQL table, with the table name matching your query name. The settings are defined in config.yml in your apollo config directory.

apollo's People

Contributors

fmosterts avatar mempirate avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar

Forkers

pynchmeister

apollo's Issues

Add ABI tools

  • Add a fetch-abi subcommand that will fetch the ABI from a third party API like etherscan, given an address and a chain
  • Add a ls-abi subcommand to list all the ABIs available

`SmartFilterLogs` algo should be concurrent

The SmartFilterLogs function on our CachedClient currently gets all the logs serially. We can implement some light concurrency (not too much, filtering logs is a very intensive process for the node), to speed things up.

`BlockByTimestamp` in `block_dater` improvements

Sometimes, the BlockByTimestamp method does not work. I think this is because the algo tries to estimate a distance of blocks based on the average block time, but this can result in getting stuck when it jumps between 2 blocks, with no changes. There should probably be some variability to the estimated block number to make sure this doesn't happen.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.