5

In myetherwallet, there is an +Advanced: Add Data Section where I can add data to the transaction.

How can I add this same data in a transaction I make in Solidity?

enter image description here

eth
  • 85,679
  • 53
  • 285
  • 406
ZeroPointThree
  • 101
  • 1
  • 2
  • 4

2 Answers2

3

you need first to convert your string to the hexadecimal form using an online converter like http://string-functions.com/string-hex.aspx.

then put it in the data field (with 0x in the begining)

for example i've converted StackExchange into 0x537461636b45786368616e6765

enter image description here

put it in the data field and then i've send successfully my transaction. you could check the transaction here.

enter image description here

you could press the convert to asci button in the bottom to get back the encoded sent string.

Badr Bellaj
  • 18,780
  • 4
  • 58
  • 75
1

You can use Solidity's low-level call, but be careful of What does `call.value` mean and how did it allow the attack to The DAO?

Example:

bytes calldata = 0xabcdef;
address to = 0x...
if (to.call(calldata)) {  // checks the return value
    ....
} else {
    ....
}

If you wanted to send along 2 ETH, the syntax would be:

to.call.value(2 * 10**18)(calldata);  // should check the return value

Some information in the Solidity docs and FAQ.

Whenever you use call, code in the to contract can be executed and since it can call you back in ways that you might not expect (like the TheDAO), one should follow guidelines such as updating state variables first and making call the very last step (an example that shows a state variable being set to zero first, and then a making a call to msg.sender).

eth
  • 85,679
  • 53
  • 285
  • 406
  • why not to use send instead call? – Sig Touri Jun 06 '17 at 08:42
  • @SigTouri Yes send or transfer is better if that's all that's needed. The question was asking about adding data. – eth Jun 07 '17 at 01:19
  • @eth How does one do this with token contract? value() seems to expect ether. – nu everest Mar 09 '18 at 19:08
  • @nueverest Tokens work quite different: you invoke a transfer function on a token contract and specify recipient and amount of tokens. You're correct that value() should not be used. Be careful and test first. – eth Mar 11 '18 at 09:54