Skip to content

Commit

Permalink
1. Added MetaCoin smart contract wrapper to integration-tests module …
Browse files Browse the repository at this point in the history
…and build script.

2. Documentation updates.
  • Loading branch information
conor10 committed Nov 13, 2017
1 parent 1884378 commit 31b30ad
Show file tree
Hide file tree
Showing 6 changed files with 176 additions and 5 deletions.
5 changes: 4 additions & 1 deletion README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,10 @@ Features
client API over HTTP and IPC
- Ethereum wallet support
- Auto-generation of Java smart contract wrappers to create, deploy, transact with and call smart
contracts from native Java code (Solidity and Truffle definition formats supported)
contracts from native Java code
(`Solidity <http://solidity.readthedocs.io/en/latest/using-the-compiler.html#using-the-commandline-compiler>`_
and
`Truffle <https://github.com/trufflesuite/truffle-contract-schema>`_ definition formats supported)
- Reactive-functional API for working with filters
- `Ethereum Name Service (ENS) <https://ens.domains/>`_ support
- Support for Parity's
Expand Down
20 changes: 20 additions & 0 deletions codegen/src/test/resources/truffle/build.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
#!/usr/bin/env bash

targets="
MetaCoin/MetaCoin
"

for target in ${targets}; do
dirName=$(dirname $target)
fileName=$(basename $target)

cd $dirName
echo "Generating web3j bindings"
web3j truffle generate \
build/contracts/${fileName}.json \
-p org.web3j.generated \
-o ../../../../../../integration-tests/src/test/java/ > /dev/null
echo "Complete"

cd -
done
4 changes: 2 additions & 2 deletions docs/source/command_line.rst
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ To send Ether to another address:
.. code-block:: bash
$ web3j wallet send <walletfile> <destination-address>
$ web3j wallet send <walletfile> 0x<address>|<ensName>
When sending Ether to another address you will be asked a series of questions before the
transaction takes place. See the below for a full example
Expand All @@ -73,7 +73,7 @@ The following example demonstrates using web3j to send Ether to another wallet.
.. code-block:: bash
$ ./web3j-<version>/bin/web3j wallet send <walletfile> <destination-address>
$ ./web3j-<version>/bin/web3j wallet send <walletfile> 0x<address>|<ensName>
_ _____ _ _
| | |____ (_) (_)
Expand Down
5 changes: 4 additions & 1 deletion docs/source/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,10 @@ Features
client API over HTTP and IPC
- Ethereum wallet support
- Auto-generation of Java smart contract wrappers to create, deploy, transact with and call smart
contracts from native Java code (Solidity and Truffle definition formats supported)
contracts from native Java code
(`Solidity <http://solidity.readthedocs.io/en/latest/using-the-compiler.html#using-the-commandline-compiler>`_
and
`Truffle <https://github.com/trufflesuite/truffle-contract-schema>`_ definition formats supported)
- Reactive-functional API for working with filters
- `Ethereum Name Service (ENS) <https://ens.domains/>`_ support
- Support for Parity's
Expand Down
5 changes: 4 additions & 1 deletion docs/source/smart_contracts.rst
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,10 @@ The smart contract wrappers support all common operations for working with smart
Any method calls that requires an underlying JSON-RPC call to take place will return a Future to
avoid blocking.

web3j also supports the generation of Java smart contract function wrappers directly from `Truffle <http://truffleframework.com/>`_ via the :doc:`command_line` utility.
web3j also supports the generation of Java smart contract function wrappers directly from
`Truffle's <http://truffleframework.com/>`_
`Contract Schema <https://github.com/trufflesuite/truffle-contract-schema>`_
via the :doc:`command_line` utility.

.. code-block:: bash
Expand Down
142 changes: 142 additions & 0 deletions integration-tests/src/test/java/org/web3j/generated/MetaCoin.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,142 @@
package org.web3j.generated;

import java.math.BigInteger;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import org.web3j.abi.EventEncoder;
import org.web3j.abi.EventValues;
import org.web3j.abi.TypeReference;
import org.web3j.abi.datatypes.Address;
import org.web3j.abi.datatypes.Event;
import org.web3j.abi.datatypes.Function;
import org.web3j.abi.datatypes.Type;
import org.web3j.abi.datatypes.generated.Uint256;
import org.web3j.crypto.Credentials;
import org.web3j.protocol.Web3j;
import org.web3j.protocol.core.DefaultBlockParameter;
import org.web3j.protocol.core.RemoteCall;
import org.web3j.protocol.core.methods.request.EthFilter;
import org.web3j.protocol.core.methods.response.Log;
import org.web3j.protocol.core.methods.response.TransactionReceipt;
import org.web3j.tx.Contract;
import org.web3j.tx.TransactionManager;
import rx.Observable;
import rx.functions.Func1;

