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

Koios Java Governance Endpoints #420

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
Original file line number Diff line number Diff line change
Expand Up @@ -62,4 +62,9 @@ public MetadataService getMetadataService() {
public ScriptService getScriptService() {
return new BFScriptService(getBaseUrl(), getProjectId());
}

@Override
public GovernanceService getGovernanceService() {
throw new UnsupportedOperationException("Not supported yet");
}
}
3 changes: 3 additions & 0 deletions backend-modules/koios/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@ dependencies {
exclude group: 'com.fasterxml.jackson.core', module: 'jackson-core'
exclude group: 'com.fasterxml.jackson.core', module: 'jackson-annotations '
}

implementation 'org.mapstruct:mapstruct:1.5.5.Final'
annotationProcessor 'org.mapstruct:mapstruct-processor:1.5.5.Final'
}

publishing {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,146 @@
package com.bloxbean.cardano.client.backend.koios.it;

import com.bloxbean.cardano.client.api.exception.ApiException;
import com.bloxbean.cardano.client.api.model.Result;
import com.bloxbean.cardano.client.backend.api.GovernanceService;
import com.bloxbean.cardano.client.backend.model.*;
import lombok.extern.slf4j.Slf4j;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

import java.util.List;

@Slf4j
class KoiosGovernanceServiceIT extends KoiosBaseTest {

private GovernanceService governanceService;

@BeforeEach
public void setup() {
governanceService = backendService.getGovernanceService();
}

@Test
void getDRepsEpochSummaryTest() throws ApiException {
Integer epochNo = 31; // Example epoch number
Result<List<DRepEpochSummary>> result = governanceService.getDRepsEpochSummary(epochNo);
Assertions.assertTrue(result.isSuccessful());
Assertions.assertNotNull(result.getValue());
System.out.println("DReps Epoch Summary: " + result.getValue());
}

@Test
void getDRepsListTest() throws ApiException {
Result<List<DRep>> result = governanceService.getDRepsList();
Assertions.assertTrue(result.isSuccessful());
Assertions.assertNotNull(result.getValue());
System.out.println("DReps List: " + result.getValue());
}

@Test
void getDRepsInfoTest() throws ApiException {
List<String> drepIds = List.of("drep1kxtwaqtayj6vklc57u93xayjvkwgvefh8drscqp5a5y6jz7m6rd", "drep14x62vyme8l8dkhvxg6rauc6vpcd2va9fquz8k3jstsqczwlvqqh");
Result<List<DRepInfo>> result = governanceService.getDRepsInfo(drepIds);
Assertions.assertTrue(result.isSuccessful());
Assertions.assertNotNull(result.getValue());
System.out.println("DReps Info: " + result.getValue());
}

@Test
void getDRepsMetadataTest() throws ApiException {
List<String> drepIds = List.of("drep1kxtwaqtayj6vklc57u93xayjvkwgvefh8drscqp5a5y6jz7m6rd", "drep14x62vyme8l8dkhvxg6rauc6vpcd2va9fquz8k3jstsqczwlvqqh");
Result<List<DRepMetadata>> result = governanceService.getDRepsMetadata(drepIds);
Assertions.assertTrue(result.isSuccessful());
Assertions.assertNotNull(result.getValue());
System.out.println("DReps Metadata: " + result.getValue());
}

@Test
void getDRepsUpdatesTest() throws ApiException {
String drepId = "drep1kxtwaqtayj6vklc57u93xayjvkwgvefh8drscqp5a5y6jz7m6rd";
Result<List<DRepUpdate>> result = governanceService.getDRepsUpdates(drepId);
Assertions.assertTrue(result.isSuccessful());
Assertions.assertNotNull(result.getValue());
System.out.println("DReps Updates: " + result.getValue());
}

@Test
void getDRepsVotesTest() throws ApiException {
String drepId = "drep1kxtwaqtayj6vklc57u93xayjvkwgvefh8drscqp5a5y6jz7m6rd";
Result<List<DRepVote>> result = governanceService.getDRepsVotes(drepId);
Assertions.assertTrue(result.isSuccessful());
Assertions.assertNotNull(result.getValue());
System.out.println("DReps Votes: " + result.getValue());
}

@Test
void getDRepsDelegatorsTest() throws ApiException {
String drepId = "drep1kxtwaqtayj6vklc57u93xayjvkwgvefh8drscqp5a5y6jz7m6rd";
Result<List<DRepDelegator>> result = governanceService.getDRepsDelegators(drepId);
Assertions.assertTrue(result.isSuccessful());
Assertions.assertNotNull(result.getValue());
System.out.println("DReps Delegators: " + result.getValue());
}

@Test
void getCommitteeInformationTest() throws ApiException {
Result<List<CommitteeInfo>> result = governanceService.getCommitteeInformation();
Assertions.assertTrue(result.isSuccessful());
Assertions.assertNotNull(result.getValue());
System.out.println("Committee Information: " + result.getValue());
}

@Test
void getCommitteeVotesTest() throws ApiException {
String ccHotId = "cc_hot1qgqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqvcdjk7";
Result<List<CommitteeVote>> result = governanceService.getCommitteeVotes(ccHotId);
Assertions.assertTrue(result.isSuccessful());
Assertions.assertNotNull(result.getValue());
System.out.println("Committee Votes: " + result.getValue());
}

@Test
void getProposalListTest() throws ApiException {
Result<List<Proposal>> result = governanceService.getProposalList();
Assertions.assertTrue(result.isSuccessful());
Assertions.assertNotNull(result.getValue());
System.out.println("Proposal List: " + result.getValue());
}

@Test
void getVoterProposalsTest() throws ApiException {
String voterId = "drep1kxtwaqtayj6vklc57u93xayjvkwgvefh8drscqp5a5y6jz7m6rd";
Result<List<Proposal>> result = governanceService.getVoterProposals(voterId);
Assertions.assertTrue(result.isSuccessful());
Assertions.assertNotNull(result.getValue());
System.out.println("Voter Proposals: " + result.getValue());
}

@Test
void getProposalVotingSummaryTest() throws ApiException {
String proposalId = "gov_action1qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqpzklpgpf";
Result<List<ProposalVotingSummary>> result = governanceService.getProposalVotingSummary(proposalId);
Assertions.assertTrue(result.isSuccessful());
Assertions.assertNotNull(result.getValue());
System.out.println("Proposal Voting Summary: " + result.getValue());
}

@Test
void getProposalVotesTest() throws ApiException {
String proposalId = "gov_action1qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqpzklpgpf";
Result<List<ProposalVote>> result = governanceService.getProposalVotes(proposalId);
Assertions.assertTrue(result.isSuccessful());
Assertions.assertNotNull(result.getValue());
System.out.println("Proposal Votes: " + result.getValue());
}

@Test
void getPoolVotesTest() throws ApiException {
String poolBech32 = "pool1x4p3cwemsm356vpxnjwuud7w76jz64hyss729zp7xa6wuey6yr9";
Result<List<PoolVote>> result = governanceService.getPoolVotes(poolBech32);
Assertions.assertTrue(result.isSuccessful());
Assertions.assertNotNull(result.getValue());
System.out.println("Pool Votes: " + result.getValue());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -86,4 +86,9 @@ public MetadataService getMetadataService() {
public ScriptService getScriptService() {
return new KoiosScriptService(backendServiceImpl.getScriptService(), backendServiceImpl.getTransactionsService());
}

@Override
public GovernanceService getGovernanceService() {
return new KoiosGovernanceService(backendServiceImpl.getGovernanceService());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,168 @@
package com.bloxbean.cardano.client.backend.koios;

import com.bloxbean.cardano.client.api.exception.ApiException;
import com.bloxbean.cardano.client.api.model.Result;
import com.bloxbean.cardano.client.backend.api.GovernanceService;
import com.bloxbean.cardano.client.backend.koios.mapper.GovernanceMapper;
import com.bloxbean.cardano.client.backend.model.*;

import java.util.List;
import java.util.function.Function;

/**
* Koios Governance Service
*/
public class KoiosGovernanceService implements GovernanceService {

private final rest.koios.client.backend.api.governance.GovernanceService governanceService;
private final GovernanceMapper mapper = GovernanceMapper.INSTANCE;

public KoiosGovernanceService(rest.koios.client.backend.api.governance.GovernanceService governanceService) {
this.governanceService = governanceService;
}

@Override
public Result<List<DRepEpochSummary>> getDRepsEpochSummary(Integer epochNo) throws ApiException {
try {
rest.koios.client.backend.api.base.Result<List<rest.koios.client.backend.api.governance.model.DRepEpochSummary>> result = governanceService.getDRepsEpochSummary(epochNo, null);
return map(result, mapper::mapToDRepEpochSummaryList);
} catch (rest.koios.client.backend.api.base.exception.ApiException e) {
throw new ApiException(e.getMessage(), e);
}
}

@Override
public Result<List<DRep>> getDRepsList() throws ApiException {
try {
return map(governanceService.getDRepsList(null), mapper::mapToDRepList);
} catch (rest.koios.client.backend.api.base.exception.ApiException e) {
throw new ApiException(e.getMessage(), e);
}
}

@Override
public Result<List<DRepInfo>> getDRepsInfo(List<String> drepIds) throws ApiException {
try {
return map(governanceService.getDRepsInfo(drepIds, null), mapper::mapToDRepInfoList);
} catch (rest.koios.client.backend.api.base.exception.ApiException e) {
throw new ApiException(e.getMessage(), e);
}
}

@Override
public Result<List<DRepMetadata>> getDRepsMetadata(List<String> drepIds) throws ApiException {
try {
return map(governanceService.getDRepsMetadata(drepIds, null), mapper::mapToDRepMetadataList);
} catch (rest.koios.client.backend.api.base.exception.ApiException e) {
throw new ApiException(e.getMessage(), e);
}
}

@Override
public Result<List<DRepUpdate>> getDRepsUpdates(String drepId) throws ApiException {
try {
return map(governanceService.getDRepsUpdates(drepId, null), mapper::mapToDRepUpdateList);
} catch (rest.koios.client.backend.api.base.exception.ApiException e) {
throw new ApiException(e.getMessage(), e);
}
}

@Override
public Result<List<DRepVote>> getDRepsVotes(String drepId) throws ApiException {
try {
return map(governanceService.getDRepsVotes(drepId, null), mapper::mapToDRepVoteList);
} catch (rest.koios.client.backend.api.base.exception.ApiException e) {
throw new ApiException(e.getMessage(), e);
}
}

@Override
public Result<List<DRepDelegator>> getDRepsDelegators(String drepId) throws ApiException {
try {
return map(governanceService.getDRepsDelegators(drepId, null), mapper::mapToDRepDelegatorList);
} catch (rest.koios.client.backend.api.base.exception.ApiException e) {
throw new ApiException(e.getMessage(), e);
}
}

@Override
public Result<List<CommitteeInfo>> getCommitteeInformation() throws ApiException {
try {
rest.koios.client.backend.api.base.Result<List<rest.koios.client.backend.api.governance.model.CommitteeInfo>> result = governanceService.getCommitteeInformation(null);
return map(result, mapper::mapToCommitteeInfoList);
} catch (rest.koios.client.backend.api.base.exception.ApiException e) {
throw new ApiException(e.getMessage(), e);
}
}

@Override
public Result<List<CommitteeVote>> getCommitteeVotes(String ccHotId) throws ApiException {
try {
return map(governanceService.getCommitteeVotes(ccHotId, null), mapper::mapToCommitteeVoteList);
} catch (rest.koios.client.backend.api.base.exception.ApiException e) {
throw new ApiException(e.getMessage(), e);
}
}

@Override
public Result<List<Proposal>> getProposalList() throws ApiException {
try {
return map(governanceService.getProposalList(null), mapper::mapToProposalList);
} catch (rest.koios.client.backend.api.base.exception.ApiException e) {
throw new ApiException(e.getMessage(), e);
}
}

@Override
public Result<List<Proposal>> getVoterProposals(String voterId) throws ApiException {
try {
return map(governanceService.getVoterProposals(voterId, null), mapper::mapToProposalList);
} catch (rest.koios.client.backend.api.base.exception.ApiException e) {
throw new ApiException(e.getMessage(), e);
}
}

@Override
public Result<List<ProposalVotingSummary>> getProposalVotingSummary(String proposalId) throws ApiException {
try {
return map(governanceService.getProposalVotingSummary(proposalId, null), mapper::mapToProposalVotingSummaryList);
} catch (rest.koios.client.backend.api.base.exception.ApiException e) {
throw new ApiException(e.getMessage(), e);
}
}

@Override
public Result<List<ProposalVote>> getProposalVotes(String proposalId) throws ApiException {
try {
return map(governanceService.getProposalVotes(proposalId, null), mapper::mapToProposalVoteList);
} catch (rest.koios.client.backend.api.base.exception.ApiException e) {
throw new ApiException(e.getMessage(), e);
}
}

@Override
public Result<List<PoolVote>> getPoolVotes(String poolBech32) throws ApiException {
try {
return map(governanceService.getPoolVotes(poolBech32, null), mapper::mapToPoolVoteList);
} catch (rest.koios.client.backend.api.base.exception.ApiException e) {
throw new ApiException(e.getMessage(), e);
}
}

/**
* Utility method to map a KoiosResult to the Result used by this API.
*
* @param koiosResult The result from Koios API
* @param mapperFunc The mapping function to convert Koios model to API model
* @param <K> The type of data in the Koios result
* @param <T> The type of data in the mapped result
* @return The mapped result
*/
private <K, T> Result<T> map(rest.koios.client.backend.api.base.Result<K> koiosResult, Function<K, T> mapperFunc) {
if (koiosResult.isSuccessful()) {
return Result.success("OK").withValue(mapperFunc.apply(koiosResult.getValue()));
} else {
return Result.error(koiosResult.getResponse()).code(koiosResult.getCode());
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
import com.fasterxml.jackson.databind.JsonNode;
import rest.koios.client.backend.api.script.ScriptService;
import rest.koios.client.backend.api.script.model.DatumInfo;
import rest.koios.client.backend.api.script.model.NativeScript;
import rest.koios.client.backend.api.script.model.PlutusScript;
import rest.koios.client.backend.api.script.model.ScriptInfo;
import rest.koios.client.backend.api.transactions.TransactionsService;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package com.bloxbean.cardano.client.backend.koios.mapper;

import com.bloxbean.cardano.client.backend.model.*;
import org.mapstruct.Mapper;
import org.mapstruct.factory.Mappers;

import java.util.List;

@Mapper
public interface GovernanceMapper {

GovernanceMapper INSTANCE = Mappers.getMapper(GovernanceMapper.class);

List<DRepEpochSummary> mapToDRepEpochSummaryList(List<rest.koios.client.backend.api.governance.model.DRepEpochSummary> dRepEpochSummaryList);

List<DRep> mapToDRepList(List<rest.koios.client.backend.api.governance.model.DRep> dRepList);

List<DRepInfo> mapToDRepInfoList(List<rest.koios.client.backend.api.governance.model.DRepInfo> dRepInfoList);

List<DRepMetadata> mapToDRepMetadataList(List<rest.koios.client.backend.api.governance.model.DRepMetadata> dRepMetadataList);

List<DRepUpdate> mapToDRepUpdateList(List<rest.koios.client.backend.api.governance.model.DRepUpdate> dRepUpdateList);

List<DRepVote> mapToDRepVoteList(List<rest.koios.client.backend.api.governance.model.DRepVote> dRepVoteList);

List<DRepDelegator> mapToDRepDelegatorList(List<rest.koios.client.backend.api.governance.model.DRepDelegator> dRepDelegators);

List<CommitteeInfo> mapToCommitteeInfoList(List<rest.koios.client.backend.api.governance.model.CommitteeInfo> committeeInfo);

List<CommitteeVote> mapToCommitteeVoteList(List<rest.koios.client.backend.api.governance.model.CommitteeVote> committeeVoteList);

List<Proposal> mapToProposalList(List<rest.koios.client.backend.api.governance.model.Proposal> proposalList);

List<ProposalVotingSummary> mapToProposalVotingSummaryList(List<rest.koios.client.backend.api.governance.model.ProposalVotingSummary> proposalVotingSummaryList);

List<ProposalVote> mapToProposalVoteList(List<rest.koios.client.backend.api.governance.model.ProposalVote> proposalVoteList);

List<PoolVote> mapToPoolVoteList(List<rest.koios.client.backend.api.governance.model.PoolVote> poolVoteList);
}
Loading
Loading