Calling function in one contract from another contract in private blockchain - ethereum

I have problem with smart contract development using solidity. i have two smart contracts such as Factory contract and Contract contract. Here is my code.
Contract Factory {
address[] newContracts;
function createContract(uint num) {
address newContract = new Contract(num);
newContracts.push(newContract);
}
function getContract() view public returns(address[]) {
return newContracts;
}
function getNum(address _add) view public returns(uint) {
return Contract(_add).getNum();
}
}
contract Contract {
uint public Num;
function Contract(uint num) {
Num = num;
}
function getNum() public returns(uint) {
return Num;
}
}
I create the Factory contract in private blockchain. I call the createContract and getContract function, it works normally but when i call the getNum function i can not get the number. Thank you for your answer!
the picture of smartcontract problem

After some tests, please check whether you have byzantiumBlock: 0 in your genesis file. If not, please add it and regenerate your private chain. See my genesis file below.
{
"config": {
"chainId": 1994,
"homesteadBlock": 0,
"eip155Block": 0,
"eip158Block": 0,
"byzantiumBlock": 0
},
"difficulty": "400",
"gasLimit": "2000000",
"alloc": {
}
}
After doing this, your code should work.

Related

How to transfer on truffle develop

I made this contract:
pragma solidity ^0.4.25;
contract MyTransfer {
address owner;
uint data;
uint private amount;
string greeting = "Hello World";
constructor() public {
owner = msg.sender;
}
function greet () constant public returns (string) {
return greeting;
}
function deposit() public payable {
amount += msg.value;
}
function withdraw() public {
msg.sender.transfer(amount);
}
function kill () public {
require(owner == msg.sender);
selfdestruct(owner);
}
}
Compile and deploy successfully finished.
Then on truffle develop console.
mt = MyTransfer.at(MyTransfer.address);
>mt.greet();
works
>mt.deposit(1);
Error: Invalid number of arguments to Solidity function
How can I make transfer on truffle console?
It is not working because your deposit() function is not waiting for any params. You need to send some value with your transaction in order for it to work. Try this:
mt.deposit({value: 'the amount of ether you want to send'});

How to get one value from struct of array in solidity?

I want to get the value that is inside of struct in Solidity,
but I have no idea how to get it.
pragma solidity >=0.4.21 <0.6.0;
contract PlaceList {
struct hoge {
uint id;
address user;
}
hoge[] public hoges;
constructor() public {
admin = msg.sender;
}
function set(uint id) public {
hoges.push(hoge(id, msg.sender));
}
function getId() public view returns(uint) {
return (hoges[0].id);
}
}
When I call getId, console command say this,
ƒ () {
if (abiItemModel.isOfType('constructor')) {
return target.executeMethod(abiItemModel, arguments, 'contract-deployment');
}
return targe…
Could you give me any advise how to get id by using solidity function, please?
hoges[0][0] will work as it points to the first element, 'id' .

Interact between smart contracts does NOT work

I deployed two contracts, one is Callee and the other is Caller. Caller consumes functions provided by Callee. Function call directly to Callee is success, however, Caller does NOT work. Actually, I'v tried different cases from internet, none of them works. Do I miss some tricky things? Below is source code:
Callee.sol
pragma solidity ^0.4.6;
contract Callee {
uint[] public values;
function getValue(uint initial) public pure returns(uint) {
return initial + 150;
}
function storeValue(uint value) public {
values.push(value);
}
function getValues() public view returns(uint) {
return values.length;
}
}
Caller.sol
pragma solidity ^0.4.6;
contract Caller {
function someAction(address addr) public returns(uint) {
Callee c = Callee(addr);
return c.getValue(100);
}
function storeAction(address addr) public returns(uint) {
Callee c = Callee(addr);
c.storeValue(100);
return c.getValues();
}
function someUnsafeAction(address addr) public returns(bool){
return addr.call(bytes4(keccak256("storeValue(uint256)")), 100);
}
}
contract Callee {
function getValue(uint initialValue) public returns(uint);
function storeValue(uint value) public;
function getValues() public returns(uint);
}
Caller is technically working, but not doing what you're expecting.
The problem is that Caller.someAction is not marked with the pure or view modifiers, so your function call is triggering a new transaction. Transaction calls can't return values (it compiles and runs, but nothing is returned).
If you change Caller.someAction to be a pure function (and, also change Callee.getValue in the interface to be pure as well to match the contract), you will get the expected 250 returned.
pragma solidity ^0.4.6;
contract Caller {
function someAction(address addr) public pure returns(uint) {
Callee c = Callee(addr);
return c.getValue(100);
}
function storeAction(address addr) public returns(uint) {
Callee c = Callee(addr);
c.storeValue(100);
return c.getValues();
}
function someUnsafeAction(address addr) public returns(bool){
return addr.call(bytes4(keccak256("storeValue(uint256)")), 100);
}
}
contract Callee {
function getValue(uint initialValue) public pure returns(uint);
function storeValue(uint value) public;
function getValues() public returns(uint);
}
In terminal:
$ truffle migrate --reset
Compiling .\contracts\Callee.sol...
Compiling .\contracts\SimpleContract.sol...
Writing artifacts to .\build\contracts
Using network 'development'.
Running migration: 1_initial_migration.js
Replacing Migrations...
... 0x58a14c93acc733bb08e4bb56978d0bb466f8aca7659673426d989ee7e0e626f3
Migrations: 0x69ed5e4d6172639ed7d3c456ea8b2f2562c7dbcd
Saving successful migration to network...
... 0x9a3831076748cc6179ef3e4b3e466c3a4a871e196376bc22c984e91e95fdc567
Saving artifacts...
Running migration: 2_deploy_contracts.js
Replacing Caller...
... 0x7be6840c86a356decacc13967d50ef4fea30e86d30d69b19fb9998ce500c95e8
Caller: 0xb8c5e079af71813acac73bff9ca8e9e068660e86
Replacing Callee...
... 0xba7f588f9119e67968a7d6ab0110d79e1ecd3003e10cce3134018d42e966c8be
Callee: 0xd5c110c2f6566fd56749ec1a11328405f9935385
Saving successful migration to network...
... 0xc1526876e365189be1529a0943042e3a25e395a5cc19930ad907a694165104f1
Saving artifacts...
$ truffle console
truffle(development)> var caller = Caller.at('0xb8c5e079af71813acac73bff9ca8e9e068660e86');
undefined
truffle(development)> caller.someAction('0xd5c110c2f6566fd56749ec1a11328405f9935385');
{ [String: '250'] s: 1, e: 2, c: [ 250 ] }

Creating a struct causes weird behaviors in remix ide

Problem:
Remix produces weird behaviors with a string param followed by an array param
Reproduce:
contract ItemMarket is ERC721 {
struct Item {
string name;
uint[3] others;
}
Item[] public items;
function createItem(string _name, uint[6] _others) public {
uint tokenId = items.push(Item({name: _name, traits:_traits})) - 1;
}
}
When you call createItem() in remix with the arguments "hello", [1,2,3] the first argument gets converted to \u0000. The same function call with the same arguments works fine when interacted with the contract through MEW
This now works in the latest version of Remix IDE:
pragma solidity 0.5.1;
import "https://github.com/0xcert/ethereum-erc721/src/contracts/tokens/nf-token.sol";
contract ItemMarket is ERC721 {
struct Item {
string name;
uint[3] traits;
}
Item[] public items;
function createItem(string memory name, uint[3] memory traits) public {
items.push(Item({name:name, traits:traits})) - 1;
}
}

How to call contract function from other contract in Ethereum

I have a deployed contract "greeter" in Ethereum
contract mortal {
address owner;
function mortal() { owner = msg.sender; }
function kill() { if (msg.sender == owner) selfdestruct(owner); }
}
contract greeter is mortal {
string greeting;
function greeter(string _greeting) public {
greeting = _greeting;
}
function greet() constant returns (string) {
return greeting;
}
}
And I want to create another contract, which will call function "kill" from my first contract. The main idea is that this 2 contracts is different. I publish contract A and then publish contract B, which calls A.
How I can do that?
Something like this, but from contract...
var contract = web3.eth.contract(contractAddress, ABI);
contract.call().kill();
Approximately like this, but there's a catch.
pragma solidity ^0.4.6;
contract Mortal {
address owner;
function Mortal() { owner = msg.sender; }
function kill() { if (msg.sender == owner) selfdestruct(owner); }
}
contract Greeter is Mortal {
string greeting;
function Greeter(string _greeting) public {
greeting = _greeting;
}
function greet() constant returns (string) {
return greeting;
}
}
contract Killer {
function destroyVictim(address victim) {
Greeter g = Greeter(victim);
g.kill();
}
}
That's a basic syntax. The ABI gets picked up by including Greeter in the source file along with Killer; that is, the compiler can "see it".
So far, so good.
The issue that arises is that Greeter is going to ignore the command, owing to if(msg.sender==owner). It won't be. It will be whatever address Killer got.
A possible solution to this before Greeter is deployed is to anticipate the need for a changeOwner() function, usually reserved for only the current owner.
Hope it helps.