Now we will deploy the Counter smart contract to our CLV local dev node.
First make sure CLV is started using below command:
./clover--dev--alice
In the following steps we assume you're using the local CLV node. You may need to adjust some parameters if you're connecting to other CLV nodes.
Once CLV is started, you could see it's producing blocks every 6 seconds.
hello.sh
# Ain't no code for that yet, sorryecho'You got to trust me on this, I saved the world'
Sometimes the local node stop producing blocks and reports the error "unexpected era change", it normally happens in dev node. To fix this problem, the simplest method is purge the chain using below command and start over.
./cloverpurge-chain--dev--alice
Setup the private key provider
We need to access our local node using the key keys preimported in the dev chain. We need setup a custom private key provider for truffle. Install required packages using:
$yarnaddweb3-provider-engineethereumjs-wallet
Create private-provider.js and set the content as below:
Here we imported the private-provider package and added the network section in the configuration. We defined the development network by using the pkProvider which uses the private key and connects to the local clover rpc port.
The private key is defined in the dev chain and has enough CLV for testing. It's important to use the private-provider.js here because we need add some customization to make truffle pass the correct information to the clover chain.
Clover node uses the 1337 network id and it supports a higher gas limit. We specified the gas price in the configuration. because clover limits the gas price to be at least 1 gwei .
Write the migration
Migrations are used to do stuff like contract migration and initialization scripts. We'll just write a simple migration to deploy the Counter contract.
First create the migrations folder
$ mkdir migrations
Add the 1_deploy_counter.js file, set the content to:
We wrote a standard truffle migration which load the Counter contract and deploy it to the chain in the migration function. Checkout truffle migration document for more details.
Deploy the counter contract
It's time to deploy counter using below command!
$ truffle deploy --network development
You should get some output as below:
truffle deploy --network developmentCompiling your contracts...===========================> Everything is up to date, there is nothing to compile.Starting migrations...======================> Network name:'development'> Network id:1337> Block gas limit:0 (0x0)1_deploy_counter.js=================== Deploying 'Counter'-------------------> transaction hash:0x18eacdeebbd40fec104a3de505e9c1065c89b21ff68664b722834cea9a6f0ea9> Blocks:0 Seconds:0> contract address:0xeB1c50679f8fe33542C44a4A6D779Fb47886E27f> block number:5> block timestamp:1609225848> account:0xAEd40f2261ba43b4dFFE484265ce82D8fFE2B4DB> balance:9999999.99813969> gas used:186031 (0x2d6af)> gas price:10 gwei> value sent:0ETH> total cost:0.00186031ETH Pausing for 2 confirmations...------------------------------> confirmation number:1 (block:6) > confirmation number: 2 (block:7) > Saving artifacts ------------------------------------- > Total cost: 0.00186031 ETHSummary=======> Total deployments: 1> Final cost: 0.00186031 ETH
It says that the counter contract was deployed successfully to the network and the deployed contract address is 0xeB1c50679f8fe33542C44a4A6D779Fb47886E27f . It also include the transaction details like the gas price, gas used and total cost in the output.
Interact with the Counter contract in the console
We can interact with the counter contract instance using the truffle console from cli. Start truffle console using:
In above commands we can use read the state current_value using instance.current_value() And we use the instance.inc() method to send a transaction which results the current value was increased by 1.
The result of the instance.inc() call was the transaction details which includes the block, block hash, events and fee information.
The source code of this chapter could be found at the revision 3e76c43d3 in the counter-dapp source repo.