Skip to content

Commit

Permalink
refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
popenta committed Mar 7, 2024
1 parent 69a8156 commit 127937c
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 28 deletions.
1 change: 1 addition & 0 deletions src/adapters/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from "./smartContractQueriesAdapter";
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,11 @@ import { Address } from "../address";
import { IAddress } from "../interface";
import { IContractQueryResponse } from "../interfaceOfNetwork";
import { SmartContractQuery, SmartContractQueryResponse } from "../smartContractQuery";
import { ContractFunction, EndpointDefinition } from "../smartcontracts";

interface INetworkProvider {
queryContract(query: ILegacyQuery): Promise<IContractQueryResponse>;
}

interface IAbi {
getEndpoint(name: string | ContractFunction): EndpointDefinition;
}

interface ILegacyQuery {
address: IAddress;
caller?: IAddress;
Expand All @@ -20,12 +15,10 @@ interface ILegacyQuery {
getEncodedArguments(): string[];
}

export class SmartContractQueriesAdaptor {
abi?: IAbi;
export class SmartContractQueriesAdapter {
networkProvider: INetworkProvider;

constructor(options: { abi?: IAbi; networkProvider: INetworkProvider }) {
this.abi = options.abi;
constructor(options: { networkProvider: INetworkProvider }) {
this.networkProvider = options.networkProvider;
}

Expand Down
1 change: 0 additions & 1 deletion src/adaptors/index.ts

This file was deleted.

28 changes: 14 additions & 14 deletions src/smartContractQueriesController.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,12 @@ import { SmartContractQueryResponse } from "./smartContractQuery";
import { AbiRegistry, BigUIntValue, BooleanValue, BytesValue, Tuple, U16Value, U64Value } from "./smartcontracts";
import { bigIntToBuffer } from "./smartcontracts/codec/utils";
import { MockNetworkProvider, loadAbiRegistry } from "./testutils";
import { SmartContractQueriesAdaptor } from "./adaptors/smartContractQueriesAdaptor";
import { SmartContractQueriesAdapter } from "./adapters/smartContractQueriesAdapter";

describe("test smart contract queries controller", () => {
describe("createQuery", () => {
it("works without ABI, when arguments are buffers", function () {
const adapter = new SmartContractQueriesAdaptor({ networkProvider: new MockNetworkProvider() });
const adapter = new SmartContractQueriesAdapter({ networkProvider: new MockNetworkProvider() });
const controller = new SmartContractQueriesController({
queryRunner: adapter,
});
Expand All @@ -28,7 +28,7 @@ describe("test smart contract queries controller", () => {
});

it("works without ABI, when arguments are typed values", function () {
const adapter = new SmartContractQueriesAdaptor({ networkProvider: new MockNetworkProvider() });
const adapter = new SmartContractQueriesAdapter({ networkProvider: new MockNetworkProvider() });
const controller = new SmartContractQueriesController({
queryRunner: adapter,
});
Expand All @@ -45,7 +45,7 @@ describe("test smart contract queries controller", () => {
});

it("fails without ABI, when arguments aren't buffers, nor typed values", function () {
const adapter = new SmartContractQueriesAdaptor({ networkProvider: new MockNetworkProvider() });
const adapter = new SmartContractQueriesAdapter({ networkProvider: new MockNetworkProvider() });
const controller = new SmartContractQueriesController({
queryRunner: adapter,
});
Expand All @@ -60,11 +60,11 @@ describe("test smart contract queries controller", () => {
});

it("works with ABI, when arguments are native JS objects", async function () {
const adapter = new SmartContractQueriesAdaptor({
abi: await loadAbiRegistry("src/testdata/lottery-esdt.abi.json"),
const adapter = new SmartContractQueriesAdapter({
networkProvider: new MockNetworkProvider(),
});
const controller = new SmartContractQueriesController({
abi: await loadAbiRegistry("src/testdata/lottery-esdt.abi.json"),
queryRunner: adapter,
});

Expand All @@ -80,11 +80,11 @@ describe("test smart contract queries controller", () => {
});

it("works with ABI, when arguments typed values", async function () {
const adapter = new SmartContractQueriesAdaptor({
abi: await loadAbiRegistry("src/testdata/lottery-esdt.abi.json"),
const adapter = new SmartContractQueriesAdapter({
networkProvider: new MockNetworkProvider(),
});
const controller = new SmartContractQueriesController({
abi: await loadAbiRegistry("src/testdata/lottery-esdt.abi.json"),
queryRunner: adapter,
});

Expand Down Expand Up @@ -123,11 +123,11 @@ describe("test smart contract queries controller", () => {
],
});

const adapter = new SmartContractQueriesAdaptor({
abi: abi,
const adapter = new SmartContractQueriesAdapter({
networkProvider: new MockNetworkProvider(),
});
const controller = new SmartContractQueriesController({
abi: abi,
queryRunner: adapter,
});

Expand Down Expand Up @@ -162,7 +162,7 @@ describe("test smart contract queries controller", () => {
describe("runQuery", () => {
it("calls queryContract on the network provider", async function () {
const networkProvider = new MockNetworkProvider();
const adapter = new SmartContractQueriesAdaptor({
const adapter = new SmartContractQueriesAdapter({
networkProvider: networkProvider,
});
const controller = new SmartContractQueriesController({
Expand Down Expand Up @@ -192,7 +192,7 @@ describe("test smart contract queries controller", () => {

describe("parseQueryResponse", () => {
it("works without ABI", function () {
const adapter = new SmartContractQueriesAdaptor({
const adapter = new SmartContractQueriesAdapter({
networkProvider: new MockNetworkProvider(),
});
const controller = new SmartContractQueriesController({
Expand All @@ -212,11 +212,11 @@ describe("test smart contract queries controller", () => {
});

it("works with ABI", async function () {
const adapter = new SmartContractQueriesAdaptor({
abi: await loadAbiRegistry("src/testdata/lottery-esdt.abi.json"),
const adapter = new SmartContractQueriesAdapter({
networkProvider: new MockNetworkProvider(),
});
const controller = new SmartContractQueriesController({
abi: await loadAbiRegistry("src/testdata/lottery-esdt.abi.json"),
queryRunner: adapter,
});

Expand Down
6 changes: 2 additions & 4 deletions src/smartContractQueriesController.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { Address } from "./address";
import { Err } from "./errors";
import { IAddress } from "./interface";
import { IContractQueryResponse } from "./interfaceOfNetwork";
Expand All @@ -22,7 +21,6 @@ interface ILegacyQuery {
}

interface IQueryRunner {
abi?: IAbi;
networkProvider: INetworkProvider;
queryContract(query: SmartContractQuery): Promise<SmartContractQueryResponse>;
}
Expand All @@ -32,8 +30,8 @@ export class SmartContractQueriesController {
private readonly queryRunner: IQueryRunner;
private readonly legacyResultsParser: ResultsParser;

constructor(options: { queryRunner: IQueryRunner }) {
this.abi = options.queryRunner.abi;
constructor(options: { abi?: IAbi; queryRunner: IQueryRunner }) {
this.abi = options.abi;
this.queryRunner = options.queryRunner;
this.legacyResultsParser = new ResultsParser();
}
Expand Down

0 comments on commit 127937c

Please sign in to comment.