3

I want to deploy transaction with raw transaction without using hardhat or web3 method.

my code looks like this.

// constructor params
const name = 'My Contract';
const symbol = 'MC';
const maxSupply = 500;
const baseURI = 'https://sample/link';

const jsonRpcProvider = new ethers.providers.JsonRpcProvider(MUMBAI_URL);
const executer = new Wallet(PRIVATE_KEY, jsonRpcProvider);
const executerAddress = await executer.getAddress();

const MyContractAbi = new ethers.utils.Interface(MyContract__factory.abi);
const constructorEncodedData = MyContractAbi.encodeDeploy([name, symbol, maxSupply, baseURI]);
const txData = hexlify(concat([MyContract__factory.bytecode, constructorEncodedData]));

const unserializedTx = {
    to: '0x0000000000000000000000000000000000000000',
    value: BigNumber { _hex: '0x00', _isBigNumber: true },
    data: txData,
    gasLimit: BigNumber { _hex: '0x1e8480', _isBigNumber: true },
    maxPriorityFeePerGas: BigNumber { _hex: '0x06fc23ac00', _isBigNumber: true },
    maxFeePerGas: BigNumber { _hex: '0x06fc23ac00', _isBigNumber: true },
    nonce: 28,
    type: 2,
    chainId: 80001
  };

const signedTx = await executer.signTransaction(unserializedTx);
const txResponse = await (await jsonRpcProvider.sendTransaction(signedTx)).wait();

when I send transaction, it does executed, but it doesn't seem to create contract.
my transaction result link is here.

I refer to 'deploy' method in @ethersproject, but I don't know why it doesn't work.
any ideas?

4
  • 2
    In order to deploy a contract 'to' has to be empty (not the zero address). In your example you are sending the contract bytecode to the zero address. Since the zero address is a regular account then anything send to it is just ignored.
    – Ismael
    Feb 14, 2023 at 22:05
  • 1
    after deleting 'to' property and increase gas Limit(2_000_000 -> 5_000_000), it works. Thanks!!
    – rrrrssss
    Feb 15, 2023 at 5:37
  • 1
    You should write the answer with those details, that way you can mark the question as resolved.
    – Ismael
    Feb 15, 2023 at 19:15
  • This is awesome! It's fun and demystifying doing things in low-level ways.
    – devdanke
    Mar 3, 2023 at 15:21

1 Answer 1

1

As written in the comment, just delete 'to' property in unserializedTx

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.