图片

Last time we created a hands-on article on creating a component to get Ethereum price data from an Ethereum contract, the snapshot indexer EVM uses the ABI as an interface to retrieve data from EVM-compatible chains.

In this article, we will try to retrieve data from the Internet Computer using Candid, an interface on the Internet Computer running Chainsight. In this article, we will try to create a component that collects data from the Internet Computer using the snapshot indexer ICP.

The project is a price snapshot indexer ICP that collects ICP prices using DEX contracts on the Internet Computer, and as of February 2024, 1 ICP is trading at approximately $12.

Completed component definition

ICPSwap is one of the well-known DEXs on the Internet. It is used to obtain ICP prices. The ICPSwap contract (container) is called regularly to collect ICP price data. The snapshot indexer is built with a collection interval of 1 hour.

图片

prerequisites

Chainsight CLI requires multiple tools and csx to be installed. Please refer to the following articles for preliminary preparations:

  • dev.to/hide_yoshi/step-by-step-creating-an-evm-price-oracle-with-chainsight-469g

Create a project and add a manifest template

As before, build from an empty project and create the template with csx new --no-samples .

图片

Then use csx add to add the snapshot indexer EVM, and select the snapshot_indexer_icp type.

图片

Let’s update the manifest we added so that we can call the container that we have deployed on the internet computer, in this case we will use the container used in ICPSwap.

Home ID: 2ixw4-taaaa-aaaag-qcpdq-cai

图片

In the datasource field, specify the container and its function to be called this time.

In the location.id field, specify the ID of the container to call. In this case, set 2ixw4-taaaa-aaaag-qcpdq-cai. Next, in datasource.method, specify the container function to call.

In method.identifier , specify the function in Candid format, which is the Internet Computer's interface notation.

Candid is an interface description language. Its main purpose is to describe the public interface of a service, usually in the form of a program deployed as a container running on the Internet Computer. One of the main advantages of Candid is that it is language-agnostic and allows interoperability between services and frontends written in different programming languages, including Motoko, Rust, and JavaScript.

source:

  • internetcomputer.org/docs/current/developer-docs/backend/candid/candid-concepts

Internet Computer can easily view the container's interface through the dashboard.

图片图片

source:

  • dashboard.internetcomputer.org/canister/2ixw4-taaaa-aaaag-qcpdq-cai

This is the Candid interface of the container that we will be calling. The declaration in service is the interface provided by the container. In this case we will use the get_exchange_rates function described here. We need to do two things to specify this function.

Specify the function declaration containing the parameters and response information in method.identifier: get_exchange_rates: () -> (vec ExchangeRate__1).

Because this declaration uses a custom type that is not the Candid built-in type, create a .did file that contains the Candid interface so that it can be referenced, and specify the file in method.interface.

After selecting Candid in the Canister Interface of the Dashboard screen mentioned earlier, click "Copy" in the upper right corner to copy the declaration in the interface. Then, create target_interface.did under the interfaces folder and paste the content you just copied into its content. The actual folders and files will be as shown below.

图片

Once the file is created, specify interfaces/target_interface.did in method.interface and the data source field should now look like this.

图片

Finally, add a setting for this component to call a container outside the Chainsight Platform. is_target_component should be set to false. If this flag is not set and is true, the component will call the component within the Chainsight Platform. This completes the update of the manifest. The completed manifest is as follows.

图片

For metadata, any arbitrary settings are fine, check the data source field and is_target_component again, and if you have successfully modified the code so far, let's generate the code now.

Generate code and build modules

Let’s generate the container code using csxgenerate , if you actually run this command you will get the following output.

图片

As with the snapshot indexer EVM, this interface in the snapshot indexer ICP requires no additional customization and the build process can begin without any additional customization.

You can use csx build to generate the module from your code, so let's run it.

图片

The build will complete in a few minutes. The command generates the WASM module and the candid interface. For a deeper understanding of these behaviors, please refer to the following articles:

  • medium.com/@Chainsight_Network/behind-chainsight-how-modules-are-created-pt-2-4cf29234b98b

Finally, deploy and start the component!

Deployment Components

Like the previous hands-on lessons, use csx deploy and csx exec to deploy this component on the Chainsight platform.

图片

When you run these you should get the following logs.

图片

The deployment and operation initiation instructions are now complete, and now you just need to wait for the automated execution interval to reference the data.

The components that can be created in this hands-on course are also actually running (we slightly changed the description and execution interval).

图片

source:

  • beta.chainsight.network/explore/components/lqf7x-4yaaa-aaaal-qdg7q-cai

This concludes our hands-on experience with the Snapshot Indexer EVM. We will continue to create and publish new hands-on courses in the future, so stay tuned!

Stay connected with us for more exciting updates and to explore how to build the future of web3 as we continue to shape a more modular and innovative financial future!

图片

Contact Channels

Official website:

chainsight.network

X:

@Chainsight_

Github:

github.com/horizonx-tech?q=chainsight&type=all

Medium:

medium.com/@Chainsight_Network

Docs:

docs.chainsight.network

图片

#Chainsight #EVM #Web3 #ICP🚀🚀 $ICP

IC content you care about

Technology Progress | Project Information | Global Activities

Collect and follow IC Binance Channel

Get the latest news