我可以将文本附加到交易记录上吗?

发布于 2025-02-04 17:26:16 字数 1437 浏览 1 评论 0原文

根据 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

韬韬不绝 2025-02-11 17:26:16

这是特定于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).

余生共白头 2025-02-11 17:26:16

我需要编写自定义的智能合同才能实现这一目标吗?

您还可以以坚固性编写智能合约功能,将数据作为函数参数接收,但对此无能为力。因此,Goethereum节点将此数据存储为交易的calldata,可以稍后检索。

我很确定有些跨链桥以这种方式运行。交易仅将数据作为CallData的一部分(比固体存储便宜),然后从那里阅读。

Do I need to write a custom smart-contract in order to achieve this ?

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文