5

The idea is simple: you can send transactions using any ethereum wallet, and every such wallet has an additional data to send called "transaction data". For example, I want users to be able to type in that data field name of the product they are purchasing, a simple string or maybe an integer value (index of a product). Is that possible? I just want users be able to not only interact with my contract using metamask or mist, but also just using their maybe online wallets or etc.

As I read in docs, I have to use a standard fallback payable function, like

contract Test {
   function() payable {}
}

and somehow read the msg.data. How is that possible to read it? I searched the questions but none of them had the right answer. But there is more: fallback function executes only 2300 gas, so how can I handle it then?

1

The limit of 2300 gas was introduced to reduce the risk of malicious behaviour (like re-entrant attack used in the famous DAO hack) from contracts which receive ether. The limit is enough to submit an event so at worse your application may listen to that event and then process further the purchase.

However, this limit is not connected to the default fallback function but to the sending methods: send() and transfer(). For the latter, you are able to override the limit using .gas(xxx) method. And you can obviously use the low level call() function that goes without any limitations to gas.

You can add data to any of this function, remember to encode it first using tools such as this.

  • I mean only sending transactions to a contract number using any wallet. That means I can't really choose functions, I can only send ether to a 'fallback' function. How is that possible to decode the data in the contract? – porfavorite Jul 24 '17 at 20:33
  • Yes, all of the parameters (like product name or index) are passed in msg.data. Here you can find some detailed instruction on how to parse them ethereum.stackexchange.com/questions/5684/…. – Jakub Wojciechowski Jul 24 '17 at 21:02
  • Am I rigth that in my contract I should work with that msg.data through function() payable {} ? – porfavorite Jul 24 '17 at 23:51
  • Yes, but be aware of the gas limitation. It may be wiser to submit an event containing msg.data from payable function and process it further in another place. – Jakub Wojciechowski Jul 25 '17 at 0:32

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.