繁体   English   中英

使用Web3j在Geth中进行智能合约

[英]Smart Contract in Geth using web3j

我正在尝试在以太坊testrpc网络上部署我的Solidity智能合约。 这是我的固定合同:

pragma solidity ^0.4.11;

contract Fibonacci {

    event Notify(uint input, uint result);

    function fibonacci(uint number) constant returns(uint result) {
        if (number == 0) return 0;
        else if (number == 1) return 1;
        else return Fibonacci.fibonacci(number - 1) + Fibonacci.fibonacci(number - 2);
    }

    function fibonacciNotify(uint number) returns(uint result) {
        result = fibonacci(number);
        Notify(number, result);
    }
}

我正在使用web3j在以太坊testrpc网络中部署此合同,该网络现在仅在一台计算机上运行。 以下是我在JAVA控制台中使用的命令:

Future<FibonacciZKF> fib= FibonacciZKF.deploy(Web3j.build(new HttpService()), ALICE, GAS_PRICE, GAS_LIMIT, BigInteger.valueOf(5));

我已经为上述合同生成了所有必要的abi和bin。

我什至根据上述合同生成了一个包装器。 这是我的合同包装:

public final class Fibonacci extends Contract {
    private static final String BINARY = "6060604052341561000f57600080fd5b5b61015a8061001f6000396000f300606060405263ffffffff7c01000000000000000000000000000000000000000000000000000000006000350416633c7fdc70811461004857806361047ff414610070575b600080fd5b341561005357600080fd5b61005e600435610098565b60405190815260200160405180910390f35b341561007b57600080fd5b61005e6004356100e5565b60405190815260200160405180910390f35b60006100a3826100e5565b90507f71e71a8458267085d5ab16980fd5f114d2d37f232479c245d523ce8d23ca40ed828260405191825260208201526040908101905180910390a15b919050565b60008115156100f6575060006100e0565b8160011415610107575060016100e0565b610113600283036100e5565b61011f600184036100e5565b0190506100e0565b5b5b9190505600a165627a7a723058208c691bf2e824f14fefeb8b2e4932d9a9122cbe5835242992958daf1fc4a81af60029";

private Fibonacci(String contractAddress, Web3j web3j, Credentials credentials, BigInteger gasPrice, BigInteger gasLimit) {
    super(BINARY, contractAddress, web3j, credentials, gasPrice, gasLimit);
}

private Fibonacci(String contractAddress, Web3j web3j, TransactionManager transactionManager, BigInteger gasPrice, BigInteger gasLimit) {
    super(BINARY, contractAddress, web3j, transactionManager, gasPrice, gasLimit);
}

public List<NotifyEventResponse> getNotifyEvents(TransactionReceipt transactionReceipt) {
    final Event event = new Event("Notify", 
            Arrays.<TypeReference<?>>asList(),
            Arrays.<TypeReference<?>>asList(new TypeReference<Uint256>() {}, new TypeReference<Uint256>() {}));
    List<EventValues> valueList = extractEventParameters(event, transactionReceipt);
    ArrayList<NotifyEventResponse> responses = new ArrayList<NotifyEventResponse>(valueList.size());
    for (EventValues eventValues : valueList) {
        NotifyEventResponse typedResponse = new NotifyEventResponse();
        typedResponse.input = (Uint256) eventValues.getNonIndexedValues().get(0);
        typedResponse.result = (Uint256) eventValues.getNonIndexedValues().get(1);
        responses.add(typedResponse);
    }
    return responses;
}

public Observable<NotifyEventResponse> notifyEventObservable(DefaultBlockParameter startBlock, DefaultBlockParameter endBlock) {
    final Event event = new Event("Notify", 
            Arrays.<TypeReference<?>>asList(),
            Arrays.<TypeReference<?>>asList(new TypeReference<Uint256>() {}, new TypeReference<Uint256>() {}));
    EthFilter filter = new EthFilter(startBlock, endBlock, getContractAddress());
    filter.addSingleTopic(EventEncoder.encode(event));
    return web3j.ethLogObservable(filter).map(new Func1<Log, NotifyEventResponse>() {
        @Override
        public NotifyEventResponse call(Log log) {
            EventValues eventValues = extractEventParameters(event, log);
            NotifyEventResponse typedResponse = new NotifyEventResponse();
            typedResponse.input = (Uint256) eventValues.getNonIndexedValues().get(0);
            typedResponse.result = (Uint256) eventValues.getNonIndexedValues().get(1);
            return typedResponse;
        }
    });
}

public Future<TransactionReceipt> fibonacciNotify(Uint256 number) {
    Function function = new Function("fibonacciNotify", Arrays.<Type>asList(number), Collections.<TypeReference<?>>emptyList());
    return executeTransactionAsync(function);
}

public Future<Uint256> fibonacci(Uint256 number) {
    Function function = new Function("fibonacci", 
            Arrays.<Type>asList(number), 
            Arrays.<TypeReference<?>>asList(new TypeReference<Uint256>() {}));
    return executeCallSingleValueReturnAsync(function);
}

public static Future<Fibonacci> deploy(Web3j web3j, Credentials credentials, BigInteger gasPrice, BigInteger gasLimit, BigInteger initialWeiValue) {
    return deployAsync(Fibonacci.class, web3j, credentials, gasPrice, gasLimit, BINARY, "", initialWeiValue);
}

public static Future<Fibonacci> deploy(Web3j web3j, TransactionManager transactionManager, BigInteger gasPrice, BigInteger gasLimit, BigInteger initialWeiValue) {
    return deployAsync(Fibonacci.class, web3j, transactionManager, gasPrice, gasLimit, BINARY, "", initialWeiValue);
}

public static Fibonacci load(String contractAddress, Web3j web3j, Credentials credentials, BigInteger gasPrice, BigInteger gasLimit) {
    return new Fibonacci(contractAddress, web3j, credentials, gasPrice, gasLimit);
}

public static Fibonacci load(String contractAddress, Web3j web3j, TransactionManager transactionManager, BigInteger gasPrice, BigInteger gasLimit) {
    return new Fibonacci(contractAddress, web3j, transactionManager, gasPrice, gasLimit);
}

public static class NotifyEventResponse {
    public Uint256 input;

    public Uint256 result;
}

}

但是我不能在这个tetrpc以太坊网络上进行部署。 谁能回答为什么?

您必须检查您的客户是否有; 已连接到testnet并正在运行一个节点,或者已连接到已连接到testnet的geth节点。

您能否提供显示如何尝试连接到测试网的代码?

我可以通过以下方式附加到我的geth节点;

    try {
        web3j = Web3j.build(new HttpService("Http://localhost:8545"));
    } catch (InterruptedException | ExecutionException e) {
        e.printStackTrace();
    }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM