I am adding functionality to the Truffle Pet Shop example dApp. I have created a returnPet function in the solidity contract, and have tested it via console and test contracts. I would now like to call it in my JS app.
The problem is, when I call it, the handleReturn() function,
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);
});
});
generates multiple transactions, and does not affect the blockchain (a different problem. If you have answers to this, I would love to hear them.). As far as I can tell, handleReturn() only gets called once, so why is it generating multiple transactions?
This should work:
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);
});
It calling multiple transactions because you are calling it under the web3.eth.getAccounts methods,if it doesn't work call your contract methods/fuctions using truffle by importing your contracrt artifacts and use webpack to bundle it