2021-05-28 11:26:40 +00:00
|
|
|
import assert from 'assert';
|
|
|
|
import { GraphQLClient } from 'graphql-request';
|
|
|
|
|
|
|
|
import { Cache } from '@vulcanize/cache';
|
|
|
|
|
|
|
|
import ethQueries from './eth-queries';
|
2021-06-01 12:43:41 +00:00
|
|
|
import { padKey } from './utils';
|
2021-05-28 11:26:40 +00:00
|
|
|
|
2021-06-04 06:34:12 +00:00
|
|
|
interface Config {
|
|
|
|
gqlEndpoint: string;
|
|
|
|
cache: Cache;
|
|
|
|
}
|
2021-05-28 11:26:40 +00:00
|
|
|
|
2021-06-04 06:34:12 +00:00
|
|
|
interface Vars {
|
|
|
|
blockHash: string;
|
|
|
|
contract: string;
|
|
|
|
slot?: string;
|
|
|
|
}
|
|
|
|
|
|
|
|
export class EthClient {
|
|
|
|
_config: Config;
|
|
|
|
_client: GraphQLClient;
|
2021-05-28 11:26:40 +00:00
|
|
|
_cache: Cache;
|
|
|
|
|
2021-06-04 06:34:12 +00:00
|
|
|
constructor (config: Config) {
|
2021-05-28 11:26:40 +00:00
|
|
|
this._config = config;
|
|
|
|
|
|
|
|
const { gqlEndpoint, cache } = config;
|
|
|
|
assert(gqlEndpoint, 'Missing gql endpoint');
|
|
|
|
|
|
|
|
this._client = new GraphQLClient(gqlEndpoint);
|
|
|
|
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-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);
|
|
|
|
const { getLogs: logs } = result;
|
|
|
|
|
|
|
|
return logs;
|
2021-05-28 11:26:40 +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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Not cached or cache disabled, need to perform an upstream GQL query.
|
|
|
|
const result = await this._client.request(ethQueries[queryName], vars);
|
|
|
|
|
|
|
|
// Cache the result and return it, if cache is enabled.
|
|
|
|
if (this._cache) {
|
|
|
|
await this._cache.put(keyObj, result);
|
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
}
|