- Testing and developing smart contract with
truffle, written withsolidity truffleis written inNodeJS- To interact with smart contract, we use
web3to interface with it. - Compile and Deploy a contract:
truffle migrate
truffle init .to create an empty truffle project in the current directory- All smart contracts are inside
contracts/directory migrations/we can deploy our smart contract by creating a javascript file2_simple_contract. 4.test/are test file for smart contract, this can be injsorsolidity, sample test code:
const SimpleSmartContract = artifacts.require('SimpleSmartContract');
// Each contract block is a different smart contract object and will be independent from another object
contract('SimpleSmartContract', () => {
it('Description of the first test: Deployment', async () => {
/*
Create an contract instance, waiting for the returned promised,
the artifact object has a `deployed()` method that will return you the deployed
instance of the smart contract.
*/
const simpleSmartContract = await SimpleSmartContract.deployed();
console.log(SimpleSmartContract.address);
// Need an assertion for each test, check if our address is not empty
assert(simpleSmartContract.address != '');
});
});- We can run the test file with
truffle test
THIS IS A MUST HAVE FILE IN ORDER TO INTERACT WITH CONTRACT
- create
2_simple_smart_contract.jsundermigration/to migrate your smart contract.
/*
Artifact is an object that is injected by `truffle`, it represents all the compiled
smart contract that are in the `build/` compiled by truffle, with this artifacts object
we can pass in the name of a smart contract and return a contract artifact.
*/
const SimpleSmartContract = artifacts.require('SimpleSmartContract');
/*
Each migration needs to export a function, and this function will be given a deployer object,
and this delpyer object will have a deploy method and takes in a contract artifacts created above.
*/
module.exports = function(deployer) {
deployer.deploy(SimpleSmartContract);
}-
Then we can run
truffle develop, which will do two things.- First, it will start the local development blockchain using the library
ganache, and generate 10 ethereum addresses - Second, it will start a console that allows you to interact with the blockchain.
- First, it will start the local development blockchain using the library
-
migratecommand does the following two things: (can use the--resetflag to clear thebuild/)- First, it will compile our smart contract inside
contract/, and then create abuild/and put the contract artifact inside this directory. - Second, it will deploy our smart contract by running all the migration files in our
migrations/
- First, it will compile our smart contract inside
-
After running
migrate, we can go tobuild/contracts/to see the artifacts of our smart contract. This will containABI,addressof our contract. (and some other metadata).ABIis basically a set of functions that can interact with the outside world in JSON format
- Install
static-sever, which is anpmpackage:sudo npm install -g static-server - create a
public/to be our front end of our DAPP- Can run the static server with
static-server public/, or withnpm init, and edited thescriptfield to include automatically start thestatic -server "start": "static-server public"
- Can run the static server with
index.htmlis the default front end page of our front endweb3.jsis the javascript library that is used to interact with our smart contract.bundle.jswill be our javascript code for our personalized front end.- Inside
bundle.js, we can interact with the backend as follows:
// First grab the corresponding `ABI` from the `build/contracts/`
var contractABI = [];
// Contract address can be found in the truffle console after running `migrate --reset`
var contractAddress = 0x0;
// Make sure to load web3 before this javascript file
// Create a Web3 instance, it takes in an URL to our local blockchain development instance
// started by `ganache`, can be found in truffle console
var web3 = new Webs('http://localhost:9545');
// Create an instance so that we can talk to our smart contract, we can use web3 to do that,
// it first takes contractABI, and then the contract address.
var simpleSmartContract = new web3.eth.Contract(contractABI, contractAddress);
// Simple log to see our contract
console.log(simpleSmartContract);
// We can also log all the accounts that are generated by the local development block,
// we can do this with web3.eth.getAccounts(), this method returns a promise,
web3.eth.getAccounts()
.then(console.log);- Now, we can interact with it from the URL hosted by
static-sever