Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Interacting with the Blockchain via Android

serjec
September 24, 2019

Interacting with the Blockchain via Android

Blockchain is disrupting software industry. The term itself has become a popular topic. Everyone is talking about blockchain and its usages in decentralized applications. How can we be part of it? In this talk, i am going to explain to you roughly about dApps, Blockchains, and how can we interact our Mobile Android App with the Ethereum Network.

serjec

September 24, 2019
Tweet

Other Decks in Programming

Transcript

  1. Why Blockchain? • >$268B • Disrupted technology • Digital Freedom

    • Keep ID and personal info secure • Goverment and companies taking it „serious“
  2. What it is Blockchain? Blockchain is a technology that allows

    to make instantaneous transactions on a network without any middlemen. Keeping a record of what happens
  3. Why is it called Blockchain? Transactions Transactions Transactions Previous Hash

    Root Hash Nonce Timestamp Previous Hash Root Hash Nonce Timestamp Previous Hash Root Hash Nonce Timestamp
  4. How does it work? A User request a transaction A

    block representing the transaction is created The block it‘s broadcasted to all the nodes of the network All the nodes validate the block and the transaction The block is added to the chain The transaction is verified and executed
  5. Advantages of Blockchain • Transparency • Security • Instanteneous Transactions

    • Decentralized • Immutable • Double spend problem solved
  6. Brief History 1990 2009 2011/12 2012/13 The concept of distributed

    computing Bitcoin Currency payments Smart Contracts 2013/14 2014/15 Finantial markets using blockchain beyond cash transaction Contracts Origin Transactions 2015/16 2016/17 2018/19 NASDAQ blockchain trials Market consolidation and more developments Year of ICO and Adoption Application
  7. Bitcoin • Created in 2009 • Market Cap of >$180B

    • All Digital „Cryptocurrency“
  8. The Rise of Ethereum • Decentralization Application Platform • The

    world of computer • Turing-complete virtual machine • Other assets • Smart Contract • Public blockchain (main & test) • Ether • 2nd largest crypto
  9. Smart Contracts • Smartcontract = human being • Computarized contract

    • Immutable code which lives on the blockchain in certain address
  10. Power of Smart Contracts 1. Trust each other 2. Sign

    a legal agreement 3. Get help from a mutual friend
  11. What do we need? • Web3j • Infura • Metamask

    • Web3j Gradle plugin • Remix IDE
  12. Web3j • Type safe Java/Android library • Command line tools

    • Ethereum wallet support • Smart contract wrappers • Easier integration with nodes on the Ethereum Network
  13. Connecting to Ethereum Network web3 = Web3j.build(HttpService("https://ropsten.infura.io/v3/YOUR_KEY}")) try { val

    clientVersion = web3.web3ClientVersion().sendAsync().get() if (!clientVersion.hasError()) { toast("Connected!") } else { toast(clientVersion.error.message) } } catch (e: Exception) { toast(e.message!!) }
  14. Create a Wallet try { lastWalletFile = WalletUtils.generateNewWalletFile(PASSWORD, walletDir) toast("Wallet

    created!") } catch (e: Exception) { toast("Error creating wallet!") e.printStackTrace() }
  15. Send Ether val credentials = WalletUtils.loadCredentials(PASSWORD, File(walletPath + "/${lastWalletFile}")) val

    receipt = sendFunds( web3, credentials, “to-adddress“ BigDecimal(“how much ether?“), Convert.Unit.ETHER ).sendAsync().get()
  16. Get Balance val ethGetBalance = web3 .ethGetBalance(walletAddressText.text.toString(), DefaultBlockParameterName.LATEST) .sendAsync() .get()

    // 1 wei = 10^-18 Ether toast("Balance ${Convert.fromWei(ethGetBalance.balance.toString(), Convert.Unit.ETHER)} ether")
  17. Smart Contract contract Mortal { /* Define variable owner of

    the type address */ address owner; /* This constructor is executed at initialization and sets the owner of the contract */ constructor() public { owner = msg.sender; } /* Function to recover the funds on the contract */ function kill() public { if (msg.sender == owner) selfdestruct(msg.sender); } }
  18. Smart Contract contract Greeter is Mortal { /* Define variable

    greeting of the type string */ string greeting; /* This runs when the contract is executed */ constructor(string _greeting) public { greeting = _greeting; } /* change greeting */ function changeGreeting(string _greeting) public { greeting = _greeting; } /* Main function */ function greet() public view returns (string) { return greeting; } }
  19. Interacting with a Smart Contract buildscript { repositories { mavenCentral()

    } dependencies { classpath ''org.web3j:web3j-gradle-plugin:4.3.0‘ } } apply plugin: 'org.web3j' plugins { id 'org.web3j' version '0.1.4' }
  20. Read/Write into a Smart Contract val greeter = Greeter.load(contractAddress, web3,

    credentials, gasLimit, gasPrice) // check contract validity toast("Greeter valid: ${greeter.isValid}") // read from contract val greeting: Future<String>? = greeter.greet().sendAsync() val convertToString: String? = greeting?.get() toast(“Smart contract returned: $convertToString") // write to contract val transactionReceipt: Future<TransactionReceipt>? = greeter.changeGreeting(smartContractText.text.toString()).sendAsync() val result = "Successful transaction. Gas used: ${transactionReceipt?.get()?.blockNumber} ${transactionReceipt?.get()?.gasUsed}" toast(result)