In this section, we will guide you through deploying a Subgraph using the Alchemy Subgraph Service. Ensure you have your DEPLOY_KEY ready, as it will be used later for deployment.
For more information, refer to the documentation provided by The Graph and Alchemy
Before deploying the Subgraph, we need to modify schema.graphql to query the balance of an account.
Open kip-token-example/schema.graphql.
Append the following code at the end of the file:
typeAccount @entity { id: ID! balance: BigInt!}
Run the compile command to update ./generated/schema.ts file:
cdkip-token-example&&graphcodegen
Modify ./src/token-20.ts as follows:
import { BigInt } from"@graphprotocol/graph-ts";import { Approval as ApprovalEvent, Transfer as TransferEvent,} from"../generated/Token20/Token20";import { Approval, Transfer, Account } from"../generated/schema";exportfunctionhandleApproval(event:ApprovalEvent):void {let entity =newApproval(event.transaction.hash.concatI32(event.logIndex.toI32()) );entity.owner =event.params.owner;entity.spender =event.params.spender;entity.value =event.params.value;entity.blockNumber =event.block.number;entity.blockTimestamp =event.block.timestamp;entity.transactionHash =event.transaction.hash;entity.save();}exportfunctionhandleTransfer(event:TransferEvent):void {let entity =newTransfer(event.transaction.hash.concatI32(event.logIndex.toI32()) );entity.from =event.params.from;entity.to =event.params.to;entity.value =event.params.value;entity.blockNumber =event.block.number;entity.blockTimestamp =event.block.timestamp;entity.transactionHash =event.transaction.hash;entity.save();// Load or create the 'from' accountlet fromAccount =Account.load(event.params.from.toHex());if (fromAccount ==null) { fromAccount =newAccount(event.params.from.toHex());fromAccount.balance =BigInt.fromI32(0); }// Load or create the 'to' accountlet toAccount =Account.load(event.params.to.toHex());if (toAccount ==null) { toAccount =newAccount(event.params.to.toHex());toAccount.balance =BigInt.fromI32(0); }// Update balancesfromAccount.balance =fromAccount.balance.minus(event.params.value);toAccount.balance =toAccount.balance.plus(event.params.value);// Save the updated accountsfromAccount.save();toAccount.save();}
Deploy the Subgraph using the Alchemy Subgraph Service:
# Run a following command to deploy# Ensure "DEPLOY_KEY" is providedgraphdeploykip-token-example \--version-labelv0.0.1-new-version \--nodehttps://subgraphs.alchemy.com/api/subgraphs/deploy \--deploy-key<YOUR_DEPLOY_KEY> \--ipfshttps://ipfs.satsuma.xyz
Once successful, you will see output similar to the following: