Skip to content

Commit

Permalink
Merge pull request #454 from multiversx/query-improvements
Browse files Browse the repository at this point in the history
Query improvements: add SmartContractQueriesController.query()
  • Loading branch information
andreibancioiu authored Jun 4, 2024
2 parents add4011 + 471f089 commit fc14c4e
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 1 deletion.
12 changes: 12 additions & 0 deletions src/errors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -372,3 +372,15 @@ export class ErrParseTransactionOutcome extends Err {
super(message);
}
}

/**
* Signals an error when querying a smart contract.
*/
export class ErrSmartContractQuery extends Err {
public returnCode: string;

public constructor(returnCode: string, message: string) {
super(message);
this.returnCode = returnCode;
}
}
22 changes: 21 additions & 1 deletion src/smartContractQueriesController.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Err } from "./errors";
import { Err, ErrSmartContractQuery } from "./errors";
import { IContractQueryResponse } from "./interfaceOfNetwork";
import { SmartContractQuery, SmartContractQueryResponse } from "./smartContractQuery";
import { ArgSerializer, ContractFunction, EndpointDefinition, NativeSerializer, ResultsParser } from "./smartcontracts";
Expand All @@ -23,6 +23,26 @@ export class SmartContractQueriesController {
this.legacyResultsParser = new ResultsParser();
}

async query(options: {
contract: string;
caller?: string;
value?: bigint;
function: string;
arguments: any[];
}): Promise<any[]> {
const query = this.createQuery(options);
const queryResponse = await this.runQuery(query);
this.raiseForStatus(queryResponse);
return this.parseQueryResponse(queryResponse);
}

private raiseForStatus(queryResponse: SmartContractQueryResponse): void {
const isOk = queryResponse.returnCode === "ok";
if (!isOk) {
throw new ErrSmartContractQuery(queryResponse.returnCode, queryResponse.returnMessage);
}
}

createQuery(options: {
contract: string;
caller?: string;
Expand Down

0 comments on commit fc14c4e

Please sign in to comment.