Migrations are not being processed - ethereum

I'm trying to migrate election file but no migrations are being proceesed.
I've tried running truffle migrate --reset and remigrating process but nothing happens.
Elections.sol
pragma solidity ^0.5.0;
contracts Election{
string public candidate;
constructor() public{
candidate="Candidate 1"
}
}
2_deploy_contracts.js
var Election = artifacts.require("./Election.sol");
module.exports = function(deployer) {
deployer.deploy(Election);
};

Did you have compiled your smart contract?
So, at first fix syntax errors:
contract Election {
string public candidate;
constructor() public{
candidate="Candidate 1";
}
}
Then compile your contract:
truffle compile
After that, migrate:
truffle migrate

Related

An error when executing truffle.migrate, Invalid number of parameters for "undefined"

I'am trying to deploy a smart contract :
`
pragma solidity >=0.4.21;
contract SimpleStorage {
uint public storedData;
constructor(uint initVal) public {
storedData = initVal;
}
function set(uint x) public {
storedData = x;
}
function get() view public returns (uint retVal) {
return storedData;
}
}`
I have created 2_deploy.js
var SimpleStorage = artifacts.require("SimpleStorage");
module.exports = function(deployer) {
// Pass 42 to the contract as the first constructor parameter
deployer.deploy(SimpleStorage, 42, {privateFor:
["ROAZBWtSacxXQrOe3FGAqJDyJjFePR5ce4TSIzmJ0Bc="]})
};
but when I execute truffle.migration I obtain this error:
'Error encountered, bailing. Network state unknown. Review
successful transactions manually.
Error: Invalid number of parameters for "undefined". Got 2
expected 1!
at Object.InvalidNumberOfParams
(/usr/lib/node_modules/truffle/build/webpack:/~/web3-eth-
contract/~/web3-core-helpers/src/errors.js:32:1)
at Object._createTxObject
(/usr/lib/node_modules/truffle/build/webpack:/~/web3-eth-
contract/src/index.js:699:1)
at Contract.deploy
(/usr/lib/node_modules/truffle/build/webpack:/~/web3-eth-
contract/src/index.js:504:1)
at Function.deploy
(/usr/lib/node_modules/truffle/build/webpack:/packages/truffle-
contract/lib/execute.js:214:1)
at constructor.detectNetwork.then.network
(/usr/lib/node_modules/truffle/build/webpack:/packages/truffle-
contract/lib/contract/constructorMethods.js:56:1)
at <anonymous>
at process._tickCallback (internal/process/next_tick.js:188:7)
Truffle v5.0.13 (core: 5.0.13)
Node v8.9.4''
do anyone knows how to deal with the problem?
Truffle has only recently added support for quorum private transactions. So you need Truffle version 5.0.14.
I hope that helps.

Calling a public method in smart contract with Web3.js version: '1.0.0-beta.46'

