DEV Community

megared05
megared05

Posted on • Originally published at chainsight.network

Chainsight Hands-on: Collecting Data from Onchain

Previously we created a hands-on article on creating a component to collect bitcoin price data from the web. We have built Snapshot Indexer HTTPS to collect data from the web with reliability, but now let's try to get data on-chain. In this article, we will create a component that collects data from Ethereum using the Snapshot Indexer EVM.

In this project, let's create a Price Snapshot Indexer EVM that can get the price of a specified currency using the Price Aggregator Contract on Ethereum.

Completed Component Definition

Let's create a Snapshot Indexer EVM that collects Ethereum prices. In order to get the Ethereum price, we will use the ETH/USD Aggregator Contract in the Ethereum Blockchain to collect the price data. This component is constructed with a collection interval of 1 hour.

chainsight_deepdive_into_showcase-handson-snapshot_indexer_evm.png

Prerequisite

The Chainsight CLI requires several tools and this CLI installation. Please refer to the following article for this preliminary preparation.

Step-by-step creating an EVM Price Oracle with Chainsight - DEV Community

Create a project & add a manifest template

As before, build from an empty project; use csx new --no-samples to create a template.

% csx new --no-samples sample_evm
Jan 30 13:24:51.690 INFO Start creating new project 'sample_evm'...
Jan 30 13:24:51.691 INFO Project 'sample_evm' created successfully
Jan 30 13:24:51.691 INFO You can add components with:

  cd sample_evm && csx add
Enter fullscreen mode Exit fullscreen mode

Then use csx add to add the Snapshot Indexer EVM. snapshot_indexer_evm type should be selected.

% cd sample_evm 
% csx add
✔ Please input Component Name to add · sample_component
✔ Please select Component Type to add · snapshot_indexer_evm
Jan 30 13:25:38.714 INFO Start creating new component 'sample_component'...
Enter fullscreen mode Exit fullscreen mode

Let's update this manifest that we have added and generate a contract that retrieves data from the Aggregator Contract. The contract to be called this time is as follows. Use the Aggregator Contract that Chainlink deploys on Ethereum.

chainsight-handson-evm-1.png
Source: https://docs.chain.link/data-feeds/price-feeds/addresses?network=ethereum&page=1#ethereum-mainnet

In the datasource field, specify the contract and its function to be called this time, setting location.id to 5f4eC3Df9cbd43714FE2740f5E3616155c5b8419 without prefix and method.identifier to latestAnswer():(int256) to get the latest price data from this contract. Finally, an ABI file in JSON format must be obtained, placed in the interface folder, and specified in method.interface. The contents of the ABI can be getted from the following url.

https://etherscan.io/address/0x5f4eC3Df9cbd43714FE2740f5E3616155c5b8419#code

The final modified datasource field will look like this

datasource:
  location:
    id: 5f4eC3Df9cbd43714FE2740f5E3616155c5b8419
    args:
      network_id: 1
      rpc_url: https://mainnet.infura.io/v3/${INFURA_MAINNET_RPC_URL_KEY}
  method:
    identifier: latestAnswer():(int256)
    interface: Aggregator.json
    args: []
Enter fullscreen mode Exit fullscreen mode

Once you have the above modifications at hand, let's get started with the code generation!

Generate a code & build a module

csx generate will generate canister code with the manifest in your project as input. Now let's run it to generate the code for the Snapshot Indexer EVM we mentioned earlier.

% csx generate
Feb 02 13:24:12.258 INFO Start code generation for project 'sample_evm'
Feb 02 13:24:12.259 INFO [sample_component] Start processing...
Feb 02 13:24:12.259 INFO [sample_component] Interface file 'Aggregator.json' copied from user's interface
Feb 02 13:24:12.411 INFO [sample_component] Generate interfaces (.did files) ...
Feb 02 13:24:13.039 INFO [sample_component] Succeeded: Generate interfaces (.did files)
Feb 02 13:24:13.039 INFO Project 'sample_evm' codes/resources generated successfully
Enter fullscreen mode Exit fullscreen mode

If the log is displayed like this, the command has successfully completed. Depending on some Component and its parameters, it is possible to make customizations and adjustments to this generated canister code, but in the case of this Snapshot Indexer EVM, this is not necessary. This is the completed canister code. Therefore, this is all that is needed to begin the build process.

csx build builds the generated code into modules and generates what is needed to deploy them as components of Chainsight Platform. Let's run it immediately.

% csx build
Feb 02 13:28:31.518 INFO Start building project 'sample_evm'
Feb 02 13:28:31.518 INFO Start code generation for project 'sample_evm'
Feb 02 13:28:31.519 INFO [sample_component] Start processing...
Feb 02 13:28:31.519 INFO [sample_component] Interface file 'Aggregator.json' copied from user's interface
Feb 02 13:28:31.524 INFO [sample_component] Skip creating logic project: './src/logics/sample_component' already exists
Feb 02 13:28:31.524 INFO [sample_component] Generate interfaces (.did files) ...
Feb 02 13:28:32.443 INFO [sample_component] Succeeded: Generate interfaces (.did files)
Feb 02 13:28:32.443 INFO Project 'sample_evm' codes/resources generated successfully
Feb 02 13:28:32.443 INFO Start building...
Feb 02 13:28:32.443 INFO Compile canisters...
Feb 02 13:29:10.484 INFO Succeeded: Compile canisters
Feb 02 13:29:10.484 INFO Shrink/Optimize modules...
Feb 02 13:29:10.590 INFO Succeeded: Shrink/Optimize modules
Feb 02 13:29:10.590 INFO Add metadata to modules...
Feb 02 13:29:10.685 INFO Succeeded: Add metadata to modules
Feb 02 13:29:10.685 INFO Project 'sample_evm' built successfully
Enter fullscreen mode Exit fullscreen mode

