简体   繁体   English

我使用 ethers.js 部署合约,但返回错误:未捕获(承诺)错误:未知交易覆盖 0

[英]I use ethers.js deployment contract, but returned error: uncaught (in promise) error: unknown transaction override 0

I use ethers.js to deploy the contract, but returned an error: uncaught (in promise) error: unknown transaction override 0我使用 ethers.js 部署合约,但返回错误:未捕获(承诺中)错误:未知交易覆盖 0

at ContractFactory.在合同工厂。 getDeployTransaction (index.ts:1196:1) getDeployTransaction (index.ts:1196:1)

at ContractFactory.< anonymous> (index.ts:1246:1)在 ContractFactory.<匿名> (index.ts:1246:1)

at Generator.在发电机。 next ()下一个 ()

at fulfilled (index.ts:2:1)完成时 (index.ts:2:1)

Instead, the same contract can be successfully deployed with web3.js, except that the three parameters (contractname, contractsymbol, and contracturi) become garbled after deployment.取而代之的是,使用 web3.js 可以成功部署同一个合约,只是部署后三个参数(contractname、contractsymbol 和contracturi)变得乱码。

The code of using ethers.js deploy contract is as follows:使用 ethers.js 部署合约的代码如下:

...
    window.ethereum.request({ method: 'eth_requestAccounts' });
    const provider = new ethers.providers.Web3Provider(window.ethereum);

    const signer = provider.getSigner()
    console.log("Account:", await signer.getAddress());
    
    const factory = new ethers.ContractFactory( bodyJSON2.abi, bodyJSON2.bytecode, signer );
    console.log("factory.signer:", factory.signer);
    await factory.deploy([contractName, contractSymbol, contractURI])
    .then(async function(contract){ 
      console.log("for debug. Contract Path: ", contract.address); 
    });                

Contract code is as followed:合约代码如下:

function initialize(string memory name, string memory symbol, string memory url
    ) public virtual initializer {
        __BXPP_init(name, symbol, url);
    }
    using CountersUpgradeable for CountersUpgradeable.Counter;
    CountersUpgradeable.Counter private _tokenIds;

    string private _baseTokenURI;

...

The relevant error codes in ethers.js are as follows: ethers.js中的相关错误码如下:

getDeployTransaction(...args: Array<any>): TransactionRequest {
    let tx: TransactionRequest = { };

    // If we have 1 additional argument, we allow transaction overrides
    if (args.length === this.interface.deploy.inputs.length + 1 && typeof(args[args.length - 1]) === "object") {
        tx = shallowCopy(args.pop());
        for (const key in tx) {
            if (!allowedTransactionKeys[key]) {
                throw new Error("unknown transaction override " + key);
            }
        }
    }

The code of using web3.js deploy contract is as follows:使用 web3.js 部署合约的代码如下:

    const web3 = new Web3(window.ethereum);
    const BXPP = new web3.eth.Contract(bodyJSON2.abi);
    await BXPP.deploy({
      'data': bodyJSON2.bytecode,
      'arguments': [contractName, contractSymbol, contractURI]
    })         
    .send({
      'from': address,
      // gas: ,
      // gasPrice: '3000000'
    }, function(error, transactionHash){ console.log("error/transactionHash: ", error, transactionHash) })
    .on('error', function(error){ console.log("error: ", error) })
    .on('transactionHash', function(transactionHash){ console.log("transactionHash: ", transactionHash) })
    .on('receipt', function(receipt){ console.log(receipt.contractAddress) 
    })
    .on('confirmation', function(confirmationNumber, receipt){ console.log("confirmationNumber:", confirmationNumber) })
    .then(async function(newContractInstance){
        console.log("for debug. ContractID: ", newContractInstance.options.address);                     
    });  

I searched Google for almost a week, but I didn't find the answer.我在谷歌上搜索了将近一个星期,但我没有找到答案。 Does anyone have the same problem as me?有没有人和我一样的问题? Thanks!谢谢!

The problem has been solved.问题已解决。 There are two points:有两点:

  1. Use ethers (the version in my machine) to deploy the contract, and pass the parameters without using arrays.使用ethers(我机器中的版本)部署合约,并传递参数而不使用arrays。 Like this:像这样:
 await factory.deploy(contractName, contractSymbol, contractURI)

Not like this:不是这样的:

await factory.deploy([contractName, contractSymbol, contractURI])
  1. I used an initializable contract (initialized with initializer) and changed it to a normal contract (initialized with constructor).我使用了一个可初始化的合约(使用初始化程序初始化)并将其更改为普通合约(使用构造函数初始化)。 Like this:像这样:
constructor(string memory name, string memory symbol, string memory url
    ) {
        __ContractTemplate_init(name, symbol, url);
    }

Not like this:不是这样的:

function initialize(string memory name, string memory symbol, string memory url
    ) public virtual initializer {
        __ContractTemplate_init(name, symbol, url);
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM