0

链上记事本

 2 years ago
source link: https://learnblockchain.cn/question/2997
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

3 DApp教程:用Truffle 开发一个链上记事本 遇到的问题

根据教程粘贴的代码,跑起来之后页面报错说没有部署合约 检查了几次 应该是在我知识范围之外的报错了16420815591.jpg

代码是根据教程所说,用狗狗商城那个代码改来的
狗狗商城那个我跑通了 没有说合约部署有问题。

image.png
这是部署文件

16420815631.jpg
这是web3的调用

感谢各位有时间帮忙解决下。

另外还想问一下 用Truffle自带的lite 服务器,启动后
web3加载的时候是怎么连接上metamask的
在狗狗商城那个demo中领养一只狗又是怎么调用metamask验证 并且把这个狗狗放入到metamask中的
这两步连接metamask代码里面都没看见 领养的代码不是仅仅把地址赋给了狗的mapping么

最佳答案 20小时前

把小狐狸切换到127.0.0.1:8545这个网络上来

把部署后的合约的address和abi对应填写到代码中

truffle-contract。js并不指定对metamask的调用,我想你要的是

initContract: function () {
$.getJSON('abcContract.json', function (data) {
App.contracts.noteContract = TruffleContract(data);
App.contracts.noteContract.setProvider(App.web3Provider);

App.contracts.noteContract.deployed().then(function (instance) {
    App.noteIntance = instance;
    App.noteIntance.合约接口函数(参数);
  });
});

web3.eth.getAccounts(function (error, accounts) {
  if (error) {
    console.log(error);
  }
  var account = accounts[0];
  App.account = account
});

App.currentIndex = 0
return App.bindEvents();
},

metamask 就是在127.0.0.1:8545 那个狗狗商城 那个demo我已经跑通了 也能购买狗到 测试的metamask 账号上了 这个笔记本的demo 我的metamask也已经连接上了 但是 获取不到合约 我写的代码和你一模一样 我在浏览器上DEBUG了
App.contracts.noteContract.deployed().then(function (instance) { App.noteIntance = instance; App.noteIntance.合约接口函数(参数); }); }); 这段代码中instance是空值 说是合约没部署 所以实例没成功

你的这个代码 变成这种块 是怎么做的 我复制出来 就只能是一行代码。。。。。

lite 服务器,启动后 是怎么自动连接 metamask的? web3的哪一段代码实现的? 这个是额外的问题 和这个错误没关系 就是想问问 web3加载的时候是怎么连接上metamask的 在狗狗商城那个demo中领养一只狗又是怎么调用metamask验证 并且把这个狗狗放入到metamask中的 代码里面都没看见 领养的代码不是仅仅把地址赋给了狗的mapping么

await window.ethereum.enable(); 这句激活钱包的

return adoptionInstance.adopt(petId, {from: account}); 这句中的adoptionInstance就是已经实例化的合约本地web3js对象,当调用它的adopt方法时,web3js通过小狐狸这个代理,吊用了链上虚拟机里的合约端adopt方法

采纳率 17% | 回答于 6天前

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK