Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: support deposit through smart contract #2

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -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'
}
49 changes: 41 additions & 8 deletions src/main/java/com/firestack/kit/PollingDeposits.java
Original file line number Diff line number Diff line change
@@ -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();
}

Expand All @@ -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() {
Expand Down Expand Up @@ -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("---------------------------------------------------------");
Expand All @@ -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<Object> 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);
Expand All @@ -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<Object> 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());
}
}

}
}
}
Expand Down
3 changes: 2 additions & 1 deletion src/main/java/com/firestack/kit/TxPool.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
Expand Down