Using eth-cli to Interact with Contracts
eth-cli enables users to interact with contracts in any network regardless of how they were initially deployed.
Installing eth-cli
eth-cli is a collection of commands that help to develop Dapps for Ethereum and interact with contracts deployed in any blockchain network.
To install eth-cli, we need to run npm install -g eth-cli
. This will expose the eth
command that starts an interaction with eth-cli.
By running eth
, you will see the list of available commands, with the most useful ones listed below:
eth randomAdress
generates a random address and a private key pair. Optionally, you can pass anint
argument to generate more than one pair.eth contract:deploy
allows you to deploy a contract in any blockchain network. It needs the compiled version of the(.bin)
contract and a private key.eth networks
shows information about the most known networks.eth repl
is the most important one. This command is invoked with a contract’s address and ABI. It will provide you a web3.js contract instance that you can interact with.
Interacting with a deployed contract
eth-cli is a fantastic tool for interacting with any deployed contract in any blockchain network. You will only need the contract’s ABI version and its address.
Each time you deploy a contract, you need to save the address where it is deployed. Almost every existing tool for deploying contracts provides you with the address of the contract after it is created.
Let’s interact with a contract deployed on Ropsten. Its functionality is very simple. It will persist a phrase for any sender. For this example, we will deploy the contract and ABI from this Gist.
First, let’s run the command below. Note that ./MyPhrase.abi
refers to the path of the contract’s ABI, and @address
indicates where the contract is deployed.
eth repl --ropsten ./MyPhrase.abi@0x46C637aaF2aFfb2661561DBbDfCa4EA5C56ecfAB
Now, we have entered the repl
mode. Let’s continue by running the command below.
myPhrase.methods.phrases(“0xDe75665F3BE46D696e5579628fA17b662e6fC04e”).call()
myPhrase
is a web3.eth.Contract
instance of our deployed contract. It will return the eth-cli just works
string. Great! We have interacted with a deployed contract in a really simple way.
Now, we can send a transaction to the setPhrase
method to persist in our own phrase. To do so, we need to exit from the current repl
mode using Ctrl+d. Now, we can enter into repl
again, but this time unlocking an account.
This time, we’ll change the contract state. This is why we need to unlock an account to prove that we are the owner, if not, anyone could act on our behalf and change the contract state from an address they do not own. In order to send the transaction, the account related to the private key provided to repl
must have ether that will be used to pay for the transaction.
Remember that we can get an account and its private key by running the eth
randomAddress
command. Then, we have to send ether to that account.
If you want to try this example in Ropsten, you can charge your account through the Ropsten Ethereum Faucet. Enter the newly created address to charge your account.
eth repl --ropsten --pk 0x3480AD3FB5FD2BA5DDAFC274B8E2A8CF655FB17C6956FCD2E89C2E2F68456C37 ./MyPhrase.abi@0x46C637aaF2aFfb2661561DBbDfCa4EA5C56ecfAB
Note that we need to specify the private key starting with 0x
. Now, we can send our transaction.
myPhrase.methods.setPhrase(“testing eth-cli”).send({from: “0x1ed028B94a378ad0DdB2d19ba2091A11aBE3E275”, gas: 55000}).then(console.log)
(Note: The process may take a while.)
In the from
field, we are sending the address that belongs to the private key.
When it’s time to interact with a deployed contract, we can modify the contract state or query data from it. The first example we did was to get the phrase for a specific address. The last example was to modify the contract state by setting the phrase for a different address. This is why we need to provide a private key.
Once the transaction is mined, you can call the phrases
method again with the same address to see that our phrase was persisted.
Note that we have full access to web3.js while in therepl
mode. In this case, we can run the following command.
web3.eth.getAccounts().then(console.log)
Deploying a contract
Next, let’s take a look at how to create a simple smart contract and deploy it using eth-cli. First, we need to have contract.sol
and the compiled version of it.
To compile a contract, we can use solc-js (if you don’t have this, you can install it with npm install -g solc
) and then run solcjs --bin --abi SomeContract.sol
. This creates two files, one with the ABI version and another with the bin version of our contract.
Now that we have the .bin version, we can run the command below.
eth contract:deploy --ropsten privateKey ./MyToken.bin
--ropsten flag
indicates that we want to deploy our contract in the Ropsten testnet. You can use the network that you want by passing the --url=http://localhost:9545
flag.
Ropsten is a testnet for development purposes. It works like a mainnet, but with some differences. Basically, you can get ether for free, and blocks are mined much faster than in the mainnet network.
Remember that eth-cli has configured several networks. Run eth networks to take a look at them. If you don’t pass a URL or a network flag, it will use http://localhost:8545
by default. If everything goes well, the output will show the address of the deployed contract.
Interacting with standard contracts
eth-cli comes with ABIs for the ERC20 and ERC721 standards, which means that if we want to interact with a contract, and we know that the contract inherits from ERC20, ERC721, or both, we can interact with the contract directly without having to provide the contract’s ABI. However, you will only be able to use the methods from the ERC standard.
Let’s interact with a contract already deployed in Ropsten that implements ERC20.
eth repl --ropsten erc20@0x0d8c501779f81494f32da6f8c2eaf08c1b8fb5b3
erc20.methods.balanceOf(“0x771c55f3aaa54facb99afd0cfe0ae6c437134115”).call()
It will return 1,000 because that was the amount configured by the contract
constructor. As you can see, we can call the contact method using the balanceof
standard method from ERC20 and repl
responds correctly.
eth-cli is a flexible and easy-to-use tool. It can be applied to any contract on any blockchain network regardless of how the contract was initially deployed. The solution is under continuous development to improve the existing and add new functionality. Anyone interested in trying out eth-cli can track the project’s evolution in its GitHub repo.
About the authors
Nicolás Dominguez is Senior Software Engineer at Protofire. With 9+ years in IT, he is now focusing on blockchain-based development with a special interest in applying GraphQL to build production-grade Dapps. Nicolás has experience of working as a full-stack JavaScript developer with a profound knowledge of React and Node.js. He is also enthusiastic about machine learning.
Manuel Garcia is CTO at Protofire, where he is responsible for helping token-based startups with protocol and smart contract engineering. He has spent the last 15+ years working at the intersection of business and software innovation. Manuel is a certified Scrum Master and Agile practices advocate and was involved in all stages of creating software products from project kick-off to deployment and maintenance. His experience spans across software development, project management, and product management.