松露合同与Ganache合作,但不适用于Testnet

时间:2018-02-15 19:17:27

标签: blockchain ethereum solidity truffle

我能够使用Truffle在本地创建和部署合同。我也可以成功设置并获得一个值。我将此合同部署到Rinkeby和Ropsten并且能够调用get函数,但set函数失败并出现错误:

Error: Invalid JSON RPC response: ""

使用MyEtherWallet / MetaMask,我能够在Rinkeby上成功调用get和set函数。有没有什么可以跳出来为什么它会在本地工作,而不是在测试网上?

设置功能:

ProcessHistoryStoreInstance.setStore(
    uuid,
    attribute,
    value,
    {from: account}
  ).then(function(result) {
      console.log('TX Hash: ' + result.tx);
      response.status(200).json({success: true, msg: result.tx});
  }, function(error) {
      console.log('Error setting attribute: ' + error);
      response.status(500).json({success: false, msg: error});
  });

获取功能:

ProcessHistoryStoreInstance.getFromStore.call(uuid, attribute).then(function(result) {
      console.log('Result: ' + result);
      response.status(200).json({success: true, msg: result});
  }, function(error) {
      console.log('Error getting attribute: ' + error);
      response.status(500).json({success: false, msg: error});
  });

合同合同:

pragma solidity^0.4.11;

import "./AttributeStore.sol";

contract ProcessHistoryStore {
    AttributeStore.Data store;

    function getFromStore(bytes32 _UUID, string _attrName) public constant returns (uint) {
      return AttributeStore.getAttribute(store, _UUID, _attrName);
    }

    function setStore(bytes32 _UUID, string _attrName, uint _attrVal) public {
      AttributeStore.setAttribute(store, _UUID, _attrName, _attrVal);
    }

}

1 个答案:

答案 0 :(得分:0)

想出来。我使用web3来设置我的提供者而不是truffle-hdwallet-provider。

web3(无效):

if (typeof web3 !== 'undefined') {
  web3 = new Web3(web3.currentProvider);
} else {
  // set the provider you want from Web3.providers
  web3 = new Web3(new Web3.providers.HttpProvider("http://127.0.0.1:7545"));
}

truffle-hdwallet-provider(现在有效):

new HDWalletProvider(mnemonic, "http://127.0.0.1:7545");
相关问题