Implement getting storage value for uint256 type in watcher.

This commit is contained in:
nikugogoi 2021-06-08 11:14:27 +05:30 committed by Ashwin Phatak
parent c3d1737ee1
commit a13a909a85
6 changed files with 42 additions and 33 deletions

View File

@ -90,13 +90,13 @@ describe('Get value from storage', () => {
await integers.setInt1(expectedValue);
let blockHash = await getBlockHash();
let { value } = await getStorageValue(storageLayout, getStorageAt, blockHash, integers.address, 'int1');
expect(value).to.equal(expectedValue);
expect(value).to.equal(BigInt(expectedValue));
expectedValue = 34;
await integers.setInt2(expectedValue);
blockHash = await getBlockHash();
({ value } = await getStorageValue(storageLayout, getStorageAt, blockHash, integers.address, 'int2'));
expect(value).to.equal(expectedValue);
expect(value).to.equal(BigInt(expectedValue));
});
it('get value for integer type variables using single slot', async () => {
@ -109,7 +109,7 @@ describe('Get value from storage', () => {
await integers.setInt3(expectedValue);
const blockHash = await getBlockHash();
const { value } = await getStorageValue(storageLayout, getStorageAt, blockHash, integers.address, 'int3');
expect(value).to.equal(expectedValue);
expect(value).to.equal(BigInt(expectedValue));
});
it('get value for unsigned integer type variables packed together', async () => {
@ -122,13 +122,13 @@ describe('Get value from storage', () => {
await unsignedIntegers.setUint1(expectedValue);
let blockHash = await getBlockHash();
let { value } = await getStorageValue(storageLayout, getStorageAt, blockHash, unsignedIntegers.address, 'uint1');
expect(value).to.equal(expectedValue);
expect(value).to.equal(BigInt(expectedValue));
expectedValue = 34;
await unsignedIntegers.setUint2(expectedValue);
blockHash = await getBlockHash();
({ value } = await getStorageValue(storageLayout, getStorageAt, blockHash, unsignedIntegers.address, 'uint2'));
expect(value).to.equal(expectedValue);
expect(value).to.equal(BigInt(expectedValue));
});
it('get value for unsigned integer type variables using single slot', async () => {
@ -141,7 +141,7 @@ describe('Get value from storage', () => {
await unsignedIntegers.setUint3(expectedValue);
const blockHash = await getBlockHash();
const { value } = await getStorageValue(storageLayout, getStorageAt, blockHash, unsignedIntegers.address, 'uint3');
expect(value).to.equal(expectedValue);
expect(value).to.equal(BigInt(expectedValue));
});
it('get value for boolean type', async () => {
@ -237,7 +237,7 @@ describe('Get value from storage', () => {
await testEnums.setChoicesEnum1(expectedValue);
const blockHash = await getBlockHash();
const { value } = await getStorageValue(storageLayout, getStorageAt, blockHash, testEnums.address, 'choicesEnum1');
expect(value).to.equal(expectedValue);
expect(value).to.equal(BigInt(expectedValue));
});
describe('string type', () => {
@ -287,7 +287,7 @@ describe('Get value from storage', () => {
await testFixedArrays.setUint16Array(expectedValue);
blockHash = await getBlockHash();
({ value } = await getStorageValue(storageLayout, getStorageAt, blockHash, testFixedArrays.address, 'uint16Array'));
expect(value).to.eql(expectedValue);
expect(value).to.eql(expectedValue.map(el => BigInt(el)));
});
// Test for array variables which are more than 32 bytes and use multiple slots.
@ -302,11 +302,11 @@ describe('Get value from storage', () => {
await testFixedArrays.setInt128Array(expectedValue);
let blockHash = await getBlockHash();
let { value } = await getStorageValue(storageLayout, getStorageAt, blockHash, testFixedArrays.address, 'int128Array');
expect(value).to.eql(expectedValue);
expect(value).to.eql(expectedValue.map(el => BigInt(el)));
await testFixedArrays.setUintArray(expectedValue);
blockHash = await getBlockHash();
({ value } = await getStorageValue(storageLayout, getStorageAt, blockHash, testFixedArrays.address, 'uintArray'));
expect(value).to.eql(expectedValue);
expect(value).to.eql(expectedValue.map(el => BigInt(el)));
});
});

View File

@ -68,7 +68,7 @@ export const getStorageValue = async (storageLayout: StorageLayout, getStorageAt
* @param storageValue
* @param typeLabel
*/
export const getValueByType = (storageValue: string, typeLabel: string): number | string | boolean => {
export const getValueByType = (storageValue: string, typeLabel: string): bigint | string | boolean => {
// Parse value for boolean type.
if (typeLabel === 'bool') {
return !BigNumber.from(storageValue).isZero();
@ -76,7 +76,7 @@ export const getValueByType = (storageValue: string, typeLabel: string): number
// Parse value for uint/int type or enum type.
if (typeLabel.match(/^enum|u?int[0-9]+/)) {
return BigNumber.from(storageValue).toNumber();
return BigInt(storageValue);
}
// Parse value for string type.

View File

@ -34,6 +34,7 @@
"graphql": "^15.5.0",
"graphql-import-node": "^0.0.4",
"graphql-request": "^3.4.0",
"json-bigint": "^1.0.0",
"lodash": "^4.17.21",
"pg": "^8.6.0",
"reflect-metadata": "^0.1.13",
@ -46,6 +47,7 @@
"@ethersproject/abi": "^5.3.0",
"@types/express": "^4.17.11",
"@types/fs-extra": "^9.0.11",
"@types/json-bigint": "^1.0.0",
"@types/yargs": "^17.0.0",
"@typescript-eslint/eslint-plugin": "^4.25.0",
"@typescript-eslint/parser": "^4.25.0",

View File

@ -3,6 +3,7 @@ import debug from 'debug';
import { invert } from 'lodash';
import { JsonFragment } from '@ethersproject/abi';
import { DeepPartial } from 'typeorm';
import JSONbig from 'json-bigint';
import { EthClient, getMappingSlot, topictoAddress } from '@vulcanize/ipld-eth-client';
import { getStorageInfo, getEventNameTopics, getStorageValue, GetStorageAt, StorageLayout } from '@vulcanize/solidity-mapper';
@ -63,17 +64,10 @@ export class Indexer {
}
async totalSupply (blockHash: string, token: string): Promise<ValueResult> {
// TODO: Use getStorageValue when it supports uint256 values.
const { slot } = getStorageInfo(this._storageLayout, '_totalSupply');
const result = await this._getStorageValue(blockHash, token, '_totalSupply');
const vars = {
blockHash,
contract: token,
slot
};
const result = await this._getStorageAt(vars);
log(JSON.stringify(result, null, 2));
// https://github.com/GoogleChromeLabs/jsbi/issues/30#issuecomment-521460510
log(JSONbig.stringify(result, null, 2));
return result;
}
@ -98,10 +92,10 @@ export class Indexer {
};
const result = await this._getStorageAt(vars);
log(JSON.stringify(result, null, 2));
log(JSONbig.stringify(result, null, 2));
const { value, proof } = result;
await this._db.saveBalance({ blockHash, token, owner, value: BigInt(value), proof: JSON.stringify(proof) });
await this._db.saveBalance({ blockHash, token, owner, value: BigInt(value), proof: JSONbig.stringify(proof) });
return result;
}
@ -126,10 +120,10 @@ export class Indexer {
};
const result = await this._getStorageAt(vars);
log(JSON.stringify(result, null, 2));
log(JSONbig.stringify(result, null, 2));
const { value, proof } = result;
await this._db.saveAllowance({ blockHash, token, owner, spender, value: BigInt(value), proof: JSON.stringify(proof) });
await this._db.saveAllowance({ blockHash, token, owner, spender, value: BigInt(value), proof: JSONbig.stringify(proof) });
return result;
}
@ -137,7 +131,7 @@ export class Indexer {
async name (blockHash: string, token: string): Promise<ValueResult> {
const result = await this._getStorageValue(blockHash, token, '_name');
log(JSON.stringify(result, null, 2));
log(JSONbig.stringify(result, null, 2));
return result;
}
@ -145,7 +139,7 @@ export class Indexer {
async symbol (blockHash: string, token: string): Promise<ValueResult> {
const result = await this._getStorageValue(blockHash, token, '_symbol');
log(JSON.stringify(result, null, 2));
log(JSONbig.stringify(result, null, 2));
return result;
}
@ -206,7 +200,7 @@ export class Indexer {
};
});
log(JSON.stringify(result, null, 2));
log(JSONbig.stringify(result, null, 2));
return result;
}
@ -224,7 +218,7 @@ export class Indexer {
async _fetchAndSaveEvents ({ blockHash, token }: { blockHash: string, token: string }): Promise<void> {
const logs = await this._ethClient.getLogs({ blockHash, contract: token });
log(JSON.stringify(logs, null, 2));
log(JSONbig.stringify(logs, null, 2));
const eventNameToTopic = getEventNameTopics(this._abi);
const logTopicToEventName = invert(eventNameToTopic);
@ -243,8 +237,8 @@ export class Indexer {
token,
eventName,
proof: JSON.stringify({
data: JSON.stringify({
proof: JSONbig.stringify({
data: JSONbig.stringify({
blockHash,
receipt: {
cid,

View File

@ -7,6 +7,7 @@ import toml from 'toml';
import yargs from 'yargs';
import { hideBin } from 'yargs/helpers';
import debug from 'debug';
import JSONbig from 'json-bigint';
import { createSchema } from './gql';
@ -30,7 +31,7 @@ export const createServer = async (): Promise<Application> => {
}
const config = toml.parse(await fs.readFile(configFilePath, 'utf8'));
log('config', JSON.stringify(config, null, 2));
log('config', JSONbig.stringify(config, null, 2));
assert(config.server, 'Missing server config');

View File

@ -1174,6 +1174,11 @@
dependencies:
"@types/node" "*"
"@types/json-bigint@^1.0.0":
version "1.0.0"
resolved "https://registry.yarnpkg.com/@types/json-bigint/-/json-bigint-1.0.0.tgz#7a4726540cc6fe47cfa54b9b3022b89cf7fe1517"
integrity sha512-WW+0cfH3ovFN6ROV+p/Xfw36dT6s16hbXBYIG49PYw6+j6e+AkpqYccctgxwyicBmC8CZDBnPhOH94shFhXgHQ==
"@types/json-schema@^7.0.3":
version "7.0.7"
resolved "https://registry.yarnpkg.com/@types/json-schema/-/json-schema-7.0.7.tgz#98a993516c859eb0d5c4c8f098317a9ea68db9ad"
@ -5855,6 +5860,13 @@ jsesc@~0.5.0:
resolved "https://registry.yarnpkg.com/jsesc/-/jsesc-0.5.0.tgz#e7dee66e35d6fc16f710fe91d5cf69f70f08911d"
integrity sha1-597mbjXW/Bb3EP6R1c9p9w8IkR0=
json-bigint@^1.0.0:
version "1.0.0"
resolved "https://registry.yarnpkg.com/json-bigint/-/json-bigint-1.0.0.tgz#ae547823ac0cad8398667f8cd9ef4730f5b01ff1"
integrity sha512-SiPv/8VpZuWbvLSMtTDU8hEfrZWg/mH/nV/b4o0CYbSxu1UIQPLdwKOCIyLQX+VIPO5vrLX3i8qtqFyhdPSUSQ==
dependencies:
bignumber.js "^9.0.0"
json-buffer@3.0.0:
version "3.0.0"
resolved "https://registry.yarnpkg.com/json-buffer/-/json-buffer-3.0.0.tgz#5b1f397afc75d677bde8bcfc0e47e1f9a3d9a898"