Skip to content

Commit

Permalink
initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
owl352 committed Sep 3, 2024
1 parent 0294c90 commit 6966c18
Show file tree
Hide file tree
Showing 4 changed files with 128 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,8 @@ const {
GetIdentityKeysResponse: PBJSGetIdentityKeysResponse,
GetTotalCreditsInPlatformRequest: PBJSGetTotalCreditsInPlatformRequest,
GetTotalCreditsInPlatformResponse: PBJSGetTotalCreditsInPlatformResponse,
GetStatusRequest: PBJSGetStatusRequest,
GetStatusResponse: PBJSGetStatusResponse,
GetIdentityBalanceRequest: PBJSGetIdentityBalanceRequest,
GetIdentityBalanceResponse: PBJSGetIdentityBalanceResponse,
},
},
},
Expand All @@ -87,7 +87,7 @@ const {
GetIdentityNonceResponse: ProtocGetIdentityNonceResponse,
GetIdentityKeysResponse: ProtocGetIdentityKeysResponse,
GetTotalCreditsInPlatformResponse: ProtocGetTotalCreditsInPlatformResponse,
GetStatusResponse: ProtocGetStatusResponse,
GetIdentityBalanceResponse: ProtocGetIdentityBalanceResponse,
} = require('./platform_protoc');

const getPlatformDefinition = require('../../../../lib/getPlatformDefinition');
Expand Down Expand Up @@ -182,8 +182,8 @@ class PlatformPromiseClient {
this.client.getTotalCreditsInPlatform.bind(this.client),
);

this.client.getStatus = promisify(
this.client.getStatus.bind(this.client),
this.client.getIdentityBalance = promisify(
this.client.getIdentityBalance.bind(this.client),
);

this.protocolVersion = undefined;
Expand Down Expand Up @@ -733,27 +733,27 @@ class PlatformPromiseClient {
);
}

getStatus(
getStatusRequest,
getIdentityBalance(
getIdentityBalanceRequest,
metadata = {},
options = {},
) {
if (!isObject(metadata)) {
throw new Error('metadata must be an object');
}

return this.client.getStatus(
getStatusRequest,
return this.client.getIdentityBalance(
getIdentityBalanceRequest,
convertObjectToMetadata(metadata),
{
interceptors: [
jsonToProtobufInterceptorFactory(
jsonToProtobufFactory(
ProtocGetStatusResponse,
PBJSGetStatusResponse,
ProtocGetIdentityBalanceResponse,
PBJSGetIdentityBalanceResponse,
),
protobufToJsonFactory(
PBJSGetStatusRequest,
PBJSGetIdentityBalanceRequest,
),
),
],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ const getIdentityContractNonceFactory = require('./getIdentityContractNonce/getI
const getIdentityNonceFactory = require('./getIdentityNonce/getIdentityNonceFactory');
const getIdentityKeysFactory = require('./getIdentityKeys/getIdentityKeysFactory');
const getTotalCreditsInPlatformFactory = require('./getTotalCreditsInPlatform/getTotalCreditsInPlatformFactory');
const getStatusFactory = require('./getStatus/getStatusFactory');
const getIdentityBalanceFactory = require('./getIdentityBalance/getIdentityBalanceFactory');

class PlatformMethodsFacade {
/**
Expand All @@ -39,7 +39,7 @@ class PlatformMethodsFacade {
this.getIdentityNonce = getIdentityNonceFactory(grpcTransport);
this.getIdentityKeys = getIdentityKeysFactory(grpcTransport);
this.getTotalCreditsInPlatform = getTotalCreditsInPlatformFactory(grpcTransport);
this.getStatus = getStatusFactory(grpcTransport);
this.getIdentityBalance = getIdentityBalanceFactory(grpcTransport);
}
}

Expand Down
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 {Buffer} identity
* @param {Metadata} metadata
* @param {Proof} [proof]
*/
constructor(balance, metadata, proof = undefined) {
super(metadata, proof);

this.balance = balance;
}

/**
* @returns {Buffer}
*/
getIdentity() {
return this.balance;
}

/**
* @param proto
* @returns {GetIdentityBalanceResponse}
*/
static createFromProto(proto) {
const balance = proto.getV0().getBalance();
const { metadata, proof } = AbstractResponse.createMetadataAndProofFromProto(proto);

if (!balance && !proof) {
throw new InvalidResponseError('Balance is not defined');
}

return new GetIdentityBalanceResponse(
balance,
metadata,
proof,
);
}
}

module.exports = GetIdentityBalanceResponse;
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;

0 comments on commit 6966c18

Please sign in to comment.