Skip to content

Commit

Permalink
Inspect storage, storageLayout, bytecode, opcodes (#23)
Browse files Browse the repository at this point in the history
* command: .inspect

* .fetch storage <slot> <num> [address]

* fix hexview col headers
show if fork-mode is enabled

* prep v0.2.2

* update changelog

* fix example

* simplify getDeployed
  • Loading branch information
tintinweb committed Aug 10, 2022
1 parent 17d0570 commit 7793abf
Show file tree
Hide file tree
Showing 7 changed files with 171 additions and 6 deletions.
14 changes: 14 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,20 @@
# Change Log
All notable changes will be documented in this file.

## v0.2.2
- new: inspection commands: `.inspect` contract raw storage, show generated bytecode, opcodes, storageLayout - #23

<img width="941" alt="image" src="https://user-images.githubusercontent.com/2865694/183737870-8faa103d-2564-435b-8789-d9b4cde94c10.png">

```
.inspect
bytecode ... show bytecode of underlying contract
opcodes ... show disassembled opcodes of underlying contract
storageLayout ... show variable to storage slot mapping for underlying contract
storage <slot> <num> [<address>] ... show raw storage at slot of underlying deployed contract
deployed ... debug: show internal contract object
```

## v0.2.1
- fix: feed current compiler version into abi-to-sol; strip attribution and other code #20 #21
- update: compiler list
Expand Down
63 changes: 63 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -85,9 +85,17 @@ BNB
.help ... this help :)
.exit ... exit the shell


Source:
.fetch
interface <address> <name> [chain=mainnet] ... fetch and load an interface declaration from an ABI spec on etherscan.io
.inspect
bytecode ... show bytecode of underlying contract
opcodes ... show disassembled opcodes of underlying contract
storageLayout ... show variable to storage slot mapping for underlying contract
storage <slot> <num> [<address>] ... show raw storage at slot of underlying deployed contract
deployed ... debug: show internal contract object


