web3连接到testnetwork

时间:2018-01-22 10:00:11

标签: ethereum go-ethereum

我尝试将web3.php连接到智能合约,通过metamask在ropsten测试网络中发布。我使用了以下web3.php库:(https://github.com/sc0Vu/web3.php)。要创建一个rpc服务器,我使用ethereum go并运行以下命令:

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

哪个应该成为测试节点,连接到ropsten测试网。现在我在php中运行以下代码,但我得到的结果总是" 0x0",这意味着,它找不到智能合约。有谁有想法,为什么它没有在ropsten测试网络中找到合同?我想我以某种方式与ethereum-go没有联系。我还在当地的testrpc上发布了智能合约,它的工作正常。

<?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;
    }
});

0 个答案:

没有答案