如何将合同部署到ganache测试网络并与之交互?

时间:2018-05-29 20:38:46

标签: blockchain truffle

尝试部署合同时出现以下错误

错误:尚未将迁移部署到检测到的网络(网络/工件不匹配)     在/Users/rohank2/.nvm/versions/node/v8.11.1/lib/node_modules/truffle/build/webpack:

我用truffle init来创建自己的松露项目。我初始化了一个名为Migrations的智能合约。

这是智能合约的代码:

    pragma solidity ^0.4.23;

contract Migrations{

 address public admin;
  struct Bank{
    string bankname;
    string PWCcode;

  }

  struct graybarbranch{
    string branchname ;
    uint currentapr;
    uint currentdebt;
    uint amtreturned;
    uint totalborrowed;
    string bankborrowedfrom;
  }


  mapping(address=>Bank) public Banks;
  event graybar(address accountaddress,string branchname,uint totalamt);
  mapping(address=>graybarbranch) public graybarbranches;

  constructor() public {
    admin =msg.sender;
  }

  function borrow(address from, address to,uint currentapr,uint currentamt) public {
        graybarbranches[to].bankborrowedfrom=Banks[from].bankname;
        graybarbranches[to].currentapr=currentapr;
        graybarbranches[to].totalborrowed+=currentamt;
        graybarbranches[to].currentdebt=graybarbranches[to].totalborrowed-graybarbranches[to].amtreturned;
  }
  function ret(address to,uint amt) public {
    graybarbranches[to].amtreturned+=amt;
    graybarbranches[to].currentdebt=graybarbranches[to].totalborrowed-graybarbranches[to].amtreturned;
  }
  function initializebank(string bankname,string PWCcode,address ad)public{
    Banks[ad].bankname=bankname;
    Banks[ad].PWCcode = PWCcode;
  }
  function initializegraybar(address ad,string branchname) public {
    graybarbranches[ad].branchname=branchname;

    graybarbranches[ad].currentapr=0;
    graybarbranches[ad].currentdebt=0;
    graybarbranches[ad].totalborrowed=0;
    graybarbranches[ad].amtreturned=0;
  }
  function display(address ad) public{
    emit graybar(ad,graybarbranches[ad].branchname,graybarbranches[ad].totalborrowed);
  }

}

以下是迁移文件:

var Migrations = artifacts.require("Migrations");

module.exports = function(deployer) {
  deployer.deploy(Migrations);
};

最后这是我的truffle.js文件:

module.exports = {
  networks: {
    development: {
      host: "127.0.0.1",
      port: 7545,
      network_id: "*"
    }
  }
};

不知道如何与合同互动。

1 个答案:

答案 0 :(得分:1)

要在Ganache上运行以下命令:  $ truffle migrate 您将在控制台上获得txHash和合同地址。您可以使用合同地址与Geth控制台进行交互。

要与Ganache客户端进行交互,您必须在您的计算机上安装“Geth”。

运行以下命令:

  1. $ geth attach http://127.0.0.1:7545
  2. 您将获得“欢迎使用Geth Javascript控制台!”消息和命令提示符交互区块链。
  3. 使用eth.getCode({contract_address})传递您在松露部署中收到的合同地址。