Skip to content

Commit

Permalink
feat: add the changed syntax of the ABCI query
Browse files Browse the repository at this point in the history
  • Loading branch information
jinoosss committed Jun 24, 2024
1 parent c897766 commit 67d3b64
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 9 deletions.
11 changes: 8 additions & 3 deletions src/provider/jsonrpc/jsonrpc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,12 @@ import {
} from '@gnolang/tm2-js-client';
import { FunctionSignature } from '../types';
import { VMEndpoint } from '../endpoints';
import { extractStringFromResponse, prepareVMABCIQuery } from '../utility';
import {
extractStringFromResponse,
prepareVMABCIEvaluateExpressionQuery,
prepareVMABCIQuery,
prepareVMABCIRenderQuery,
} from '../utility';

/**
* Provider based on JSON-RPC HTTP requests
Expand All @@ -32,7 +37,7 @@ export class GnoJSONRPCProvider extends JSONRPCProvider implements GnoProvider {
{
request: newRequest(ABCIEndpoint.ABCI_QUERY, [
`vm/${VMEndpoint.EVALUATE}`,
prepareVMABCIQuery([packagePath, expression]),
prepareVMABCIEvaluateExpressionQuery([packagePath, expression]),
'0', // Height; not supported > 0 for now
false,
]),
Expand Down Expand Up @@ -92,7 +97,7 @@ export class GnoJSONRPCProvider extends JSONRPCProvider implements GnoProvider {
{
request: newRequest(ABCIEndpoint.ABCI_QUERY, [
`vm/${VMEndpoint.RENDER}`,
prepareVMABCIQuery([packagePath, path]),
prepareVMABCIRenderQuery([packagePath, path]),
'0', // Height; not supported > 0 for now
false,
]),
Expand Down
32 changes: 29 additions & 3 deletions src/provider/utility/provider.utility.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,41 @@ import { stringToBase64 } from '@gnolang/tm2-js-client';

/**
* Prepares the VM ABCI query params by concatenating them
* with a newline separation and encoding them to base64
* with new line or "." or ":" characters separation and encoding them to base64
* `evaluateExpression` uses the "." character to separate parameters.
* `getRenderOutput` uses the ":" character to separate parameters.
* @param {string[]} params the params for the ABCI call
* @param {string} separator the separator for ABCI call parameters (default: "\n")
*/
export const prepareVMABCIQuery = (params: string[]): string => {
export const prepareVMABCIQuery = (
params: string[],
separator = '\n'
): string => {
if (params.length == 1) {
return stringToBase64(params[0]);
}

return stringToBase64(params.join('\n'));
return stringToBase64(params.join(separator));
};

/**
* Prepare the VM ABCI `evaluateExpression` query parameters by concatenating them
* with the "." character delimiter and encoding them with base64.
* @param {string[]} params the params for the ABCI call
*/
export const prepareVMABCIEvaluateExpressionQuery = (
params: string[]
): string => {
return prepareVMABCIQuery(params, '.');
};

/**
* Prepare the VM ABCI `render` query parameters by concatenating them
* with the ":" character delimiter and encoding them with base64.
* @param {string[]} params the params for the ABCI call
*/
export const prepareVMABCIRenderQuery = (params: string[]): string => {
return prepareVMABCIQuery(params, ':');
};

export const extractStringFromResponse = (abciData: string | null): string => {
Expand Down
11 changes: 8 additions & 3 deletions src/provider/websocket/ws.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,12 @@ import {
} from '@gnolang/tm2-js-client';
import { FunctionSignature } from '../types';
import { VMEndpoint } from '../endpoints';
import { extractStringFromResponse, prepareVMABCIQuery } from '../utility';
import {
extractStringFromResponse,
prepareVMABCIEvaluateExpressionQuery,
prepareVMABCIQuery,
prepareVMABCIRenderQuery,
} from '../utility';

export class GnoWSProvider extends WSProvider implements GnoProvider {
/**
Expand All @@ -27,7 +32,7 @@ export class GnoWSProvider extends WSProvider implements GnoProvider {
const response = await this.sendRequest<ABCIResponse>(
newRequest(ABCIEndpoint.ABCI_QUERY, [
`vm/${VMEndpoint.EVALUATE}`,
prepareVMABCIQuery([packagePath, expression]),
prepareVMABCIEvaluateExpressionQuery([packagePath, expression]),
'0', // Height; not supported > 0 for now
false,
])
Expand Down Expand Up @@ -87,7 +92,7 @@ export class GnoWSProvider extends WSProvider implements GnoProvider {
const response = await this.sendRequest<ABCIResponse>(
newRequest(ABCIEndpoint.ABCI_QUERY, [
`vm/${VMEndpoint.RENDER}`,
prepareVMABCIQuery([packagePath, path]),
prepareVMABCIRenderQuery([packagePath, path]),
'0', // Height; not supported > 0 for now
false,
])
Expand Down

0 comments on commit 67d3b64

Please sign in to comment.