Skip to content
This repository has been archived by the owner on Dec 16, 2021. It is now read-only.

Commit

Permalink
feat: use protocol version interceptor in clients (#63)
Browse files Browse the repository at this point in the history
Co-authored-by: Djavid Gabibiyan <djavidoff@gmail.com>
  • Loading branch information
Konstantin Shuplenkov and jawid-h committed May 12, 2020
1 parent a336b15 commit 0665f41
Show file tree
Hide file tree
Showing 5 changed files with 259 additions and 188 deletions.
196 changes: 106 additions & 90 deletions clients/nodejs/CorePromiseClient.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ const {
client: {
interceptors: {
jsonToProtobufInterceptorFactory,
protocolVersionInterceptorFactory,
},
converters: {
jsonToProtobufFactory,
Expand Down Expand Up @@ -55,90 +56,6 @@ const getCoreDefinition = require('../../lib/getCoreDefinition');

const CoreNodeJSClient = getCoreDefinition();

const getStatusOptions = {
interceptors: [
jsonToProtobufInterceptorFactory(
jsonToProtobufFactory(
ProtocGetStatusResponse,
PBJSGetStatusResponse,
),
protobufToJsonFactory(
PBJSGetStatusRequest,
),
),
],
};

const getBlockOptions = {
interceptors: [
jsonToProtobufInterceptorFactory(
jsonToProtobufFactory(
ProtocGetBlockResponse,
PBJSGetBlockResponse,
),
protobufToJsonFactory(
PBJSGetBlockRequest,
),
),
],
};

const sendTransactionOptions = {
interceptors: [
jsonToProtobufInterceptorFactory(
jsonToProtobufFactory(
ProtocSendTransactionResponse,
PBJSSendTransactionResponse,
),
protobufToJsonFactory(
PBJSSendTransactionRequest,
),
),
],
};

const getTransactionOptions = {
interceptors: [
jsonToProtobufInterceptorFactory(
jsonToProtobufFactory(
ProtocGetTransactionResponse,
PBJSGetTransactionResponse,
),
protobufToJsonFactory(
PBJSGetTransactionRequest,
),
),
],
};

const getEstimatedTransactionFeeOptions = {
interceptors: [
jsonToProtobufInterceptorFactory(
jsonToProtobufFactory(
ProtocGetEstimatedTransactionFeeResponse,
PBJSGetEstimatedTransactionFeeResponse,
),
protobufToJsonFactory(
PBJSGetEstimatedTransactionFeeRequest,
),
),
],
};

const subscribeToBlockHeadersWithChainLocksOptions = {
interceptors: [
jsonToProtobufInterceptorFactory(
jsonToProtobufFactory(
ProtocBlockHeadersWithChainLocksResponse,
PBJSBlockHeadersWithChainLocksResponse,
),
protobufToJsonFactory(
PBJSBlockHeadersWithChainLocksRequest,
),
),
],
};

class CorePromiseClient {
/**
* @param {string} hostname
Expand Down Expand Up @@ -167,6 +84,8 @@ class CorePromiseClient {
this.client.getEstimatedTransactionFee = promisify(
this.client.getEstimatedTransactionFee.bind(this.client),
);

this.protocolVersion = undefined;
}

/**
Expand All @@ -182,7 +101,22 @@ class CorePromiseClient {
return this.client.getStatus(
getStatusRequest,
convertObjectToMetadata(metadata),
getStatusOptions,
{
interceptors: [
protocolVersionInterceptorFactory(
this.protocolVersion,
),
jsonToProtobufInterceptorFactory(
jsonToProtobufFactory(
ProtocGetStatusResponse,
PBJSGetStatusResponse,
),
protobufToJsonFactory(
PBJSGetStatusRequest,
),
),
],
},
);
}

Expand All @@ -199,7 +133,22 @@ class CorePromiseClient {
return this.client.getBlock(
getBlockRequest,
convertObjectToMetadata(metadata),
getBlockOptions,
{
interceptors: [
protocolVersionInterceptorFactory(
this.protocolVersion,
),
jsonToProtobufInterceptorFactory(
jsonToProtobufFactory(
ProtocGetBlockResponse,
PBJSGetBlockResponse,
),
protobufToJsonFactory(
PBJSGetBlockRequest,
),
),
],
},
);
}

Expand All @@ -216,7 +165,22 @@ class CorePromiseClient {
return this.client.sendTransaction(
sendTransactionRequest,
convertObjectToMetadata(metadata),
sendTransactionOptions,
{
interceptors: [
protocolVersionInterceptorFactory(
this.protocolVersion,
),
jsonToProtobufInterceptorFactory(
jsonToProtobufFactory(
ProtocSendTransactionResponse,
PBJSSendTransactionResponse,
),
protobufToJsonFactory(
PBJSSendTransactionRequest,
),
),
],
},
);
}

Expand All @@ -233,7 +197,22 @@ class CorePromiseClient {
return this.client.getTransaction(
getTransactionRequest,
convertObjectToMetadata(metadata),
getTransactionOptions,
{
interceptors: [
protocolVersionInterceptorFactory(
this.protocolVersion,
),
jsonToProtobufInterceptorFactory(
jsonToProtobufFactory(
ProtocGetTransactionResponse,
PBJSGetTransactionResponse,
),
protobufToJsonFactory(
PBJSGetTransactionRequest,
),
),
],
},
);
}

Expand All @@ -250,7 +229,22 @@ class CorePromiseClient {
return this.client.getEstimatedTransactionFee(
getEstimatedTransactionFeeRequest,
convertObjectToMetadata(metadata),
getEstimatedTransactionFeeOptions,
{
interceptors: [
protocolVersionInterceptorFactory(
this.protocolVersion,
),
jsonToProtobufInterceptorFactory(
jsonToProtobufFactory(
ProtocGetEstimatedTransactionFeeResponse,
PBJSGetEstimatedTransactionFeeResponse,
),
protobufToJsonFactory(
PBJSGetEstimatedTransactionFeeRequest,
),
),
],
},
);
}

Expand All @@ -268,9 +262,31 @@ class CorePromiseClient {
return this.client.subscribeToBlockHeadersWithChainLocks(
blockHeadersWithChainLocksRequest,
convertObjectToMetadata(metadata),
subscribeToBlockHeadersWithChainLocksOptions,
{
interceptors: [
protocolVersionInterceptorFactory(
this.protocolVersion,
),
jsonToProtobufInterceptorFactory(
jsonToProtobufFactory(
ProtocBlockHeadersWithChainLocksResponse,
PBJSBlockHeadersWithChainLocksResponse,
),
protobufToJsonFactory(
PBJSBlockHeadersWithChainLocksRequest,
),
),
],
},
);
}

/**
* @param {string} protocolVersion
*/
setProtocolVersion(protocolVersion) {
this.setProtocolVersion = protocolVersion;
}
}

module.exports = CorePromiseClient;
Loading

0 comments on commit 0665f41

Please sign in to comment.