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

2 Answers 2

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.

1
  • This answer does not show how it is done in Solidity. Downvote. Commented Feb 16, 2022 at 12:23
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).

4
  • why not to use send instead call?
    – Sig Touri
    Commented Jun 6, 2017 at 8:42
  • @SigTouri Yes send or transfer is better if that's all that's needed. The question was asking about adding data.
    – eth
    Commented Jun 7, 2017 at 1:19
  • @eth How does one do this with token contract? value() seems to expect ether.
    – nu everest
    Commented Mar 9, 2018 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
    Commented Mar 11, 2018 at 9:54

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.