web3和truffle-contract调用合约之间区别

web3调用合约

首先需要安装web3npm install web3如果安装失败,请查看我之前的文章

var MyContract = web3.eth.contract(abi);//合约abi接口

//部署好的合约地址实例化该合约
var myContractInstance = MyContract.at('0x78e97bcc5b5dd9ed228fed7a4887c0d7287344a9');

myContract.methods.myMethod(123).call({from: '0xde0B295669a9FD93d5F28D9Ec85E40f4cb697BAe'}, function(error, result){
    //do something
});

truffle-contract

首先需要安装truffle-contractnpm install truffle-contract

var provider = new Web3.providers.HttpProvider("http://localhost:8545");
var contract = require("truffle-contract");//引入
var MyContract = contract(abi)//合约abi文件
MyContract.setProvider(provider);
//函数调用
  MyContract.deployed().then(function(instance) {
      meta = instance
      return meta.balanceOf.call(account,{from: account})
    }).then(function(value) {

你可能感兴趣的:(web3和truffle-contract调用合约之间区别)