我可以将文本附加到交易记录上吗?
根据 和 eth_sendtransaction :
交易对象可以包含一个可选的data
参数,该参数应为string
,它由:
ABI 组成字节字符串包含合同上函数调用的数据,或者在合同创建交易的情况下,初始化代码。
我想将字符串分配给data将交易记录与区块链上的记录一起存储,这样我可以在以后检索该交易的记录时检索该字符串。
const [testAccount] = await window.ethereum.request({ method: "eth_requestAccounts" })
const web3 = new Web3(window.ethereum)
if (!testAccount) {
return
}
let transactionHash = await web3.eth.sendTransaction({
from: testAccount,
to: testAccount,
value: web3.utils.toWei('0.0003'),
data: web3.utils.utf8ToHex(JSON.stringify({ a: 1, b: 2 }))
})
let transaction = await web3.eth.getTransaction(transactionHash)
let data = JSON.parse(web3.utils.hexToUtf8(transaction.data))
console.log(data.a) // should log 1
当我执行sendtransaction
(使用metAmask,在连接到ropsten网络时)时,我会收到以下错误:
Error: Error: TxGasUtil - Trying to call a function on a non-contract address
{
"originalError": {
"errorKey": "transactionErrorNoContract",
"getCodeResponse": "0x"
}
}
显然,您无法将任何字符串分配给data
仅通过为其分配一个值,就可以纳入区块链上的交易记录中。 这是正确的吗
?
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 want to assign a string to data
and have the string be stored along with the record of the transaction on the blockchain, so that I can retrieve that string when I retrieve the record of that transaction later.
const [testAccount] = await window.ethereum.request({ method: "eth_requestAccounts" })
const web3 = new Web3(window.ethereum)
if (!testAccount) {
return
}
let transactionHash = await web3.eth.sendTransaction({
from: testAccount,
to: testAccount,
value: web3.utils.toWei('0.0003'),
data: web3.utils.utf8ToHex(JSON.stringify({ a: 1, b: 2 }))
})
let transaction = await web3.eth.getTransaction(transactionHash)
let data = JSON.parse(web3.utils.hexToUtf8(transaction.data))
console.log(data.a) // should log 1
When I execute sendTransaction
(using Metamask, while connected to the Ropsten network), 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. Is this correct?
Question: Do I need to write a custom smart-contract in order to achieve this ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这是特定于metAmask的功能/限制。可能是为了保护想要与智能合约互动但已连接到未部署合同的网络的用户。
但是,从技术上讲,将具有非空的
数据
字段的有效交易发送到非合同地址。您只需要使用其他节点提供商即可广播交易。不幸的是,MetAmask中的节点提供商是硬编码的,因此不可能使用此钱包。示例:到
0xDAC1 ...
具有This is a feature/limitation specific to MetaMask. Possibly to protect their users who want to interact with a smart contract but are connected to a different network where the contract is not deployed.
However, it is technically possible to send a valid transaction with non-empty
data
field to a non-contract address. You just need to use a different node provider to broadcast the transaction. Unfortunately the node provider in MetaMask is hardcoded so it's not possible using this wallet.Example: This transaction on the Ropsten testnet to the
0xdac1...
address that has the USDT token contract deployed on the mainnet, but is a non-contract address on the testnet. It is a valid transaction, successfully bought gas from the sender address to cover the transaction fees, mined in a block, just didn't execute any smart contract code (as there is no smart contract on the recipient address).您还可以以坚固性编写智能合约功能,将数据作为函数参数接收,但对此无能为力。因此,Goethereum节点将此数据存储为交易的
calldata
,可以稍后检索。我很确定有些跨链桥以这种方式运行。交易仅将数据作为CallData的一部分(比固体存储便宜),然后从那里阅读。
You can also write a smart contract function in Solidity that receives data as a function argument, but does nothing on this. Thus, GoEthereum node stores this data as
calldata
of the transaction and it can be later retrieved.I am pretty sure some cross-chain bridges operate in this manner. Transactions only write data as the part of calldata (cheaper than Solidity storage) and then other clients read it from there.