【发布时间】:2018-01-22 00:23:38
【问题描述】:
在http://truffleframework.com/tutorials/pet-shop的例子中,有如下合约,函数adopt(uint petId)只有一个参数。
contract Adoption {
address[16] public adopters;
function adopt(uint petId) public returns (uint) {
require(petId >= 0 && petId <= 15);
adopters[petId] = msg.sender;
return petId;
}
function getAdopters() public returns (address[16]) {
return adopters;
}
}
但是,在 javascript 代码 app.js 中,handleAdopt 函数使用以下代码调用合约函数。
App.contracts.Adoption.deployed().then(function(instance) {
adoptionInstance = instance;
return adoptionInstance.adopt(petId, {from: account});
})
使用额外对象{from: account} 调用该函数。为什么?这个参数在solidity代码中被丢弃了吗?
顺便说一句,有一个未定义的全局变量web3?该值是否由 MetaMask 扩展分配?
【问题讨论】:
-
请参阅我发布的有关交易对象的答案。如果您安装了插件,则 MetaMask 会注入
web3对象。否则,您可以实例化您自己的 web3 对象,并传入 web3 提供程序。例如,要使用 HttpProvider: const web3 = new Web3(); web3.setProvider(new web3.providers.HttpProvider(providerUrl));
标签: ethereum solidity truffle consensys-truffle