熟悉一门语言得从Hello World! 开始,因为这是最简单的一个输出形式。
 我们先在contracts目录下建立一个helloworld.sol文件
 进入编辑
// SPDX-License-Identifier: MIT
pragma solidity >=0.4.22 <0.9.0;
contract helloworld {
  uint public balance;
/********** Begin **********/
// 函数名:sayHelloWorld
  function sayHelloWorld() public pure returns (string memory){
      return ("Hello World!");
    }
/********** End **********/
}
 
保存退出
 在migrations下新建一个部署合约的js文件:3_initial_migration.js
                        
//const Migrations = artifacts.require("Migrations");
var helloworld = artifacts.require("helloworld");
module.exports = function (deployer) {
  deployer.deploy(helloworld);
};
 
接下来在test中使用js调用智能合约
var helloworld=artifacts.require("helloworld")
contract('helloworld',function(accounts){
        it("first",function(){
                return helloworld.deployed().then(function(instance){
                        console.log(instance.address)	//输出合约地址
                        instance.sayHelloWorld().then(function(result) {
                                console.log(result);		//输出hello world
                        })
                })
        })
        it("sencode",function(){
                return helloworld.deployed().then(function(instance){
                        console.log(instance.address)
                })
        })
})
 
在另一个窗口打开ganache
ganache-cli
 
运行智能合约并调用
truffle compile
truffle migrate
truffle test ./test/helloworld.js
 