The build process takes less than a few minutes and the artifacts generated by the csx build are placed in the artifacts folder. If you look in that folder, you will see WASM modules and candid interface files that actually have component names as their file names. For a more in-depth understanding, please refer to the following article.

Behind Chainsight: How Modules are Created Pt. 2 | by Chainsight | Feb, 2024 | Medium

Finally, deploy and start the component!

Deploy a component

If you can deploy this module as a component of the Chainsight Platform, all is done.

With csx deploy we can actually deploy to the Chainsight Platform on Internet Computer, and with csx exec we can send the commands to start working. Let's run the command with some parameters to specify the component and execution network.

csx deploy --network ic --component sample_component
csx exec --network ic --component sample_component
Enter fullscreen mode Exit fullscreen mode

If you run these commands and they complete without problems, you should see the following output! The log here has slightly changed the names of the components, so just read and check that point.

% csx deploy --network ic --component eth_snapshot_indexer
Feb 02 13:35:53.077 INFO Checking environments...
Feb 02 13:35:53.077 INFO Running command: 'dfx ping ic'
Feb 02 13:35:54.957 INFO Suceeded: dfx ping ic
Feb 02 13:35:54.957 INFO Running command: 'dfx identity whoami --network ic'
Feb 02 13:35:55.044 INFO Suceeded: dfx identity whoami --network ic
Feb 02 13:35:55.044 INFO Running command: 'dfx identity get-principal --network ic'
Feb 02 13:35:55.121 INFO Suceeded: dfx identity get-principal --network ic
Feb 02 13:35:55.121 INFO Running command: 'dfx identity get-wallet --network ic'
Feb 02 13:35:55.201 INFO Suceeded: dfx identity get-wallet --network ic
Feb 02 13:35:55.201 INFO Checking environments finished successfully
Feb 02 13:35:55.201 INFO Start deploying project 'sample_evm'...
Feb 02 13:35:55.201 INFO Running command: 'dfx canister create --network ic eth_snapshot_indexer'
Feb 02 13:36:02.966 INFO Suceeded: dfx canister create --network ic eth_snapshot_indexer
Feb 02 13:36:02.966 INFO Running command: 'dfx build --network ic eth_snapshot_indexer'
Feb 02 13:36:03.218 INFO Suceeded: dfx build --network ic eth_snapshot_indexer
Feb 02 13:36:03.218 INFO Running command: 'dfx canister install --network ic eth_snapshot_indexer'
Feb 02 13:36:17.031 INFO Suceeded: dfx canister install --network ic eth_snapshot_indexer
Feb 02 13:36:17.031 INFO Running command: 'dfx canister update-settings --add-controller xkzfe-fiaaa-aaaal-qcnuq-cai --network ic eth_snapshot_indexer'
Feb 02 13:36:22.852 INFO Suceeded: dfx canister update-settings --add-controller xkzfe-fiaaa-aaaal-qcnuq-cai --network ic eth_snapshot_indexer
Feb 02 13:36:24.305 INFO Current deployed status:
Canister Name: eth_snapshot_indexer
  Canister Id: {"ic": "5saj5-jqaaa-aaaal-qdfea-cai"}
  Controllers: 5naws-oiaaa-aaaal-qbxuq-cai k245a-2cvyi-rcnzj-cpq6h-dcy7o-qrg4b-325bv-5c3om-jfceo-ikmjz-aqe xkzfe-fiaaa-aaaal-qcnuq-cai
  Module Hash: 0x6c5ee5bdde96e2ef3c49d635d44086538bc38caf43e878a0651cdbbd0584c4ba
Enter fullscreen mode Exit fullscreen mode
% csx exec --network ic --component eth_snapshot_indexer
Feb 02 13:36:34.713 INFO Execute canister processing...
Feb 02 13:36:34.713 INFO Start processing for commands generation...
Feb 02 13:36:34.713 INFO Script for Component "eth_snapshot_indexer" generated successfully
Feb 02 13:36:34.713 INFO Entrypoint Script generated successfully
Feb 02 13:36:34.713 INFO Start processing for commands execution...
Feb 02 13:36:34.713 INFO Run scripts to execute commands for deployed components
Feb 02 13:36:34.713 INFO Selected component is 'eth_snapshot_indexer'
Feb 02 13:37:15.705 INFO Executed './scripts/entrypoint.sh' successfully
Feb 02 13:37:15.705 INFO Project "sample_evm" canisters executed successfully
Enter fullscreen mode Exit fullscreen mode

The deployment and operation start instruction is now complete! Now all you have to do is wait for the interval of the automatic execution and you can refer to the data.


The components that can be created in this hands-on session are also actually running! (We have changed the description and execution interval a bit.)

chainsight-handson-evm-2
Source: Detail lcdio-qiaaa-aaaal-qdg4q-cai| Chainsight

This concludes our hands-on with the Snapshot Indexer EVM. We will continue to create and publish new hands-on sessions in the future, so please look forward to them!

Written by Megared@Chainsight

Top comments (0)