verpas
verpas

Reputation: 95

How do I use nodejs and solidity to create a contract using already compiled ABI and bytecode?

I made this previous post, which I will link here so that if anyone else has this issue in this udemy course may help them proceed. It also has some more background info for what I'm doing which may be useful.

I'm trying to create a contract using the compiled byteCode and ABI so that I can deploy my contract. In the course it mentions to create an object of type ContractFactory and he uses greetingsContract. = web3.eth.contract(contractABI) to create that object. I used greetingsContract = new web3.eth.Contract(contractABI) but it's not of that data type. It creates an object of type Contract. He then uses this code greetingsDeployed = greetingsContract.new({data:byteCode, from: web3.eth.accounts[0], gas: 47000000}) to deploy it.

From what I understand this is old formatting for this kind of code and will no longer work as the instructor does it. How would I go about creating a contract using the ABI and bytecode that I have?

Upvotes: 0

Views: 169

Answers (1)

verpas
verpas

Reputation: 95

I used these two lines of code to create a contract with my compiled ABI and byteCode. The first line creates the new contract.

The second deploys the contract and attatches the address of the new instance of the contract that has an address, to the previously created contract in line 1. This creates the contract and it appears in Ganache in the transactions tab.

greetingsContract = new web3.eth.Contract(contractABI)

greetingsDeployed = greetingsContract.deploy({data: '0x' + byteCode}).send({from: account0, gas: 6721975, gasPrice: 1}).then((greetingsInstance) => {greetingsContract.options.address=greetingsInstance.options.address});

Upvotes: 1

Related Questions