注:我见过这个堆栈交换问题,它的解决方案不起作用。
我试图在Goerli上部署一份合同,但得到了以下错误:
throw new Error(`Cannot convert string to buffer. toBuffer
only supports 0x-prefixed hex strings and this string was
given: ${v}`);
Error: Cannot convert string to buffer. toBuffer only
supports 0x-prefixed hex strings and this string was given:
0x60806...
000...0002F
at toBuffer (/home/kevin/coding/dao-voting/besu-deploy/node_modules/@ethereumjs/util/dist/bytes.js:147:19)
at new BaseTransaction (/home/kevin/coding/dao-voting/besu-deploy/node_modules/@ethereumjs/tx/dist/baseTransaction.js:53:41)
at new Transaction (/home/kevin/coding/dao-voting/besu-deploy/node_modules/@ethereumjs/tx/dist/legacyTransaction.js:28:9)
at main (/home/kevin/coding/dao-voting/besu-deploy/public_tx.js:35:14)
at process.processTicksAndRejections (node:internal/process/task_queues:95:5)
Node.js v18.7.0当我尝试执行以下操作时,就会发生这种情况:
const rawTxOptions = {
nonce: web3.utils.numberToHex(txnCount),
from: account.address,
to: null, //public tx
value: "0x00",
data: '0x'+contractBin+contractConstructorInit, // contract binary appended with initialization value
gasPrice: "0x0", //ETH per unit of gas
gasLimit: "0x24A22" //max number of gas units the tx is allowed to use
};
const tx = new Tx(rawTxOptions);我知道产生错误的字符串来自data字段,因为它与'0x'+contractBin (contractBin来自这个文件)匹配。因此,从错误中可以看到,给定的data和字符串确实以0x开头。那么,我为什么要犯这个错误呢?我创建了一个简单存储库来复制这个错误以及运行它的步骤。
发布于 2023-03-28 17:37:43
我在my @ethereumjs/tx发行上得到了一个答案:
问题似乎在于您如何构造事务数据字段。在您的示例回购中,您正在连接
data: '0x'+contractBin+contractConstructorInit,其中contractBin是一个buffer,而不是一个字符串。如果用data: '0x'+contractBin.toString('hex')+contractConstructorInit替换它,这个错误就会消失。
https://ethereum.stackexchange.com/questions/148126
复制相似问题