package com.yourcompany.web3.apps; // Super important: Our app uses the generated Java from solc! import com.yourcompany.web3.generated.TheContract; import java.math.BigInteger; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.web3j.crypto.Credentials; import org.web3j.crypto.WalletUtils; import org.web3j.protocol.Web3j; import org.web3j.protocol.core.methods.response.TransactionReceipt; import org.web3j.protocol.http.HttpService; import org.web3j.tx.Contract; // for GAS_LIMIT constant import org.web3j.tx.ManagedTransaction; // GAS_PRICE constant import org.web3j.tx.Transfer; import org.web3j.utils.Convert; import org.web3j.utils.Numeric; import org.web3j.tuples.generated.Tuple2; public class access1 { private static final Logger log = LoggerFactory.getLogger(access1.class); public static void main(String[] args) throws Exception { new access1().run(args); } private void run(String[] args) { String contractAddr = args[0]; try { // This is the geth dev server you are running. Note: NOT https! String MY_CONN_URL = "http://127.0.0.1:8545"; // This is the developer wallet autocreated and funded by the --dev option. // There should be only one file in the keystore directory; use that one. // The actual name will be different than this. Again, the point of using // --datadir with the --dev option is that this resource will be created // just once and you don't have to keep finding a new wallet file and // change the program: String MY_WALLET_FILE = "yourhome/eth/dev/data/keystore/UTC--2020-08-30T15-35-42.179527700Z--ad2a9bde6c3fe0149c29db01ed40966b1b06f3d0"; String MY_WALLET_PWD = ""; // IMPORTANT! The password for the geth --dev developer account wallet is BLANK! System.out.println("Connecting to " + MY_CONN_URL); Web3j web3j = Web3j.build(new HttpService(MY_CONN_URL)); System.out.println("Loading credentials..."); Credentials credentials = WalletUtils.loadCredentials(MY_WALLET_PWD, MY_WALLET_FILE); TheContract ct2 = TheContract.load(contractAddr, web3j, credentials, ManagedTransaction.GAS_PRICE, Contract.GAS_LIMIT); String cpty = ct2.getCounterparty().send(); System.out.println("cpty: " + cpty); // State changing function do NOT return regular types, only a // TransactionReceipt TransactionReceipt TXr = ct2.changeCounterparty("FOO").send(); System.out.println(" TX status: " + TXr.getStatus()); System.out.println(" TX root: " + TXr.getRoot()); System.out.println(" TX TXhash: " + TXr.getTransactionHash()); System.out.println(" TX blk hash: " + TXr.getBlockHash()); System.out.println(" TX blk num: " + TXr.getBlockNumber()); System.out.println(" TX gas used: " + TXr.getGasUsed()); // this is particularly cool System.out.println(" TX from: " + TXr.getFrom()); System.out.println(" TX to: " + TXr.getTo()); // Thanks to uint256, all integers coming out of smart contracts use // BigInteger, not int or long. Tuple2 result = ct2.getInfo().send(); System.out.println("info: " + result.getValue1() + "," + result.getValue2()); web3j.shutdown(); } catch(Exception e) { System.out.println("exception: " + e); } } }