diff --git a/packages/js-dapi-client/lib/methods/platform/PlatformMethodsFacade.js b/packages/js-dapi-client/lib/methods/platform/PlatformMethodsFacade.js index 135306762de..16f44665a99 100644 --- a/packages/js-dapi-client/lib/methods/platform/PlatformMethodsFacade.js +++ b/packages/js-dapi-client/lib/methods/platform/PlatformMethodsFacade.js @@ -17,7 +17,7 @@ const getTotalCreditsInPlatformFactory = require('./getTotalCreditsInPlatform/ge const getStatusFactory = require('./getStatus/getStatusFactory'); const getIdentityBalanceFactory = require('./getIdentityBalance/getIdentityBalanceFactory'); const getContestedResourceVoteStateFactory = require('./getContestedResourceVoteState/getContestedResourceVoteStateFactory'); -const getContestedResourcesFactory = require('./getContestedResources/getContestedResourceFactory'); +const getContestedResourcesFactory = require('./getContestedResources/getContestedResourcesFactory'); class PlatformMethodsFacade { /** diff --git a/packages/js-dapi-client/lib/methods/platform/getContestedResources/getContestedResourceFactory.js b/packages/js-dapi-client/lib/methods/platform/getContestedResources/getContestedResourcesFactory.js similarity index 99% rename from packages/js-dapi-client/lib/methods/platform/getContestedResources/getContestedResourceFactory.js rename to packages/js-dapi-client/lib/methods/platform/getContestedResources/getContestedResourcesFactory.js index 64b1efff119..845814e94f2 100644 --- a/packages/js-dapi-client/lib/methods/platform/getContestedResources/getContestedResourceFactory.js +++ b/packages/js-dapi-client/lib/methods/platform/getContestedResources/getContestedResourcesFactory.js @@ -5,7 +5,7 @@ const { }, } = require('@dashevo/dapi-grpc'); -const GetContestedResourcesResponse = require('./GetContestedResourceResponse'); +const GetContestedResourcesResponse = require('./GetContestedResourcesResponse'); const InvalidResponseError = require('../response/errors/InvalidResponseError'); /** diff --git a/packages/js-dapi-client/lib/methods/platform/getContestedResources/getContestedResourceResponse.js b/packages/js-dapi-client/lib/methods/platform/getContestedResources/getContestedResourcesResponse.js similarity index 82% rename from packages/js-dapi-client/lib/methods/platform/getContestedResources/getContestedResourceResponse.js rename to packages/js-dapi-client/lib/methods/platform/getContestedResources/getContestedResourcesResponse.js index 076da738313..f7783c22a76 100644 --- a/packages/js-dapi-client/lib/methods/platform/getContestedResources/getContestedResourceResponse.js +++ b/packages/js-dapi-client/lib/methods/platform/getContestedResources/getContestedResourcesResponse.js @@ -4,6 +4,8 @@ const InvalidResponseError = require('../response/errors/InvalidResponseError'); class GetContestedResourcesResponse extends AbstractResponse { /** * @param {object} contestedResourceContenders + * @param contestedResources + * @param resultCase * @param {Metadata} metadata * @param {Proof} [proof] */ @@ -23,12 +25,12 @@ class GetContestedResourcesResponse extends AbstractResponse { /** * @param proto - * @returns {GetContestedResourceResponse} + * @returns {GetContestedResourcesResponse} */ static createFromProto(proto) { // eslint-disable-next-line const contestedResourceContenders = proto.getV0().getContestedResourceValues(); - const resultCase = proto.getV0().getResultCase() + const resultCase = proto.getV0().getResultCase(); const { metadata, proof } = AbstractResponse.createMetadataAndProofFromProto( proto, @@ -39,7 +41,7 @@ class GetContestedResourcesResponse extends AbstractResponse { } return new GetContestedResourcesResponse( - contestedResourceContenders.toObject(), + contestedResourceContenders !== undefined ? contestedResourceContenders.getContestedResourceValuesList() : '', resultCase, metadata, proof, diff --git a/packages/js-dapi-client/test/unit/methods/platform/getDocuments/GetDocumentsResponse.spec.js b/packages/js-dapi-client/test/unit/methods/platform/getContestedResources/GetContestedResourcesResponse.spec.js similarity index 50% rename from packages/js-dapi-client/test/unit/methods/platform/getDocuments/GetDocumentsResponse.spec.js rename to packages/js-dapi-client/test/unit/methods/platform/getContestedResources/GetContestedResourcesResponse.spec.js index 4aa187b0b83..90160640d1e 100644 --- a/packages/js-dapi-client/test/unit/methods/platform/getDocuments/GetDocumentsResponse.spec.js +++ b/packages/js-dapi-client/test/unit/methods/platform/getContestedResources/GetContestedResourcesResponse.spec.js @@ -1,75 +1,70 @@ -const getDocumentsFixture = require('@dashevo/wasm-dpp/lib/test/fixtures/getDocumentsFixture'); const { v0: { - GetDocumentsResponse, + GetContestedResourcesResponse, ResponseMetadata, Proof: ProofResponse, }, } = require('@dashevo/dapi-grpc'); -const GetDocumentsResponseClass = require('../../../../../lib/methods/platform/getDocuments/GetDocumentsResponse'); +const GetContestedResourceResponseClass = require('../../../../../lib/methods/platform/getContestedResources/getContestedResourcesResponse'); const getMetadataFixture = require('../../../../../lib/test/fixtures/getMetadataFixture'); const InvalidResponseError = require('../../../../../lib/methods/platform/response/errors/InvalidResponseError'); const getProofFixture = require('../../../../../lib/test/fixtures/getProofFixture'); const Proof = require('../../../../../lib/methods/platform/response/Proof'); const Metadata = require('../../../../../lib/methods/platform/response/Metadata'); -describe('GetDocumentsResponse', () => { - let getDocumentsResponse; +describe('GetContestedResourcesResponse', () => { + let getContestedResourcesResponse; let metadataFixture; - let documentsFixture; let proto; - let serializedDocuments; let proofFixture; + let contestedResourceValues; beforeEach(async () => { metadataFixture = getMetadataFixture(); - documentsFixture = await getDocumentsFixture(); proofFixture = getProofFixture(); + contestedResourceValues = ['EgRkYXNo']; - serializedDocuments = documentsFixture - .map((document) => Buffer.from(JSON.stringify(document))); - - const { GetDocumentsResponseV0 } = GetDocumentsResponse; - proto = new GetDocumentsResponse(); + const { GetContestedResourcesResponseV0 } = GetContestedResourcesResponse; + proto = new GetContestedResourcesResponse(); const metadata = new ResponseMetadata(); metadata.setHeight(metadataFixture.height); metadata.setCoreChainLockedHeight(metadataFixture.coreChainLockedHeight); proto.setV0( - new GetDocumentsResponseV0() - .setDocuments( - new GetDocumentsResponseV0.Documents() - .setDocumentsList(serializedDocuments), - ).setMetadata(metadata), + new GetContestedResourcesResponseV0() + .setContestedResourceValues(new GetContestedResourcesResponseV0 + .ContestedResourceValues([contestedResourceValues])) + .setMetadata(metadata), ); - getDocumentsResponse = new GetDocumentsResponseClass( - serializedDocuments, + getContestedResourcesResponse = new GetContestedResourceResponseClass( + contestedResourceValues, + 1, new Metadata(metadataFixture), ); }); - it('should return documents', () => { - const documents = getDocumentsResponse.getDocuments(); - const proof = getDocumentsResponse.getProof(); + it('should return contested resources', () => { + const contestedResources = getContestedResourcesResponse.getContestedResources(); + const proof = getContestedResourcesResponse.getProof(); - expect(documents).to.deep.equal(serializedDocuments); + expect(contestedResources).to.deep.equal(contestedResourceValues); expect(proof).to.equal(undefined); }); it('should return proof', async () => { - getDocumentsResponse = new GetDocumentsResponseClass( - [], + getContestedResourcesResponse = new GetContestedResourceResponseClass( + '', + 1, new Metadata(metadataFixture), new Proof(proofFixture), ); + const contestedResources = getContestedResourcesResponse.getContestedResources(); + const proof = getContestedResourcesResponse.getProof(); - const documents = getDocumentsResponse.getDocuments(); - const proof = getDocumentsResponse.getProof(); - - expect(documents).to.deep.equal([]); + expect(contestedResources).to.equal(''); expect(proof).to.be.an.instanceOf(Proof); expect(proof.getGrovedbProof()).to.deep.equal(proofFixture.merkleProof); @@ -78,18 +73,19 @@ describe('GetDocumentsResponse', () => { }); it('should create an instance from proto', () => { - getDocumentsResponse = GetDocumentsResponseClass.createFromProto(proto); - expect(getDocumentsResponse).to.be.an.instanceOf(GetDocumentsResponseClass); - expect(getDocumentsResponse.getDocuments()).to.deep.equal(serializedDocuments); + getContestedResourcesResponse = GetContestedResourceResponseClass.createFromProto(proto); + expect(getContestedResourcesResponse).to.be.an.instanceOf(GetContestedResourceResponseClass); + expect(getContestedResourcesResponse.getContestedResources()) + .to.deep.equal(contestedResourceValues); - expect(getDocumentsResponse.getMetadata()) + expect(getContestedResourcesResponse.getMetadata()) .to.be.an.instanceOf(Metadata); - expect(getDocumentsResponse.getMetadata().getHeight()) + expect(getContestedResourcesResponse.getMetadata().getHeight()) .to.equal(metadataFixture.height); - expect(getDocumentsResponse.getMetadata().getCoreChainLockedHeight()) + expect(getContestedResourcesResponse.getMetadata().getCoreChainLockedHeight()) .to.equal(metadataFixture.coreChainLockedHeight); - expect(getDocumentsResponse.getProof()).to.equal(undefined); + expect(getContestedResourcesResponse.getProof()).to.equal(undefined); }); it('should create an instance with proof from proto', () => { @@ -101,19 +97,19 @@ describe('GetDocumentsResponse', () => { proto.getV0().setProof(proofProto); - getDocumentsResponse = GetDocumentsResponseClass.createFromProto(proto); + getContestedResourcesResponse = GetContestedResourceResponseClass.createFromProto(proto); - expect(getDocumentsResponse).to.be.an.instanceOf(GetDocumentsResponseClass); - expect(getDocumentsResponse.getDocuments()).to.deep.members([]); + expect(getContestedResourcesResponse).to.be.an.instanceOf(GetContestedResourceResponseClass); + expect(getContestedResourcesResponse.getContestedResources()).to.equal(''); - expect(getDocumentsResponse.getMetadata()) + expect(getContestedResourcesResponse.getMetadata()) .to.be.an.instanceOf(Metadata); - expect(getDocumentsResponse.getMetadata().getHeight()) + expect(getContestedResourcesResponse.getMetadata().getHeight()) .to.equal(metadataFixture.height); - expect(getDocumentsResponse.getMetadata().getCoreChainLockedHeight()) + expect(getContestedResourcesResponse.getMetadata().getCoreChainLockedHeight()) .to.equal(metadataFixture.coreChainLockedHeight); - const proof = getDocumentsResponse.getProof(); + const proof = getContestedResourcesResponse.getProof(); expect(proof).to.be.an.instanceOf(Proof); expect(proof.getGrovedbProof()).to.deep.equal(proofFixture.merkleProof); expect(proof.getQuorumHash()).to.deep.equal(proofFixture.quorumHash); @@ -124,7 +120,7 @@ describe('GetDocumentsResponse', () => { proto.getV0().setMetadata(undefined); try { - getDocumentsResponse = GetDocumentsResponseClass.createFromProto(proto); + getContestedResourcesResponse = GetContestedResourceResponseClass.createFromProto(proto); expect.fail('should throw InvalidResponseError'); } catch (e) { diff --git a/packages/js-dapi-client/test/unit/methods/platform/getContestedResources/getContestedResourcesFactory.spec.js b/packages/js-dapi-client/test/unit/methods/platform/getContestedResources/getContestedResourcesFactory.spec.js new file mode 100644 index 00000000000..45e6706dd92 --- /dev/null +++ b/packages/js-dapi-client/test/unit/methods/platform/getContestedResources/getContestedResourcesFactory.spec.js @@ -0,0 +1,179 @@ +const { + v0: { + PlatformPromiseClient, + GetContestedResourcesRequest, + GetContestedResourcesResponse, + ResponseMetadata, + Proof: ProofResponse, + }, +} = require('@dashevo/dapi-grpc'); + +const generateRandomIdentifier = require('@dashevo/wasm-dpp/lib/test/utils/generateRandomIdentifierAsync'); +const getContestedResourcesFactory = require('../../../../../lib/methods/platform/getContestedResources/getContestedResourcesFactory'); +const getMetadataFixture = require('../../../../../lib/test/fixtures/getMetadataFixture'); +const getProofFixture = require('../../../../../lib/test/fixtures/getProofFixture'); +const Proof = require('../../../../../lib/methods/platform/response/Proof'); + +describe('getContestedResourcesFactory', () => { + let grpcTransportMock; + let contractIdBuffer; + let metadataFixture; + let proofFixture; + let proofResponse; + let response; + let documentTypeName; + let indexName; + let startIndexValues; + let endIndexValues; + let startAtValueInfo; + let count; + let orderAscending; + let contestedResourceValues; + let getContestedResources; + + beforeEach(async function beforeEach() { + contractIdBuffer = Buffer.from('11c70af56a763b05943888fa3719ef56b3e826615fdda2d463c63f4034cb861c', 'hex'); + documentTypeName = 'domain'; + indexName = 'normalizedLabel'; + startIndexValues = []; + endIndexValues = []; + startAtValueInfo = new GetContestedResourcesRequest + .GetContestedResourcesRequestV0 + .StartAtValueInfo({ + startValue: (await generateRandomIdentifier()), + startValueIncluded: true, + }); + contestedResourceValues = ['EgRkYXNo']; + count = 1; + orderAscending = true; + + metadataFixture = getMetadataFixture(); + proofFixture = getProofFixture(); + + getContestedResources = getContestedResourcesFactory(grpcTransportMock); + + const metadata = new ResponseMetadata(); + metadata.setHeight(metadataFixture.height); + metadata.setCoreChainLockedHeight(metadataFixture.coreChainLockedHeight); + metadata.setTimeMs(metadataFixture.timeMs); + metadata.setProtocolVersion(metadataFixture.protocolVersion); + + const { GetContestedResourcesResponseV0 } = GetContestedResourcesResponse; + response = new GetContestedResourcesResponse(); + response.setV0( + new GetContestedResourcesResponseV0() + .setContestedResourceValues(new GetContestedResourcesResponseV0 + .ContestedResourceValues([contestedResourceValues])) + .setMetadata(metadata), + ); + + grpcTransportMock = { + request: this.sinon.stub().resolves(response), + }; + + getContestedResources = getContestedResourcesFactory(grpcTransportMock); + + proofResponse = new ProofResponse(); + proofResponse.setQuorumHash(proofFixture.quorumHash); + proofResponse.setSignature(proofFixture.signature); + proofResponse.setGrovedbProof(proofFixture.merkleProof); + proofResponse.setRound(proofFixture.round); + }); + + it('should return contested resources', async () => { + const options = { prove: false }; + + const result = await getContestedResources( + contractIdBuffer, + documentTypeName, + indexName, + startIndexValues, + endIndexValues, + startAtValueInfo, + count, + orderAscending, + options, + ); + + const { GetContestedResourcesRequestV0 } = GetContestedResourcesRequest; + + const request = new GetContestedResourcesRequest(); + + request.setV0( + new GetContestedResourcesRequestV0() + .setContractId(contractIdBuffer) + .setDocumentTypeName(documentTypeName) + .setIndexName(indexName) + .setStartIndexValuesList([]) + .setEndIndexValuesList([]) + .setStartAtValueInfo(startAtValueInfo) + .setCount(1) + .setOrderAscending(true) + .setProve(!!options.prove), + ); + + expect(grpcTransportMock.request).to.be.calledOnceWithExactly( + PlatformPromiseClient, + 'getContestedResources', + request, + options, + ); + expect(result.getContestedResources()).to.equal(contestedResourceValues); + expect(result.getMetadata()).to.deep.equal(metadataFixture); + expect(result.getProof()).to.equal(undefined); + }); + + it('should return proof', async () => { + const options = { prove: true }; + response.getV0().setProof(proofResponse); + + const result = await getContestedResources( + contractIdBuffer, + documentTypeName, + indexName, + startIndexValues, + endIndexValues, + startAtValueInfo, + count, + orderAscending, + options, + ); + + const { GetContestedResourcesRequestV0 } = GetContestedResourcesRequest; + const request = new GetContestedResourcesRequest(); + + request.setV0( + new GetContestedResourcesRequestV0() + .setContractId(contractIdBuffer) + .setDocumentTypeName(documentTypeName) + .setIndexName(indexName) + .setStartIndexValuesList([]) + .setEndIndexValuesList([]) + .setStartAtValueInfo(startAtValueInfo) + .setCount(1) + .setOrderAscending(true) + .setProve(!!options.prove), + ); + + expect(grpcTransportMock.request).to.be.calledOnceWithExactly( + PlatformPromiseClient, + 'getContestedResources', + request, + options, + ); + + expect(result.getContestedResources()).to.equal(''); + expect(result.getMetadata()).to.deep.equal(metadataFixture); + + expect(result.getProof()).to.be.an.instanceOf(Proof); + expect(result.getProof().getGrovedbProof()).to.deep.equal(proofFixture.merkleProof); + expect(result.getProof().getQuorumHash()).to.deep.equal(proofFixture.quorumHash); + expect(result.getProof().getSignature()).to.deep.equal(proofFixture.signature); + expect(result.getProof().getRound()).to.deep.equal(proofFixture.round); + expect(result.getMetadata()).to.deep.equal(metadataFixture); + expect(result.getMetadata().getHeight()).to.equal(metadataFixture.height); + expect(result.getMetadata().getCoreChainLockedHeight()).to.equal( + metadataFixture.coreChainLockedHeight, + ); + }); +});