在这个例子中,您可以看到,如何部署一个只有ABI和ByteCode的新合同
(例如,用于复制和部署从mainnet到testnet的契约)。
/****** 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;我做到了,效果很好。但如果我只有ByteCode而没有ABI.如何在不使用ByteCode的情况下部署合同?是真的吗?
发布于 2019-05-17 03:58:02
实际上,部署的契约事务的数据是bytecode与构造函数参数的abi编码的连接。
下面是一个例子:
Token.sol
pragma solidity 0.5.1;
contract Token {
uint256 public totalSuply;
constructor (uint256 _totalSuply) public {
totalSuply = _totalSuply;
}
}Token.sol:608060405234801561001057600080fd5b506040516020806100ef8339810180604052602081101561003057600080fd5b810190808051906020019092919050505080600081905550506098806100576000396000f3fe6080604052600436106039576000357c010000000000000000000000000000000000000000000000000000000090048063a2a9679914603e575b600080fd5b348015604957600080fd5b5060506066565b6040518082815260200191505060405180910390f35b6000548156fea165627a7a72305820fe2ba3506418c87a075f8f3ae19bc636bd4c18ebde0644bcb45199379603a72c0029的字节码
如果我想使用Token.sol 100部署totalSupply,数据必须是:0x608060405234801561001057600080fd5b506040516020806100ef8339810180604052602081101561003057600080fd5b810190808051906020019092919050505080600081905550506098806100576000396000f3fe6080604052600436106039576000357c010000000000000000000000000000000000000000000000000000000090048063a2a9679914603e575b600080fd5b348015604957600080fd5b5060506066565b6040518082815260200191505060405180910390f35b6000548156fea165627a7a72305820fe2ba3506418c87a075f8f3ae19bc636bd4c18ebde0644bcb45199379603a72c00290000000000000000000000000000000000000000000000000000000000000064
如果您知道构造函数的输入,可以使用ethereumjs-abi部署契约。
const abi = require('ethereumjs-abi')
const bytecode = '0x......'
const encodedParameters = abi.rawEncode(['uint256'], 100)
const data = bytecode + encodedParameters // send this datahttps://ethereum.stackexchange.com/questions/70774
复制相似问题