ethers.js/docs/v5/cli/ethers/index.html
2021-02-08 15:26:10 -05:00

227 lines
19 KiB
HTML

<!DOCTYPE html>
<html class="paged">
<head>
<title>Sandbox Utility</title>
<link rel="stylesheet" type="text/css" href="/v5/static/style.css">
<meta property="og:title" content="Sandbox Utility"/>
<meta property="og:description" content="Documentation for ethers, a complete, tiny and simple Ethereum library."/>
<meta property="og:image" content="/v5/static/social.jpg"/>
</head>
<body>
<div class="sidebar">
<div class="header">
<div class="logo"><a href="/v5/"><div class="image"></div><div class="name">ethers</div><div class="version">v5.0</div></a></div>
<div class="search"><form action="/v5/search/" method="GET"><input name="search" id="search" /></form><span class="search-icon">&#9906;</span></div>
</div>
<div class="toc"><div>
<div class="link title"><a href="/v5/">Documentation</a></div><div class="base show link depth-1"><a href="/v5/getting-started/">Getting Started</a></div><div class="base show link depth-1"><a href="/v5/concepts/">Ethereum Basics</a></div><div class="hide link depth-2"><a href="/v5/concepts/events/">Events</a></div><div class="hide link depth-2"><a href="/v5/concepts/gas/">Gas</a></div><div class="hide link depth-2"><a href="/v5/concepts/security/">Security</a></div><div class="hide link depth-2"><a href="/v5/concepts/best-practices/">Best Practices</a></div><div class="base show link depth-1"><a href="/v5/api-keys/">Provider API Keys</a></div><div class="base show link depth-1"><a href="/v5/api/">Application Programming Interface</a></div><div class="hide link depth-2"><a href="/v5/api/providers/">Providers</a></div><div class="hide link depth-3"><a href="/v5/api/providers/provider/">Provider</a></div><div class="hide link depth-3"><a href="/v5/api/providers/jsonrpc-provider/">JsonRpcProvider</a></div><div class="hide link depth-3"><a href="/v5/api/providers/api-providers/">API Providers</a></div><div class="hide link depth-3"><a href="/v5/api/providers/other/">Other Providers</a></div><div class="hide link depth-3"><a href="/v5/api/providers/types/">Types</a></div><div class="hide link depth-2"><a href="/v5/api/signer/">Signers</a></div><div class="hide link depth-2"><a href="/v5/api/contract/">Contract Interaction</a></div><div class="hide link depth-3"><a href="/v5/api/contract/contract/">Contract</a></div><div class="hide link depth-3"><a href="/v5/api/contract/contract-factory/">ContractFactory</a></div><div class="hide link depth-3"><a href="/v5/api/contract/example/">Example: ERC-20 Contract</a></div><div class="hide link depth-2"><a href="/v5/api/utils/">Utilities</a></div><div class="hide link depth-3"><a href="/v5/api/utils/abi/">Application Binary Interface</a></div><div class="hide link depth-4"><a href="/v5/api/utils/abi/coder/">AbiCoder</a></div><div class="hide link depth-4"><a href="/v5/api/utils/abi/formats/">ABI Formats</a></div><div class="hide link depth-4"><a href="/v5/api/utils/abi/fragments/">Fragments</a></div><div class="hide link depth-4"><a href="/v5/api/utils/abi/interface/">Interface</a></div><div class="hide link depth-3"><a href="/v5/api/utils/address/">Addresses</a></div><div class="hide link depth-3"><a href="/v5/api/utils/bignumber/">BigNumber</a></div><div class="hide link depth-3"><a href="/v5/api/utils/bytes/">Byte Manipulation</a></div><div class="hide link depth-3"><a href="/v5/api/utils/constants/">Constants</a></div><div class="hide link depth-3"><a href="/v5/api/utils/display-logic/">Display Logic and Input</a></div><div class="hide link depth-3"><a href="/v5/api/utils/encoding/">Encoding Utilities</a></div><div class="hide link depth-3"><a href="/v5/api/utils/fixednumber/">FixedNumber</a></div><div class="hide link depth-3"><a href="/v5/api/utils/hashing/">Hashing Algorithms</a></div><div class="hide link depth-3"><a href="/v5/api/utils/hdnode/">HD Wallet</a></div><div class="hide link depth-3"><a href="/v5/api/utils/logger/">Logging</a></div><div class="hide link depth-3"><a href="/v5/api/utils/properties/">Property Utilities</a></div><div class="hide link depth-3"><a href="/v5/api/utils/signing-key/">Signing Key</a></div><div class="hide link depth-3"><a href="/v5/api/utils/strings/">Strings</a></div><div class="hide link depth-3"><a href="/v5/api/utils/transactions/">Transactions</a></div><div class="hide link depth-3"><a href="/v5/api/utils/web/">Web Utilities</a></div><div class="hide link depth-3"><a href="/v5/api/utils/wordlists/">Wordlists</a></div><div class="hide link depth-2"><a href="/v5/api/other/">Other Libraries</a></div><div class="hide link depth-3"><a href="/v5/api/other/assembly/">Assembly</a></div><div class="hide link depth-4"><a href="/v5/api/other/assembly/dialect/">Ethers ASM Dialect</a></div><div class="hide link depth-4"><a href="/v5/api/other/assembly/api/">Utilities</a></div><div class="hide link depth-4"><a href="/v5/api/other/assembly/ast/">Abstract Syntax Tree</a></div><div class="hide link depth-3"><a href="/v5/api/other/hardware/">Hardware Wallets</a></div><div class="hide link depth-2"><a href="/v5/api/experimental/">Experimental</a></div><div class="base ancestor show link depth-1"><a href="/v5/cli/">Command Line Interfaces</a></div><div class="myself ancestor ancestor show link depth-2"><a href="/v5/cli/ethers/">Sandbox Utility</a></div><div class="link show child depth-3"><a href="#sandbox-utility--help">Help</a></div><div class="link show child depth-3"><a href="#sandbox-utility--examples">Examples</a></div><div class="show link depth-2"><a href="/v5/cli/asm/">Assembler</a></div><div class="show link depth-2"><a href="/v5/cli/ens/">Ethereum Naming Service</a></div><div class="show link depth-2"><a href="/v5/cli/typescript/">TypeScript</a></div><div class="show link depth-2"><a href="/v5/cli/plugin/">Making Your Own</a></div><div class="base show link depth-1"><a href="/v5/cookbook/">Cookbook</a></div><div class="hide link depth-2"><a href="/v5/cookbook/react-native/">React Native (and ilk)</a></div><div class="base show link depth-1"><a href="/v5/migration/">Migration Guide</a></div><div class="hide link depth-2"><a href="/v5/migration/web3/">Migration: From Web3.js</a></div><div class="hide link depth-2"><a href="/v5/migration/ethers-v4/">Migration: From Ethers v4</a></div><div class="base show link depth-1"><a href="/v5/testing/">Testing</a></div><div class="base show link depth-1"><a href="/v5/contributing/">Contributing and Hacking</a></div><div class="base show link depth-1"><a href="/v5/other-resources/">Other Resources</a></div><div class="base show link depth-1"><a href="/v5/documentation/">Flatworm Docs</a></div><div class="base show link depth-1"><a href="/v5/license/">License and Copyright</a></div>
</div></div>
<div class="footer">
<a href="/v5/single-page/">Single Page</a>
</div>
</div>
<div class="content">
<div class="breadcrumbs"><a href="/v5/">Documentation</a>&nbsp;&nbsp;&raquo;&nbsp;&nbsp;<a href="/v5/cli/">Command Line Interfaces</a>&nbsp;&nbsp;&raquo;&nbsp;&nbsp;<span class="current">Sandbox Utility</span></div>
<a name="sandbox-utility"></a><h1 class="show-anchors"><div>Sandbox Utility<div class="anchors"><a class="self" href="/v5/cli/ethers/#sandbox-utility"></a></div></div></h1><p>The sandbox utility provides a simple way to use the most common ethers utilities required during learning, debugging and managing interactions with the Ethereum network.</p>
<p>If no command is given, it will enter a REPL interface with many of the ethers utilities already exposed.</p>
<a name="sandbox-utility--help"></a><h2 class="show-anchors"><div>Help<div class="anchors"><a class="self" href="/v5/cli/ethers/#sandbox-utility--help"></a></div></div></h2>
<div class="code">Usage:
ethers [ COMMAND ] [ ARGS ] [ OPTIONS ]
COMMANDS (default: sandbox)
sandbox Run a REPL VM environment with ethers
init FILENAME Create a new JSON wallet
[ --force ] Overwrite any existing files
fund TARGET Fund TARGET with testnet ether
info [ TARGET ... ] Dump info for accounts, addresses and ENS names
send TARGET ETHER Send ETHER ether to TARGET form accounts[0]
[ --allow-zero ] Allow sending to the address zero
[ --data DATA ] Include data in the transaction
sweep TARGET Send all ether from accounts[0] to TARGET
sign-message MESSAGE Sign a MESSAGE with accounts[0]
[ --hex ] The message content is hex encoded
eval CODE Run CODE in a VM with ethers
run FILENAME Run FILENAME in a VM with ethers
wait HASH Wait for a transaction HASH to be mined
wrap-ether VALUE Deposit VALUE into Wrapped Ether (WETH)
unwrap-ether VALUE Withdraw VALUE from Wrapped Ether (WETH)
send-token TOKEN ADDRESS VALUE
Send VALUE tokens (at TOKEN) to ADDRESS
compile FILENAME Compiles a Solidity contract
[ --no-optimize ] Do not optimize the compiled output
[ --warnings ] Error on any warning
deploy FILENAME Compile and deploy a Solidity contract
[ --no-optimize ] Do not optimize the compiled output
[ --contract NAME ] Specify the contract to deploy
ACCOUNT OPTIONS
--account FILENAME Load from a file (JSON, RAW or mnemonic)
--account RAW_KEY Use a private key (insecure *)
--account 'MNEMONIC' Use a mnemonic (insecure *)
--account - Use secure entry for a raw key or mnemonic
--account-void ADDRESS Use an address as a void signer
--account-void ENS_NAME Add the resolved address as a void signer
--account-rpc ADDRESS Add the address from a JSON-RPC provider
--account-rpc INDEX Add the index from a JSON-RPC provider
--mnemonic-password Prompt for a password for mnemonics
--xxx-mnemonic-password Prompt for a (experimental) hard password
PROVIDER OPTIONS (default: all + homestead)
--alchemy Include Alchemy
--etherscan Include Etherscan
--infura Include INFURA
--nodesmith Include nodesmith
--rpc URL Include a custom JSON-RPC
--offline Dump signed transactions (no send)
--network NETWORK Network to connect to (default: homestead)
TRANSACTION OPTIONS (default: query network)
--gasPrice GWEI Default gas price for transactions(in wei)
--gasLimit GAS Default gas limit for transactions
--nonce NONCE Initial nonce for the first transaction
--yes Always accept Signing and Sending
OTHER OPTIONS
--wait Wait until transactions are mined
--debug Show stack traces for errors
--help Show this usage and exit
--version Show this version and exit
(*) By including mnemonics or private keys on the command line they are
possibly readable by other users on your system and may get stored in
your bash history file. This is NOT recommended.</div><a name="sandbox-utility--examples"></a><h2 class="show-anchors"><div>Examples<div class="anchors"><a class="self" href="/v5/cli/ethers/#sandbox-utility--examples"></a></div></div></h2>
<a name="cliex-init"></a><div class="code-title"><div>Creating New Wallets</div></div><div class="code">/home/ethers&gt; ethers init wallet.json
Creating a new JSON Wallet - wallet.json
Keep this password and file SAFE!! If lost or forgotten
it CANNOT be recovered, by ANYone, EVER.
Choose a password: ******
Confirm password: ******
Encrypting... 100%
New account address: 0x485bcC23ae2E5038ec7ec9b8DCB2A6A6291cC003
Saved: wallet.json
# If you are planning to try out the Ropsten testnet...
/home/ethers&gt; ethers --network ropsten fund 0x485bcC23ae2E5038ec7ec9b8DCB2A6A6291cC003
Transaction Hash: 0x8dc55b8f8dc8076acded97f9e3ed7d6162460c0221e2769806006b6d7d1156e0</div><a name="cliex-send"></a><div class="code-title"><div>Sending Ether and Tokens</div></div><div class="code"># Sending ether
/home/ricmoo&gt; ethers --account wallet.json send ricmoo.firefly.eth 0.123
Password (wallet.json): ******
Decrypting... 100%
Transaction:
To: 0x8ba1f109551bD432803012645Ac136ddd64DBA72
From: 0xaB7C8803962c0f2F5BBBe3FA8bf41cd82AA1923C
Value: 0.123 ether
Nonce: 96
Data: 0x
Gas Limit: 21000
Gas Price: 1.2 gwei
Chain ID: 1
Network: homestead
Send Transaction? (y/N/a) y
Response:
Hash: 0xc4adf8b379033d7ab679d199aa35e6ceee9a802ca5ab0656af067e911c4a589a
# Sending a token (SAI)
# NOTE: the contract address could be used instead but
# popular token contract addresses are also managed
# by ethers
/home/ricmoo&gt; ethers --account wallet.json send-token sai.tokens.ethers.eth ricmoo.firefly.eth 1.0
Sending Tokens:
To: 0x8ba1f109551bD432803012645Ac136ddd64DBA72
Token Contract: 0x89d24A6b4CcB1B6fAA2625fE562bDD9a23260359
Value: 1.0
Password (wallet.json): ******
Decrypting... 100%
Transaction:
To: 0x89d24A6b4CcB1B6fAA2625fE562bDD9a23260359
From: 0xaB7C8803962c0f2F5BBBe3FA8bf41cd82AA1923C
Value: 0.0 ether
Nonce: 95
Data: 0xa9059cbb0000000000000000000000008ba1f109551bd432803012645ac136ddd64dba720000000000000000000000000000000000000000000000000de0b6b3a7640000
Gas Limit: 37538
Gas Price: 1.0 gwei
Chain ID: 1
Network: homestead
Send Transaction? (y/N/a) y
Response:
Hash: 0xd609ecb7e3b5e8d36fd781dffceede3975ece6774b6322ea56cf1e4d0a17e3a1</div><a name="cliex-signing"></a><div class="code-title"><div>Signing Messages</div></div><div class="code">/home/ethers&gt; ethers --account wallet.json sign-message 'Hello World'
Password (wallet.json): ******
Decrypting... 100%
Message:
Message: "Hello World"
Message (hex): 0x48656c6c6f20576f726c64
Sign Message? (y/N/a) y
Signature
Flat: 0xca3f0b32a22a5ab97ca8be7e4a36b1e81d565c6822465d769f4faa4aa24539fb122ee5649c8a37c9f5fc8446593674159e3a7b039997cd6ee697a24b787b1a161b
r: 0xca3f0b32a22a5ab97ca8be7e4a36b1e81d565c6822465d769f4faa4aa24539fb
s: 0x122ee5649c8a37c9f5fc8446593674159e3a7b039997cd6ee697a24b787b1a16
vs: 0x122ee5649c8a37c9f5fc8446593674159e3a7b039997cd6ee697a24b787b1a16
v: 27
recid: 0</div><a name="cliex-scripting"></a><a name="sandbox-utility--examples--cliex-scripting"></a><h3 class="show-anchors"><div>Scripting<div class="anchors"><a class="self" href="/v5/cli/ethers/#cliex-scripting"></a></div></div></h3><p>The <code class="inline">eval</code> command can be used to execute simple one-line scripts from the command line to be passed into other commands or stored in script environment variables.</p>
<div class="code-title"><div>Get the formatted balance of an account</div></div><div class="code">/home/ethers&gt; ethers --network ropsten \
--account wallet.json \
eval \
'accounts[0].getBalance().then(b =&gt; formatEther(b))'
3.141592653589793238</div><div class="code-title"><div>Get the current block number</div></div><div class="code">/home/ethers&gt; ethers --network rinkeby \
eval "provider.getBlockNumber()"
5761009</div><div class="code-title"><div>Convert a Solidity signature to JSON</div></div><div class="code">/home/ethers&gt; ethers eval 'utils.Fragment.from(
"function balanceOf(address) view returns (uint)"
).format("json")' | json_pp
{
"inputs" : [
{
"type" : "address",
"name" : "owner"
}
],
"type" : "function",
"payble" : false,
"stateMutability" : "view",
"ouputs" : [
{
"type" : "uint256"
}
],
"name" : "balanceOf",
"constant" : true
}</div><div class="code-title"><div>Compute a topic hash</div></div><div class="code">/home/ricmoo&gt; ethers eval 'id("Transfer(address,address,uint256")'
0xd99659a21de82e379975ce8df556f939a4ccb95e92144f38bb0dd35730ffcdd5</div><div class="code-title"><div>Create a random mnemonic</div></div><div class="code">/home/ricmoo&gt; ethers eval 'Wallet.createRandom().mnemonic'
useful pond inch knock ritual matrix giggle attend dilemma convince coach amazing</div><a name="cliex-mnemonicpassword"></a><a name="sandbox-utility--examples--cliex-mnemonicpassword"></a><h3 class="show-anchors"><div>Using Mnemonics (with a password)<div class="anchors"><a class="self" href="/v5/cli/ethers/#cliex-mnemonicpassword"></a></div></div></h3><p>All mnemonic phrases have a password, but the default is to use the empty string (i.e. <code class="inline">""</code>) as the password. If you have a password on your mnemonic, the <code class="inline">--mnemonic-password</code> will prompt for the password to use to decrypt the account.</p>
<div class="code">/home/ricmoo&gt; ethers --account mnemonic.txt --mnemonic-password
Password (mnemonic): ******
network: homestead (chainId: 1)
homestead&gt; accounts[0].getAddress()
&lt;Promise id=0 resolved&gt;
'0x6d3F723EC1B73141AA4aC248c3ab34A5a1DAD776'
homestead&gt;</div><a name="cliex-mnemonicpassword-xxx"></a><a name="sandbox-utility--examples--cliex-mnemonicpassword-xxx"></a><h3 class="show-anchors"><div>Using Mnemonics (with experimental memory-hard passwords)<div class="anchors"><a class="self" href="/v5/cli/ethers/#cliex-mnemonicpassword-xxx"></a></div></div></h3><p>The <code class="inline">--xxx-mnemonic-password</code> is similar to the <code class="inline">--mnemonic-password</code> options, which uses a password to decrypt the account for a mnemonic, however it passes the password through the <a href="https://en.wikipedia.org/wiki/Scrypt">scrypt</a> <i>password-based key derivation function</i> first, which is intentionally slow and makes a brute-force attack far more difficult.</p>
<div class="code">/home/ricmoo&gt; ethers --account mnemonic.txt --xxx-mnemonic-password
Password (mnemonic; experimental - hard): ******
Decrypting... 100%
network: homestead (chainId: 1)
homestead&gt; accounts[0].getAddress()
&lt;Promise id=0 resolved&gt;
'0x56FC8792cC17971C19bEC4Ced978beEA44711EeD'
homestead&gt;</div><div class="definition container-box warning"><div class="term">Note</div><div class="body"><p>This is still an experimental feature (hence the <code class="inline">xxx</code>).</p>
</div></div>
<div class="footer">
<div class="nav previous"><a href="/v5/cli/"><span class="arrow">&larr;</span>Command Line Interfaces</a></div>
<div class="nav next"><a href="/v5/cli/asm/">Assembler<span class="arrow">&rarr;</span></a></div>
</div>
<div class="copyright">The content of this site is licensed under the <a href="https://choosealicense.com/licenses/cc-by-4.0/">Creative Commons License</a>. Generated on February 8, 2021, 3:25pm.</div>
</div>
<script src="/v5/static/script.js" type="text/javascript"></script>
<!--EXTRASCRIPT-->
</body>
</html>