How to Explore Your Smart Contract from the Command Prompt

How to Explore Your Smart Contract from the Command Prompt

Part 2 of 3. Another blockchain development tool in your toolbox

Hi, it's me again. Let's move on with our first decentralized application. In this article, I want to show you how you can explore your contract in command prompt. It is important for you to understand it so you can also understand what is going on in the test we wrote in the first part of this series.

Part 1: How to Write Your First Dapp in 10mins.

win+r type cmd and hit enter. In your command line, cd project\path\, and on the project path type truffle develop.

You should see a series of strings as in our cover photo.

Let's Explore Our Contract

For your notice, async is provided for us in the console, which means we can use await to get resolved data from our contract and its properties. Contracts methods run asynchronously.

In your truffle console type contract = await EMeet.deployed().

If you have not compiled and migrated your files, type compile and hit enter in the console. After that type migrate and hit enter before proceeding with the former command.

To view what is in the contract variable, type contract in the console.

image.png

Scroll through to see more information on your contract. What you are seeing are a declaration of your contract and its properties. You can tell from looking closely that the methods we used in the test exist, check under abi.

image.png

We can also call these methods in our console and use them as we would anywhere else.

Let's Play with Some Methods

Get Contract Address

address = await contract.address

Create a post

await contract.createPost("I am excited").

The output is different because we are writing to the blockchain and that costs some gas.

image.png

Every computation on the blockchain has a cost and this page explains it well.

View Post

post = await contract.posts(1)

To view content

post.content

To view author (address)

post.author

Let's Comment on this Post

await contract.commentOnPost(1, "This post is informative.")

View Comment

comment = await contract.comments(1)

Get Comment Number

commentNumber = await contract.commentCount()

Go on and explore the comment properties.

Let's Tip the Post

To tip the post, we have to get the accounts

let accounts = await web3.eth.getAccounts()

Tipping the post

contract.tipPost(1, {from:accounts[1], value:web3.utils.toWei("1","Ether")})

You should see the amount of gas used and the cost.

Now, using your new knowledge, get the tip amount on your post and see if it has changed.

Thank you for stopping by!

Next is using React.js to implement the user interface instead of the command prompt. This will make you a full-stack blockchain developer Read part 3 here