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

Support Local queries AccountState, DRepStakeDistribution #74

Merged
merged 5 commits into from
Aug 22, 2024
Merged
Show file tree
Hide file tree
Changes from 3 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
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package com.bloxbean.cardano.yaci.core.protocol.localstate.queries;

import co.nstant.in.cbor.model.Array;
import co.nstant.in.cbor.model.DataItem;
import co.nstant.in.cbor.model.UnsignedInteger;
import com.bloxbean.cardano.yaci.core.protocol.handshake.messages.AcceptVersion;
import com.bloxbean.cardano.yaci.core.protocol.localstate.api.Era;
import com.bloxbean.cardano.yaci.core.protocol.localstate.api.EraQuery;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NonNull;
import lombok.ToString;

import java.math.BigInteger;
import java.util.List;

@Getter
@AllArgsConstructor
@ToString
public class AccountStateQuery implements EraQuery<AccountStateQueryResult> {
@NonNull
private Era era;

public AccountStateQuery() {
this.era = Era.Conway;
}

@Override
public DataItem serialize(AcceptVersion protocolVersion) {
Array queryArray = new Array();
queryArray.add(new UnsignedInteger(29));

return wrapWithOuterArray(queryArray);
}

@Override
public AccountStateQueryResult deserializeResult(AcceptVersion protocolVersion, DataItem[] di) {
List<DataItem> dataItems = extractResultArray(di[0]);

var treasuryDI = ((Array)dataItems.get(0)).getDataItems().get(0);
var reservesDI = ((Array)dataItems.get(0)).getDataItems().get(1);

BigInteger treasury = ((UnsignedInteger)treasuryDI).getValue();
BigInteger reserves = ((UnsignedInteger)reservesDI).getValue();

return new AccountStateQueryResult(treasury, reserves);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package com.bloxbean.cardano.yaci.core.protocol.localstate.queries;

import com.bloxbean.cardano.yaci.core.protocol.localstate.api.QueryResult;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.ToString;

import java.math.BigInteger;

@Getter
@AllArgsConstructor
@ToString
public class AccountStateQueryResult implements QueryResult {
private BigInteger treasury;
private BigInteger reserves;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
package com.bloxbean.cardano.yaci.core.protocol.localstate.queries;

import co.nstant.in.cbor.model.*;
import com.bloxbean.cardano.client.transaction.spec.governance.DRep;
import com.bloxbean.cardano.yaci.core.model.governance.Drep;
import com.bloxbean.cardano.yaci.core.protocol.handshake.messages.AcceptVersion;
import com.bloxbean.cardano.yaci.core.protocol.localstate.api.Era;
import com.bloxbean.cardano.yaci.core.protocol.localstate.api.EraQuery;
import com.bloxbean.cardano.yaci.core.protocol.localstate.queries.model.DRepStake;
import com.bloxbean.cardano.yaci.core.util.HexUtil;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.ToString;
import lombok.extern.slf4j.Slf4j;

import java.util.List;

import static com.bloxbean.cardano.yaci.core.util.CborSerializationUtil.toInt;

@Getter
@ToString
@AllArgsConstructor
@Slf4j
public class DRepStakeDistributionQuery implements EraQuery<DRepStakeDistributionQueryResult> {
private Era era;
private List<DRep> dReps;

public DRepStakeDistributionQuery(List<DRep> dReps) {
this(Era.Conway, dReps);
}

@Override
public DataItem serialize(AcceptVersion protocolVersion) {
Array array = new Array();
array.add(new UnsignedInteger(26));

Array dRepArray = new Array();
dReps.forEach(dRep -> dRepArray.add(dRep.serialize()));

array.add(dRepArray);

return wrapWithOuterArray(array);
}

@Override
public DRepStakeDistributionQueryResult deserializeResult(AcceptVersion protocolVersion, DataItem[] di) {
DRepStakeDistributionQueryResult result = new DRepStakeDistributionQueryResult();

List<DataItem> dataItemList = extractResultArray(di[0]);
Map dRepStakeMap = (Map) dataItemList.get(0);

for (var key : dRepStakeMap.getKeys()) {
var dRepDI = (Array) key;
int dRepType = toInt(dRepDI.getDataItems().get(0));

var votingPower = ((UnsignedInteger) dRepStakeMap.get(key)).getValue();
Drep dRep = null;
if (dRepType == 0 || dRepType == 1) {
String hash = HexUtil.encodeHexString(((ByteString) dRepDI.getDataItems().get(1)).getBytes());
dRep = dRepType == 0 ? Drep.addrKeyHash(hash) : Drep.scriptHash(hash);
} else if (dRepType == 2) {
dRep = Drep.abstain();
} else if (dRepType == 3) {
dRep = Drep.noConfidence();
}

DRepStake dRepStake = DRepStake.builder()
.dRep(dRep)
.amount(votingPower)
.build();

result.addDRepStake(dRepStake);
}

return result;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package com.bloxbean.cardano.yaci.core.protocol.localstate.queries;

import com.bloxbean.cardano.yaci.core.protocol.localstate.api.QueryResult;
import com.bloxbean.cardano.yaci.core.protocol.localstate.queries.model.DRepStake;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.ToString;

import java.util.ArrayList;
import java.util.List;

@Getter
@ToString
@AllArgsConstructor
public class DRepStakeDistributionQueryResult implements QueryResult {
List<DRepStake> dRepStakes;

public DRepStakeDistributionQueryResult() {
this.dRepStakes = new ArrayList<>();
}

public void addDRepStake(DRepStake dRepState) {
dRepStakes.add(dRepState);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,18 @@

import com.bloxbean.cardano.yaci.core.protocol.localstate.api.QueryResult;
import com.bloxbean.cardano.yaci.core.protocol.localstate.queries.model.DRepState;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.Setter;
import lombok.ToString;

import java.util.ArrayList;
import java.util.List;

@Setter
@Getter
@ToString
@AllArgsConstructor
public class DRepStateQueryResult implements QueryResult {
List<DRepState> dRepStates;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,13 @@
import com.bloxbean.cardano.yaci.core.protocol.localstate.queries.model.RatifyState;
import lombok.Getter;
import lombok.Setter;
import lombok.ToString;

import java.util.List;

@Getter
@Setter
@ToString
public class GovStateResult implements QueryResult {
private Committee committee;
private Constitution constitution;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package com.bloxbean.cardano.yaci.core.protocol.localstate.queries.model;

import com.bloxbean.cardano.yaci.core.model.governance.Drep;
import lombok.Builder;
import lombok.Getter;
import lombok.Setter;
import lombok.ToString;

import java.math.BigInteger;

@Getter
@Setter
@Builder
@ToString
public class DRepStake {
private Drep dRep;
private BigInteger amount;
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,14 @@
import lombok.Builder;
import lombok.Getter;
import lombok.Setter;
import lombok.ToString;

import java.math.BigInteger;

@Getter
@Setter
@Builder
@ToString
public class DRepState {
private String dRepHash;
private Anchor anchor;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,14 @@
import lombok.Builder;
import lombok.Getter;
import lombok.Setter;
import lombok.ToString;

import java.util.Map;

@Getter
@Setter
@Builder
@ToString
public class EnactState {
private Committee committee;
private Constitution constitution;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,14 @@
import lombok.Builder;
import lombok.Getter;
import lombok.Setter;
import lombok.ToString;

import java.util.Map;

@Getter
@Setter
@Builder
@ToString
public class Proposal {
private GovActionId govActionId;
private Map<Credential, Vote> committeeVotes;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,14 @@
import lombok.Builder;
import lombok.Getter;
import lombok.Setter;
import lombok.ToString;

import java.util.List;

@Getter
@Setter
@Builder
@ToString
public class RatifyState {
private List<Proposal> enactedGovActions;
private List<GovActionId> expiredGovActions;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
package com.bloxbean.cardano.yaci.helper;

import com.bloxbean.cardano.client.address.Address;
import com.bloxbean.cardano.client.transaction.spec.governance.Constitution;
import com.bloxbean.cardano.client.transaction.spec.governance.DRep;
import com.bloxbean.cardano.yaci.core.common.Constants;
import com.bloxbean.cardano.yaci.core.model.Credential;
import com.bloxbean.cardano.yaci.core.model.certs.StakeCredType;
import com.bloxbean.cardano.yaci.core.protocol.chainsync.messages.Point;
import com.bloxbean.cardano.yaci.core.protocol.localstate.api.Era;
import com.bloxbean.cardano.yaci.core.protocol.localstate.queries.*;
Expand All @@ -12,7 +16,6 @@
import reactor.core.publisher.Mono;
import reactor.core.scheduler.Schedulers;
import reactor.test.StepVerifier;

import java.math.BigInteger;
import java.time.Duration;
import java.time.LocalDate;
Expand Down Expand Up @@ -297,4 +300,68 @@ void delegationRewardsQuery() {
assertThat(rewards).hasSizeGreaterThanOrEqualTo(5); //only 5 regular staking keys has rewards
}

@Test
void accountStateQuery() {
Mono<AccountStateQueryResult> mono = localStateQueryClient.executeQuery(new AccountStateQuery());
mono = mono.log();

AccountStateQueryResult result = mono.block(Duration.ofSeconds(5));
System.out.println(result);
}

@Test
void dRepStakeDistributionQuery() {
Mono<DRepStakeDistributionQueryResult> mono = localStateQueryClient.executeQuery(
new DRepStakeDistributionQuery(
List.of(
DRep.addrKeyHash("001021a9b538f693f5293b9ad77e9fd2febe5ecd66cf8bb2844b4a8d")
, DRep.scriptHash("1ffa2ae5f54e88a2e6a29642936aceebdd3aea948d70ace645912440")
))
);

mono = mono.log();

DRepStakeDistributionQueryResult result = mono.block(Duration.ofSeconds(5));
System.out.println( result);
}

@Test
void govStateQuery() {
Mono<GovStateResult> mono = localStateQueryClient.executeQuery(new GovStateQuery());
mono = mono.log();

GovStateResult result = mono.block(Duration.ofSeconds(10));
System.out.println(result);
}

@Test
void constitutionQuery() {
Mono<ConstitutionResult> mono = localStateQueryClient.executeQuery(new ConstitutionQuery());
mono = mono.log();

ConstitutionResult result = mono.block(Duration.ofSeconds(5));
System.out.println(result);
}

@Test
void dRepStateQuery() {
Mono<DRepStateQueryResult> mono = localStateQueryClient.executeQuery(new DRepStateQuery(
List.of(
Credential
.builder()
.type(StakeCredType.ADDR_KEYHASH)
.hash("5e80b2b80990a738aece6d6068b2991eaea21c52e79c7974719ac275")
.build(),
Credential
.builder()
.type(StakeCredType.ADDR_KEYHASH)
.hash("6e066d1a8bce348956b34438556abb43d597d075f9fdab03bb6f4d39")
.build()
))
);
mono = mono.log();

DRepStateQueryResult result = mono.block(Duration.ofSeconds(5));
System.out.println(result);
}
}
Loading