Place under win7 Ethernet-based truffle + ganache development environment to build and deploy intelligent call contracts

Previous introduce is based on Ethernet Square geth + remix-ide intelligent environment contract to build and deploy run, this introduction is based on the truffle + ganache.

ganache equivalent geth graphical user interface, relative to pure geth simple instruction operation approachable, and transactions generated during operation and blocks glance.

[Preparation]

1.Node.js installation (this is mentioned in the previous article, so here not to show)

2. Enter the command:

install npm - G SOLC (installation of smart contract) 

npm install -g ganache- cli (installation ganache develop end) 

npm install -g truffle (truffle installation frame)

3.vscode operating environment

 

[Intelligent] contract

1. New Smart Contract

① create a new folder eg: helloworld, and into the folder for the instruction operation:

cd helloworld

② create a truffle project:

truffle init

 

 ③ the new folder helloworldzheng overall vscode moved in:

 

 ④ contracts file in the helloworld folder to create a new contract, named helloworld.sol, and enter the following code:

pragma solidity ^0.5.2;

contract helloworld{
    function say() public pure returns(string memory){
        return "hello world";
    }
}

⑤ migrations file in the helloworld folder midpoint open 1_initial_migrations.js file and one of the "Migrations" parameter shall be changed helloworld:

const helloworld = artifacts.require("./helloworld.sol");

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

⑥ back truffle operating environment, the above-mentioned documents compiled:

Note that must enter into contracts folder compiled, otherwise an error will not find the corresponding contracts.

 

 Compile successfully.

2. Connect ganache

① run ganache

 

 ② intelligent connection contract with the ganache:

Back vscode, open truffle-config.js file:

Enter the following modules in module-exports:

 

 After we returned ganache, open the settings, click on the server, see the following operating surface, where the image above host, port, network_id parameters are filled in accordance with the corresponding parameters ganache:

 

After ③ ready to be deployed contract:

Back truffle operations, command input:

truffle migrate

If you follow the above operation step by step, so we can see the successful deployment of block under ganache adds:

 

 ④ Then you can start trying to call the contract:

Back truffle operation instruction, the following instruction enters the input console:

truffle console

 

 If you successfully enter the console which can be called the contract functions:

Enter the command:

helloworld.deployed().then(instance=>contract=instance)

The console pre-loaded library truffle-contract, the contract can be a function to operate.

The meaning of the command is to obtain helloworld contract instance is stored, and stores it for later use variable contract.

Then we can call helloworld defined in the contract say () function of:

Enter the command:

contract.say()

 

 Contract deployment is successful, the call succeeds.

 

Guess you like

Origin www.cnblogs.com/shall1995/p/11619732.html