Query using Subgraph
This section will guide you through querying contract state data from the blockchain using a deployed Subgraph.
Contracts:
Mock
KIPTokenContract: 0x4b565A132347064Ca7F1eDC9489a00b3E68431ddRetrieve the current balance of an
accountRetrieve the
allowanceamount that anaccounthas granted to aspender
Prerequisites:
Deployed Subgraph on Alchemy. If not, please follow the instructions [here].
NodeJS: version
v20.14.0orv20.15.0.Package Manager:
yarnornpm.
Query Example
Folder Structure:
query-subgraph
├── package.json
├── src
│ ├── main.ts
│ └── query.ts
└── tsconfig.jsonLet's get started:
Create a project folder:
Create
package.jsonand install dependencies:
Create
package.json
Add the following content to
package.json:
Install dependencies:
Create
tsconfig.json:
Create the file:
Add the following content to
tsconfig.json:
Create
.envfile:
Create the file:
Add the following content to
.env:
Create a script to query from the Alchemy Subgraph:
Create a script file:
Add the following code to the
query.tsscript:
Create
main.tsto test the setup:
Create the file:
Add the code into
main.ts:
Run the test:
After running, you should see output similar to the following:
Last updated