简体   繁体   English

在 Truffle 上测试部署的智能合约

[英]Testing Deployed Smart Contract on Truffle

I'm having trouble testing a contract using truffle.我在使用松露测试合同时遇到问题。 I have the latest version of truffle installed as well a TestRPC.我安装了最新版本的 truffle 以及 TestRPC。

It's to my understanding that the latest version of truffle comes with a client for testing SmartContracts so TestRPC isn't needed any more.据我了解,最新版本的 truffle 附带了一个用于测试 SmartContracts 的客户端,因此不再需要 TestRPC。

I have a simple contract my Migrations looks like so:我有一个简单的合同,我的迁移看起来像这样:

var Migrations = artifacts.require("./Migrations.sol");
var OrdersFacilitator = artifacts.require("./OrdersFacilitator.sol")

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

When I run truffle develop I see that 10 test accounts are created.当我运行 truffle develop 时,我看到创建了 10 个测试帐户。

Accounts:帐户:

(0) 0x627306090abab3a6e1400e9345bc60c78a8bef57 (0) 0x627306090abab3a6e1400e9345bc60c78a8bef57

(1) 0xf17f52151ebef6c7334fad080c5704d77216b732 //... (1) 0xf17f52151ebef6c7334fad080c5704d77216b732 //...

My Truffle js is configured like so:我的 Truffle js 配置如下:

networks: {
  development: {
    host: "localhost",
    port: 9545,
    network_id: "*"
  }
}

When I run the name of my contracts I can see its deployed with the information:当我运行我的合约名称时,我可以看到它已部署的信息:

//...
class_defaults:
{ from: '0x627306090abab3a6e1400e9345bc60c78a8bef57',
 gas: 6721975,
 gasPrice: 100000000000 },
currentProvider:
 HttpProvider {
   host: 'http://127.0.0.1:9545/',
   timeout: 0,
   user: undefined,
   password: undefined,
   headers: undefined,
   send: [Function],
   sendAsync: [Function],
   _alreadyWrapped: true },
network_id: '4447' }

the Network Id and the port seem to match the configuration fine, and I can see the information when I type the contract name, However when I try to gain access to it though网络 ID 和端口似乎与配置匹配得很好,当我输入合同名称时我可以看到信息,但是当我尝试访问它时

var facilitator;
OrdersFacilitator.deployed().then(x => facilitator = x);

I Get an Error:我收到一个错误:

OrdersFacilitator has not been deployed to detected network (network/artifact mismatch) OrdersFacilitator 尚未部署到检测到的网络(网络/工件不匹配)

I've tried deleting the build folder and migrating all with the --reset flag but that doesn't seem to work.我尝试删除构建文件夹并使用 --reset 标志迁移所有文件夹,但这似乎不起作用。 How can I Tested my deployed contract我如何测试我部署的合同

I've seen similar issues withe Truffle projects attempting the same thing.我在尝试同样事情的 Truffle 项目中看到了类似的问题。 The solution may be to reset the project to a non-cached state.解决方案可能是将项目重置为非缓存状态。

You can try running truffle compile (and/or truffle migrate) with the --reset flag.您可以尝试使用 --reset 标志运行 truffle compile(和/或 truffle migrate)。 This will remove the build dir and recompile everything.这将删除build目录并重新编译所有内容。

To fix the issue I've explicitly set the network options and network id.为了解决这个问题,我已经明确设置了网络选项和网络 ID。

//Start Test RPC with an Explicit Network
testrpc --network-id 1337

Then I've created an explicit network for TestRPC:然后我为 TestRPC 创建了一个显式网络:

networks: {
  testrpc:{
    host: "localhost",
    port: 8545,
    network_id: "1337"
  }
}

Now when I Migrate Explicitly to my new network:现在,当我显式迁移到我的新网络时:

truffle migrate --network testrpc --reset
truffle console --network testrpc

This seems to map properly to the correct network这似乎正确映射到正确的网络

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

相关问题 测试松露中的智能合约要求:如果功能不无效,则交易将被还原 - Testing Smart contract requires in Truffle: transaction reverted if function isnt void 测试我的 Solidity 智能合约时遇到问题(带松露) - Having a Problem Testing my Solidity Smart Contract (w/ Truffle) 错误 - 当调用部署在松露智能合约上时,等待仅在异步 function 中有效 - Error - await is only valid in async function, when calling deployed on a truffle smart contract Web 应用程序无法与通过 Truffle 部署的以太坊智能合约正确通信 - Web Application doesn't communicate correctly with Ethereum smart contract deployed through Truffle 松露智能合约错误:参数数量无效 - Truffle Smart Contract Error: Invalid number of parameter 如何使用松露访问已部署合约的 abi? - How to access abi of deployed contract with truffle? 如何将我的智能合约与另一个已部署的智能合约连接起来? - How to connect my smart contract with another deployed smart contract? 为什么 truffle 控制台没有在 Solidity 智能合约中打印正确的变量? - Why does the truffle console not print correct variable in solidity smart contract? Solidity 智能合约迁移错误的 Truffle 和 Ganache 教程 - Truffle and Ganache tutorial for a Solidity smart contract migration error 使用松露部署智能合约时出错 - Getting error while deploying smart contract using truffle
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM