Solidity Shell
An interactive Solidity shell with lightweight session recording and remote compiler support.
๐พ npm install -g solidity-shell
โ solidity-shell
๐ Entering interactive Solidity shell. '.help' and '.exit' are your friends.
ยป โน๏ธ ganache-mgr: starting temp. ganache instance ...
ยป
ยป uint a = 100
ยป uint b = 200
ยป a + b + 2 + uint8(50)
352
ยป $_
352
Oh, did you know that we automatically fetch a matching remote compiler when you change the solidity pragma? It is as easy as typing pragma solidity 0.5.0
and solidity-shell will do the rest ๐.
Hints
pragma solidity <version>
attempts to dynamically load the selected compiler version (remote compiler, may take a couple of seconds).- use
{ <statement>; }
to ignore a calls return value. - Sessions can be saved and restored using the
.session
command. Your previous session is always stored and can be loaded via.session load previous
(not safe when running concurrent shells). .reset
completely removes all statements..undo
removes the last statement.- See what's been generated under the hood? call
.dump
. - Settings are saved on exit (not safe when running concurrent shells). call
config set <key> <value>
to change settings like ganache port, ganache autostart, etc. $_
is a placeholder for the last known result. Feel free to use that placeholder in your scripts :)- Special commands are dot-prefixed. Everything else is evaluated as Solidity code.
import "<path>"
assumes thatpath
is relative to the current working-dir (CWD) or{CWD}/node_modules/
. There's experimental support for HTTPs URL's. You can disable https resolving by settingยป .config set resolveHttpImports false
.
ยป import "https://raw.githubusercontent.com/OpenZeppelin/openzeppelin-contracts/master/contracts/token/ERC721/IERC721.sol"
Usage
Cmdline Passthru
Any arguments provided after an empty --
are directly passed to ganacheCmd
(default: ganache-cli
). This way, for example, you can start a solidity shell on a ganache fork of mainnet via infura. Check ganache-cli --help
for a list of available options.
โ solidity-shell -- --fork https://mainnet.infura.io/v3/yourApiToken
๐ Entering interactive Solidity shell. Type '.help' for help, '.exit' to exit.
ยป โน๏ธ ganache-mgr: starting temp. ganache instance ...
ยป
ยป interface ERC20 {
multi> function name() external view returns (string memory);
multi> }
ยป ERC20(0xB8c77482e45F1F44dE1745F52C74426C631bDD52).name()
BNB
Repl
๐ Entering interactive Solidity ^0.8.11 shell. '.help' and '.exit' are your friends.
ยป โน๏ธ ganache-mgr: starting temp. ganache instance ...
ยป
ยป .help
๐ Help:
-----
$_ is a placeholder holding the most recent evaluation result.
pragma solidity <version> to change the compiler version.
General:
.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
restart ... restart the blockchain service
set-provider <fork-url> ... "internal" | <shell-command: e.g. ganache-cli> | <https://localhost:8545>
- fork url e.g. https://mainnet.infura.io/v3/yourApiKey
accounts ... return eth_getAccounts
eth_<X> [...args] ... initiate an arbitrary eth JSONrpc method call to blockchain provider.
Settings:
.config ... show settings
set <key> <value> ... set setting
unset <key> ... unset setting
Session:
.session ... list sessions
load <id> ... load session
save <id> ... save session
.undo ... undo last command
.reset ... reset cmd history. start from scratch.
Debug:
.proc ... show processes managed by solidity-shell (ganache)
.dump ... show template contract
.echo ... every shell needs an echo command
cheers ๐
@tintinweb
ConsenSys Diligence @ https://consensys.net/diligence/
https://github.com/tintinweb/solidity-shell/
Examples
msg.sender
etc.
Transaction vars: ยป msg.sender
0x70e9B09abd6A13D2F5083CD5814076b77427199F
ยป address(uint160(address(msg.sender)))
0x70e9B09abd6A13D2F5083CD5814076b77427199F
Contracts, Structs, Functions
โ solidity-shell
๐ Entering interactive Solidity shell. Type '.help' for help, '.exit' to exit.
ยป โน๏ธ ganache-mgr: starting temp. ganache instance ...
ยป
ยป contract TestContract {}
ยป new TestContract()
0xFBC1B2e79D816E36a1E1e923dd6c6fad463F4368
ยป msg.sender
0x363830C6aee2F0c43922bcB785C570a7cca613b5
ยป block.timestamp
1630339581
ยป struct yolo {uint8 x; uint8 y;}
ยป function mytest(uint x) public pure returns(uint) {
multi> return x -5;
multi> }
ยป mytest(100)
95
Advanced usage
ยป struct yolo {uint8 x; uint8 y;}
ยป .dump
// SPDX-License-Identifier: GPL-2.0-or-later
pragma solidity ^0.8.7;
contract TestContract {}
struct yolo {uint8 x; uint8 y;}
contract MainContract {
function main() public {
uint a = 100;
uint b = 200;
a + b + 2 + uint8(50);
new TestContract();
msg.sender;
block.timestamp;
return ;
}
}
Fetch Interface Declaration from Etherscan
.fetch interface <address> <interfaceName> [optional: chain=mainnet]
โ solidity-shell --fork https://mainnet.infura.io/v3/<yourApiKey>
๐ Entering interactive Solidity ^0.8.16 shell (๐ง:Ganache built-in). '.help' and '.exit' are your friends.
ยป
ยป .fetch interface 0x40cfEe8D71D67108Db46F772B7e2CD55813Bf2FB Test
ยป interface Test {
... omitted ...
function symbol() external view returns (string memory);
function tokenURI(uint256 tokenId) external view returns (string memory);
function totalSupply() external view returns (uint256);
function transferFrom(
address from,
address to,
uint256 tokenId
) external;
function transferOwnership(address newOwner) external;
function withdraw() external;
}
ยป Test t = Test(0x40cfEe8D71D67108Db46F772B7e2CD55813Bf2FB)
ยป t.symbol()
MGX
Inspect Contract Storage on Ganache Fork
- Run solidity shell in fork-mode.
- 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 ................................
Acknowledgements
- Inspired by the great but unfortunately unmaintained solidity-repl.
- Fetch interfaces from Etherscan is powered by abi-to-sol.