如何使用web3.js与以太坊交互以及调用合约方法

在DApp中,前端UI是通过JavaScript代码与以太坊、智能合约交互的,这里记录一个简单的例子,使用web3.js获取以太坊区块1的信息,使用web3.js调用智能合约方法。

0、安装web3

cnpm install web3 -g

1、创建demo.js

vim demo.js

2、编写demo.js,引入Web3模块(注意路径是web3模块目录,而不是web3.min.js的路径),新建web3实例,获取区块1的信息。

var Web3 = require("/usr/local/nodejs/lib/node_modules/web3");                                              
var web3 = new Web3(Web3.givenProvider || 'http://192.168.78.102:9545');                                    
web3.eth.getBlock(1, function(error, result){
                                                                    
    if(!error)                                                                                              
        console.log(result)                                                                                 
    else                                                                                                    
        console.error(error);                                                                               
})   

3、运行demo.js

node demo.js

如何使用web3.js与以太坊交互以及调用合约方法_第1张图片
Web3.js的中文API地址:
https://web3.tryblockchain.org/Web3.js-api-refrence.html

web3.js调用智能合约

# 一个简单的合约,传入什么,就返回什么。
pragma solidity >0.5.0;                                                                                      
                                                                                                             
contract Hello{
                                                                                                   
    function echo(string memory text) public pure returns(string memory) {
                                        
        return text;                                                                                         
    }                                                                                                        
} 

# truffle部署合约
truffle compile
truffle migrate

# web3.js调用合约
> var abi = [                                                                                                  
    {
                                                                                                            
      "constant": true,                                                                                     
      "inputs": [                                                                                           
        {
                                                                                                        
          "internalType": "string",                                                                         
          "name": "text",                                                                                   
          "type": "string"                                                                                  
        }                                                                                                   
      ],                                                                                                    
      "name": "echo",                                                                                       
      "outputs": [                                                                                          
        {
                                                                                                        
          "internalType": "string",                                                                         
          "name": "",                                                                                       
          "type": "string"                                                                                  
        }                                                                                                   
      ],                                                                                                    
      "payable": false,                                                                                     
      "stateMutability": "pure",                                                                            
      "type": "function"                                                                                    
    }                                                                                                       
  ];

> var address = "0x3f5720cd073ff6344c626f84efc22e2f3d2ae1ad";

> var echoContract = new web3.eth.Contract(abi,address);

> echoContract.methods.echo("		hello nick!").call({
     from:"0xbda045e9b1543f56d27d36d178f013f5a16cf912"},function(error,result){
     console.log(result)});                                                                              
Promise {
      <pending> }
>               hello nick!         

你可能感兴趣的:(区块链,以太坊,DApp,智能合约,web3,require)