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:
- Could I use
datato store aStringwhose 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) {
return
}
await web3.eth.sendTransaction({
from: testAccount,
to: testAccount,
value: web3.utils.toWei('0.0003'),
data: web3.utils.utf8ToHex(JSON.stringify({ a: 1, b: 2 }))
})
- 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 ?
data? Or at least a maximum size below which I don't really need to worry about it ? – Sprout Coder May 09 '22 at 18:28