区块链

Getting started with Solidity de

2018-11-22  本文已影响26人  印随2018
ethereum

ref: https://medium.com/coinmonks/getting-started-with-solidity-development-using-truffle-2cc6c1df9133

Prerequisites

Installation

Walkthrough

Installation

  1. We need to install the Truffle package first sudo npm install -g truffle
  2. Verify that it has been downloaded successfully truffle
  3. We also need to install TestRPC sudo npm install -g ethereumjs-testrpc
  4. Verify that it has been downloaded successfully testrpc
  5. Setup to initialize a default truffle project, navigate to the desired directory and run truffle init

To configure truffle to use our testRPC client, populate truffle.js with:

module.exports = {
  networks: {
    development: {
      host: "localhost",
      port: 8545,
      network_id: "*", // Match any network id
      gas: 4600000
    }
  }
};

Contract

Let’s write our first contract. Create a new file in the contracts directory called SimpleStorage.sol

Populate SimpleStorage.sol with:

pragma solidity ^0.4.24;

contract SimpleStorage {
  mapping(address => uint256) public favoriteNumbers;

  function setFavorite(uint x) public {
    favoriteNumbers[msg.sender] = x;
  }
}

Let’s compile our contract. Run truffle compile in our truffle project's root directory. You should receive some helpful warnings. You should also be notified that a new artifact has been written.

Deploy

We now have to configure truffle to deploy this artifact to our network. Create a new file in the migrations directory called 2_deploy_contracts.js.

Populate 2_deploy_contracts.js with:

var SimpleStorage = artifacts.require("./SimpleStorage.sol");

module.exports = function(deployer) {
    deployer.deploy(SimpleStorage);
};

Now if we run truffle deploy we should see our migration script running, and eventually truffle will tell us the address that the contract account has been created at.

Tests

What can we do with our contract now? We should test it before we consider deploying it permanently (on a live ethereum network)!

Let’s write our first tests. Create a new file in the test directory called simpleStorage.js.

Populate simpleStorage.js with:

const SimpleStorage = artifacts.require("SimpleStorage") 

contract('SimpleStorage', (accounts) => {
    const [bob, alice] = accounts
    it("should verify bob and alice's favorite numbers default to 0", async () => {
        const ssContract = await SimpleStorage.deployed()
        const bobNum = await ssContract.favoriteNumbers.call(bob)
        assert.equal(bobNum, 0,
            "bob's default value was non-zero")
        
        const aliceNum = await ssContract.favoriteNumbers.call(alice)
        assert.equal(aliceNum, 0,
            "alice's default value was non-zero")
    })

    it("should set bob's favorite number to 33", async () => {
        const ssContract = await SimpleStorage.deployed()
        ssContract.setFavorite(33, {from: bob})
        const newBobNum = await ssContract.favoriteNumbers.call(bob)
        assert.equal(newBobNum, 30,
            "bob's new value was not 33")
    })
})

Before running our tests, we need to make sure our Ethereum client is running. Open up another terminal window and run testrpc if you haven't already.

Now go back to your truffle project and run truffle test. One of our tests should fail. Find the error and fix it.

Troubleshooting

If you encounter problems I have GitHub repository with a working version of this guide. In your terminal run:

git clone https://github.com/dangerousfood/hello_contract.git

If truffle compile or truffle test fails in the newly cloned folder then there may be an environmental issue. Ensure that NodeJS and NPM are installed correctly.

Further Links

上一篇下一篇

猜你喜欢

热点阅读