Node.js
In this tutorial, you will learn how to use the Moxie Protocol Subgraph API to fetch data from the Moxie protocol in Node.js.
Here, you will be shown an example to fetch Moxie protocol summary data, but you can use other APIs to fetch other data available on the Moxie Protocol Subgraph API.
For more details on the Moxie Protocol Subgraph API, check out the Subgraph Studio here.
Pre-requisites
Before following the tutorial, make sure that your machine fulfills the following requirements:
Step 1: Setup Node.js project
First, create a new Node.js project by running the following command:
npm init -y
Then, install the graphql
and graphql-request
packages into your project:
- npm
- yarn
- pnpm
- bun
npm install graphql graphql-request undefined
yarn add graphql graphql-request undefined
pnpm add graphql graphql-request undefined
bun install graphql graphql graphql-request undefined
Step 2: Add Query Call To The Code
You can then create a new file and import graphql-request
to make a query call to the Moxie Protocol Subgraph API:
- TypeScript
- JavaScript
import { gql, GraphQLClient } from "graphql-request";
const graphQLClient = new GraphQLClient(
"https://api.studio.thegraph.com/query/23537/moxie_protocol_stats_mainnet/version/latest"
);
const query = gql`
query MyQuery {
summaries {
activeProtocolFeeBeneficiary {
beneficiary
totalFees
}
protocolBuyFeePct
protocolSellFeePct
subjectBuyFeePct
subjectSellFeePct
totalReserve
totalBuyVolume
totalSellVolume
totalProtocolFee
}
}
`;
try {
const data = await graphQLClient.request(query);
console.log(data);
} catch (e) {
throw new Error(e);
}
const { gql, GraphQLClient } = require("graphql-request");
const graphQLClient = new GraphQLClient(
"https://api.studio.thegraph.com/query/23537/moxie_protocol_stats_mainnet/version/latest"
);
const query = gql`
query MyQuery {
summaries {
activeProtocolFeeBeneficiary {
beneficiary
totalFees
}
protocolBuyFeePct
protocolSellFeePct
subjectBuyFeePct
subjectSellFeePct
totalReserve
totalBuyVolume
totalSellVolume
totalProtocolFee
}
}
`;
try {
const data = await graphQLClient.request(query);
console.log(data);
} catch (e) {
throw new Error(e);
}
Step 3: Execute The Code
Once you have your code ready, you can execute it by running the following command:
- TypeScript
- JavaScript
ts-node index.ts
node index.js
If it runs successfully, you should see the data returned in the terminal:
{
"data": {
"summaries": [
{
"activeProtocolFeeBeneficiary": {
"beneficiary": "0x648e7e89a70ef654b5754e9eb91bf931a477e3cd",
"totalFees": "263686938800000000000000"
},
"protocolBuyFeePct": "10000000000000000",
"protocolSellFeePct": "20000000000000000",
"subjectBuyFeePct": "30000000000000000",
"subjectSellFeePct": "40000000000000000",
"totalReserve": "26696218719309458229047159",
"totalBuyVolume": "248812605935485122316883",
"totalSellVolume": "0",
"totalProtocolFee": "0"
}
]
}
}
Developer Support
If you have any questions or need help with other use cases, feel free to join the Moxie Official Warpcast channel and ask your questions there.
Our team is always ready to help you with any questions you may have.