Deploy and Interact with Smart Contracts
Working with smart contracts and Java smart contract wrappers¶
Web3j can auto-generate smart contract wrapper code to deploy and interact with smart contracts without leaving the JVM.
To generate the wrapper code, compile your smart contract:
$ solc <contract>.sol --bin --abi --optimize -o <output-dir>/
Then generate the wrapper code using the Web3j CLI:
web3j generate solidity -b /path/to/<smart-contract>.bin -a /path/to/<smart-contract>.abi -o /path/to/src/main/java -p com.your.organisation.name
Now you can create and deploy your smart contract:
Web3j web3 = Web3j.build(new HttpService()); // defaults to http://localhost:8545/ Credentials credentials = WalletUtils.loadCredentials("password", "/path/to/walletfile"); YourSmartContract contract = YourSmartContract.deploy( <web3j>, <credentials>, GAS_PRICE, GAS_LIMIT, <param1>, ..., <paramN>).send(); // constructor params
Or use an existing contract:
YourSmartContract contract = YourSmartContract.load( "0x<address>|<ensName>", <web3j>, <credentials>, GAS_PRICE, GAS_LIMIT);
To transact with a smart contract:
TransactionReceipt transactionReceipt = contract.someMethod( <param1>, ...).send();
To call a smart contract:
Type result = contract.someMethod(<param1>, ...).send();
For more information refer to Solidity smart contract wrappers.
Smart contract examples¶
Web3j provides a number of smart contract examples in the project directory codegen/src/test/resources/solidity
It also provides integration tests for demonstrating the deploying and working with those smart contracts in the integration-tests/src/test/java/org/web3j/protocol/scenarios module.
EIP-20 Ethereum token standard smart contract¶
The EIP-20 standard provides function definitions, but does not provide an implementation example. However, there is an implementation provided in codegen/src/test/resources/solidity/contracts, which has been taken from ConsenSys' Tokens project.
Open Zeppelin also provide an example implementation on GitHub.
There are two integration tests that have been written to fully demonstrate the functionality of this token smart contract.
Alternatively, if you do not wish to use a smart contract wrapper and would like to work directly with the underlying JSON-RPC calls, please refer to HumanStandardTokenIT.