0% found this document useful (0 votes)
39 views

Truffle Notes

Uploaded by

DHRUV JAIN
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
39 views

Truffle Notes

Uploaded by

DHRUV JAIN
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

Truffle is an IDE for creating and testing DApps

Prepared by: P. Raghu Vamsi, JIIT, Noida.


------------------------------------------------------
Note: Make sure that active Internet connection is avaiable
------------------------------------------------------

Step 1: open a command prompt and create a folder ballot

$ mkdir ballot
$ cd ballot
ballot1$ truffle init

it generates some folders and files, which can be seen by typing ls command.

- contracts - solidity source files


- migrations - for contract deployment, keeps track of changes
- tests - contains solidity tests and js files
- truffle-config.js - truffle configuration file, IP, networkid, port etc.
- truffle.js

Step 2: copy the BallotV3.sol file to contracts subfolder generated in ballot

Step 3: Compile the ballot project using truffle compile

ballot1$ truffle compile

it creates a folder build -> contracts, in which json files are created.

Question: What you observed in the json file?

Step 4: Place the the following content in the truffle.js against appropriate fields

module.exports = {
networks: {
development: {
host: "localhost",
port: 9545, // RPC port alloted by truffle
network_id: "*" // match any network id
}
}
};

Step 5: add a file to the migrations directory to deploy our smart contract

create a file 2_deploy_contracts.js and type the following

var MyContract = artifacts.require("BallotV3");

module.exports = function(deployer) {
// deployment steps
deployer.deploy(MyContract);
};

Step 6: use trffule development to make project ready for deployment


ballot1$ truffle develop

It creates 1) public and private keys for 10 accounts, 2) keywords to recover the
accounts, and 3) http binding with 9545 port

and then shows the following prompt on commandline

truffle(develop)>

Step 7: Open a new termanal and go to ballot project folder and enter the following
command to migrate the smartcontract on to the blockchain network

ballot1$ truffle migrate --reset

in the above command --reset is used to overwrite already loaded smartcontracts on


the chain. It is used only in the development stage, however it is
not possible once smartcontract deployed on main chain.

Step 8: Then what can we do with the truffle console appeared in Step 6?
It provides a command line interface to peform operations on the accounts created
on the test chain and
also to access to the management APIs such as web3, eth, etc.

Let us try to transfer ethers from one account to other


(visit https://web3js.readthedocs.io/en/v1.2.1/ for documentation)

truffle(develop)>
web3.eth.sendTransaction({from:web3.eth.coinbase,to:web3.eth.accounts[1],value:99})
'0x1ca2e3a204a52333db052fe88ddaefef0432ce97f78e3b32898d2ccaa53f26f9'

truffle(develop)> web3.eth.getBalance(web3.eth.coinbase)
BigNumber { s: 1, e: 19, c: [ 999272, 87899999978901 ] }

truffle(develop)> web3.eth.getBalance(web3.eth.coinbase).toNumber()
99927287899999980000

truffle(develop)> web3.eth.getBalance(web3.eth.accounts[1]).toNumber()
100000000000000000000

truffle(develop)> web3.eth.getBalance(web3.eth.accounts[0]).toNumber()
99927287899999980000

truffle(develop)> web3.eth.getBalance(web3.eth.accounts[9]).toNumber()
100000000000000000000

truffle(develop)>
web3.eth.sendTransaction({from:web3.eth.accounts[2],to:web3.eth.accounts[1],value:99})
'0xf8fd40f9766a19aa490579181f855426aa22dad8e913c194fd22f60e8c6a3577'

truffle(develop)> web3.eth.getBalance(web3.eth.accounts[2]).toNumber()
99999999999999980000

You might also like