I am trying to figure out the right syntax for solc 0.5.7 and web3 1 beta 52
Follow my source code:
contract.sol
pragma solidity ^0.5;
contract Hello {
string public message;
constructor (string memory initialMessage) public {
message = initialMessage;
}
function setMessage(string memory newMessage) public {
message = newMessage;
}
}
hello.js
const ganache = require('ganache-cli');
const Web3 = require('web3');
const fs = require("fs");
const path = require('path');
const solc = require('solc');
const contract_sol = path.resolve(__dirname, 'contract.sol');
const source = fs.readFileSync(contract_sol, 'utf8');
var json_compile = JSON.stringify({
language: 'Solidity',
sources: {
'Hello': {
content: source,
},
},
settings: {
outputSelection: {
'*': {
'*': [ '*' ]
},
},
},
})
const rawcontract = solc.compile(json_compile);
const le_contract = JSON.parse(rawcontract);
const bytecode = le_contract.contracts.Hello.Hello.evm.bytecode.object
const abi = le_contract.contracts.Hello.Hello.abi
console.log("abi: ", abi)
console.log("bytecode:", bytecode)
async function deploy() {
const provider = ganache.provider();
provider.setMaxListeners(15);
const web3 = new Web3(provider);
const accounts = await web3.eth.getAccounts();
console.log("account 1:", accounts[0])
this.contract = await new web3.eth.Contract(abi)
.deploy({data:'0x'+ bytecode, arguments: ['hello']})
.send({from: accounts[0], gas:1000000, gasLimit: 99999999});
return this;
}
deploy().then(showContract);
function showContract(result) {
console.log('Contract: ' + result.contract);
}
When I run node hello.js I got this output after some minutes:
abi: [ { constant: false,
inputs: [ [Object] ],
name: 'setMessage',
outputs: [],
payable: false,
stateMutability: 'nonpayable',
type: 'function' },
{ constant: true,
inputs: [],
name: 'message',
outputs: [ [Object] ],
payable: false,
stateMutability: 'view',
type: 'function' },
{ inputs: [ [Object] ],
payable: false,
stateMutability: 'nonpayable',
type: 'constructor' } ]
bytecode: 608060405234801561001057600080fd5b506040516104593803806104598339810180604052602081101561003357600080fd5b81019080805164010000000081111561004b57600080fd5b8281019050602081018481111561006157600080fd5b815185600182028301116401000000008211171561007e57600080fd5b5050929190505050806000908051906020019061009c9291906100a3565b5050610148565b828054600181600116156101000203166002900490600052602060002090601f016020900481019282601f106100e457805160ff1916838001178555610112565b82800160010185558215610112579182015b828111156101115782518255916020019190600101906100f6565b5b50905061011f9190610123565b5090565b61014591905b80821115610141576000816000905550600101610129565b5090565b90565b610302806101576000396000f3fe608060405234801561001057600080fd5b50600436106100365760003560e01c8063368b87721461003b578063e21f37ce146100f6575b600080fd5b6100f46004803603602081101561005157600080fd5b810190808035906020019064010000000081111561006e57600080fd5b82018360208201111561008057600080fd5b803590602001918460018302840111640100000000831117156100a257600080fd5b91908080601f016020809104026020016040519081016040528093929190818152602001838380828437600081840152601f19601f820116905080830192505050505050509192919290505050610179565b005b6100fe610193565b6040518080602001828103825283818151815260200191508051906020019080838360005b8381101561013e578082015181840152602081019050610123565b50505050905090810190601f16801561016b5780820380516001836020036101000a031916815260200191505b509250505060405180910390f35b806000908051906020019061018f929190610231565b5050565b60008054600181600116156101000203166002900480601f0160208091040260200160405190810160405280929190818152602001828054600181600116156101000203166002900480156102295780601f106101fe57610100808354040283529160200191610229565b820191906000526020600020905b81548152906001019060200180831161020c57829003601f168201915b505050505081565b828054600181600116156101000203166002900490600052602060002090601f016020900481019282601f1061027257805160ff19168380011785556102a0565b828001600101855582156102a0579182015b8281111561029f578251825591602001919060010190610284565b5b5090506102ad91906102b1565b5090565b6102d391905b808211156102cf5760008160009055506001016102b7565b5090565b9056fea165627a7a72305820277e93fc1b4bb24bf6430ed54fba320bffbd41845fc9776b42e2ff4a0435d3060029
account 1: 0x768B810656650594e55D8e954b7A7185Dc20423a
{ error: Error: Timeout exceeded during the transaction confirmation process. Be aware the transaction could still get confirmed!
at _callee2$ (/home/myusername/ethereum/node_modules/web3-core-method/dist/web3-core-method.cjs.js:418:36)
at tryCatch (/home/myusername/ethereum/node_modules/regenerator-runtime/runtime.js:45:40)
at Generator.invoke [as _invoke] (/home/myusername/ethereum/node_modules/regenerator-runtime/runtime.js:271:22)
at Generator.prototype.(anonymous function) [as next] (/home/myusername/ethereum/node_modules/regenerator-runtime/runtime.js:97:21)
at asyncGeneratorStep (/home/myusername/ethereum/node_modules/@babel/runtime/helpers/asyncToGenerator.js:3:24)
at _next (/home/myusername/ethereum/node_modules/@babel/runtime/helpers/asyncToGenerator.js:25:9)
at <anonymous>
at process._tickCallback (internal/process/next_tick.js:188:7),
receipt:
{ transactionHash: '0xea4219567b06e84c1e18fe4c0c0c3815d8db4b114f912b1c1260aa5a0425f64a',
transactionIndex: 0,
blockHash: '0x5ca2a2eb42b35cd87e3398b8d51dc6e5a03bdfbfb66358bed4ca8f7535b8d3fb',
blockNumber: 1,
from: '0x94e69ef500502be8ee76387aba538a1c50790fa7',
to: null,
gasUsed: 299917,
cumulativeGasUsed: 299917,
contractAddress: '0x06800fC07D10191c00cC2FEab2f78799f8884B5B',
logs: [],
status: true,
logsBloom: '0x00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000',
v: '0x1b',
r: '0xa397e20cbc05aa22388e4a34c373313f71182a4579c51a653a5fd57a5513c6f9',
s: '0x3a54282acb23879c09526ce004e8f1473d12c0c959d98cb9fb9f7d81fc88d835' },
confirmations: 1,
confirmationChecks: 750 }
{ error: Error: Timeout exceeded during the transaction confirmation process. Be aware the transaction could still get confirmed!
at _callee2$ (/home/myusername/ethereum/node_modules/web3-core-method/dist/web3-core-method.cjs.js:418:36)
at tryCatch (/home/myusername/ethereum/node_modules/regenerator-runtime/runtime.js:45:40)
at Generator.invoke [as _invoke] (/home/myusername/ethereum/node_modules/regenerator-runtime/runtime.js:271:22)
at Generator.prototype.(anonymous function) [as next] (/home/myusername/ethereum/node_modules/regenerator-runtime/runtime.js:97:21)
at asyncGeneratorStep (/home/myusername/ethereum/node_modules/@babel/runtime/helpers/asyncToGenerator.js:3:24)
at _next (/home/myusername/ethereum/node_modules/@babel/runtime/helpers/asyncToGenerator.js:25:9)
at <anonymous>
at process._tickCallback (internal/process/next_tick.js:188:7),
receipt:
{ transactionHash: '0xea4219567b06e84c1e18fe4c0c0c3815d8db4b114f912b1c1260aa5a0425f64a',
transactionIndex: 0,
blockHash: '0x5ca2a2eb42b35cd87e3398b8d51dc6e5a03bdfbfb66358bed4ca8f7535b8d3fb',
blockNumber: 1,
from: '0x94e69ef500502be8ee76387aba538a1c50790fa7',
to: null,
gasUsed: 299917,
cumulativeGasUsed: 299917,
contractAddress: '0x06800fC07D10191c00cC2FEab2f78799f8884B5B',
logs: [],
status: true,
logsBloom: '0x00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000',
v: '0x1b',
r: '0xa397e20cbc05aa22388e4a34c373313f71182a4579c51a653a5fd57a5513c6f9',
s: '0x3a54282acb23879c09526ce004e8f1473d12c0c959d98cb9fb9f7d81fc88d835' },
confirmations: 1,
confirmationChecks: 750 }
I am learning from a book that uses solc 0.4.x and I am trying to convert the code to 0.5.x
gas
andgasLimit
, which are synonyms. I'm not sure which takes precedence, but if it'sgasLimit
, that might be the problem. It's too high because it exceeds the block gas limit, so miners probably won't try to include it in a block. It also seems that you're not setting agasPrice
. I would recommend setting one. ethgasstation.info says that 2 gwei should do (web3.utils.toWei('2', 'gwei')
). – user19510 Apr 9 '19 at 4:18