Smart Contract in Geth using web3j

946 views Asked by At

I am trying to deploy my solidity smart contract on the ethereum testrpc network. This is my solidity contract:

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

I am using web3j for deploying this contract in the ethereum testrpc network which is now running only in one computer. Following is the command i am using in JAVA console:

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

I have generated all the necessary abi and bin for the above contract.

I have even generated a wrapper as directed for the above contract. This is my wrapper for the contract:

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

}

But I am not able to deploy on this tetrpc Ethereum Network. Could anyone answer WHY?

1

There are 1 answers

0
swatarianess On

You must check that your client has either; Connected to the testnet and is running a node, or has attached to a geth node that is connected to the testnet.

Can you provide code that shows how you try to connect to the testnet?

I was able to attach to my geth node by the following;

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