0

I have two functions,

One that returns web.eth.sendTransaction, which triggers metamask a first time,

And one that returns contractInstance.function that updates an array in my contract. Which also triggers metamask.

Both are in the same big function in my app.js,

I would like obtain the same result, which is receive a payment, and then update the book in the contract, by only calling metamask once.

What is the best practice?

markAdopted: function(adopters, account) {
    var adoptionInstance;

    App.contracts.Adoption.deployed().then(function(instance) {
      adoptionInstance = instance;

      return adoptionInstance.getAdopters.call();
    }).then(function(adopters) {
      for (i = 0; i < adopters.length; i++) {
        console.log(adopters[i]);
        if (adopters[i] !== '0x0000000000000000000000000000000000000000') {
          $('.panel-pet').eq(i).find('button').text('Coped').attr('disabled', true);
        }
      }
    }).catch(function(err) {
      console.log(err.message);
    });
  },

  handleAdopt: function(event) {
    event.preventDefault();

    var petId = parseInt($(event.target).data('id'));
    var adoptionInstance;
    var account;
    var transactionStatus;

    web3.eth.getAccounts(function(error, accounts) {
      if (error) {
        console.log(error);
      }

      account = accounts[0];

    });

    web3.eth.sendTransaction({
      from: account,
      to: '0xC5fdf4076b8F3A5357c5E395ab970B5B54098Fef',
      value: '1000000000000000000'
    }, function(error, result){
    if(!error) {
        console.log(result);
        transactionStatus = true;
        if (transactionStatus) {
        App.contracts.Adoption.deployed().then(function(instance) {

        adoptionInstance = instance;

        return adoptionInstance.adopt(petId, {from: account});

      }).then(function(result) {

        return App.markAdopted();
      }).catch(function(err) {

        console.log(err.message);
      });
      } 
    }

    else {
        console.error(error);
        transactionStatus = false;
    }

  });
    }
};
1

Unless you are willing to make that update array function a payable and merge the code, you are essentially requesting 2 actions to be completed which will require 2 triggers.

An alternative is you connect a medium account, securely on the backend, to connect and trigger the second call through...Unfortunately, you will also incur the gas cost.

Best bet is to try to re-think how the contract is programmed and try to do more with fewer function calls.

  • How would the fact that I do a payable function will lead me to have only one call ? I do not mind having a payable function. I thought about the medium account, I do not mind bearing the gas cost. If you have some more lead on this? I mean I would gladly explore both options. – Ilan Feb 5 '18 at 17:53
  • @Ilan Is your sendTransaction acutally sending data through the optional data parameter? I recommend you show us the code for the transaction itself. – ReyHaynes Feb 5 '18 at 18:33
  • Can I show it in private ? – Ilan Feb 5 '18 at 19:32
  • 1
    @Ilan I would just put it here so you can get the maximum response. Unless you're making something revolutionary, it's pretty safe to put the code you need help with here. – ReyHaynes Feb 5 '18 at 20:47
  • I added the code, in fact it is not revolutionary its just an adapted code from a tutorial – Ilan Feb 5 '18 at 21:01

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.