node.js - 在 Hedera 区 block 链中创建智能合约时出现错误 "Transaction Oversize"

标签 node.js blockchain hedera-hashgraph hashgraph

我的 bin 文件大小只有 18kb。我也得到了使用 IPFS 的解决方案,但不知道如何使用它。如果有任何使用 IPFS 的引用,请与我分享。 :

错误:PrecheckStatusError:事务 0.0.34898094@1653658245.135892060 预检查失败,状态为 TRANSACTION_OVERSIZE

这是我的代码:

const {
  AccountId,
  PrivateKey,
  Client,
  FileCreateTransaction,
  ContractCreateTransaction,
  ContractFunctionParameters,
  ContractExecuteTransaction,
  ContractCallQuery,
  Hbar
} = require("@hashgraph/sdk");
const fs = require("fs");

const operatorId = AccountId.fromString(process.env.OPERATOR_ID);
const operatorKey = PrivateKey.fromString(process.env.OPERATOR_PVKEY);
const client = Client.forTestnet().setOperator(operatorId, operatorKey);


async function main() {
  // Import the compiled contract bytecode
  const contractBytecode = fs.readFileSync("first_contract_sol_ABC_TOKEN.bin");
  // Create a file on Hedera and store the bytecode
  const fileCreateTx = new FileCreateTransaction().setContents(contractBytecode).setKeys([operatorKey]).setMaxTransactionFee(new Hbar(1))
    .freezeWith(client);
  const fileCreateSign = await fileCreateTx.sign(operatorKey);
  console.log(Date.now() / 1000);
  const fileCreateSubmit = await fileCreateSign.execute(client);
  const fileCreateRx = await fileCreateSubmit.getReceipt(client);
  const bytecodeFileId = fileCreateRx.fileId;
  console.log(`- The bytecode file ID is: ${bytecodeFileId} \n`);  

    // Instantiate the smart contract
    const contractInstantiateTx = new ContractCreateTransaction()
    .setBytecodeFileId(bytecodeFileId)
    .setGas(100000)
    .setConstructorParameters(
      new ContractFunctionParameters().addString("Alice").addUint256(111111)
    );
  const contractInstantiateSubmit = await contractInstantiateTx.execute(client);
  const contractInstantiateRx = await contractInstantiateSubmit.getReceipt(
    client
  );
  const contractId = contractInstantiateRx.contractId;
  const contractAddress = contractId.toSolidityAddress();
  console.log(`- The smart contract ID is: ${contractId} \n`);
  console.log(`- Smart contract ID in Solidity format: ${contractAddress} \n`);

}
main();

最佳答案

您遇到了 TRANSACTION_OVERSIZE 错误,因为 Hedera 交易有 6kb 的大小限制,包括所有签名。

如果您的合约编译后的字节码比较大,那么您需要在 Hedera 上创建一个文件,然后将内容附加到多个 block 中,然后再创建合约。

当您使用 ContractCreateFlow() 时,SDK 现在会为您处理这 3 个步骤。这是一个示例:

const contractCreate = new ContractCreateFlow()
    .setGas(100000)
    .setBytecode(bytecode);

//Sign the transaction with the client operator key and submit to a Hedera network
const txResponse = contractCreate.execute(client);

//Get the receipt of the transaction
const receipt = (await txResponse).getReceipt(client);

关于node.js - 在 Hedera 区 block 链中创建智能合约时出现错误 "Transaction Oversize",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/72406476/

相关文章:

node.js - 多个字段上的 Mongoose 过滤(搜索)

bitcoin - Hyperledger Fabric 如何避免死循环?

network-programming - “Gossip about gossip” 协议(protocol)

hedera-hashgraph - Hedera 中的委托(delegate)合约 ID 是什么

docker - 错误响应 : Could not kill running container runc did not terminate sucessfully: container_linux. go:392: init process caused "permission denied"

solidity - 使用 Hedera Hashgraph 从智能合约生成随机数

javascript - 在 express 中加载 css 和 js 时遇到 500 内部服务器错误

node.js - Sequelize : how to get datetime column value as string

javascript - 通过 Node 从Mysql数据库中获取数据,然后在 typescript 中访问它

blockchain - 每个 Assets 都应该在 Fabric 中有自己的链码吗?