From e811f34f9f51cd554a4d3b8c373073f7517237f3 Mon Sep 17 00:00:00 2001 From: xiaohuo Date: Mon, 27 Apr 2020 15:54:19 +0800 Subject: [PATCH] feat: support deposit through smart contract --- build.gradle | 2 +- .../com/firestack/kit/PollingDeposits.java | 49 ++++++++++++++++--- src/main/java/com/firestack/kit/TxPool.java | 3 +- 3 files changed, 44 insertions(+), 10 deletions(-) diff --git a/build.gradle b/build.gradle index 67757b4..80c2328 100644 --- a/build.gradle +++ b/build.gradle @@ -13,6 +13,6 @@ repositories { dependencies { testCompile group: 'junit', name: 'junit', version: '4.12' - compile group: 'org.firestack', name: 'laksaj', version: '0.4.1-RELEASE' + compile group: 'org.firestack', name: 'laksaj', version: '1.0.5-RELEASE' compile 'com.google.guava:guava:27.1-jre' } diff --git a/src/main/java/com/firestack/kit/PollingDeposits.java b/src/main/java/com/firestack/kit/PollingDeposits.java index bad624d..526d936 100644 --- a/src/main/java/com/firestack/kit/PollingDeposits.java +++ b/src/main/java/com/firestack/kit/PollingDeposits.java @@ -1,21 +1,24 @@ package com.firestack.kit; +import com.fasterxml.jackson.databind.ObjectMapper; import com.firestack.laksaj.blockchain.TxBlock; +import com.firestack.laksaj.exception.ZilliqaAPIException; import com.firestack.laksaj.jsonrpc.HttpProvider; import com.firestack.laksaj.jsonrpc.Rep; import com.firestack.laksaj.transaction.Transaction; import java.io.IOException; +import java.util.LinkedHashMap; import java.util.List; public class PollingDeposits { - public static void main(String[] args) throws IOException, InterruptedException { + public static void main(String[] args) throws IOException, InterruptedException, ZilliqaAPIException { Service service = new Service(); - HttpProvider provider = new HttpProvider("https://dev-api.zilliqa.com/"); + HttpProvider provider = new HttpProvider("https://api.zilliqa.com/"); service.setHttpProvider(provider); - service.setLastFetchedTxBlock(362614); + service.setLastFetchedTxBlock(513805); service.setInterval(1000); - service.setAddress("4baf5fada8e5db92c3d3242618c5b47133ae003c"); + service.setAddress("b30fe431b52e3be050ee5f11d9ff80a091f8f5d9"); service.poll(); } @@ -24,6 +27,7 @@ public static class Service { private HttpProvider httpProvider; private Integer lastFetchedTxBlock; private long interval; + private ObjectMapper mapper = new ObjectMapper(); public String getAddress() { @@ -51,7 +55,7 @@ public void setLastFetchedTxBlock(Integer lastFetchedTxBlock) { } - public void poll() throws IOException, InterruptedException { + public void poll() throws IOException, InterruptedException, ZilliqaAPIException { while (true) { task(); System.out.println("---------------------------------------------------------"); @@ -67,7 +71,20 @@ public void setInterval(long interval) { this.interval = interval; } - private void task() throws IOException { + public static class Msg { + public String _amount; + public String _recipient; + public String _tag; + public List params; + } + + public static class Transition { + public String addr; + public Integer depth; + public Msg msg; + } + + private void task() throws IOException, ZilliqaAPIException { TxBlock currentBlock = httpProvider.getLatestTxBlock().getResult(); String currentBlockNumber = currentBlock.getHeader().getBlockNum(); System.out.println("last fetched block number = " + lastFetchedTxBlock); @@ -85,8 +102,24 @@ private void task() throws IOException { } for (String hash : list) { Transaction transaction = httpProvider.getTransaction(hash).getResult(); - if (transaction.getToAddr().equalsIgnoreCase(this.address)) { - System.out.printf("Found deposit for %s, amount = %s\n", this.address, transaction.getAmount()); + if (transaction.getReceipt().isSuccess()) { + if (transaction.getReceipt().getTransitions() != null && transaction.getReceipt().getTransitions().size() != 0) { + // indicate it is a smart contract transaction + List transitions = transaction.getReceipt().getTransitions(); + for (Object transitionObj : transitions) { + String transitionStr = mapper.writeValueAsString(transitionObj); + Transition ts = mapper.readerFor(Transition.class).readValue(transitionStr); + if (ts.msg._recipient.equalsIgnoreCase("0x" + this.address)) { + System.out.printf("Found deposit for %s, amount = %s\n", this.address, ts.msg._amount); + } + } + } else { + // indicate it is a payment transaction + if (transaction.getToAddr().equalsIgnoreCase(this.address)) { + System.out.printf("Found deposit for %s, amount = %s\n", this.address, transaction.getAmount()); + } + } + } } } diff --git a/src/main/java/com/firestack/kit/TxPool.java b/src/main/java/com/firestack/kit/TxPool.java index 2759672..706709e 100644 --- a/src/main/java/com/firestack/kit/TxPool.java +++ b/src/main/java/com/firestack/kit/TxPool.java @@ -7,6 +7,7 @@ import com.firestack.laksaj.blockchain.DsBlock; import com.firestack.laksaj.blockchain.TxBlock; +import com.firestack.laksaj.exception.ZilliqaAPIException; import com.firestack.laksaj.jsonrpc.HttpProvider; import com.firestack.laksaj.transaction.Transaction; import com.firestack.laksaj.transaction.TransactionFactory; @@ -89,7 +90,7 @@ private void poll() { if (shouldReconcile) { this.reconcileTxPool(); } - } catch (IOException e) { + } catch (IOException | ZilliqaAPIException e) { System.out.println("Failed"); } }, 5, TimeUnit.SECONDS);