2021-05-28 11:26:40 +00:00
|
|
|
import assert from 'assert';
|
2021-07-13 12:02:57 +00:00
|
|
|
import _ from 'lodash';
|
2021-07-06 11:25:11 +00:00
|
|
|
|
2021-05-28 11:26:40 +00:00
|
|
|
import { Cache } from '@vulcanize/cache';
|
|
|
|
|
|
|
|
import ethQueries from './eth-queries';
|
2021-06-01 12:43:41 +00:00
|
|
|
import { padKey } from './utils';
|
2021-07-07 05:40:10 +00:00
|
|
|
import { GraphQLClient, GraphQLConfig } from './graphql-client';
|
2021-06-08 10:37:52 +00:00
|
|
|
|
2021-07-07 05:40:10 +00:00
|
|
|
interface Config extends GraphQLConfig {
|
2021-06-08 10:37:52 +00:00
|
|
|
cache: Cache | undefined;
|
2021-06-04 06:34:12 +00:00
|
|
|
}
|
2021-05-28 11:26:40 +00:00
|
|
|
|
2021-06-04 06:34:12 +00:00
|
|
|
interface Vars {
|
|
|
|
blockHash: string;
|
2021-07-12 11:36:33 +00:00
|
|
|
contract?: string;
|
2021-06-04 06:34:12 +00:00
|
|
|
slot?: string;
|
|
|
|
}
|
|
|
|
|
|
|
|
export class EthClient {
|
|
|
|
_config: Config;
|
2021-07-06 11:25:11 +00:00
|
|
|
_graphqlClient: GraphQLClient;
|
2021-06-08 10:37:52 +00:00
|
|
|
_cache: Cache | undefined;
|
2021-05-28 11:26:40 +00:00
|
|
|
|
2021-06-04 06:34:12 +00:00
|
|
|
constructor (config: Config) {
|
2021-05-28 11:26:40 +00:00
|
|
|
this._config = config;
|
|
|
|
|
2021-06-08 10:37:52 +00:00
|
|
|
const { gqlEndpoint, gqlSubscriptionEndpoint, cache } = config;
|
|
|
|
|
2021-05-28 11:26:40 +00:00
|
|
|
assert(gqlEndpoint, 'Missing gql endpoint');
|
2021-06-08 10:37:52 +00:00
|
|
|
assert(gqlSubscriptionEndpoint, 'Missing gql subscription endpoint');
|
|
|
|
|
2021-07-06 11:25:11 +00:00
|
|
|
this._graphqlClient = new GraphQLClient({ gqlEndpoint, gqlSubscriptionEndpoint });
|
2021-05-28 11:26:40 +00:00
|
|
|
|
|
|
|
this._cache = cache;
|
|
|
|
}
|
|
|
|
|
2021-06-04 06:34:12 +00:00
|
|
|
async getStorageAt ({ blockHash, contract, slot }: { blockHash: string, contract: string, slot: string }): Promise<{ value: string, proof: { data: string } }> {
|
2021-06-01 12:43:41 +00:00
|
|
|
slot = `0x${padKey(slot)}`;
|
|
|
|
|
|
|
|
const result = await this._getCachedOrFetch('getStorageAt', { blockHash, contract, slot });
|
2021-05-31 08:36:48 +00:00
|
|
|
const { getStorageAt: { value, cid, ipldBlock } } = result;
|
|
|
|
|
2021-06-01 12:43:41 +00:00
|
|
|
return {
|
|
|
|
value,
|
|
|
|
proof: {
|
|
|
|
// TODO: Return proof only if requested.
|
|
|
|
data: JSON.stringify({
|
|
|
|
blockHash,
|
|
|
|
account: {
|
|
|
|
address: contract,
|
|
|
|
storage: {
|
|
|
|
cid,
|
|
|
|
ipldBlock
|
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
};
|
2021-05-28 11:26:40 +00:00
|
|
|
}
|
|
|
|
|
2021-06-25 11:05:47 +00:00
|
|
|
async getBlockWithTransactions (blockNumber: string): Promise<any> {
|
2021-07-06 11:25:11 +00:00
|
|
|
return this._graphqlClient.query(ethQueries.getBlockWithTransactions, { blockNumber });
|
2021-06-25 11:05:47 +00:00
|
|
|
}
|
|
|
|
|
2021-06-04 06:34:12 +00:00
|
|
|
async getLogs (vars: Vars): Promise<any> {
|
2021-05-31 08:36:48 +00:00
|
|
|
const result = await this._getCachedOrFetch('getLogs', vars);
|
2021-07-21 07:30:26 +00:00
|
|
|
const {
|
|
|
|
getLogs: resultLogs,
|
|
|
|
block: {
|
|
|
|
number: blockNumHex,
|
|
|
|
timestamp: timestampHex,
|
|
|
|
parent: {
|
|
|
|
hash: parentHash
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} = result;
|
|
|
|
|
|
|
|
const block = {
|
|
|
|
hash: vars.blockHash,
|
|
|
|
number: parseInt(blockNumHex, 16),
|
|
|
|
timestamp: parseInt(timestampHex, 16),
|
|
|
|
parent: {
|
|
|
|
hash: parentHash
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2021-07-15 07:40:07 +00:00
|
|
|
const logs = resultLogs.map((logEntry: any) => _.merge({}, logEntry, { transaction: { block } }));
|
2021-07-14 12:30:26 +00:00
|
|
|
|
|
|
|
return { logs, block };
|
|
|
|
}
|
|
|
|
|
|
|
|
async watchBlocks (onNext: (value: any) => void): Promise<ZenObservable.Subscription> {
|
|
|
|
return this._graphqlClient.subscribe(ethQueries.subscribeBlocks, onNext);
|
2021-05-28 11:26:40 +00:00
|
|
|
}
|
|
|
|
|
2021-06-08 10:37:52 +00:00
|
|
|
async watchLogs (onNext: (value: any) => void): Promise<ZenObservable.Subscription> {
|
2021-07-06 11:25:11 +00:00
|
|
|
return this._graphqlClient.subscribe(ethQueries.subscribeLogs, onNext);
|
2021-06-08 10:37:52 +00:00
|
|
|
}
|
|
|
|
|
2021-06-21 13:25:13 +00:00
|
|
|
async watchTransactions (onNext: (value: any) => void): Promise<ZenObservable.Subscription> {
|
2021-07-06 11:25:11 +00:00
|
|
|
return this._graphqlClient.subscribe(ethQueries.subscribeTransactions, onNext);
|
2021-06-21 13:25:13 +00:00
|
|
|
}
|
|
|
|
|
2021-06-04 06:34:12 +00:00
|
|
|
async _getCachedOrFetch (queryName: keyof typeof ethQueries, vars: Vars): Promise<any> {
|
2021-05-28 11:26:40 +00:00
|
|
|
const keyObj = {
|
|
|
|
queryName,
|
|
|
|
vars
|
|
|
|
};
|
|
|
|
|
|
|
|
// Check if request cached in db, if cache is enabled.
|
|
|
|
if (this._cache) {
|
2021-06-04 06:34:12 +00:00
|
|
|
const [value, found] = await this._cache.get(keyObj) || [undefined, false];
|
2021-05-28 11:26:40 +00:00
|
|
|
if (found) {
|
|
|
|
return value;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-06-08 10:37:52 +00:00
|
|
|
// Result not cached or cache disabled, need to perform an upstream GQL query.
|
2021-07-06 11:25:11 +00:00
|
|
|
const result = await this._graphqlClient.query(ethQueries[queryName], vars);
|
2021-05-28 11:26:40 +00:00
|
|
|
|
|
|
|
// Cache the result and return it, if cache is enabled.
|
|
|
|
if (this._cache) {
|
|
|
|
await this._cache.put(keyObj, result);
|
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
}
|