/**
* <p>Auto generated code.
* <p><strong>Do not modify!</strong>
* <p>Please use the <a href="https://docs.web3j.io/command_line.html">web3j command line tools</a>,
* or the org.web3j.codegen.SolidityFunctionWrapperGenerator in the
* <a href="https://github.com/web3j/web3j/tree/master/codegen">codegen module</a> to update.
*
* <p>Generated with web3j version 3.1.0.
*/
public final class MetaCoin extends Contract {
private static final String BINARY = "0x6060604052341561000f57600080fd5b6127106000803273ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff168152602001908152602001600020819055506103c5806100636000396000f300606060405260043610610057576000357c0100000000000000000000000000000000000000000000000000000000900463ffffffff1680637bd703e81461005c57806390b98a11146100a9578063f8b2cb4f14610103575b600080fd5b341561006757600080fd5b610093600480803573ffffffffffffffffffffffffffffffffffffffff16906020019091905050610150565b6040518082815260200191505060405180910390f35b34156100b457600080fd5b6100e9600480803573ffffffffffffffffffffffffffffffffffffffff169060200190919080359060200190919050506101f8565b604051808215151515815260200191505060405180910390f35b341561010e57600080fd5b61013a600480803573ffffffffffffffffffffffffffffffffffffffff16906020019091905050610351565b6040518082815260200191505060405180910390f35b600073__ConvertLib____________________________6396e4ee3d61017584610351565b60026000604051602001526040518363ffffffff167c0100000000000000000000000000000000000000000000000000000000028152600401808381526020018281526020019250505060206040518083038186803b15156101d657600080fd5b6102c65a03f415156101e757600080fd5b505050604051805190509050919050565b6000816000803373ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff168152602001908152602001600020541015610249576000905061034b565b816000803373ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff16815260200190815260200160002060008282540392505081905550816000808573ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff168152602001908152602001600020600082825401925050819055508273ffffffffffffffffffffffffffffffffffffffff163373ffffffffffffffffffffffffffffffffffffffff167fddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef846040518082815260200191505060405180910390a3600190505b92915050565b60008060008373ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff1681526020019081526020016000205490509190505600a165627a7a72305820f791829bf0c920a6d43123cac9b9894757ddc838c17efbad6d56773d6e3dcf4c0029";

protected static final HashMap<String, String> _addresses;

static {
_addresses = new HashMap<>();
_addresses.put("4", "0xaea9d31a4aeda9e510f7d85559261c16ea0b6b8b");
_addresses.put("4447", "0x254dffcd3277c0b1660f6d42efbb754edababc2b");
}

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

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

public List<TransferEventResponse> getTransferEvents(TransactionReceipt transactionReceipt) {
final Event event = new Event("Transfer",
Arrays.<TypeReference<?>>asList(new TypeReference<Address>() {}, new TypeReference<Address>() {}),
Arrays.<TypeReference<?>>asList(new TypeReference<Uint256>() {}));
List<EventValues> valueList = extractEventParameters(event, transactionReceipt);
ArrayList<TransferEventResponse> responses = new ArrayList<TransferEventResponse>(valueList.size());
for (EventValues eventValues : valueList) {
TransferEventResponse typedResponse = new TransferEventResponse();
typedResponse._from = (String) eventValues.getIndexedValues().get(0).getValue();
typedResponse._to = (String) eventValues.getIndexedValues().get(1).getValue();
typedResponse._value = (BigInteger) eventValues.getNonIndexedValues().get(0).getValue();
responses.add(typedResponse);
}
return responses;
}

public Observable<TransferEventResponse> transferEventObservable(DefaultBlockParameter startBlock, DefaultBlockParameter endBlock) {
final Event event = new Event("Transfer",
Arrays.<TypeReference<?>>asList(new TypeReference<Address>() {}, new TypeReference<Address>() {}),
Arrays.<TypeReference<?>>asList(new TypeReference<Uint256>() {}));
EthFilter filter = new EthFilter(startBlock, endBlock, getContractAddress());
filter.addSingleTopic(EventEncoder.encode(event));
return web3j.ethLogObservable(filter).map(new Func1<Log, TransferEventResponse>() {
@Override
public TransferEventResponse call(Log log) {
EventValues eventValues = extractEventParameters(event, log);
TransferEventResponse typedResponse = new TransferEventResponse();
typedResponse._from = (String) eventValues.getIndexedValues().get(0).getValue();
typedResponse._to = (String) eventValues.getIndexedValues().get(1).getValue();
typedResponse._value = (BigInteger) eventValues.getNonIndexedValues().get(0).getValue();
return typedResponse;
}
});
}

public RemoteCall<BigInteger> getBalanceInEth(String addr) {
Function function = new Function("getBalanceInEth",
Arrays.<Type>asList(new org.web3j.abi.datatypes.Address(addr)),
Arrays.<TypeReference<?>>asList(new TypeReference<Uint256>() {}));
return executeRemoteCallSingleValueReturn(function, BigInteger.class);
}

public RemoteCall<TransactionReceipt> sendCoin(String receiver, BigInteger amount) {
Function function = new Function(
"sendCoin",
Arrays.<Type>asList(new org.web3j.abi.datatypes.Address(receiver),
new org.web3j.abi.datatypes.generated.Uint256(amount)),
Collections.<TypeReference<?>>emptyList());
return executeRemoteCallTransaction(function);
}

public RemoteCall<BigInteger> getBalance(String addr) {
Function function = new Function("getBalance",
Arrays.<Type>asList(new org.web3j.abi.datatypes.Address(addr)),
Arrays.<TypeReference<?>>asList(new TypeReference<Uint256>() {}));
return executeRemoteCallSingleValueReturn(function, BigInteger.class);
}

public static RemoteCall<MetaCoin> deploy(Web3j web3j, Credentials credentials, BigInteger gasPrice, BigInteger gasLimit) {
return deployRemoteCall(MetaCoin.class, web3j, credentials, gasPrice, gasLimit, BINARY, "");
}

public static RemoteCall<MetaCoin> deploy(Web3j web3j, TransactionManager transactionManager, BigInteger gasPrice, BigInteger gasLimit) {
return deployRemoteCall(MetaCoin.class, web3j, transactionManager, gasPrice, gasLimit, BINARY, "");
}

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

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

protected String getStaticDeployedAddress(String networkId) {
return _addresses.get(networkId);
}

public static class TransferEventResponse {
public String _from;

public String _to;

public BigInteger _value;
}
}

0 comments on commit 31b30ad

Please sign in to comment.