如何与节点上的智能契约进行交互?我用松露把我的合同部署到我的私人连锁店。但是,在我的nodejs代码中,我没有得到已部署合同的实例。
我有一个很小的合同示例,如下所示
pragma solidity ^0.5.1;
contract MyContract {
uint num;
function someFunction(uint _num) public {
num = _num;
}
}然后,我编译并将合同迁移到我的私人连锁(ganache-cli)。
>truffle compile
...
>truffle migrate
...
2_deploy_contracts.js
=====================
Deploying 'MyContract'
----------------------
> transaction hash: 0xa9161613e7c398c5425b3bb7c306d494a657193c965203902c5732192b394979
> Blocks: 0 Seconds: 0
> contract address: 0x93Da9d36ECcd5eeceBe9b469A65cBbA397b6c85E
> account: 0x9576316A79287D03c92F9157056e5BCde1cAEc5C
> balance: 99.99152546
> gas used: 98463
> gas price: 20 gwei
> value sent: 0 ETH
> total cost: 0.00196926 ETH
MyContract Address: 0x93Da9d36ECcd5eeceBe9b469A65cBbA397b6c85E
> Saving migration to chain.
> Saving artifacts
-------------------------------------
> Total cost: 0.00196926 ETH
Summary
=======
> Total deployments: 2
> Final cost: 0.00763398 ETH
>这么长时间一切看起来都很好。但是我想和我的私人连锁公司的合同进行互动。因此,我使用以下代码:
const path = require('path');
var MyContractABI = require(path.join(__dirname, '../build/contracts/MyContract'))
var Web3 = require('web3');
var provider = new Web3.providers.HttpProvider("http://localhost:8545");
var contract = require("truffle-contract");
var MyContract = contract(MyContractABI);
MyContract.setProvider(provider);
var deployed;
MyContract.deployed().then(function(instance) {
var deployed = instance;
return instance.someFunction(5);
}).then(function(result) {
// Do something with the result or continue with more transactions.
console.log(result);
});但现在一切都崩溃了。MyContractABI包含contracts jsons接口。但我得到了一个"UnhandledPromiseRejectionWarning: TypeError:无法读取未定义的属性‘应用’“。它在MyContract.deployed()上失败。
节点版本: v8.11.3松露版本: v5.0.0-beta.2
示例代码:https://github.com/manmountain/truffle-example
整个堆栈跟踪
(节点:5888) (C:\Users\goran\Documents\development\truffle-example\node_modules\truffle-contract\contract.js:24:36) at RequestManager.sendAsync (C:\Users\goran\Documents\development\truffle-example\node_modules\truffle-contract\node_modules\web3\lib\web3\requestmanager.js:80:19) at Object.get as getNetwork at Provider.sendAsync : TypeError:无法读取未定义属性的“应用”C:\Users\goran\Documents\development\truffle-example\node_modules\truffle-contract\contract.js:512:27 at new Promise () at Function.detectNetwork (C:\Users\goran\Documents\development\truffle-example\node_modules\truffle-contract\contract.js:503:14) at Function.deployed (C:\Users\goran\Documents\development\truffle-example\node_modules\truffle-contract\contract.js:451:19) at Object(C:\Users\goran\Documents\development\truffle-example\example\example.js:11:12) at Module._compile (module.js:652:30) at Object.Module._extensions..js (module.js:663:10) at Module.load (module.js:565:32) at tryModuleLoad (module.js:505:12) at Function.Module._load (module.js:497:3) at Function。Module.runMain (module.js:693:10)启动时(bootstrap_node.js:191:16)在bootstrap_node.js:612:3 (节点:5888) UnhandledPromiseRejectionWarning:未处理的承诺拒绝。此错误起源于在异步函数中抛出而不带catch块,或者拒绝使用.catch()处理的承诺。(拒绝id: 2) (节点:5888) DEP0018 DeprecationWarning:未处理的承诺拒绝被取消。在未来,承诺不处理的拒绝将使用非零退出代码终止Node.js进程。
发布于 2018-12-18 16:52:42
由于这是您的专用网络,您说您需要指定松露控制台中的网络是什么-network测试网(给出您在truffle.config文件中的网络名称),您应该能够进入与您的网络相对应的松露外壳。默认情况下,松露控制台将采用本地网络,即开发默认网络。
https://stackoverflow.com/questions/53832680
复制相似问题