Blockchain:
.chain
Expand Down Expand Up @@ -227,6 +235,61 @@ contract MainContract {
» t.symbol()
MGX
```

### Inspect Contract Storage on Ganache Fork

1. Run solidity shell in **fork-mode**.
2. Display contract storage at latest block.


```
⇒ solidity-shell --fork https://mainnet.infura.io/v3/<yourApiKey>
🚀 Entering interactive Solidity ^0.8.16 shell (🧁:Ganache built-in, ⇉ fork-mode). '.help' and '.exit' are your friends.
» .inspect storage 0 10 0x40cfEe8D71D67108Db46F772B7e2CD55813Bf2FB
»
📚 Contract: 0x40cfee8d71d67108db46f772b7e2cd55813bf2fb @ latest block
slot 1f 1e 1d 1c 1b 1a 19 18 17 16 15 14 13 12 11 10 0f 0e 0d 0c 0b 0a 09 08 07 06 05 04 03 02 01 00
--------------------------------------------------------------------------------------------------------------------
0x000000 ( 0) 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 1d c7 ................................
0x000001 ( 1) 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................................
0x000002 ( 2) 54 68 65 20 4d 61 67 69 78 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 12 The Magix.......................
0x000003 ( 3) 4d 47 58 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 06 MGX.............................
0x000004 ( 4) 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................................
0x000005 ( 5) 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................................
0x000006 ( 6) 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................................
0x000007 ( 7) 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................................
0x000008 ( 8) 00 00 00 00 00 00 00 00 00 00 00 00 d7 4e 84 57 2f 5f 7b 5d 41 47 4e be d9 b3 02 0a 2e 52 6f c6 .............N.W/_{]AGN......Ro.
0x000009 ( 9) 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 27 0f ..............................'.
```

### Inspect Generated Contract

```
solidity-shell
🚀 Entering interactive Solidity ^0.8.16 shell (🧁:Ganache built-in, ⇉ fork-mode). '.help' and '.exit' are your friends.
» 1+1
2
» .inspect bytecode
6080604052348015610010576000 ... 03a7bab64736f6c63430008100033
» .inspect opcodes
PUSH1 0x80 PUSH1 0x40 MSTORE CALLVALUE ... SLOAD 0xDA POP GASPRICE PUSH28 0xAB64736F6C6343000810003300000000000000000000000000000000
» .inspect storageLayout
{ storage: [], types: null }
» .inspect storage 0 4
»
📚 Contract: 0xCa1061046396daF801dEB0D848FcfeA055fAfBFC @ latest block
slot 1f 1e 1d 1c 1b 1a 19 18 17 16 15 14 13 12 11 10 0f 0e 0d 0c 0b 0a 09 08 07 06 05 04 03 02 01 00
--------------------------------------------------------------------------------------------------------------------
0x000000 ( 0) 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................................
0x000001 ( 1) 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................................
0x000002 ( 2) 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................................
0x000003 ( 3) 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................................
```

____


Expand Down
85 changes: 84 additions & 1 deletion bin/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,13 @@ function handleRepl(input, cb) {
${c.bold('Source:')}
.fetch
interface <address> <name> [chain=mainnet] ... fetch and load an interface declaration from an ABI spec on etherscan.io
.inspect
bytecode ... show bytecode of underlying contract
opcodes ... show disassembled opcodes of underlying contract
storageLayout ... show variable to storage slot mapping for underlying contract
storage <slot> <num> [<address>] ... show raw storage at slot of underlying deployed contract
deployed ... debug: show internal contract object
${c.bold('Blockchain:')}
.chain
Expand Down Expand Up @@ -239,6 +246,79 @@ cheers 🙌
return cb();
}
return cb(`${c.bold(c.yellow(shell.blockchain.proc.pid))} - ${shell.blockchain.proc.spawnargs.join(', ')}`)
case '.inspect':
let deployed = shell.blockchain.getDeployed();
switch (commandParts[1]) {
case 'storage':

function getStorageAt(target, start, num, atBlock) {

atBlock = atBlock || "latest";
start = start || 0;
num = num || 1;
let slots = [...Array(num).keys()].map((idx) => (start + idx))

return slots.map(slot => shell.blockchain.web3.eth.getStorageAt(target, slot));
}
let start = commandParts.length > 2 ? parseInt(commandParts[2]) : 0;
let num = commandParts.length > 3 ? parseInt(commandParts[3]) : 10;

if (isNaN(start) || isNaN(num)) {
console.error("start and num must be numbers!")
break;
}

var target;
if (commandParts.length > 4) {
target = commandParts[4].trim().toLowerCase();
} else if (deployed) {
target = deployed.instance.options.address;
} else {
console.error("not yet ready. execute repl command first.");
break;
}

Promise.all(getStorageAt(target, start, num))
.then(r => {

let head = `
📚 Contract: ${target} @ latest block
slot 1f 1e 1d 1c 1b 1a 19 18 17 16 15 14 13 12 11 10 0f 0e 0d 0c 0b 0a 09 08 07 06 05 04 03 02 01 00
--------------------------------------------------------------------------------------------------------------------
`;

let lines = r.map((v, i) => {
let datawideBytes = v.replace("0x", "").padStart(256 / 8 * 2, '0').match(/.{2}/g);
let strline = datawideBytes.map(b => {
let bint = parseInt(b, 16);
if (bint >= 32 && bint <= 126) {
return String.fromCharCode(bint);
} else {
return '.';
}
}).join("");
return ` 0x${(start + i).toString(16).padStart(6, '0')} (${(start + i).toString().padStart(4, ' ')}) ${datawideBytes.map(b => b == "00" ? b : c.bold(c.bgYellowBright(b))).join(" ")} ${strline}`;
}).join('\n');

console.log(head + lines);
});

break;
case 'bytecode':
deployed && cb(c.yellow(deployed.bytecode));
break;
case 'deployed':
cb(deployed);
break;
case 'storageLayout':
deployed && cb(deployed.storageLayout);
break;
case 'opcodes':
deployed && cb(deployed.opcodes);
break;
}
break;
case '.fetch':
if (commandParts.length < 4) {
cb("Invalid params: .fetch interface <address> <name> [chain=mainnet] ... fetch and load an interface declaration from an ABI spec on etherscan.io")
Expand Down Expand Up @@ -310,7 +390,7 @@ vorpal
.mode('repl', 'Enters Solidity Shell Mode')
.delimiter(c.bold('» '))
.init(function (args, cb) {
this.log(`🚀 Entering interactive Solidity ${c.bold(shell.settings.installedSolidityVersion)} shell (🧁:${c.bold(shell.blockchain.name)}). '${c.bold('.help')}' and '${c.bold('.exit')}' are your friends.`);
this.log(`🚀 Entering interactive Solidity ${c.bold(shell.settings.installedSolidityVersion)} shell (🧁:${c.bold(shell.blockchain.name)}${shell.settings.ganacheOptions && shell.settings.ganacheOptions.fork ? c.bold(', ⇉ fork-mode'):''}). '${c.bold('.help')}' and '${c.bold('.exit')}' are your friends.`);
return cb();
})
.action(handleRepl);
Expand Down Expand Up @@ -347,6 +427,9 @@ vorpal
vorpal
.command(".fetch")
.autocomplete(["interface"])
vorpal
.command(".inspect")
.autocomplete(["bytecode", "opcodes", "storage", "storageLayout", "deployed"])
vorpal
.command(".echo <msg>")
vorpal
Expand Down
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "solidity-shell",
"version": "0.2.1",
"version": "0.2.2",
"description": "An interactive Solidity shell with lightweight session recording and remote compiler support",
"main": "src/index.js",
"bin": {
Expand Down
7 changes: 6 additions & 1 deletion src/blockchain.js
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,10 @@ class AbsBlockchainBase {
return resolve(result);
});
});
}

getDeployed(){
return this.deployed[this.shell.settings.templateContractName];
}

async deploy(contracts, callback) {
Expand All @@ -100,10 +103,12 @@ class AbsBlockchainBase {

let thisContract = {
bytecode: o.evm.bytecode.object,
opcodes: o.evm.bytecode.opcodes,
abi: o.abi,
proxy: new this.web3.eth.Contract(o.abi, null),
instance: undefined,
main: o.main,
storageLayout: o.storageLayout,
accounts: undefined
}

Expand All @@ -112,10 +117,10 @@ class AbsBlockchainBase {
.then(accounts => {
thisContract.accounts = accounts;
let instance = thisContract.proxy.deploy({ data: thisContract.bytecode }).send({ from: accounts[0], gas: this.shell.settings.deployGas })
thisContract.instance = instance;
return instance;
})
.then(contract => {
thisContract.instance = contract;
if (thisContract.main) {
contract.methods[thisContract.main]().call({ from: thisContract.accounts[0], gas: this.shell.settings.callGas }, callback);
}
Expand Down
2 changes: 1 addition & 1 deletion src/handler.js
Original file line number Diff line number Diff line change
Expand Up @@ -300,7 +300,7 @@ contract ${this.settings.templateContractName} {
},
},
}
input.settings.outputSelection['*']['*'] = ['abi', 'evm.bytecode']
input.settings.outputSelection['*']['*'] = ['abi', 'evm.bytecode', 'storageLayout']

const callbacks = {
'import': (sourcePath) => readFileCallback(
Expand Down

0 comments on commit 7793abf

Please sign in to comment.