5

In that example you can see, how to deploy a new contract having just ABI and ByteCode

(uses for copy and deploy contracts from mainnet to your testnet for example).

/****** Deploying new contract to ethereum network via Geth console             *****/
/****** Using only ABI and BYTEcode of a new contract                           *****/
/****** Example created by Vladimir S. Udartsev (https://github.com/udartsev)   *****/

/* 
 * (1) Connect to your ethereum network via Geth console:
 *
 * @note http://127.0.0.1:8545 - is a default ganache-cli host address, be free to change it
 * USE CLI COMMAND: 
 */
$ geth attach 'http://127.0.0.1:8545' 

/*
 * (2) Set you new contract variables inside Geth:
 *
 * @note contract abi object must be in array [], like: '[{"constant": false}]'
 * @note contract byteCode must start from 0x606060
 */
var abi = '[{"constant": false,"inputs": [{"name": "_winnings","type": "uint256"}],"name": "nothingIsImpossible","outputs": [],"payable": false,  "stateMutability": "nonpayable",  "type": "function"},{  "payable": true,  "stateMutability": "payable",  "type": "fallback"}]';
var byteCode = '0x6060604052341561000f57600080fd5b60db8061001d6000396000f300606060405260043610603f576000357c0100000000000000000000000000000000000000000000000000000000900463ffffffff168063241f673d146041575b005b3415604b57600080fd5b605f60048080359060200190919050506061565b005b600081111560ac573373ffffffffffffffffffffffffffffffffffffffff166108fc600283029081150290604051600060405180830381858888f19350505050151560ab57600080fd5b5b505600a165627a7a723058209dccf8f370dde2ae64186418da896aafb9d45bf83b13aeaf0f8710f0a68bb2470029';

/*
 * (3) Unlock your account for deployment:
 *
 * USE COMMAND IN GETH:
 */ 
var accountAddr = web3.eth.accounts[0];
var gas = 5000000;
personal.unlockAccount(accountAddr)

/*
 * (4) Prepare contract data for deployment
 */
var newContractAbi = web3.eth.contract(JSON.parse(abi));
var newContractObject = {from: accountAddr,data: byteCode, gas: gas};

/*
 * (5) Deploy contract
 */
var deployedContract = newContractAbi.new(newContractObject);

/*
 * (6) Check deployment and get delpoyed contract address
 */
var deployedContract = web3.eth.getTransactionReceipt(deployedContract.transactionHash);
var contractAddr = deployedContract.contractAddress;
contractAddr;

I did it and it works well. But what if I have only ByteCode without ABI... How can I deploy contract WITHOUT abi only with ByteCode? Does it real?

Vladimir
  • 181
  • 1
  • 6

1 Answers1

5

Actually the data of deployed contract transaction is bytecode concatenate with abi encoded of constructor parameters.

Here is example:

Token.sol

pragma solidity 0.5.1;


contract Token {
    uint256 public totalSuply;

    constructor (uint256 _totalSuply) public {
        totalSuply = _totalSuply;
    }
}

The bytecode of Token.sol: 608060405234801561001057600080fd5b506040516020806100ef8339810180604052602081101561003057600080fd5b810190808051906020019092919050505080600081905550506098806100576000396000f3fe6080604052600436106039576000357c010000000000000000000000000000000000000000000000000000000090048063a2a9679914603e575b600080fd5b348015604957600080fd5b5060506066565b6040518082815260200191505060405180910390f35b6000548156fea165627a7a72305820fe2ba3506418c87a075f8f3ae19bc636bd4c18ebde0644bcb45199379603a72c0029

If I want to deploy a Token.sol with totalSupply 100, the data must be: 0x608060405234801561001057600080fd5b506040516020806100ef8339810180604052602081101561003057600080fd5b810190808051906020019092919050505080600081905550506098806100576000396000f3fe6080604052600436106039576000357c010000000000000000000000000000000000000000000000000000000090048063a2a9679914603e575b600080fd5b348015604957600080fd5b5060506066565b6040518082815260200191505060405180910390f35b6000548156fea165627a7a72305820fe2ba3506418c87a075f8f3ae19bc636bd4c18ebde0644bcb45199379603a72c00290000000000000000000000000000000000000000000000000000000000000064

If you know inputs of constructor, you can use ethereumjs-abi to deploy contract.

const abi = require('ethereumjs-abi')
const bytecode = '0x......'
const encodedParameters = abi.rawEncode(['uint256'], 100)
const data = bytecode + encodedParameters // send this data
Peter Lai
  • 317
  • 1
  • 3