Skip to content
This repository was archived by the owner on Feb 26, 2024. It is now read-only.

Commit 65967b4

Browse files
committed
Improve request typing
1 parent 4da769c commit 65967b4

File tree

4 files changed

+23
-16
lines changed

4 files changed

+23
-16
lines changed

packages/truffle-debugger/lib/data/sagas/index.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,13 +65,15 @@ export function* decode(definition, ref) {
6565
while (!result.done) {
6666
let request = result.value;
6767
let response;
68-
switch (request.requesting) {
68+
switch (request.type) {
6969
//yes, this is a little silly right now
7070
case "storage":
7171
//the debugger supplies all storage it knows at the beginning.
7272
//any storage it does not know is presumed to be zero.
7373
response = ZERO_WORD;
7474
break;
75+
default:
76+
debug("unrecognized request type!");
7577
}
7678
result = decoder.next(response);
7779
}

packages/truffle-decoder/lib/interface/contract-decoder.ts

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import * as storage from "../allocate/storage";
1212
import { StoragePointer, isStoragePointer } from "../types/pointer";
1313
import { StorageAllocations, StorageMemberAllocations, StorageMemberAllocation } from "../types/allocation";
1414
import { Slot, isWordsLength } from "../types/storage";
15-
import { DecoderRequest } from "../types/request";
15+
import { DecoderRequest, isStorageRequest } from "../types/request";
1616
import decode from "../decode";
1717
import { Definition as DefinitionUtils, EVM, AstDefinition, AstReferences } from "truffle-decode-utils";
1818
import { BlockType, Transaction } from "web3/eth/types";
@@ -178,17 +178,16 @@ export default class TruffleContractDecoder extends AsyncEventEmitter {
178178
while(!result.done) {
179179
let request = <DecoderRequest>(result.value);
180180
let response: any
181-
switch(request.requesting) {
182-
//yes, this is a little silly right now
183-
case "storage":
184-
response = DecodeUtils.Conversion.toBytes(
185-
await this.web3.eth.getStorageAt(
186-
this.contractAddress,
187-
request.slot,
188-
block),
189-
DecodeUtils.EVM.WORD_SIZE);
190-
break;
181+
if(isStorageRequest(request)) {
182+
response = DecodeUtils.Conversion.toBytes(
183+
await this.web3.eth.getStorageAt(
184+
this.contractAddress,
185+
request.slot,
186+
block),
187+
DecodeUtils.EVM.WORD_SIZE);
191188
}
189+
//note: one of the above conditionals *must* be true by the type system.
190+
//yes, right now there's only one such conditional.
192191
result = decoder.next(response);
193192
}
194193
//at this point, result.value holds the final value

packages/truffle-decoder/lib/read/storage.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ export function* read(storage: WordMapping, slot: Slot): IterableIterator<Uint8A
7373
//say 0)
7474
if(word === undefined) {
7575
word = yield {
76-
requesting: "storage",
76+
type: "storage",
7777
slot: address
7878
};
7979
}
Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,12 @@
11
import BN from "bn.js";
22

3-
export interface DecoderRequest {
4-
requesting: "storage";
5-
slot?: BN; //will add more fields as needed
3+
export type DecoderRequest = StorageRequest; //will add more later
4+
5+
export interface StorageRequest {
6+
type: "storage";
7+
slot: BN; //will add more fields as needed
8+
}
9+
10+
export function isStorageRequest(request: DecoderRequest): request is StorageRequest {
11+
return request.type === "storage";
612
}

0 commit comments

Comments
 (0)