Skip to content

Commit

Permalink
Comments for methods
Browse files Browse the repository at this point in the history
  • Loading branch information
AntonD3 committed Feb 15, 2021
1 parent 7034c84 commit 61979e2
Showing 1 changed file with 7 additions and 3 deletions.
10 changes: 7 additions & 3 deletions contracts/scripts/read-variable.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,13 @@ async function getStorageAt(address: string, slot: BigNumber): Promise<string> {
return cache.get(slot);
}

// Read bytes from storage like utf-8 string
// Read bytes from storage like hex string
async function readBytes(slot: BigNumber, shift: number, bytes: number, address: string): Promise<string> {
const data = await getStorageAt(address, slot);
return '0x' + data.substr(66 - bytes * 2 - shift * 2, bytes * 2);
}

// Read dynamic sized bytes (encoding - bytes in solidity)
// Read dynamic sized bytes (encoding: bytes)
async function readDynamicBytes(slot: BigNumber, address: string): Promise<string> {
const data = await getStorageAt(address, slot);
if (Number.parseInt(data.substr(64, 2), 16) % 2 === 0) {
Expand Down Expand Up @@ -105,6 +105,7 @@ async function readPrimitive(slot: BigNumber, shift: number, address: string, ty
}
}

// Read user defined struct
async function readStruct(slot: BigNumber, address: string, type: string): Promise<object> {
const result = {};
const data = new Map();
Expand All @@ -120,6 +121,7 @@ async function readStruct(slot: BigNumber, address: string, type: string): Promi
return result;
}

// Read array (Static or dynamic sized)
async function readArray(slot: BigNumber, address: string, type: string): Promise<any> {
let length: number;
const baseType = types[type].base;
Expand Down Expand Up @@ -150,6 +152,7 @@ async function readArray(slot: BigNumber, address: string, type: string): Promis
return Promise.all(data);
}

// Read any type, except mapping (it needs key for reading)
async function readVariable(slot: BigNumber, shift: number, address: string, type: string): Promise<any> {
if (type.substr(0, 7) === 't_array') return readArray(slot, address, type);
if (type.substr(0, 8) === 't_struct') return readStruct(slot, address, type);
Expand Down Expand Up @@ -225,6 +228,7 @@ async function readPartOfMap(slot: BigNumber, address: string, type: string, par
return readPartOfVariable(slot, 0, address, valueType, params);
}

// Read part of variable (By indexes, field names, keys)
async function readPartOfVariable(
slot: BigNumber,
shift: number,
Expand All @@ -247,7 +251,7 @@ async function readPartOfVariable(
return readPrimitive(slot, shift, address, type);
}

// Get array indexes, struct fields and mapping keys from name
// Get array of indexes, struct fields and mapping keys from name
function parseName(fullName: string): string[] {
const firstPoint = fullName.indexOf('.');
const firstBracket = fullName.indexOf('[');
Expand Down

0 comments on commit 61979e2

Please sign in to comment.