对合约函数的一次调用会生成多个交易

  • 本文关键字:调用 交易 一次 函数 web3js
  • 更新时间 :
  • 英文 :


我正在为Truffle Pet Shop示例dApp添加功能。我在solidity合约中创建了一个returnPet函数,并通过控制台和测试合约对其进行了测试。我现在想在我的JS应用程序中调用它。

问题是,当我调用它时,handleReturn((函数,

handleReturn: function(event) {
event.preventDefault();
var petId = parseInt($(event.target).data('id'));
var adoptionInstance;
web3.eth.getAccounts(function(error, accounts) {
if (error) {
console.log(error);
}
var account = accounts[0];
App.contracts.Adoption.deployed().then(function(instance) {
adoptionInstance = instance;
return adoptionInstance.returnPet(petId);
}).then(function(result) {
return App.markAdopted();
}).catch(function(err) {
console.log(err.message);
});
});

生成多个交易,并且不会影响区块链(这是另一个问题。如果你有答案,我很乐意听到。(。据我所知,handleReturn((只被调用一次,那么它为什么要生成多个事务呢?

这应该有效:

handleReturn: function(event) {
event.preventDefault();
var petId = parseInt($(event.target).data('id'));
var adoptionInstance;
web3.eth.getAccounts(function(error, accounts) {
if (error) {
console.log(error);
}
var account = accounts[0];
});
App.contracts.Adoption.deployed().then(function(instance) {
adoptionInstance = instance;
})
adoptionInstance.returnPet(petId).then(function(result) {
return App.markAdopted();
}).catch(function(err) {
console.log(err.message);
});

它调用多个事务,因为您是在web3.eth.getAccounts方法下调用它的,如果它不起作用,请通过导入您的contract工件来使用truffle调用您的合同方法/功能,并使用webpack将其捆绑为

最新更新