Part 2 — Deploy contract to local Blockchain

Part 2 — Deploy contract to local Blockchain

beCWnx - Part 2 — Deploy contract to local Blockchain

After deployed the smart contract to Ganache, you can interact with it using Truffle Console. To start, issue the command below:

By default, Ganache will create 10 test accounts, each of them has 100ETH. The command truffle migrate above will deploy the contract to Ganache test net on behalf of one of these 10 accounts (default is the first one). You can see transaction details for this creation (Sender, Created Contract Address, Gas Used, Gas Price, Gas Limit, Mined In Block, etc.) on Ganache as well.

Let’s play a bit with the console. Firstly, we need to get the instance SimpleStorage that we have just deployed. The instance is required to call contract’s method later on:

let instance = await SimpleStorage.deployed()

Issue below command and see details info of the instance object:

instance

The console will list out all properties of the smart contract. There are 2 most important info: abi and address. abi is a prototype of the contract which defines its methods and arguments. Others can follow this prototype to call methods of the contract. address is public address of this contract on the network. Knowing addressand abi of a contract is a must for other users to interact with the contract.

abi:
[ ,
,
,
,
,
],
address: '0x8e360d2e0d794e87a8c876051DD9d3D03ca38edA',

Another way to instantiate the contract is calling web3.eth.Contract with abi and address:

let instance = new web3.eth.Contract(abi, address)

Now we can call methods of SimpleStorage via this instance. You can try below command in your truffle console. Notice that the value is stored as hexadecimal, we will need to convert it to decimal to receive the exactly value we have set.

instance.get.call()
instance.set(9999)
instance.get.call() // Output: <BN: 270f>
instance.get.call().then(value => console.log(parseInt(value, 10))) // Output: 9999

In this article, we have simulate an Ethereum network in our local PC, deploy a smart contract on it, and try to call some methods using web3 library. In next article, I will show you how to deploy the contract to RinkeBy, a popular test net of Ethereum.

Published at Tue, 29 Oct 2019 08:47:33 +0000

{flickr|100|campaign}

Spread the word. Share this post!

Leave a comment