Skip to content

Commit

Permalink
add support for new rpc (#14)
Browse files Browse the repository at this point in the history
* add support for new rpc
* optimize for response types

Co-authored-by: PanaW <[email protected]>
  • Loading branch information
Pana and PanaW authored Dec 11, 2020
1 parent f5b2fc2 commit a8ed3a9
Show file tree
Hide file tree
Showing 7 changed files with 199 additions and 0 deletions.
6 changes: 6 additions & 0 deletions src/main/java/conflux/web3j/Cfx.java
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,12 @@ static Cfx create(Web3jService service, int retry, long intervalMillis) {

Request<String, StringResponse> getClientVersion();

Request<List<DepositInfo>, DepositInfo.ListResponse> getDepositList(String address, Epoch... epoch);

Request<List<VoteStakeInfo>, VoteStakeInfo.ListResponse> getVoteList(String address, Epoch... epoch);

Request<SupplyInfo, SupplyInfo.Response> getSupplyInfo(Epoch... epoch);

<T,R extends Response<?> & HasValue<T>> Request<T, R> getCustomizedRequest(Class<R> responseType, String method, Object... params);

default Receipt waitForReceipt(String txHash) throws InterruptedException {
Expand Down
42 changes: 42 additions & 0 deletions src/main/java/conflux/web3j/Web3j.java
Original file line number Diff line number Diff line change
Expand Up @@ -315,12 +315,54 @@ public Request<String, StringResponse> getClientVersion() {
.withRetry(this.retry, this.intervalMillis);
}

/*
Request<List<DepositInfo>, DepositInfo.ListResponse> getDepositList();
Request<List<VoteStakeInfo>, VoteStakeInfo.ListResponse> getVoteList();
* */

@Override
public Request<List<DepositInfo>, DepositInfo.ListResponse> getDepositList(String address, Epoch... epoch) {
if (epoch.length == 0) {
return new Request<>(this.service, "cfx_getDepositList", DepositInfo.ListResponse.class, address)
.withRetry(this.retry, this.intervalMillis);
} else {
return new Request<>(this.service, "cfx_getDepositList", DepositInfo.ListResponse.class, address, epoch[0].getValue())
.withRetry(this.retry, this.intervalMillis);
}
}

@Override
public Request<List<VoteStakeInfo>, VoteStakeInfo.ListResponse> getVoteList(String address, Epoch... epoch) {
if (epoch.length == 0) {
return new Request<>(this.service, "cfx_getVoteList", VoteStakeInfo.ListResponse.class, address)
.withRetry(this.retry, this.intervalMillis);
} else {
return new Request<>(this.service, "cfx_getVoteList", VoteStakeInfo.ListResponse.class, address, epoch[0].getValue())
.withRetry(this.retry, this.intervalMillis);
}
}

@Override
public Request<SupplyInfo, SupplyInfo.Response> getSupplyInfo(Epoch... epoch) {
if (epoch.length == 0) {
return new Request<>(this.service, "cfx_getSupplyInfo", SupplyInfo.Response.class)
.withRetry(this.retry, this.intervalMillis);
} else {
return new Request<>(this.service, "cfx_getSupplyInfo", SupplyInfo.Response.class, epoch[0].getValue())
.withRetry(this.retry, this.intervalMillis);
}
}

@Override
public <T,R extends Response<?> & HasValue<T>> Request<T, R> getCustomizedRequest(Class<R> responseType, String method, Object... params){
return new Request<>(this.service, method, responseType, params)
.withRetry(this.retry, this.intervalMillis);
}




@Override
public Flowable<NewHeadsNotification> subscribeNewHeads() {
return service.subscribe(
Expand Down
25 changes: 25 additions & 0 deletions src/main/java/conflux/web3j/response/DepositInfo.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package conflux.web3j.response;

import org.web3j.utils.Numeric;

import java.math.BigInteger;

public class DepositInfo {
public static class ListResponse extends CfxListResponse<DepositInfo> {}

private String amount;
private String depositTime;
private String accumulatedInterestRate;

public BigInteger getAmount() {return Numeric.decodeQuantity(amount);}

public void setAmount(String amount) {this.amount = amount;}

public long getDepositTime() {return Long.valueOf(depositTime);}

public void setDepositTime(String time) {this.depositTime = time;}

public BigInteger getAccumulatedInterestRate() {return Numeric.decodeQuantity(accumulatedInterestRate);}

public void setAccumulatedInterestRate(String rate) {this.accumulatedInterestRate = rate;}
}
37 changes: 37 additions & 0 deletions src/main/java/conflux/web3j/response/Receipt.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,11 @@ public static class Response extends CfxNullableResponse<Receipt> {}
private String stateRoot;
private String outcomeStatus;
private String txExecErrorMsg;
private boolean gasCoveredBySponsor;
private boolean storageCoveredBySponsor;
private String storageCollateralized;
private List<StorageChange> storageReleased;


public String getTransactionHash() {
return transactionHash;
Expand Down Expand Up @@ -133,4 +138,36 @@ public String getTxExecErrorMsg() {
}

public void setTxExecErrorMsg(String errorMsg) {this.txExecErrorMsg = errorMsg;}

public boolean getGasCoveredBySponsor() {
return gasCoveredBySponsor;
}

public void setGasCoveredBySponsor(boolean gasCovered) {
this.gasCoveredBySponsor = gasCovered;
}

public boolean getStorageCoveredBySponsor() {
return storageCoveredBySponsor;
}

public void setStorageCoveredBySponsor(boolean storageCovered) {
this.storageCoveredBySponsor = storageCovered;
}

public BigInteger getStorageCollateralized() {
return Numeric.decodeQuantity(storageCollateralized);
}

public void setStorageCollateralized(String collateralized) {
this.storageCollateralized = collateralized;
}

public List<StorageChange> getStorageReleased() {
return this.storageReleased;
}

public void setStorageReleased(List<StorageChange> change) {
this.storageReleased = change;
}
}
28 changes: 28 additions & 0 deletions src/main/java/conflux/web3j/response/StorageChange.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package conflux.web3j.response;

import org.web3j.utils.Numeric;

import java.math.BigInteger;

public class StorageChange {
// public static class Response extends CfxResponse<StorageChange> {}

private String address;
private String collaterals;

public String getAddress() {
return address;
}

public void setAddress(String address) {
this.address = address;
}

public BigInteger getCollaterals () {
return Numeric.decodeQuantity(collaterals);
}

public void setCollaterals(String collaterals) {
this.collaterals = collaterals;
}
}
42 changes: 42 additions & 0 deletions src/main/java/conflux/web3j/response/SupplyInfo.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package conflux.web3j.response;

import org.web3j.utils.Numeric;

import java.math.BigInteger;

public class SupplyInfo {
public static class Response extends CfxResponse<SupplyInfo> {}

private String totalIssued;
private String totalStaking;
private String totalCollateral;

public BigInteger getTotalIssued() {
return Numeric.decodeQuantity(totalIssued);
}

public void setTotalIssued(String totalIssued) {
this.totalIssued = totalIssued;
}

public BigInteger getTotalStaking() {
return Numeric.decodeQuantity(totalStaking);
}

public void setTotalStaking(String totalStaking) {
this.totalStaking = totalStaking;
}

public BigInteger getTotalCollateral () {
return Numeric.decodeQuantity(totalCollateral);
}

public void setTotalCollateral(String totalCollateral) {
this.totalCollateral = totalCollateral;
}

@Override
public String toString() {
return String.format("{totalIssued = %s, totalStaking = %s, totalCollateral = %s}", this.getTotalIssued(), this.getTotalStaking(), this.getTotalCollateral());
}
}
19 changes: 19 additions & 0 deletions src/main/java/conflux/web3j/response/VoteStakeInfo.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package conflux.web3j.response;

import org.web3j.utils.Numeric;

import java.math.BigInteger;

public class VoteStakeInfo {
public static class ListResponse extends CfxListResponse<VoteStakeInfo> {}
private String amount;
private String unlockBlockNumber;

public BigInteger getAmount() {return Numeric.decodeQuantity(amount);}

public void setAmount(String amount) {this.amount = amount;}

public BigInteger getUnlockBlockNumber() {return Numeric.decodeQuantity(unlockBlockNumber);}

public void setUnlockBlockNumber(String blockNumber) {this.unlockBlockNumber = blockNumber;}
}

0 comments on commit a8ed3a9

Please sign in to comment.