Fetch Data Example

This section will guide you through fetching contract state data from the blockchain using a Node RPC provider.

Contracts:

Prerequisites:

  • RPC Node Provider: Alchemy, Infura, or others.

  • NodeJS: version v20.14.0 or v20.15.0.

  • Package Manager: yarn or npm.

Fetch Data Example

Folder Structure:

fetch-data-example
├── package.json
├── src
   ├── abis
      ├── KIPIdentification.json
      └── MockKIPToken.json
   ├── fetchKIPIdentification.ts
   ├── fetchKIPToken.ts
   └── main.ts
└── tsconfig.json

Let's get started:

  1. Create a project folder:

  1. Create package.json and install dependencies:

  • Create package.json

  • Add the following content to package.json:

  • Install dependencies:

  1. Create tsconfig.json:

  • Create the file:

  • Add the following content to tsconfig.json:

  1. Create .env file:

  • Create the file:

  • Add the following content to .env:

  1. Create src and abis folders:

  1. Create ABI files for the smart contracts:

  • KIPIdentification Contract ABI:

  • Add the ABI content to KIPIdentification.json:

  • Mock KIPToken Contract ABI:

  • Add the ABI content to MockKIPToken.json:

  1. Create scripts to interact with the contracts:

  • Fetch data from KIPIdentification contract:

  • Add the code to interact with the KIPIdentification contract into fetchKIPIdentification.ts:

  • Fetch data from the mock KIPToken contract:

  • Add the code to interact with the mock KIPToken contract into fetchKIPToken.ts:

  1. Create main.ts to test the setup:

  • Create the file:

  • Add the code into main.ts:

  1. Run the test:

  • After running, you should see output similar to the following:

Last updated