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(dapi): getContestedResourceVoteStateRequest implementation #2285

Draft
wants to merge 5 commits into
base: v1.6-dev-ugly
Choose a base branch
from
Draft
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 @@ -64,6 +64,8 @@ const {
GetStatusResponse: PBJSGetStatusResponse,
GetIdentityBalanceRequest: PBJSGetIdentityBalanceRequest,
GetIdentityBalanceResponse: PBJSGetIdentityBalanceResponse,
GetContestedResourceVoteStateRequest: PBJSGetContestedResourceVoteStateRequest,
GetContestedResourceVoteStateResponse: PBJSGetContestedResourceVoteStateResponse,
},
},
},
Expand Down Expand Up @@ -91,6 +93,7 @@ const {
GetTotalCreditsInPlatformResponse: ProtocGetTotalCreditsInPlatformResponse,
GetStatusResponse: ProtocGetStatusResponse,
GetIdentityBalanceResponse: ProtocGetIdentityBalanceResponse,
GetContestedResourceVoteStateResponse: ProtocGetContestedResourceVoteStateResponse,
} = require('./platform_protoc');

const getPlatformDefinition = require('../../../../lib/getPlatformDefinition');
Expand Down Expand Up @@ -193,6 +196,10 @@ class PlatformPromiseClient {
this.client.getIdentityBalance.bind(this.client),
);

this.client.getContestedResourceVoteState = promisify(
this.client.getContestedResourceVoteState.bind(this.client),
);

this.protocolVersion = undefined;
}

Expand Down Expand Up @@ -798,6 +805,35 @@ class PlatformPromiseClient {
);
}

getContestedResourceVoteState(
getContestedResourceVoteStateRequest,
metadata = {},
options = {},
) {
if (!isObject(metadata)) {
throw new Error('metadata must be an object');
}

return this.client.getContestedResourceVoteState(
getContestedResourceVoteStateRequest,
convertObjectToMetadata(metadata),
{
interceptors: [
jsonToProtobufInterceptorFactory(
jsonToProtobufFactory(
ProtocGetContestedResourceVoteStateResponse,
PBJSGetContestedResourceVoteStateResponse,
),
protobufToJsonFactory(
PBJSGetContestedResourceVoteStateRequest,
),
),
],
...options,
},
);
}

/**
* @param {string} protocolVersion
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ const getIdentityKeysFactory = require('./getIdentityKeys/getIdentityKeysFactory
const getTotalCreditsInPlatformFactory = require('./getTotalCreditsInPlatform/getTotalCreditsInPlatformFactory');
const getStatusFactory = require('./getStatus/getStatusFactory');
const getIdentityBalanceFactory = require('./getIdentityBalance/getIdentityBalanceFactory');
const getContestedResourceVoteStateFactory = require('./getContestedResourceVoteState/getContestedResourceVoteStateFactory');

class PlatformMethodsFacade {
/**
Expand All @@ -42,6 +43,7 @@ class PlatformMethodsFacade {
this.getTotalCreditsInPlatform = getTotalCreditsInPlatformFactory(grpcTransport);
this.getStatus = getStatusFactory(grpcTransport);
this.getIdentityBalance = getIdentityBalanceFactory(grpcTransport);
this.getContestedResourceVoteState = getContestedResourceVoteStateFactory(grpcTransport);
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
const AbstractResponse = require('../response/AbstractResponse');
const InvalidResponseError = require('../response/errors/InvalidResponseError');

class GetContestedResourceVoteStateResponse extends AbstractResponse {
/**
* @param {object} contestedResourceContenders
* @param {Metadata} metadata
* @param {Proof} [proof]
*/
constructor(contestedResourceContenders, metadata, proof = undefined) {
super(metadata, proof);

this.contestedResourceContenders = contestedResourceContenders;
}

/**
* @returns {object}
*/
getContestedResourceContenders() {
return this.contestedResourceContenders;
}

/**
* @param proto
* @returns {GetContestedResourceVoteStateResponse}
*/
static createFromProto(proto) {
// eslint-disable-next-line
const contestedResourceContenders = proto.getV0().getContestedResourceContenders();

const { metadata, proof } = AbstractResponse.createMetadataAndProofFromProto(
proto,
);

if ((typeof contestedResourceContenders === 'undefined' || contestedResourceContenders === null) && !proof) {
throw new InvalidResponseError('Contested Resource Contenders data is not defined');
}

return new GetContestedResourceVoteStateResponse(
contestedResourceContenders.toObject(),
metadata,
proof,
);
}
}

module.exports = GetContestedResourceVoteStateResponse;
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
const {
v0: {
PlatformPromiseClient,
GetContestedResourceVoteStateRequest,
},
} = require('@dashevo/dapi-grpc');

const GetContestedResourceVoteStateResponse = require('./GetContestedResourceVoteStateResponse');
const InvalidResponseError = require('../response/errors/InvalidResponseError');

/**
* @param {GrpcTransport} grpcTransport
* @returns {getContestedResourceVoteStateRequest}
*/
function getContestedResourceVoteStateFactory(grpcTransport) {
/**
* Fetch the version upgrade votes status
* @typedef {getContestedResourceVoteState}
* @param contractId
* @param documentTypeName
* @param indexName
* @param resultType
* @param indexValuesList
* @param startAtIdentifierInfo
* @param allowIncludeLockedAndAbstainingVoteTally
* @param count
* @param {DAPIClientOptions & {prove: boolean}} [options]
* @returns {Promise<gрetContestedResourceVoteStateResponse>}
*/
async function getContestedResourceVoteState(
contractId,
documentTypeName,
indexName,
resultType,
indexValuesList,
startAtIdentifierInfo,
allowIncludeLockedAndAbstainingVoteTally,
count,
options = {},
) {
const { GetContestedResourceVoteStateRequestV0 } = GetContestedResourceVoteStateRequest;

// eslint-disable-next-line max-len
const getContestedResourceVoteStateRequest = new GetContestedResourceVoteStateRequest();

if (Buffer.isBuffer(contractId)) {
// eslint-disable-next-line no-param-reassign
contractId = Buffer.from(contractId);
}

getContestedResourceVoteStateRequest.setV0(
new GetContestedResourceVoteStateRequestV0()
.setContractId(contractId)
.setDocumentTypeName(documentTypeName)
.setIndexName(indexName)
.setResultType(resultType)
.setIndexValuesList(indexValuesList)
.setStartAtIdentifierInfo(startAtIdentifierInfo)
.setAllowIncludeLockedAndAbstainingVoteTally(allowIncludeLockedAndAbstainingVoteTally)
.setCount(count)
.setProve(!!options.prove),
);

let lastError;

// TODO: simple retry before the dapi versioning is properly implemented
for (let i = 0; i < 3; i += 1) {
try {
// eslint-disable-next-line no-await-in-loop
const getContestedResourceVoteStateResponse = await grpcTransport.request(
PlatformPromiseClient,
'getContestedResourceVoteState',
getContestedResourceVoteStateRequest,
options,
);

return GetContestedResourceVoteStateResponse
.createFromProto(getContestedResourceVoteStateResponse);
} catch (e) {
if (e instanceof InvalidResponseError) {
lastError = e;
} else {
throw e;
}
}
}

// If we made it past the cycle it means that the retry didn't work,
// and we're throwing the last error encountered
throw lastError;
}

return getContestedResourceVoteState;
}

module.exports = getContestedResourceVoteStateFactory;