简体   繁体   English

松露合约适用于Ganache,但不适用于Testnet

[英]Truffle contract works with Ganache but not on Testnet

I was able to create and deploy a contract locally to Ganache using Truffle. 我能够使用Truffle在本地创建合同并将其部署到Ganache。 I could set and get a value successfully as well. 我也可以成功设置并获取值。 I deployed this contract to both Rinkeby and Ropsten and was able to call the get function, but the set function failed with the error: 我将此合同部署到Rinkeby和Ropsten上,并且能够调用get函数,但是set函数失败并显示以下错误:

Error: Invalid JSON RPC response: ""

Using MyEtherWallet/MetaMask, I was able to call both get and set functions successfully on Rinkeby. 使用MyEtherWallet / MetaMask,我可以在Rinkeby上成功调用get和set函数。 Is there anything that jumps out as to why it would work locally, but not on the testnets? 关于为什么它可以在本地运行而不是在测试网上运行的功能,是否还有什么可以跳过的?

Set function: 设定功能:

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});
  });

Get function: 获取功能:

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});
  });

Solidity contract: 固守合同:

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);
    }

}

Figured it out. 弄清楚了。 I was using web3 to set my provider instead of truffle-hdwallet-provider. 我正在使用web3设置我的提供程序,而不是truffle-hdwallet-provider。

web3 (wasn't working): 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 (now works): truffle-hdwallet-provider(现已运行):

new HDWalletProvider(mnemonic, "http://127.0.0.1:7545");

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM