0

I try to connect with web3.php to a smart contract, published in the ropsten test network via metamask. I used the following web3.php library: (https://github.com/sc0Vu/web3.php). To create an rpc server, I used ethereum go and run the following commands:

geth --testnet removedb
geth --testnet --rpc

Which should make a test node, connected to the ropsten test net. Now I run the following code in php, but the result i get back is always "error", which means, it does not find the smart contract. Has anyone an idea, why it does not find the contract in the ropsten test network? I think somehow I'm not connected to it with ethereum-go. I also published the smart contract on a local testrpc, where it works fine.

<?php

require('vendor/autoload.php');

use Web3\Web3;
use Web3\Contract;
use Web3\Providers\HttpProvider;
use Web3\RequestManagers\HttpRequestManager;

$abi='[
    {
        "constant": true,
        "inputs": [],
        "name": "value",
        "outputs": [
            {
                "name": "",
                "type": "uint256"
            }
        ],
        "payable": false,
        "stateMutability": "view",
        "type": "function"
    },
    {
        "constant": true,
        "inputs": [],
        "name": "getValue",
        "outputs": [
            {
                "name": "",
                "type": "uint256"
            }
        ],
        "payable": false,
        "stateMutability": "view",
        "type": "function"
    },
    {
        "inputs": [],
        "payable": false,
        "stateMutability": "nonpayable",
        "type": "constructor"
    }
]';

$web3 = new Web3(new HttpProvider(new HttpRequestManager('http://localhost:8545')));

$contract = new Contract($web3->provider, $abi);
$contractAddress='0x3ed9bd43bb425c0acc202421f54e8de72b8d91f9';
$functionName='getValue';

$contract->at($contractAddress)->call($functionName, function ($err, $result) use ($contract) {
    if ($err !== null) {
        echo "error";
        throw $err;
    }

    if ($result) {
        echo $result;
    }
});

When I start my node, i get the following command output: enter image description here

Edit: eth.getBlock('latest') enter image description here

  • Is it synced up with the network? if you use eth.syncing and get a false but eth.getBlock('latest') differs from the actual tip of the chain, or the fist comands returns something other than false, then the chain is not synced up. As you have launched the node without a console you should attach to it to do this (geth attach http:localhost:8545) – Hari GTT Psicolabis Jan 24 '18 at 20:48
  • Hi Hari, eth.syncing gives me back false, so this should be alright. Can I somehow see if the contract is actually in the network? How can I see uf eth.getBlock('latest') is the actual tip of the chain? – HansPeterLoft Jan 25 '18 at 16:34
  • I think i found the problem, when I run "geth --testnet --rpc", it won't begin to sync. So what do I need to do to sync the testnet with go-ethereum? Only "geth --rpc" will start to sync, but on the main net, that's not what I want for testing. – HansPeterLoft Jan 25 '18 at 20:25
0

Based on your comments, its a fairly common problem. Default bootnodes tend to be overloaded, so adding others should help you. This gist should be enough

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.