Solidity

TypeError:this._deployData.startsWith 不是函式

  • December 5, 2021

我正在嘗試部署我的智能合約,該合約編譯沒有任何問題,並將所有編譯數據儲存在兩個單獨的文件中。然後我需要 ABI 和字節碼並嘗試部署它。我收到以下錯誤。

1) "before each" hook for "deploys a factory and campaign":
    TypeError: this._deployData.startsWith is not a function
     at Object._encodeMethodABI (node_modules\web3-eth-contract\lib\index.js:456:31)
     at Object._processExecuteArguments (node_modules\web3-eth-contract\lib\index.js:713:39)
     at Object._executeMethod (node_modules\web3-eth-contract\lib\index.js:732:68)
     at Context.<anonymous> (test\projectFund.test.js:24:6)
     at processTicksAndRejections (node:internal/process/task_queues:96:5)

這是我的 test.js 文件

const assert = require('assert');
const ganache = require('ganache-cli');
const Web3 = require('web3');
const web3 = new Web3(ganache.provider());

const compiledFactory = require('../EthereumProject/build/projectFundFactory.json')
const compiledFund = require('../EthereumProject/build/ProjectFund.json');

let accounts;
let factory;
let campaignAddress;
let campaign;

beforeEach(async()=>{

   accounts = await web3.eth.getAccounts();
   factory = await new web3.eth.Contract(compiledFactory.abi)
   .deploy({ data: compiledFactory.evm.bytecode })
   .send({ from: accounts[0], gas:'1000000' }); //the above error points here

   await factory.methods.createProjectFund('100').send({
       from: accounts[0],
       gas: '1000000'
   });

   [ campaignAddress ] = await factory.methods.getDeployedProjectFund().call();
   campaign = await new web3.eth.Contract(
       compiledFund.abi,
       campaignAddress
   );

});

describe('Campaigns', ()=>{
   it('deploys a factory and campaign', ()=>{
       assert.ok(factory.options.address);
       assert.ok(campaign.options.address);
   });
});

我認為您沒有正確傳遞字節碼。您缺少字節碼對象 - compiledFactory.evm.bytecode.object

所以它應該是這樣的:

factory = await new web3.eth.Contract(compiledFactory.abi)
   .deploy({ data: compiledFactory.evm.bytecode.object })
   .send({ from: accounts[0], gas:'1000000' });

希望這可以幫助!

引用自:https://ethereum.stackexchange.com/questions/114996