-
Notifications
You must be signed in to change notification settings - Fork 39
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(js-dapi-client): add contested resource method
- Loading branch information
Showing
5 changed files
with
293 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
47 changes: 47 additions & 0 deletions
47
...b/methods/platform/getContestedResourceVoteState/GetContestedResourceVoteStateResponse.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
96 changes: 96 additions & 0 deletions
96
...ib/methods/platform/getContestedResourceVoteState/getContestedResourceVoteStateFactory.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
96 changes: 96 additions & 0 deletions
96
.../js-dapi-client/lib/methods/platform/getContestedResources/getContestedResourceFactory.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
const { | ||
v0: { | ||
PlatformPromiseClient, | ||
GetContestedResourcesRequest, | ||
}, | ||
} = require('@dashevo/dapi-grpc'); | ||
|
||
const GetContestedResourcesResponse = require('./GetContestedResourceResponse'); | ||
const InvalidResponseError = require('../response/errors/InvalidResponseError'); | ||
|
||
/** | ||
* @param {GrpcTransport} grpcTransport | ||
* @returns {getContestedResourcesRequest} | ||
*/ | ||
function getContestedResourcesFactory(grpcTransport) { | ||
/** | ||
* Fetch the contested resources for specific contract | ||
* @typedef {getContestedResources} | ||
* @param contractId | ||
* @param documentTypeName | ||
* @param indexName | ||
* @param startIndexValues | ||
* @param endIndexValues | ||
* @param startAtValueInfo | ||
* @param count | ||
* @param orderAscending | ||
* @param {DAPIClientOptions & {prove: boolean}} [options] | ||
* @returns {Promise<getContestedResourcesResponse>} | ||
*/ | ||
async function getContestedResources( | ||
contractId, | ||
documentTypeName, | ||
indexName, | ||
startIndexValues, | ||
endIndexValues, | ||
startAtValueInfo, | ||
count, | ||
orderAscending, | ||
options = {}, | ||
) { | ||
const { GetContestedResourcesRequestV0 } = GetContestedResourcesRequest; | ||
|
||
// eslint-disable-next-line max-len | ||
const getContestedResourcesRequest = new GetContestedResourcesRequest(); | ||
|
||
if (Buffer.isBuffer(contractId)) { | ||
// eslint-disable-next-line no-param-reassign | ||
contractId = Buffer.from(contractId); | ||
} | ||
|
||
getContestedResourcesRequest.setV0( | ||
new GetContestedResourcesRequestV0() | ||
.setContractId(contractId) | ||
.setDocumentTypeName(documentTypeName) | ||
.setIndexName(indexName) | ||
.setStartIndexValuesList(startIndexValues) | ||
.setEndIndexValuesList(endIndexValues) | ||
.setStartAtValueInfo(startAtValueInfo) | ||
.setCount(count) | ||
.setOrderAscending(orderAscending) | ||
.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 getContestedResourcesResponse = await grpcTransport.request( | ||
PlatformPromiseClient, | ||
'getContestedResources', | ||
getContestedResourcesRequest, | ||
options, | ||
); | ||
|
||
return GetContestedResourcesResponse | ||
.createFromProto(getContestedResourcesResponse); | ||
} 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 getContestedResources; | ||
} | ||
|
||
module.exports = getContestedResourcesFactory; |
50 changes: 50 additions & 0 deletions
50
...js-dapi-client/lib/methods/platform/getContestedResources/getContestedResourceResponse.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
const AbstractResponse = require('../response/AbstractResponse'); | ||
const InvalidResponseError = require('../response/errors/InvalidResponseError'); | ||
|
||
class GetContestedResourcesResponse extends AbstractResponse { | ||
/** | ||
* @param {object} contestedResourceContenders | ||
* @param {Metadata} metadata | ||
* @param {Proof} [proof] | ||
*/ | ||
constructor(contestedResources, resultCase, metadata, proof = undefined) { | ||
super(metadata, proof); | ||
|
||
this.contestedResources = contestedResources; | ||
this.resultCase = resultCase; | ||
} | ||
|
||
/** | ||
* @returns {object} | ||
*/ | ||
getContestedResources() { | ||
return this.contestedResources; | ||
} | ||
|
||
/** | ||
* @param proto | ||
* @returns {GetContestedResourceResponse} | ||
*/ | ||
static createFromProto(proto) { | ||
// eslint-disable-next-line | ||
const contestedResourceContenders = proto.getV0().getContestedResourceValues(); | ||
const resultCase = proto.getV0().getResultCase() | ||
|
||
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 GetContestedResourcesResponse( | ||
contestedResourceContenders.toObject(), | ||
resultCase, | ||
metadata, | ||
proof, | ||
); | ||
} | ||
} | ||
|
||
module.exports = GetContestedResourcesResponse; |