0

The contract has a function:

function purchaseTokens() external payable {
    require(msg.value>0);
    ...etceteras...
}

What I need is an app.js part, something that would look like this (lets say I want to hard-code 12 wei for a deposit):

purchaseTokens : function (){
    App.contracts.name_of_contract.deployed().then(async function(instance){
        let message = await instance.purchaseTokens.call('12');
        App.showMessage(message);
    }).catch((err) =>{
        App.showError(err);
    })
},

However, the js function returns: TypeError: Cannot read property 'toString' of null.

What should I do for the app.js part ?

1 Answer 1

1

I'm not the strongest player in this dialect, but this looks familier:

await instance.purchaseTokens.call('12');

Try adding transaction arguments (gas, gasPrice, value) like this:

await instance.purchaseTokens.call({value: <amount>});

call is probably wrong, so:

`await instance.purchaseTokens({value: });

Hope it helps.

p.s. If there are arguments for the function and you also want transaction arguments:

instance.function(arg1, arg2, {gas: <qty>, value: <amount>});

4
  • Thank you, I had tried a similar solution but none produced results using truffle. At this point I have no idea why I can't get any results, although calling the views does work. Nov 8, 2019 at 22:03
  • 1
    Sorta grasping at straws here, and this is long shot. "can't get any results". Just in case it is a cause of confusion, you MUST make this a state-changing transaction, not a read-only call() because it's payable and you NEED the ledger to update (write). Therefore, you can only get a transaction receipt and you must wait for the transaction to mine before any result is possible. blog.b9lab.com/… Nov 8, 2019 at 22:30
  • I understand, however, I have tried a simple transfer of a token from address(0), that is, minting, and no minting was done using truffle... Your comment makes sense and therefore I will try to work on Ropsten and will accept the answer as soon as I confirm it working. Nov 8, 2019 at 22:34
  • Works on Ropsten, however, source code to connect to metamask etc... must be added. Also we must make sure that we are using the right ABI (something that took me about 1 day to find out). Nov 14, 2019 at 2:27

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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