GQL mutation to start watching contract.

This commit is contained in:
Ashwin Phatak 2021-06-09 15:38:07 +05:30
parent 7035a513b1
commit c916e61a9b
3 changed files with 27 additions and 1 deletions

View File

@ -268,7 +268,14 @@ export class Indexer {
async isWatchedContract (address : string): Promise<boolean> { async isWatchedContract (address : string): Promise<boolean> {
assert(address); assert(address);
return this._db.isWatchedContract(address); return this._db.isWatchedContract(ethers.utils.getAddress(address));
}
async watchContract (address: string, startingBlock: number): Promise<boolean> {
// Always use the checksum address (https://docs.ethers.io/v5/api/utils/address/#utils-getAddress).
await this._db.saveContract(ethers.utils.getAddress(address), startingBlock);
return true;
} }
// TODO: Move into base/class or framework package. // TODO: Move into base/class or framework package.

View File

@ -26,6 +26,13 @@ export const createResolvers = async (indexer: Indexer): Promise<any> => {
} }
}, },
Mutation: {
watchToken: (_: any, { token, startingBlock = 1 }: { token: string, startingBlock: number }): Promise<boolean> => {
log('watchToken', token, startingBlock);
return indexer.watchContract(token, startingBlock);
}
},
Query: { Query: {
totalSupply: (_: any, { blockHash, token }: { blockHash: string, token: string }): Promise<ValueResult> => { totalSupply: (_: any, { blockHash, token }: { blockHash: string, token: string }): Promise<ValueResult> => {

View File

@ -139,4 +139,16 @@ type Subscription {
# Watch for token events (at head of chain). # Watch for token events (at head of chain).
onTokenEvent: WatchedEvent! onTokenEvent: WatchedEvent!
} }
#
# Mutations
#
type Mutation {
# Actively watch and index data for the token.
watchToken(
token: String!
startingBlock: Int
): Boolean!
}
`; `;