-
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(dapi): implement getIdentityBalance (#2105)
Co-authored-by: owl352 <kokosinca123@gmail.com> Co-authored-by: owl352 <64574305+owl352@users.noreply.github.com>
- Loading branch information
1 parent
50d42c2
commit 2954e29
Showing
9 changed files
with
466 additions
and
1 deletion.
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
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
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
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
43 changes: 43 additions & 0 deletions
43
...ages/js-dapi-client/lib/methods/platform/getIdentityBalance/GetIdentityBalanceResponse.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,43 @@ | ||
const AbstractResponse = require('../response/AbstractResponse'); | ||
const InvalidResponseError = require('../response/errors/InvalidResponseError'); | ||
|
||
class GetIdentityBalanceResponse extends AbstractResponse { | ||
/** | ||
* @param {number} balance | ||
* @param {Metadata} metadata | ||
* @param {Proof} [proof] | ||
*/ | ||
constructor(balance, metadata, proof = undefined) { | ||
super(metadata, proof); | ||
|
||
this.balance = balance; | ||
} | ||
|
||
/** | ||
* @returns {number} | ||
*/ | ||
getBalance() { | ||
return this.balance; | ||
} | ||
|
||
/** | ||
* @param proto | ||
* @returns {GetIdentityBalanceResponse} | ||
*/ | ||
static createFromProto(proto) { | ||
const balance = proto.getV0().getBalance(); | ||
const { metadata, proof } = AbstractResponse.createMetadataAndProofFromProto(proto); | ||
|
||
if ((balance === null || balance === undefined) && !proof) { | ||
throw new InvalidResponseError('Balance is not defined'); | ||
} | ||
|
||
return new GetIdentityBalanceResponse( | ||
balance, | ||
metadata, | ||
proof, | ||
); | ||
} | ||
} | ||
|
||
module.exports = GetIdentityBalanceResponse; |
71 changes: 71 additions & 0 deletions
71
packages/js-dapi-client/lib/methods/platform/getIdentityBalance/getIdentityBalanceFactory.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,71 @@ | ||
const { | ||
v0: { | ||
PlatformPromiseClient, | ||
GetIdentityBalanceRequest, | ||
}, | ||
} = require('@dashevo/dapi-grpc'); | ||
|
||
const GetIdentityBalanceResponse = require('./GetIdentityBalanceResponse'); | ||
const InvalidResponseError = require('../response/errors/InvalidResponseError'); | ||
|
||
/** | ||
* @param {GrpcTransport} grpcTransport | ||
* @returns {getIdentityBalance} | ||
*/ | ||
function getIdentityBalanceFactory(grpcTransport) { | ||
/** | ||
* Fetch the identity balance by id | ||
* @typedef {getIdentityBalance} | ||
* @param {Buffer} id | ||
* @param {DAPIClientOptions & {prove: boolean}} [options] | ||
* @returns {Promise<GetIdentityBalanceResponse>} | ||
*/ | ||
async function getIdentityBalance(id, options = {}) { | ||
const { GetIdentityBalanceRequestV0 } = GetIdentityBalanceRequest; | ||
const getIdentityBalanceRequest = new GetIdentityBalanceRequest(); | ||
// need to convert objects inherited from Buffer to pure buffer as google protobuf | ||
// doesn't support extended buffers | ||
// https://github.com/protocolbuffers/protobuf/blob/master/js/binary/utils.js#L1049 | ||
if (Buffer.isBuffer(id)) { | ||
// eslint-disable-next-line no-param-reassign | ||
id = Buffer.from(id); | ||
} | ||
|
||
getIdentityBalanceRequest.setV0( | ||
new GetIdentityBalanceRequestV0() | ||
.setId(id) | ||
.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 getIdentityBalanceResponse = await grpcTransport.request( | ||
PlatformPromiseClient, | ||
'getIdentityBalance', | ||
getIdentityBalanceRequest, | ||
options, | ||
); | ||
|
||
return GetIdentityBalanceResponse.createFromProto(getIdentityBalanceResponse); | ||
} 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 getIdentityBalance; | ||
} | ||
|
||
module.exports = getIdentityBalanceFactory; |
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
126 changes: 126 additions & 0 deletions
126
...i-client/test/unit/methods/platform/getIdentityBalance/GetIdentityBalanceResponse.spec.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,126 @@ | ||
const { | ||
v0: { | ||
GetIdentityBalanceResponse, | ||
ResponseMetadata, | ||
Proof: ProofResponse, | ||
}, | ||
} = require('@dashevo/dapi-grpc'); | ||
|
||
const GetIdentityBalanceResponseClass = require('../../../../../lib/methods/platform/getIdentityBalance/GetIdentityBalanceResponse'); | ||
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('GetIdentityBalanceResponse', () => { | ||
let getIdentityBalanceResponse; | ||
let metadataFixture; | ||
let balance; | ||
let proto; | ||
let proofFixture; | ||
|
||
beforeEach(async () => { | ||
metadataFixture = getMetadataFixture(); | ||
proofFixture = getProofFixture(); | ||
balance = 1337; | ||
|
||
const { GetIdentityBalanceResponseV0 } = GetIdentityBalanceResponse; | ||
proto = new GetIdentityBalanceResponse(); | ||
|
||
const metadata = new ResponseMetadata(); | ||
metadata.setHeight(metadataFixture.height); | ||
metadata.setCoreChainLockedHeight(metadataFixture.coreChainLockedHeight); | ||
metadata.setTimeMs(metadataFixture.timeMs); | ||
metadata.setProtocolVersion(metadataFixture.protocolVersion); | ||
|
||
proto.setV0( | ||
new GetIdentityBalanceResponseV0() | ||
.setBalance(balance) | ||
.setMetadata(metadata), | ||
); | ||
|
||
getIdentityBalanceResponse = new GetIdentityBalanceResponseClass( | ||
balance, | ||
new Metadata(metadataFixture), | ||
); | ||
}); | ||
|
||
it('should return Identity balance', () => { | ||
const identityBalance = getIdentityBalanceResponse.getBalance(); | ||
const identityProof = getIdentityBalanceResponse.getProof(); | ||
|
||
expect(identityBalance).to.equal(balance); | ||
expect(identityProof).to.equal(undefined); | ||
}); | ||
|
||
it('should return proof', () => { | ||
getIdentityBalanceResponse = new GetIdentityBalanceResponseClass( | ||
balance, | ||
new Metadata(metadataFixture), | ||
new Proof(proofFixture), | ||
); | ||
|
||
const identityBalance = getIdentityBalanceResponse.getBalance(); | ||
const proof = getIdentityBalanceResponse.getProof(); | ||
|
||
expect(identityBalance).to.equal(balance); | ||
expect(proof).to.be.an.instanceOf(Proof); | ||
expect(proof.getGrovedbProof()).to.deep.equal(proofFixture.merkleProof); | ||
expect(proof.getQuorumHash()).to.deep.equal(proofFixture.quorumHash); | ||
expect(proof.getSignature()).to.deep.equal(proofFixture.signature); | ||
expect(proof.getRound()).to.deep.equal(proofFixture.round); | ||
}); | ||
|
||
it('should create an instance from proto', () => { | ||
getIdentityBalanceResponse = GetIdentityBalanceResponseClass.createFromProto(proto); | ||
expect(getIdentityBalanceResponse).to.be | ||
.an.instanceOf(GetIdentityBalanceResponseClass); | ||
expect(getIdentityBalanceResponse.getBalance()).to.equal(balance); | ||
|
||
expect(getIdentityBalanceResponse.getMetadata()) | ||
.to.be.an.instanceOf(Metadata); | ||
expect(getIdentityBalanceResponse.getMetadata().getHeight()) | ||
.to.equal(metadataFixture.height); | ||
expect(getIdentityBalanceResponse.getMetadata().getCoreChainLockedHeight()) | ||
.to.equal(metadataFixture.coreChainLockedHeight); | ||
|
||
expect(getIdentityBalanceResponse.getProof()).to.equal(undefined); | ||
}); | ||
|
||
it('should create an instance with proof from proto', () => { | ||
const proofProto = new ProofResponse(); | ||
|
||
proofProto.setQuorumHash(proofFixture.quorumHash); | ||
proofProto.setSignature(proofFixture.signature); | ||
proofProto.setGrovedbProof(proofFixture.merkleProof); | ||
proofProto.setRound(proofFixture.round); | ||
|
||
proto.getV0().setBalance(undefined); | ||
proto.getV0().setProof(proofProto); | ||
|
||
getIdentityBalanceResponse = GetIdentityBalanceResponseClass.createFromProto(proto); | ||
|
||
expect(getIdentityBalanceResponse.getBalance()).to.equal(0); | ||
expect(getIdentityBalanceResponse.getMetadata()).to.deep.equal(metadataFixture); | ||
|
||
const proof = getIdentityBalanceResponse.getProof(); | ||
expect(proof).to.be.an.instanceOf(Proof); | ||
expect(proof.getGrovedbProof()).to.deep.equal(proofFixture.merkleProof); | ||
expect(proof.getQuorumHash()).to.deep.equal(proofFixture.quorumHash); | ||
expect(proof.getSignature()).to.deep.equal(proofFixture.signature); | ||
expect(proof.getRound()).to.deep.equal(proofFixture.round); | ||
}); | ||
|
||
it('should throw InvalidResponseError if Metadata is not defined', () => { | ||
proto.getV0().setMetadata(undefined); | ||
|
||
try { | ||
getIdentityBalanceResponse = GetIdentityBalanceResponseClass.createFromProto(proto); | ||
|
||
expect.fail('should throw InvalidResponseError'); | ||
} catch (e) { | ||
expect(e).to.be.an.instanceOf(InvalidResponseError); | ||
} | ||
}); | ||
}); |
Oops, something went wrong.