I started first steps into ethereum blockchain with parity on a private network. I was able to configure parity and perform the deployment of smart contract on a dev mode chain on my private network through Parity UI which also is able to call methods of the contract.
The problem that I face is related to calling a function in smart contract using Web3.js. I was able to connect to the chain using the Web.js Library;
Web3 = require('web3')
web3 = new Web3('ws://localhost:8546')
mycontract = web3.eth.Contract([{"constant":false,"inputs":[],"name":"greet","outputs":[{"name":"","type":"string"}],"payable":false,"stateMutability":"nonpayable","type":"function"},{"inputs":[],"payable":false,"stateMutability":"nonpayable","type":"constructor"}],0xEb112542a487941805F7f3a04591F1D6b04D513c)
When I call the method below;
mycontract.methods.greet().call()
It gives me the following output instead of returning the expected string "OK Computer" through a promise object as written in smart contract greet function.
{ [Function: anonymousFunction]
send: { [Function] request: [Function] },
estimateGas: [Function],
encodeABI: [Function] }
Smart Contract code:
pragma solidity ^0.4.22;
//Compiler Version: 0.4.22
contract Greeter {
address owner;
constructor() public {
owner = msg.sender;
}
function greet() public returns(string){
return "OK Computer";
}
}
Every transaction or smart contract method call which involves a change on the blockchain state will return a promise. So you just need to handle the promise accordingly:
mycontract.methods.greet.call().then(function(resp) {
console.log(resp) // This will output "OK Computer"
}
More in web docs

Why runs my SmartContract out of gas when I add a simple function

I have a simple token derived from openzeppelin's MintableToken.
However, when I either add a Constructor OR another function, I am constantly running out of gas. But when I add ONLY one of both, either the Constructor OR the function, everything works fine.
My question is: how can I add several functions together with a constructor into my SmartContract?
The Token-Code:
pragma solidity ^0.4.22;
import "openzeppelin-solidity/contracts/token/ERC20/MintableToken.sol";
contract HaioToken is MintableToken {
string public name = "Haio Token";
string public symbol = "HAI";
uint8 public decimals = 18;
uint256 public cap;
constructor(uint256 _cap) public {
cap = _cap;
}
function test(address _to) public returns (bool) {
return true;
}
}
Migrations:
2_deploy_contracts.js:
var HaioToken = artifacts.require("HaioToken");
module.exports = function(deployer, network, accounts) {
const hardCap = 25000000;
return deployer
.then(() => {
return deployer.deploy(HaioToken, hardCap);
})
};
When I want to deploy the code, I get the following error message:
Error: VM Exception while processing transaction: out of gas
If I remove either the constructor or the test-function, everything works fine.
I guess you are running the migration with truffles default settings that came out of the box after running "truffle init" isn't it?
You should raise the gas you want to send on contract deployment this way in truffle.js (or truffle-config.js on Windows):
module.exports = {
networks: {
development: {
host: "localhost",
port: 7545,
network_id: "*",
gas: 5000000
}
}
};
(The value of 5000000 is an example that mostly works out of the box and if you don't have to care because developing on a local testnet :) )

Kaleido vm in read-only mode

I've successfully deployed the following contract on Kaleido:
pragma solidity ^0.4.0;
contract Greeter {
string public greeting;
function Greeter() {
greeting = 'Hello';
}
function setGreeting(string _greeting) public {
greeting = _greeting;
}
function greet() constant returns (string) {
return greeting;
}
}
I try to interact with the contract like so:
from web3 import Web3
from web3.providers import HTTPProvider
from solc import compile_source
from web3.contract import ConciseContract
# Solidity source code
contract_source_code = '''
pragma solidity ^0.4.0;
contract Greeter {
string public greeting;
function Greeter() {
greeting = 'Hello';
}
function setGreeting(string _greeting) public {
greeting = _greeting;
}
function greet() constant returns (string) {
return greeting;
}
}
'''
compiled_sol = compile_source(contract_source_code)
contract_interface = compiled_sol[':Greeter']
w3 = Web3(HTTPProvider("https://user:password#u0telyzine-u0od4ny83j-rpc.us-east-2.kaleido.io"))
# address from previous deployment
contract_address = Web3.toChecksumAddress("0x4c94e89d5ec3125339906109f143673f40868df2")
greeter = w3.eth.contract(
address=contract_address,
abi=contract_interface['abi'],
)
print('Default contract greeting: {}'.format(
greeter.functions.greet().call()
))
# --- this hangs ---
print('Setting the greeting to Nihao...')
tx_hash = greeter.functions.setGreeting('Nihao').transact({ 'from': w3.eth.accounts[0], 'gas': 100000})
w3.eth.waitForTransactionReceipt(tx_hash)
print('Updated contract greeting: {}'.format(
greeter.functions.greet().call()
))
reader = ConciseContract(greeter)
assert reader.greet() == "Nihao"
However, when I try to submit a transaction which calls setGreeting the transaction hangs. Viewing the Kaleido logs, I see VM in read-only mode. Mutating opcode prohibited. Also, when I visit the block explorer for my node, the transactions don't load while the blocks do.
What can I do about this read only mode?
moghadasian
I could not recreate your "VM in read-only mode" when submitting a transaction - that worked successfully.
However, I had to do a bit of investigation to get web3/python connecting to Kaleido - so I'm adding a separate answer to help others trying to get going.
Configuring HTTPS authentication to Kaleido from Python web3
On my Mac, with a default pip3 installation of web3, I found the only way to configure the Python Session with auth was to use a $HOME/.netrc file such as:
machine u0oaXXXXXX-u0c4XXXXXX-rpc.us-east-2.kaleido.io
login u0d0bxXXXX
password jA-pJdIrcRaIx7XXXXXXXXXXXXXXXXXXXXXXXXX
Configure web3 for Geth/PoA
My chain was using Geth/PoA, so I had to follow the instructions here, to install the required middleware:
http://web3py.readthedocs.io/en/stable/middleware.html#geth-style-proof-of-authority
Updated example including deployment of contract
Here is the python3 that successfully deployed and reported Updated contract greeting: Nihao.
You will need to change your HTTPProvider to the HTTPS RPC URL of your node, but without the authentication headers.
from web3 import Web3
from web3.providers import HTTPProvider
from solc import compile_source
from web3.contract import ConciseContract
from web3.middleware import geth_poa_middleware
# Solidity source code
contract_source_code = '''
pragma solidity ^0.4.0;
contract Greeter {
string public greeting;
function Greeter() {
greeting = 'Hello';
}
function setGreeting(string _greeting) public {
greeting = _greeting;
}
function greet() constant returns (string) {
return greeting;
}
}
'''
compiled_sol = compile_source(contract_source_code)
contract_interface = compiled_sol['<stdin>:Greeter']
w3 = Web3(HTTPProvider("https://u0oaXXXXXX-u0c4XXXXXX-rpc.us-east-2.kaleido.io"))
w3.middleware_stack.inject(geth_poa_middleware, layer=0)
Greeter = w3.eth.contract(abi=contract_interface['abi'], bytecode=contract_interface['bin'])
tx_hash = Greeter.constructor().transact({ 'from': w3.eth.accounts[0], 'gas': 1000000})
tx_receipt = w3.eth.waitForTransactionReceipt(tx_hash)
print('Deployed greeter contract: {}'.format(tx_receipt.contractAddress))
# address from previous deployment
contract_address = Web3.toChecksumAddress(tx_receipt.contractAddress)
greeter = w3.eth.contract(
address=contract_address,
abi=contract_interface['abi'],
)
print('Default contract greeting: {}'.format(
greeter.functions.greet().call()
))
print('Setting the greeting to Nihao...')
tx_hash = greeter.functions.setGreeting('Nihao').transact({ 'from': w3.eth.accounts[0], 'gas': 100000})
w3.eth.waitForTransactionReceipt(tx_hash)
print('Updated contract greeting: {}'.format(
greeter.functions.greet().call()
))
reader = ConciseContract(greeter)
assert reader.greet() == "Nihao"
moghadasian,
The "VM in read-only mode" is because you are using call to interact with your Smart Contract method. So it's just calling your method in a read-only mode. You would use this to call methods on contracts that query data - without having to submit a transaction to the chain.
[edit] - the above advice is generally helpful for "VM in read-only mode", but if you're trying out python web3, you pobably want the other answer with a full working example: https://stackoverflow.com/a/51155413/4972840 [/edit]
Regards, Peter

Unable to get value from another contract

I am facing a head scratching issue. I have created two contracts UserRole which has a map of username to a role and a Base contract that has a modifier which checks if the role is < 10.
So I deploy the first UserRole contract first and then I called the set function with the parameters _username = "jamesbond" and _role=7.
After the transaction is mined I call the getRole passing _username = "jamesbond" and I get back 7.
Now I deploy Base and pass the address of the the UserRole contract that I deployed earlier. I call the testModifier function and I pass it _username = "jamesbond". I expect that I get the value 7 back.
I tested this on http://remix.ethereum.org first. Then I tried it on quorum and parity. On remix it works as expected but on both quorum and parity I do not get any values back.
I am not sure what I am doing wrong.
pragma solidity ^0.4.24;
contract UserRole {
address owner;
mapping (string => uint8) userRoles;
constructor()
public
{
owner = msg.sender;
}
function set(string _username, uint8 _role)
public
returns (bool sucesss)
{
userRoles[_username] = _role;
return true;
}
function getRole(string _username)
public
view
returns (uint8 _role)
{
return userRoles[_username];
}
}
contract Base {
address owner;
UserRole userRole;
address public userRoleAddress;
constructor(address _t)
public
{
owner = msg.sender;
userRoleAddress = _t;
userRole = UserRole(_t);
}
modifier isAuthorized(string _username) {
uint8 role = 5;
require(role < 10);
_;
}
function testModifier(string _username)
public
isAuthorized(_username)
view
returns (uint8 result)
{
return userRole.getRole(_username);
}
}
I have faced similar issues when compiling the contract with Remix.
The solution is as follows:
Install solcjs using
npm install -g solc
It will provide executable binary of solcjs.
2.Create two file named UserRole.sol and Base.sol and copy the respective code in the files. Compile both the contracts using solcjs compiler (binary installed in your system.).
solcjs -o output --bin --abi UserRole.sol
solcjs -o output --bin --abi Base.sol
It will produce two abi and two bin file inside output folder.
Use these abi and bin of respective contracts to create similar script like web3deploy and deploy them in quorum or parity.
This will work.