mirror of
https://github.com/cerc-io/watcher-ts
synced 2025-07-27 02:32:07 +00:00
Remove default ERC20 code from generated hooks (#93)
This commit is contained in:
parent
44e18fee48
commit
feac6bad16
@ -4,15 +4,8 @@
|
|||||||
|
|
||||||
import assert from 'assert';
|
import assert from 'assert';
|
||||||
|
|
||||||
import { UNKNOWN_EVENT_NAME, updateStateForMappingType, updateStateForElementaryType } from '@vulcanize/util';
|
|
||||||
|
|
||||||
import { Indexer, ResultEvent } from './indexer';
|
import { Indexer, ResultEvent } from './indexer';
|
||||||
|
|
||||||
const ACCOUNTS = [
|
|
||||||
'0xDC7d7A8920C8Eecc098da5B7522a5F31509b5Bfc',
|
|
||||||
'0xCA6D29232D1435D8198E3E5302495417dD073d61'
|
|
||||||
];
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Hook function to store an initial state.
|
* Hook function to store an initial state.
|
||||||
* @param indexer Indexer instance.
|
* @param indexer Indexer instance.
|
||||||
@ -20,19 +13,17 @@ const ACCOUNTS = [
|
|||||||
* @param contractAddress Address of the concerned contract.
|
* @param contractAddress Address of the concerned contract.
|
||||||
* @returns Data block to be stored.
|
* @returns Data block to be stored.
|
||||||
*/
|
*/
|
||||||
export async function createInitialState (indexer: Indexer, contractAddress: string, blockHash: string): Promise<any> { assert(indexer);
|
export async function createInitialState (indexer: Indexer, contractAddress: string, blockHash: string): Promise<any> {
|
||||||
assert(indexer);
|
assert(indexer);
|
||||||
assert(blockHash);
|
assert(blockHash);
|
||||||
assert(contractAddress);
|
assert(contractAddress);
|
||||||
|
|
||||||
// Store the initial state values in an IPLDBlock.
|
// Store an empty state in an IPLDBlock.
|
||||||
let ipldBlockData: any = {};
|
const ipldBlockData: any = {
|
||||||
|
state: {}
|
||||||
|
};
|
||||||
|
|
||||||
// Setting the initial balances of accounts.
|
// Use updateStateForMappingType to update initial state.
|
||||||
for (const account of ACCOUNTS) {
|
|
||||||
const balance = await indexer._balances(blockHash, contractAddress, account);
|
|
||||||
ipldBlockData = updateStateForMappingType(ipldBlockData, '_balances', [account], balance.value.toString());
|
|
||||||
}
|
|
||||||
|
|
||||||
// Return initial state data to be saved.
|
// Return initial state data to be saved.
|
||||||
return ipldBlockData;
|
return ipldBlockData;
|
||||||
@ -47,64 +38,7 @@ export async function createStateDiff (indexer: Indexer, blockHash: string): Pro
|
|||||||
assert(indexer);
|
assert(indexer);
|
||||||
assert(blockHash);
|
assert(blockHash);
|
||||||
|
|
||||||
// Get events for current block and make an entry of updated values in IPLDBlock.
|
// Use indexer.createStateDiff() method to create a custom diff.
|
||||||
const events = await indexer.getEventsByFilter(blockHash);
|
|
||||||
|
|
||||||
// No IPLDBlock entry if there are no events.
|
|
||||||
if (!events) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
for (const event of events) {
|
|
||||||
if (event.eventName === UNKNOWN_EVENT_NAME) {
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
|
|
||||||
const contractAddress = event.contract;
|
|
||||||
|
|
||||||
const eventData = indexer.getResultEvent(event);
|
|
||||||
|
|
||||||
let ipldBlockData: any = {};
|
|
||||||
|
|
||||||
switch (event.eventName) {
|
|
||||||
case 'Transfer': {
|
|
||||||
const { from, to } = eventData.event;
|
|
||||||
|
|
||||||
const fromBalance = await indexer._balances(blockHash, contractAddress, from);
|
|
||||||
const toBalance = await indexer._balances(blockHash, contractAddress, to);
|
|
||||||
|
|
||||||
// {
|
|
||||||
// "_balances": {
|
|
||||||
// "0xCA6D29232D1435D8198E3E5302495417dD073d61": "100",
|
|
||||||
// "0xDC7d7A8920C8Eecc098da5B7522a5F31509b5Bfc": "999999999999999999900"
|
|
||||||
// }
|
|
||||||
// }
|
|
||||||
ipldBlockData = updateStateForMappingType(ipldBlockData, '_balances', [from], fromBalance.value.toString());
|
|
||||||
ipldBlockData = updateStateForMappingType(ipldBlockData, '_balances', [to], toBalance.value.toString());
|
|
||||||
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
|
|
||||||
case 'Approval': {
|
|
||||||
const { owner, spender } = eventData.event;
|
|
||||||
const allowance = await indexer._allowances(blockHash, contractAddress, owner, spender);
|
|
||||||
|
|
||||||
// {
|
|
||||||
// "_allowances": {
|
|
||||||
// "0xDC7d7A8920C8Eecc098da5B7522a5F31509b5Bfc": {
|
|
||||||
// "0xCA6D29232D1435D8198E3E5302495417dD073d61": "10"
|
|
||||||
// }
|
|
||||||
// }
|
|
||||||
// }
|
|
||||||
ipldBlockData = updateStateForMappingType(ipldBlockData, '_allowances', [owner, spender], allowance.value.toString());
|
|
||||||
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Use indexer.createStateDiff() method to create a custom state.
|
|
||||||
await indexer.createDiff(contractAddress, blockHash, ipldBlockData);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -119,16 +53,7 @@ export async function createStateCheckpoint (indexer: Indexer, contractAddress:
|
|||||||
assert(blockHash);
|
assert(blockHash);
|
||||||
assert(contractAddress);
|
assert(contractAddress);
|
||||||
|
|
||||||
let ipldBlockData: any = {};
|
|
||||||
|
|
||||||
// Setting the balances of accounts.
|
|
||||||
for (const account of ACCOUNTS) {
|
|
||||||
const balance = await indexer._balances(blockHash, contractAddress, account);
|
|
||||||
ipldBlockData = updateStateForMappingType(ipldBlockData, '_balances', [account], balance.value.toString());
|
|
||||||
}
|
|
||||||
|
|
||||||
// Use indexer.createStateCheckpoint() method to create a custom checkpoint.
|
// Use indexer.createStateCheckpoint() method to create a custom checkpoint.
|
||||||
await indexer.createCheckpoint(contractAddress, blockHash, ipldBlockData);
|
|
||||||
|
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
@ -141,38 +66,4 @@ export async function createStateCheckpoint (indexer: Indexer, contractAddress:
|
|||||||
export async function handleEvent (indexer: Indexer, eventData: ResultEvent): Promise<void> {
|
export async function handleEvent (indexer: Indexer, eventData: ResultEvent): Promise<void> {
|
||||||
assert(indexer);
|
assert(indexer);
|
||||||
assert(eventData);
|
assert(eventData);
|
||||||
|
|
||||||
// The following code is for ERC20 contract implementation.
|
|
||||||
|
|
||||||
// Perform indexing based on the type of event.
|
|
||||||
switch (eventData.event.__typename) {
|
|
||||||
// In case of ERC20 'Transfer' event.
|
|
||||||
case 'TransferEvent': {
|
|
||||||
// On a transfer, balances for both parties change.
|
|
||||||
// Therefore, trigger indexing for both sender and receiver.
|
|
||||||
|
|
||||||
// Get event fields from eventData.
|
|
||||||
const { from, to } = eventData.event;
|
|
||||||
|
|
||||||
// Update balance entry for sender in the database.
|
|
||||||
await indexer._balances(eventData.block.hash, eventData.contract, from);
|
|
||||||
|
|
||||||
// Update balance entry for receiver in the database.
|
|
||||||
await indexer._balances(eventData.block.hash, eventData.contract, to);
|
|
||||||
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
// In case of ERC20 'Approval' event.
|
|
||||||
case 'ApprovalEvent': {
|
|
||||||
// On an approval, allowance for (owner, spender) combination changes.
|
|
||||||
|
|
||||||
// Get event fields from eventData.
|
|
||||||
const { owner, spender } = eventData.event;
|
|
||||||
|
|
||||||
// Update allowance entry for (owner, spender) combination in the database.
|
|
||||||
await indexer._allowances(eventData.block.hash, eventData.contract, owner, spender);
|
|
||||||
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user