diff --git a/CHANGELOG.md b/CHANGELOG.md index f49f1b9..182d045 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 + +image + +``` + .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 [
] ... 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 diff --git a/README.md b/README.md index cefca27..ff4880e 100644 --- a/README.md +++ b/README.md @@ -85,9 +85,17 @@ BNB .help ... this help :) .exit ... exit the shell + Source: .fetch interface
[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 [
] ... show raw storage at slot of underlying deployed contract + deployed ... debug: show internal contract object + Blockchain: .chain @@ -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/ + +๐Ÿš€ 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 ................................ +``` + ____ diff --git a/bin/main.js b/bin/main.js index fb0a029..8158d8e 100755 --- a/bin/main.js +++ b/bin/main.js @@ -123,6 +123,13 @@ function handleRepl(input, cb) { ${c.bold('Source:')} .fetch interface
[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 [
] ... show raw storage at slot of underlying deployed contract + deployed ... debug: show internal contract object + ${c.bold('Blockchain:')} .chain @@ -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
[chain=mainnet] ... fetch and load an interface declaration from an ABI spec on etherscan.io") @@ -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); @@ -347,6 +427,9 @@ vorpal vorpal .command(".fetch") .autocomplete(["interface"]) +vorpal + .command(".inspect") + .autocomplete(["bytecode", "opcodes", "storage", "storageLayout", "deployed"]) vorpal .command(".echo ") vorpal diff --git a/package-lock.json b/package-lock.json index f128033..4b72ba7 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { "name": "solidity-shell", - "version": "0.2.1", + "version": "0.2.2", "lockfileVersion": 2, "requires": true, "packages": { "": { "name": "solidity-shell", - "version": "0.2.1", + "version": "0.2.2", "license": "MIT", "dependencies": { "abi-to-sol": "^0.6.5", diff --git a/package.json b/package.json index b1073c6..fd6b184 100644 --- a/package.json +++ b/package.json @@ -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": { diff --git a/src/blockchain.js b/src/blockchain.js index 114a57d..75ece10 100644 --- a/src/blockchain.js +++ b/src/blockchain.js @@ -88,7 +88,10 @@ class AbsBlockchainBase { return resolve(result); }); }); + } + getDeployed(){ + return this.deployed[this.shell.settings.templateContractName]; } async deploy(contracts, callback) { @@ -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 } @@ -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); } diff --git a/src/handler.js b/src/handler.js index 2bf6791..d769484 100644 --- a/src/handler.js +++ b/src/handler.js @@ -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(