According to the docs for web3.eth.sendTransaction and the docs for eth_sendTransaction:

The transaction object can contain an optional data parameter which should be a String that consists of:

either an ABI byte string containing the data of the function call on a contract, or in the case of a contract-creation transaction the initialisation code.

I'd like to know the following:

  1. Could I use data to store a String whose value would be anything I like? As in:
const [testAccount] = await window.ethereum.request({ method: "eth_requestAccounts" })
const web3 = new Web3(window.ethereum)

if (!testAccount) {

await web3.eth.sendTransaction({
  from: testAccount,
  to: testAccount,
  value:  web3.utils.toWei('0.0003'), 
  data: web3.utils.utf8ToHex(JSON.stringify({ a: 1, b: 2 }))
  1. If the answer is yes, how big (in bytes) can this string be and if the gas fee increases according to the size of data, can I calculate that in advance ?

Thank you for your help

UPDATE: When I execute the code above, I get the following error:

Error:  Error: TxGasUtil - Trying to call a function on a non-contract address
  "originalError": {
    "errorKey": "transactionErrorNoContract",
    "getCodeResponse": "0x"

Apparently, you cannot assign any string to data and expect the string to be incorporated in the record of the transaction on the blockchain, out-of-the-box, simply by assigning a value to it.

Question: Do I need to create a custom smart-contract in order to achieve this ?

1 Answer 1


This is a MetaMask issue.

See comment in their code -

if there's data in the params, but there's no contract code, it's not a valid transaction

This issue was reported here.

FYI your code works perfectly on a non MetaMask env.

You can store a string, but encoded as a hex string. To store 'I love lamp' you need to set data: '0x49206c6f7665206c616d70'.

Use utf8ToHex to convert a string to hex bytes -

web3.utils.utf8ToHex('I have 100€');
> "0x49206861766520313030e282ac"

To know how much gas it costs, use estimateGas, either on a contract's method, or without.

    to: "0x11f4d0A3c12e86B4b5F39B213F7E19D048276DAe",
    data: "0xc6888fa10000000000000000000000000000000000000000000000000000000000000003"

Re transaction size -

the transaction size limit, at the time of writing, is about 780kB (about 3 million gas).

info taken from this answer.

  • Thanks! Is there a size limit regarding data ? Or at least a maximum size below which I don't really need to worry about it ? Commented May 9, 2022 at 18:28
  • 1
    Added this to my answer. Feel free to accept the answer if this helps
    – Kof
    Commented May 9, 2022 at 19:22
  • 1
    I tried your suggestion but it doesn't work. I get the error I mention in the updated OP. It looks like I need to create a custom contract ? Commented Jun 6, 2022 at 12:57
  • Updated my answer.
    – Kof
    Commented Jun 6, 2022 at 19:11

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.