mirror of
https://github.com/cerc-io/watcher-ts
synced 2024-11-19 20:36:19 +00:00
Pass provider while instantiating the loader and use block hash while making the eth-calls (#74)
* Pass provider while instantiating the loader * Use custom provider for graph-node tests * Use block hash while making an eth-call
This commit is contained in:
parent
3d3ebb0e43
commit
3b835e81f8
@ -7,10 +7,12 @@ import chai, { assert, expect } from 'chai';
|
||||
import spies from 'chai-spies';
|
||||
import { utils } from 'ethers';
|
||||
|
||||
import { getDummyEventData, getDummyGraphData, getTestDatabase, getTestIndexer } from '../test/utils';
|
||||
import { BaseProvider } from '@ethersproject/providers';
|
||||
|
||||
import { getDummyEventData, getDummyGraphData, getTestDatabase, getTestIndexer, getTestProvider } from '../test/utils';
|
||||
import abi from '../test/subgraph/example1/build/Example1/abis/Example1.json';
|
||||
import { instantiate } from './loader';
|
||||
import { createEvent, createBlock, Block } from './utils';
|
||||
import { createEvent, createBlock, Block, EventData } from './utils';
|
||||
import { Database } from './database';
|
||||
import { Indexer } from '../test/utils/indexer';
|
||||
|
||||
@ -22,14 +24,19 @@ describe('call handler in mapping code', () => {
|
||||
let exports: any;
|
||||
let db: Database;
|
||||
let indexer: Indexer;
|
||||
let provider: BaseProvider;
|
||||
|
||||
// Create dummy test data.
|
||||
const dummyEventData = getDummyEventData();
|
||||
const dummyGraphData = getDummyGraphData();
|
||||
let dummyEventData: EventData;
|
||||
let dummyGraphData: any;
|
||||
|
||||
before(async () => {
|
||||
db = getTestDatabase();
|
||||
indexer = getTestIndexer();
|
||||
provider = getTestProvider();
|
||||
|
||||
// Create dummy test data.
|
||||
dummyEventData = await getDummyEventData();
|
||||
dummyGraphData = getDummyGraphData();
|
||||
|
||||
sandbox.on(indexer, 'createDiffStaged', (contractAddress: string, blockHash: string, data: any) => {
|
||||
assert(contractAddress);
|
||||
@ -63,6 +70,7 @@ describe('call handler in mapping code', () => {
|
||||
const instance = await instantiate(
|
||||
db,
|
||||
indexer,
|
||||
provider,
|
||||
{ event: { block: dummyEventData.block } },
|
||||
filePath,
|
||||
dummyGraphData
|
||||
|
@ -8,12 +8,14 @@ import path from 'path';
|
||||
import chai from 'chai';
|
||||
import spies from 'chai-spies';
|
||||
|
||||
import { BaseProvider } from '@ethersproject/providers';
|
||||
|
||||
import { instantiate } from './loader';
|
||||
import { createEvent, Block, createBlock } from './utils';
|
||||
import { createEvent, Block, createBlock, EventData } from './utils';
|
||||
import edenNetworkAbi from '../test/subgraph/eden/EdenNetwork/abis/EdenNetwork.json';
|
||||
import merkleDistributorAbi from '../test/subgraph/eden/EdenNetworkDistribution/abis/MerkleDistributor.json';
|
||||
import distributorGovernanceAbi from '../test/subgraph/eden/EdenNetworkGovernance/abis/DistributorGovernance.json';
|
||||
import { getDummyEventData, getTestDatabase, getTestIndexer } from '../test/utils';
|
||||
import { getDummyEventData, getTestDatabase, getTestIndexer, getTestProvider } from '../test/utils';
|
||||
import { Database } from './database';
|
||||
import { Indexer } from '../test/utils/indexer';
|
||||
|
||||
@ -26,13 +28,17 @@ const sandbox = chai.spy.sandbox();
|
||||
describe('eden wasm loader tests', async () => {
|
||||
let db: Database;
|
||||
let indexer: Indexer;
|
||||
let provider: BaseProvider;
|
||||
|
||||
// Create dummy event data.
|
||||
const dummyEventData = getDummyEventData();
|
||||
let dummyEventData: EventData;
|
||||
|
||||
before(async () => {
|
||||
db = getTestDatabase();
|
||||
indexer = getTestIndexer();
|
||||
provider = getTestProvider();
|
||||
|
||||
// Create dummy test data.
|
||||
dummyEventData = await getDummyEventData();
|
||||
|
||||
sandbox.on(indexer, 'createDiffStaged', (contractAddress: string, blockHash: string, data: any) => {
|
||||
assert(contractAddress);
|
||||
@ -81,6 +87,7 @@ describe('eden wasm loader tests', async () => {
|
||||
({ exports } = await instantiate(
|
||||
db,
|
||||
indexer,
|
||||
provider,
|
||||
{ event: { block: dummyEventData.block } },
|
||||
filePath,
|
||||
data
|
||||
@ -195,6 +202,7 @@ describe('eden wasm loader tests', async () => {
|
||||
const filePath = path.resolve(__dirname, '../test/subgraph/eden/EdenNetworkDistribution/EdenNetworkDistribution.wasm');
|
||||
({ exports } = await instantiate(db,
|
||||
indexer,
|
||||
provider,
|
||||
{ event: { block: dummyEventData.block } },
|
||||
filePath,
|
||||
data
|
||||
@ -307,6 +315,7 @@ describe('eden wasm loader tests', async () => {
|
||||
({ exports } = await instantiate(
|
||||
db,
|
||||
indexer,
|
||||
provider,
|
||||
{ event: { block: dummyEventData.block } },
|
||||
filePath,
|
||||
data
|
||||
|
@ -5,16 +5,20 @@
|
||||
import assert from 'assert';
|
||||
import path from 'path';
|
||||
|
||||
import { BaseProvider } from '@ethersproject/providers';
|
||||
|
||||
import { instantiate } from './loader';
|
||||
import exampleAbi from '../test/subgraph/example1/build/Example1/abis/Example1.json';
|
||||
import { getTestDatabase, getTestIndexer } from '../test/utils';
|
||||
import { getTestDatabase, getTestIndexer, getTestProvider, getDummyEventData } from '../test/utils';
|
||||
import { Database } from './database';
|
||||
import { Indexer } from '../test/utils/indexer';
|
||||
import { EventData } from './utils';
|
||||
|
||||
describe('eth-call wasm tests', () => {
|
||||
let exports: any;
|
||||
let db: Database;
|
||||
let indexer: Indexer;
|
||||
let provider: BaseProvider;
|
||||
|
||||
const contractAddress = process.env.EXAMPLE_CONTRACT_ADDRESS;
|
||||
assert(contractAddress);
|
||||
@ -28,9 +32,15 @@ describe('eth-call wasm tests', () => {
|
||||
}
|
||||
};
|
||||
|
||||
let dummyEventData: EventData;
|
||||
|
||||
before(async () => {
|
||||
db = getTestDatabase();
|
||||
indexer = getTestIndexer();
|
||||
provider = getTestProvider();
|
||||
|
||||
// Create dummy test data.
|
||||
dummyEventData = await getDummyEventData();
|
||||
});
|
||||
|
||||
it('should load the subgraph example wasm', async () => {
|
||||
@ -38,7 +48,8 @@ describe('eth-call wasm tests', () => {
|
||||
const instance = await instantiate(
|
||||
db,
|
||||
indexer,
|
||||
{ event: {} },
|
||||
provider,
|
||||
{ event: { block: dummyEventData.block } },
|
||||
filePath,
|
||||
data
|
||||
);
|
||||
|
@ -5,8 +5,10 @@
|
||||
import path from 'path';
|
||||
import { expect } from 'chai';
|
||||
|
||||
import { BaseProvider } from '@ethersproject/providers';
|
||||
|
||||
import { instantiate } from './loader';
|
||||
import { getTestDatabase, getTestIndexer } from '../test/utils';
|
||||
import { getTestDatabase, getTestIndexer, getTestProvider } from '../test/utils';
|
||||
import { Database } from './database';
|
||||
import { Indexer } from '../test/utils/indexer';
|
||||
|
||||
@ -16,15 +18,18 @@ describe('wasm loader tests', () => {
|
||||
let exports: any;
|
||||
let db: Database;
|
||||
let indexer: Indexer;
|
||||
let provider: BaseProvider;
|
||||
|
||||
before(async () => {
|
||||
db = getTestDatabase();
|
||||
indexer = getTestIndexer();
|
||||
provider = getTestProvider();
|
||||
|
||||
const filePath = path.resolve(__dirname, WASM_FILE_PATH);
|
||||
const instance = await instantiate(
|
||||
db,
|
||||
indexer,
|
||||
provider,
|
||||
{ event: {} },
|
||||
filePath
|
||||
);
|
||||
|
@ -7,7 +7,6 @@ import fs from 'fs/promises';
|
||||
import {
|
||||
utils,
|
||||
BigNumber,
|
||||
getDefaultProvider,
|
||||
Contract,
|
||||
ContractInterface
|
||||
} from 'ethers';
|
||||
@ -15,6 +14,7 @@ import JSONbig from 'json-bigint';
|
||||
import BN from 'bn.js';
|
||||
import debug from 'debug';
|
||||
|
||||
import { BaseProvider } from '@ethersproject/providers';
|
||||
import loader from '@vulcanize/assemblyscript/lib/loader';
|
||||
import { IndexerInterface, GraphDecimal, getGraphDigitsAndExp } from '@vulcanize/util';
|
||||
|
||||
@ -27,8 +27,6 @@ import {
|
||||
} from './utils';
|
||||
import { Database } from './database';
|
||||
|
||||
const NETWORK_URL = 'http://127.0.0.1:8081';
|
||||
|
||||
// Endianness of BN used in bigInt store host API.
|
||||
// Negative bigInt is being stored in wasm in 2's compliment, 'le' representation.
|
||||
// (for eg. bigInt.fromString(negativeI32Value))
|
||||
@ -56,13 +54,13 @@ const log = debug('vulcanize:graph-node');
|
||||
export const instantiate = async (
|
||||
database: Database,
|
||||
indexer: IndexerInterface,
|
||||
provider: BaseProvider,
|
||||
context: Context,
|
||||
filePath: string,
|
||||
data: GraphData = {}
|
||||
): Promise<loader.ResultObject & { exports: any }> => {
|
||||
const { abis = {}, dataSource } = data;
|
||||
const buffer = await fs.readFile(filePath);
|
||||
const provider = getDefaultProvider(NETWORK_URL);
|
||||
|
||||
const imports: WebAssembly.Imports = {
|
||||
index: {
|
||||
@ -160,8 +158,10 @@ export const instantiate = async (
|
||||
|
||||
functionParams = await Promise.all(functionParamsPromise);
|
||||
|
||||
assert(context.event.block);
|
||||
|
||||
// TODO: Check for function overloading.
|
||||
let result = await contract[functionName](...functionParams);
|
||||
let result = await contract[functionName](...functionParams, { blockTag: context.event.block.blockHash });
|
||||
|
||||
// Using function signature does not work.
|
||||
const { outputs } = contract.interface.getFunction(functionName);
|
||||
|
@ -7,9 +7,10 @@ import { expect } from 'chai';
|
||||
import BN from 'bn.js';
|
||||
|
||||
import { GraphDecimal } from '@vulcanize/util';
|
||||
import { BaseProvider } from '@ethersproject/providers';
|
||||
|
||||
import { instantiate } from './loader';
|
||||
import { getTestDatabase, getTestIndexer } from '../test/utils';
|
||||
import { getTestDatabase, getTestIndexer, getTestProvider } from '../test/utils';
|
||||
import { Database } from './database';
|
||||
import { Indexer } from '../test/utils/indexer';
|
||||
import {
|
||||
@ -29,15 +30,18 @@ describe('numbers wasm tests', () => {
|
||||
let exports: any;
|
||||
let db: Database;
|
||||
let indexer: Indexer;
|
||||
let provider: BaseProvider;
|
||||
|
||||
before(async () => {
|
||||
db = getTestDatabase();
|
||||
indexer = getTestIndexer();
|
||||
provider = getTestProvider();
|
||||
|
||||
const filePath = path.resolve(__dirname, EXAMPLE_WASM_FILE_PATH);
|
||||
const instance = await instantiate(
|
||||
db,
|
||||
indexer,
|
||||
provider,
|
||||
{ event: {} },
|
||||
filePath
|
||||
);
|
||||
|
@ -6,8 +6,10 @@ import path from 'path';
|
||||
import { expect } from 'chai';
|
||||
import { utils, BigNumber } from 'ethers';
|
||||
|
||||
import { BaseProvider } from '@ethersproject/providers';
|
||||
|
||||
import { instantiate } from './loader';
|
||||
import { getTestDatabase, getTestIndexer } from '../test/utils';
|
||||
import { getTestDatabase, getTestIndexer, getTestProvider } from '../test/utils';
|
||||
import { Database } from './database';
|
||||
import { Indexer } from '../test/utils/indexer';
|
||||
|
||||
@ -17,16 +19,19 @@ describe('typeConversion wasm tests', () => {
|
||||
let exports: any;
|
||||
let db: Database;
|
||||
let indexer: Indexer;
|
||||
let provider: BaseProvider;
|
||||
|
||||
before(async () => {
|
||||
db = getTestDatabase();
|
||||
indexer = getTestIndexer();
|
||||
provider = getTestProvider();
|
||||
|
||||
const filePath = path.resolve(__dirname, EXAMPLE_WASM_FILE_PATH);
|
||||
const instance = await instantiate(
|
||||
db,
|
||||
indexer,
|
||||
{ event: { } },
|
||||
provider,
|
||||
{ event: {} },
|
||||
filePath
|
||||
);
|
||||
exports = instance.exports;
|
||||
|
@ -75,7 +75,7 @@ export class GraphWatcher {
|
||||
assert(this._indexer);
|
||||
|
||||
return {
|
||||
instance: await instantiate(this._database, this._indexer, this._context, filePath, data),
|
||||
instance: await instantiate(this._database, this._indexer, this._ethProvider, this._context, filePath, data),
|
||||
contractInterface
|
||||
};
|
||||
}, {});
|
||||
|
@ -2,17 +2,27 @@
|
||||
// Copyright 2021 Vulcanize, Inc.
|
||||
//
|
||||
|
||||
import { BaseProvider } from '@ethersproject/providers';
|
||||
import { getCustomProvider } from '@vulcanize/util';
|
||||
|
||||
import { EventData } from '../../src/utils';
|
||||
import { Database } from '../../src/database';
|
||||
import { Indexer } from './indexer';
|
||||
|
||||
const NETWORK_URL = 'http://127.0.0.1:8081';
|
||||
|
||||
export const ZERO_ADDRESS = '0x0000000000000000000000000000000000000000';
|
||||
export const ZERO_HASH = '0x0000000000000000000000000000000000000000000000000000000000000000';
|
||||
|
||||
export const getDummyEventData = (): EventData => {
|
||||
export const getDummyEventData = async (): Promise<EventData> => {
|
||||
// Get the latest mined block from the chain.
|
||||
const provider = getCustomProvider(NETWORK_URL);
|
||||
const blockNumber = await provider.getBlockNumber();
|
||||
const ethersBlock = await provider.getBlock(blockNumber);
|
||||
|
||||
const block = {
|
||||
blockHash: ZERO_HASH,
|
||||
blockNumber: '0',
|
||||
blockHash: ethersBlock.hash,
|
||||
blockNumber: ethersBlock.number.toString(),
|
||||
timestamp: '0',
|
||||
parentHash: ZERO_HASH,
|
||||
stateRoot: ZERO_HASH,
|
||||
@ -58,3 +68,9 @@ export const getTestDatabase = (): Database => {
|
||||
export const getTestIndexer = (): Indexer => {
|
||||
return new Indexer();
|
||||
};
|
||||
|
||||
export const getTestProvider = (): BaseProvider => {
|
||||
const provider = getCustomProvider(NETWORK_URL);
|
||||
|
||||
return provider;
|
||||
};
|
||||
|
Loading…
Reference in New Issue
Block a user