package Default;

import org.web3j.crypto.Credentials;
import org.web3j.crypto.WalletUtils;
import org.web3j.protocol.Web3j;
import org.web3j.protocol.core.methods.response.Web3ClientVersion;
import org.web3j.protocol.http.HttpService;
import org.web3j.tx.Contract;
import org.web3j.tx.gas.DefaultGasProvider;

public class EthereumSolidityApplication {

	public static void main(String[] args) {

		/*
		 * Open a connection to the geth node using rpc
		 */
		Web3j web3 = Web3j.build(new HttpService("http://localhost:8545"));
		
		try {
			/*
			 * Use RPC send() to get the client version of geth
			 */
			Web3ClientVersion web3ClientVersion = web3.web3ClientVersion().send();
			String clientVersion = web3ClientVersion.getWeb3ClientVersion();
			System.out.println("Client is version: "+clientVersion);
			
			/*
			 * Open a keystore file using the provided password 
			 * to load transaction signing credentials
			 */
			System.out.println("Getting credentials");
	        Credentials credentials = WalletUtils.loadCredentials("password", "/home/ubuntu/.ethereum/keystore/UTC--2019-04-25T17-44-11.125457608Z--7d01838cde5ab81b171b64e4216c5f3dcbe45c66");

	        /*
	         * We use the wrapper class to deploy the smart 
	         * contract, using a synchronous call 
	         * to wait until it is mined and available
	         */
			System.out.println("Deploying contract");
	        Contract1 contract = Contract1.deploy(web3, credentials, new DefaultGasProvider()).send();
	        
	        /*
	         * The return value will have the contract address
	         * (as well as the transaction receipt for more info)
	         */
	        System.out.println("Smart contract address: " + contract.getContractAddress());

	        System.out.println("\nTransaction Info");
	        System.out.println("  Tx Hash: " + contract.getTransactionReceipt().get().getTransactionHash());
	        System.out.println("  Block Hash: " + contract.getTransactionReceipt().get().getBlockHash());
	        System.out.println("  Block Number: " + contract.getTransactionReceipt().get().getBlockNumber());
	        System.out.println("  Tx Index: " + contract.getTransactionReceipt().get().getTransactionIndex());
	        System.out.println("  Status: " + contract.getTransactionReceipt().get().getStatus());
	        System.out.println("  Gas Used: " + contract.getTransactionReceipt().get().getGasUsed());

	        /*
	         * Let's check that it's valid
	         */
//	        if (contract.isValid()) {
	        	System.out.println("Contract is valid!");
		        /*
		         * We can now use the wrapper class to interact with the contract
		         * 
		         * Again, we use synchronous calls to wait until the change is 
		         * mined into the blockchain before we continue
		         */
		        System.out.println("Counter value: " + contract.getCounter().send());
		        contract.inc().send();
		        System.out.println("Counter value: " + contract.getCounter().send());
		        contract.dec().send();
		        System.out.println("Counter value: " + contract.getCounter().send());

		        /*
		         * When we are done playing, we get the contract to self-destruct
		         */
		        contract.kill().send();
		        System.out.println("Done.");
//	        } else {
//	        	System.out.println("Contract is NOT valid; something went wrong; Oops.");
//	        }
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		/*
		 * Close the RPC connection when we're done
		 */
		web3.shutdown();
	}

}
