Skip to content

saccharide/playing-with-truffle

Repository files navigation

Notes

  • Testing and developing smart contract with truffle, written with solidity
  • truffle is written in NodeJS
  • To interact with smart contract, we use web3 to interface with it.
  • Compile and Deploy a contract: truffle migrate

Work flow of deploying a smart contract

  1. truffle init . to create an empty truffle project in the current directory
  2. All smart contracts are inside contracts/ directory
  3. migrations/ we can deploy our smart contract by creating a javascript file 2_simple_contract. 4. test/ are test file for smart contract, this can be in js or solidity, 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

  1. create 2_simple_smart_contract.js under migration/ 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);
}
  1. 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.
  2. migrate command does the following two things: (can use the --reset flag to clear the build/)

    • First, it will compile our smart contract inside contract/, and then create a build/ and put the contract artifact inside this directory.
    • Second, it will deploy our smart contract by running all the migration files in our migrations/
  3. After running migrate, we can go to build/contracts/ to see the artifacts of our smart contract. This will contain ABI, address of our contract. (and some other metadata). ABI is basically a set of functions that can interact with the outside world in JSON format

Front End

  1. Install static-sever, which is a npm package: sudo npm install -g static-server
  2. create a public/ to be our front end of our DAPP
    • Can run the static server with static-server public/, or with npm init, and edited the script field to include automatically start the static -server
    • "start": "static-server public"
  3. index.html is the default front end page of our front end
  4. web3.js is the javascript library that is used to interact with our smart contract.
  5. bundle.js will be our javascript code for our personalized front end.
  6. 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);
  1. Now, we can interact with it from the URL hosted by static-sever

About

No description, website, or topics provided.

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

No releases published

Packages

 
 
 

Contributors