Creating Ethereum Smart Contracts via Command Line

An interactive command line alternative for your favorite Smart Contract development tool (like Truffle!)

Gaurav Agrawal
May 16, 2019 · 5 min read

In last week’s article, we talked about how to interact with an already-deployed Smart Contract using . This week we are going to create a Smart Contract using !

not only allows you to create Smart Contracts, but also helps you manage your web3 projects (like one would with Truffle), though ’s rich command line tool. (it just makes your life easier as a web3 developer!)

If you just stumbled upon this article, we strongly recommend you check the previous part of this series.

Part 1- sbt-ethereum — A Tool for Interacting with the Ethereum Blockchain via Command Line

Part 2- Interact with Smart Contracts using the Command Line

Before diving in, we need to install a few things (if you don’t have them already):

  • Install Java (version ≥ 8 )
  • Install Git
  • Install Maven (Maven is dependency management for Java ecosystem; what is npm in nodejs, maven is for Java ecosystem)

Once installed we are ready to dive in 👉

In previous tutorials, we’ve run by executing the wrapper script within the repository. This time, we need to create our own repository.

To get project’s template, we will use giter8. Giter8 is a command line tool to generate files and directories from templates published on Github or any other git repository. Giter8 helps us by scaffolding projects based on a pre-defined template.

Use this command to create a project repository:

sbt new swaldman/solidity-seed.g8

As is an interactive build tool, this command will ask a few questions:

[info] Loading project definition from /Users/testuser/project
[info] Set current project to testuser (in build file:/Users/testuser/)

A minimal solidity project for sbt-ethereum

name [my-solidity-project]: eth-timelock
version [0.0.1-SNAPSHOT]:
sbt_ethereum_version [0.1.7-SNAPSHOT]:
sbt_version [1.2.8]:

Template applied in ./eth-timelock

This will create a new directory (see above, we have given as a project name). This is how the directory structure looks like:

Image for post
Image for post

Now we are ready to create our first contract. Remember, create all contracts under .

We are going to create contract. It accepts payment of when the contract is created, and only allows that to be withdrawn by the creator of the contract after a specified delay.

Create a file under

Timelock.sol

Now let's compile our project.

First, run inside your eth-timelock directory:

$ sbt
[info] Loading settings for project eth-timelock from build.sbt ...
[info] Set current project to eth-timelock (in build file:/Users/testuser/eth-timelock/)
[info] Updating available solidity compiler set.
[info] sbt-ethereum-0.1.7-SNAPSHOT successfully initialized (built Sun, 17 Feb 2019 21:58:11 -0800)

Next run :

sbt:eth-timelock> compile
[info] Compiling 1 Solidity source to /Users/testuser/eth-timelock/target/ethereum/solidity...
[info] Compiling 'Timelock.sol'. (Debug source: '/Users/testuser/eth-timelock/target/ethereum/solidity/Timelock.sol')
[info] No Scala stubs will be generated as the setting 'ethcfgScalaStubsPackage' has not ben set.
[info] If you'd like Scala stubs to be generated, please define 'ethcfgScalaStubsPackage'.
[info] Updating ...
[info] Done updating.
[success] Total time: 1 s, completed Feb 25, 2019 12:16:01 AM

If you see any warning, just ignore it. You can see ABI get created under .

Now we are ready to deploy our Smart Contract. But before that, if you want to switch the network, you can like this:

First, check your network with . has MainNet set by default. Let’s change it to Ropsten (if you want some other testnet, feel free to do so too).

ethNodeChainIdOverrideSet 3

Here, 3 is the Ropsten network id. Remember, you should have a bit of ETH in your account to deploy the contract.

Now let’s start deploying:

sbt:eth-timelock> ethTransactionDeploy Timelock <TAB>

Always use Tab, has very rich Tab support for command completion. When you press Tab, it will tell you what to do next.

Our Smart Contract will need two parameters:

— The number of days when the owner will be able to withdraw Ether.

— Number of seconds when the owner will be able to withdraw Ether.

We’ll need the Smart Contract’s compute time using days + seconds, so let’s pass 0 days and 300 seconds (5 minutes):

ethTransactionDeploy Timelock 0 300 <TAB>

Now we need to pass the Ether amount, which will lock the contract for the given time. You can pass ETH amount using , , , , or units.

Let’s lock 10 for 5 minutes (300 seconds):

ethTransactionDeploy Timelock 0 300 10 wei

This will ask you to unlock your wallet using your passphrase and then ask you to submit the transaction. It will also ask to provide an alias name for your deployed contract.

Once done, it will give you deployed contract address which you can see on Etherscan. Let check the balance of the contract using:

ethAddressBalance timelock

Here is alias name which we used while deploying our contract.

Interacting with your Smart Contract

Now, before 5 minutes runs out, let’s mock-test our Smart Contract using . This simulates a transaction on our Ethereum Node, but does not actually execute the transaction. So let’s try that:

sbt:eth-timelock> ethTransactionMock timelock withdraw

If you execute this within 5 minutes of deploying the Smart Contract, you will see an error. Execute the above command after 5 minutes and you will see a success message, but it does not run the command actually.

Now let’s actually withdraw our 10 from the contract after 5 minutes using:

ethTransactionInvoke timelock withdraw

This will create a transaction and withdraw the locked 10 . Once this command succeeds, you can check the balance of the contract again:

ethAddressBalance timelock

Conclusion

That’s it for today! Now you have another way to #buidl and deploy Smart Contracts. Let us know if you liked the article in the comment section and if you want to see more on this (or similar) topic 🙏

About QuikNode

QuikNode is building infrastructure to support the future of Web3. Since 2017, we’ve worked with hundreds of developers & companies, helping scale dApps and providing high-performance Ethereum nodes. We’re working on something interesting from the past few months and will be launching soon, so subscribe our newsletter for more updates!! 😃

QuikNode

Cloud-hosted Ethereum nodes for dApps, developers, and…

Gaurav Agrawal

Written by

Editor — Coinmonks publication (medium.com/coinmonks) and working on (https://coincodecap.com)

QuikNode

QuikNode

Cloud-hosted Ethereum nodes for dApps, developers, and power-users. Run a dedicated ETH node in minutes!

Gaurav Agrawal

Written by

Editor — Coinmonks publication (medium.com/coinmonks) and working on (https://coincodecap.com)

QuikNode

QuikNode

Cloud-hosted Ethereum nodes for dApps, developers, and power-users. Run a dedicated ETH node in minutes!

Medium is an open platform where 170 million readers come to find insightful and dynamic thinking. Here, expert and undiscovered voices alike dive into the heart of any topic and bring new ideas to the surface. Learn more

Follow the writers, publications, and topics that matter to you, and you’ll see them on your homepage and in your inbox. Explore

If you have a story to tell, knowledge to share, or a perspective to offer — welcome home. It’s easy and free to post your thinking on any topic. Write on Medium

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store