5

I'm trying to test a payable function from Truffle my function looks like so:

function recordOrder(bytes32 orderNumber) payable returns(bool success) {
    if(msg.value==0)
    {
        return false;
    }

    if(!orderStructs[orderNumber].exists) {
        orderStructs[orderNumber].sender = msg.sender;
        orderStructs[orderNumber].amountReceived = msg.value;
        orderStructs[orderNumber].exists = true;
    }
    else {
        orderStructs[orderNumber].amountReceived += msg.value;
    }

    LogOrder(msg.sender, orderNumber, msg.value);
    withdrawFee(msg.value, orderNumber);
    return true;
}

I'm trying to figure out how to send payments to a function but I'm confused on how. If I call the function like so:

var facilitator;
OrdersFacilitator.deployed().then(x => facilitator = x);

var orderNumber = '33075588-8408-484b-8a53-e9c7f61bcb12';
facilitator.recordOrder.call(orderNumber);

I get a return value of false which make sense considering I don't think I've specified an amount to send. However when I call the function directly:

facilitator.recordOrder(orderNumber);

I'm returned some json with a reciept:

{ tx: '0x5e38d5bb4f933a58e56620e8b0ae9ae0acb191b92ce0fef2f8fe784c9d40ef2c',
  receipt:
   { transactionHash: '0x5e38d5bb4f933a58e56620e8b0ae9ae0acb191b92ce0fef2f8fe784c9d40ef2c',
     transactionIndex: 0,
     blockHash: '0x038a57ce4a73efc59d57403d652119d3114602311756251be74e75cbe4d06e39',
     blockNumber: 5,
     gasUsed: 24150,
     cumulativeGasUsed: 24150,
     contractAddress: null,
     logs: [],
     status: 1 },
  logs: [] }

Which I have no idea what is going on here, because I never specified an amount, and I'm calling from the truffle console, So I don't even know how gas when I'm not connected to an account (that I know of).

How can I send value to my payable function from truffle?

4
  • 1
    try this facilitator.recordOrder.call(orderNumber, {value: 100 }); to specify an amount
    – Tony Dang
    Aug 22, 2018 at 3:38
  • 1
    I don't think you want .call() or it wont actually write to the blockchain, right? Aug 22, 2018 at 4:00
  • I'm mostly just trying to figure out if my contract works, What does call do is that just a mock?
    – johnny 5
    Aug 22, 2018 at 4:24
  • .call will simulate the function call, and do things locally. It is good for testing a positive result, but does not actually send a transaction or update the contract state. For example, it would return true or false based on your function, but not actually LogOrder or withdrawFee Aug 22, 2018 at 4:30

2 Answers 2

9

Web3 offers a number of optional parameters which you can add to a function call:

  • from - The address transactions should be made from.
  • gasPrice - The gas price in wei to use for transactions.
  • gas - The maximum gas provided for a transaction (gas limit).
  • value - The value transferred for the transaction in Wei, also the endowment if it's a contract-creation transaction.

etc...

To use them, simply include them as an object as the last parameter in your function call:

facilitator.recordOrder(orderNumber, {value: web3.toWei(2, 'ether')});
7
  • I'm using console in truffle, accounts is not defined, How do I link to accounts[0]?
    – johnny 5
    Aug 22, 2018 at 4:27
  • If you do not define from it will automatically pick the account loaded (accounts[0]), but if you want access to the other accounts available from your web provider, you can do something like var accounts; web3.eth.getAccounts(function(err,res) { accounts = res; }); Aug 22, 2018 at 4:29
  • Hmm, that doesn't seem to work I get an error when I try to call web3.eth.getAccounts() callback is not a function
    – johnny 5
    Aug 22, 2018 at 4:33
  • See minor update to include a callback function. FYI, I was short handing things, but you should be very familiar with error first callback functions if you are playing with Web3. It is pretty common people will shorthand, and you will need to make sure you are always implementing the asynchronous versions of the functions. Aug 22, 2018 at 4:34
  • 1
    I assume you have something like ganache running as a local blockchain? Ganache will create 10 accounts for you to play with each with 100 ETH to start, all locally of course. Truffle connects to this local blockchain as a part of your migration script. Aug 22, 2018 at 4:41
3

Update: With web3 1.0 the web3.toWei is no longer available, rather web3.utils.toWei

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.