Skip to content
This repository was archived by the owner on Sep 5, 2024. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions .eslintignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# /node_modules/* in the project root is ignored by default
# build artefacts
dist/*
coverage/*
# data definition files
**/*.d.ts
# 3rd party libs
/src/public/
# custom definition files
/src/types/
/src/sdk/generated
/src/resources
29 changes: 29 additions & 0 deletions .eslintrc.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
module.exports = {
parser: "@typescript-eslint/parser",
extends: [
"eslint:recommended",
"plugin:@typescript-eslint/eslint-recommended",
"plugin:@typescript-eslint/recommended",
"prettier",
],
plugins: ["@typescript-eslint", "simple-import-sort", "prettier"],
rules: {
semi: ["error", "always"],
"@typescript-eslint/explicit-function-return-type": "off",
"@typescript-eslint/no-explicit-any": 1,
"prettier/prettier": ["error", { quote: false }],
"@typescript-eslint/no-inferrable-types": [
"warn",
{
ignoreParameters: true,
},
],
"simple-import-sort/imports": "error",
"@typescript-eslint/no-use-before-define": [
"error",
{ functions: false, classes: true },
],
"@typescript-eslint/no-unused-vars": "warn",
"@typescript-eslint/no-var-requires": 0,
},
};
1 change: 1 addition & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,5 @@ jobs:
with:
bun-version: latest
- run: bun i
- run: bun run lint
- run: bun run test
Binary file modified bun.lockb
Binary file not shown.
9 changes: 9 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
"fetch:schema": "curl -s https://graph.defined.fi/schema/latest.graphql --output src/resources/schema.graphql",
"codegen": "graphql-codegen --config codegen.ts",
"prepack": "cp src/resources/* dist/resources/",
"lint": "eslint --ext .ts src",
"test": "jest"
},
"files": [
Expand All @@ -34,8 +35,16 @@
"@graphql-codegen/client-preset": "^4.1.0",
"@graphql-codegen/introspection": "4.0.0",
"@graphql-codegen/typescript": "4.0.1",
"@typescript-eslint/eslint-plugin": "^6.7.3",
"@typescript-eslint/parser": "^6.7.3",
"eslint": "^8.50.0",
"eslint-config-prettier": "^9.0.0",
"eslint-plugin-import": "^2.28.1",
"eslint-plugin-prettier": "^5.0.0",
"eslint-plugin-simple-import-sort": "^10.0.0",
"jest": "^29.6.4",
"merge": "^2.1.1",
"prettier": "^3.0.3",
"ts-jest": "^29.1.1"
}
}
4 changes: 2 additions & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
export * as GraphQL from './resources/graphql';
export * from './sdk';
export * as GraphQL from "./resources/graphql";
export * from "./sdk";
54 changes: 31 additions & 23 deletions src/sdk/Query.test.ts
Original file line number Diff line number Diff line change
@@ -1,32 +1,40 @@
import { Event } from "../resources/graphql";
import { Defined } from "./index";

describe('Query', () => {
const sdk = new Defined('dummy-key')
describe("Query", () => {
const sdk = new Defined("dummy-key");

it("should set up queries", async () => {
jest.spyOn(sdk, 'query').mockResolvedValue({
jest.spyOn(sdk, "query").mockResolvedValue({
pairEvents: {
items: [{
address: "0x1234",
}] as Event[]
}
})
const res = await sdk.queries.pairEvents({ query: { address: "0x1234", networkId: 1 } })
expect(res.pairEvents?.items?.[0]?.address).toEqual("0x1234")
expect(sdk.queries).toBeDefined()
})
items: [
{
address: "0x1234",
},
] as Event[],
},
});
const res = await sdk.queries.pairEvents({
query: { address: "0x1234", networkId: 1 },
});
expect(res.pairEvents?.items?.[0]?.address).toEqual("0x1234");
expect(sdk.queries).toBeDefined();
});

it("should have a pairEvents query", async () => {
jest.spyOn(sdk, 'query').mockResolvedValue({
jest.spyOn(sdk, "query").mockResolvedValue({
pairEvents: {
items: [{
address: "0x1234",
}] as Event[]
}
})
const res = await sdk.queries.pairEvents({ query: { address: "0x1234", networkId: 1 } })
expect(res.pairEvents?.items?.[0]?.address).toEqual("0x1234")
expect(sdk.queries).toBeDefined()
})
})
items: [
{
address: "0x1234",
},
] as Event[],
},
});
const res = await sdk.queries.pairEvents({
query: { address: "0x1234", networkId: 1 },
});
expect(res.pairEvents?.items?.[0]?.address).toEqual("0x1234");
expect(sdk.queries).toBeDefined();
});
});
53 changes: 40 additions & 13 deletions src/sdk/Query.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,43 @@
import { Defined } from "./index";
import { GetSparklinesDocument, GetSparklinesQueryVariables, GetTokenPriceDocument, GetTokensDocument, GetTokensQuery, QueryGetTokenPricesArgs, SubscriptionOnPriceUpdatedArgs, PairEventsDocument, PairEventsQueryVariables, QueryTokenArgs, GetBarsDocument, QueryListTopTokensArgs, ListTopTokensDocument, FilterTokensQueryVariables, FilterTokensDocument, FilterExchangesQueryVariables, FilterExchangesDocument, GetSymbolQueryVariables, GetSymbolDocument } from "./generated/graphql";
import { QueryGetBarsArgs } from "../resources/graphql";
import {
FilterExchangesDocument,
FilterExchangesQueryVariables,
FilterTokensDocument,
FilterTokensQueryVariables,
GetBarsDocument,
GetSparklinesDocument,
GetSparklinesQueryVariables,
GetSymbolDocument,
GetSymbolQueryVariables,
GetTokenPriceDocument,
GetTokensDocument,
ListTopTokensDocument,
PairEventsDocument,
PairEventsQueryVariables,
QueryGetTokenPricesArgs,
QueryListTopTokensArgs,
QueryTokenArgs,
} from "./generated/graphql";
import { Defined } from "./index";

export class Query {
constructor(private sdk: Defined) { }
pairEvents = async (vars: PairEventsQueryVariables) => this.sdk.query(PairEventsDocument, vars)
sparklines = async (vars: GetSparklinesQueryVariables) => this.sdk.query(GetSparklinesDocument, vars)
token = async (vars: QueryTokenArgs) => this.sdk.query(GetTokensDocument, vars)
price = async (vars: QueryGetTokenPricesArgs) => this.sdk.query(GetTokenPriceDocument, vars)
bars = async (vars: QueryGetBarsArgs) => this.sdk.query(GetBarsDocument, vars)
topTokens = async (vars: QueryListTopTokensArgs) => this.sdk.query(ListTopTokensDocument, vars)
filterTokens = async (vars: FilterTokensQueryVariables) => this.sdk.query(FilterTokensDocument, vars)
filterExchanges = async (vars: FilterExchangesQueryVariables) => this.sdk.query(FilterExchangesDocument, vars)
symbol = async (vars: GetSymbolQueryVariables) => this.sdk.query(GetSymbolDocument, vars)
}
constructor(private sdk: Defined) {}
pairEvents = async (vars: PairEventsQueryVariables) =>
this.sdk.query(PairEventsDocument, vars);
sparklines = async (vars: GetSparklinesQueryVariables) =>
this.sdk.query(GetSparklinesDocument, vars);
token = async (vars: QueryTokenArgs) =>
this.sdk.query(GetTokensDocument, vars);
price = async (vars: QueryGetTokenPricesArgs) =>
this.sdk.query(GetTokenPriceDocument, vars);
bars = async (vars: QueryGetBarsArgs) =>
this.sdk.query(GetBarsDocument, vars);
topTokens = async (vars: QueryListTopTokensArgs) =>
this.sdk.query(ListTopTokensDocument, vars);
filterTokens = async (vars: FilterTokensQueryVariables) =>
this.sdk.query(FilterTokensDocument, vars);
filterExchanges = async (vars: FilterExchangesQueryVariables) =>
this.sdk.query(FilterExchangesDocument, vars);
symbol = async (vars: GetSymbolQueryVariables) =>
this.sdk.query(GetSymbolDocument, vars);
}
26 changes: 18 additions & 8 deletions src/sdk/Subscribe.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,22 @@
import { print } from "graphql";
import { Sink } from "graphql-ws";

import {
OnBarsUpdatedDocument,
OnEventsCreatedDocument,
OnPriceUpdatedDocument,
SubscriptionOnBarsUpdatedArgs,
SubscriptionOnEventsCreatedArgs,
SubscriptionOnPriceUpdatedArgs,
} from "./generated/graphql";
import { Defined } from "./index";
import { print } from 'graphql'
import { OnBarsUpdatedDocument, OnEventsCreatedDocument, OnPriceUpdatedDocument, SubscriptionOnBarsUpdatedArgs, SubscriptionOnEventsCreatedArgs, SubscriptionOnPriceUpdatedArgs } from "./generated/graphql";
import { Sink } from "graphql-ws";

export class Subscribe {
constructor(private sdk: Defined) { }
tokenPrices = async (vars: SubscriptionOnPriceUpdatedArgs, sink: Sink) => this.sdk.subscribe(print(OnPriceUpdatedDocument), vars, sink)
tokenEvents = async (vars: SubscriptionOnEventsCreatedArgs, sink: Sink) => this.sdk.subscribe(print(OnEventsCreatedDocument), vars, sink)
bars = async (vars: SubscriptionOnBarsUpdatedArgs, sink: Sink) => this.sdk.subscribe(print(OnBarsUpdatedDocument), vars, sink)
}
constructor(private sdk: Defined) {}
tokenPrices = async (vars: SubscriptionOnPriceUpdatedArgs, sink: Sink) =>
this.sdk.subscribe(print(OnPriceUpdatedDocument), vars, sink);
tokenEvents = async (vars: SubscriptionOnEventsCreatedArgs, sink: Sink) =>
this.sdk.subscribe(print(OnEventsCreatedDocument), vars, sink);
bars = async (vars: SubscriptionOnBarsUpdatedArgs, sink: Sink) =>
this.sdk.subscribe(print(OnBarsUpdatedDocument), vars, sink);
}
92 changes: 65 additions & 27 deletions src/sdk/index.test.ts
Original file line number Diff line number Diff line change
@@ -1,40 +1,78 @@
import { Defined } from "./index"
import { DocumentNode } from "graphql";

const getNetworksDocument = {"kind":"Document","definitions":[{"kind":"OperationDefinition","operation":"query","name":{"kind":"Name","value":"Networks"},"selectionSet":{"kind":"SelectionSet","selections":[{"kind":"Field","name":{"kind":"Name","value":"getNetworks"},"selectionSet":{"kind":"SelectionSet","selections":[{"kind":"Field","name":{"kind":"Name","value":"id"}},{"kind":"Field","name":{"kind":"Name","value":"name"}}]}}]}}]} as any;
const getNetworksString = `query GetNetworks { getNetworks { id } }`
const subscribePriceUpdatedString = `subscription onPriceUpdated($address: String!, $networkId: Int!) { onPriceUpdated(address: $address, networkId: $networkId) { address networkId priceUsd timestamp } }`
import { Defined } from "./index";

describe('Defined', () => {
let sdk: Defined
const getNetworksDocument = {
kind: "Document",
definitions: [
{
kind: "OperationDefinition",
operation: "query",
name: { kind: "Name", value: "Networks" },
selectionSet: {
kind: "SelectionSet",
selections: [
{
kind: "Field",
name: { kind: "Name", value: "getNetworks" },
selectionSet: {
kind: "SelectionSet",
selections: [
{ kind: "Field", name: { kind: "Name", value: "id" } },
{ kind: "Field", name: { kind: "Name", value: "name" } },
],
},
},
],
},
},
],
} as unknown as DocumentNode;

const getNetworksString = `query GetNetworks { getNetworks { id } }`;
const subscribePriceUpdatedString = `subscription onPriceUpdated($address: String!, $networkId: Int!) { onPriceUpdated(address: $address, networkId: $networkId) { address networkId priceUsd timestamp } }`;

describe("Defined", () => {
let sdk: Defined;
beforeEach(() => {
sdk = new Defined('dummy-key')
})
sdk = new Defined("dummy-key");
});

describe("query", () => {
it("send a fetch request with the correct params", async () => {
const fetchSpy = jest.spyOn(sdk['client'], 'request').mockResolvedValue({} as any)
await sdk.query(getNetworksDocument, {})
expect(fetchSpy).toHaveBeenCalledWith(getNetworksDocument, {})
})
})
const fetchSpy = jest
.spyOn(sdk["client"], "request")
.mockResolvedValue({} as unknown);
await sdk.query(getNetworksDocument, {});
expect(fetchSpy).toHaveBeenCalledWith(getNetworksDocument, {});
});
});
describe("send", () => {
it("should return the results of the query", async () => {
const fetchSpy = jest.spyOn(sdk['client'], 'request').mockResolvedValue({} as any)
await sdk.send(getNetworksString, {})
expect(fetchSpy).toHaveBeenCalledWith(getNetworksString, {})
})
})
const fetchSpy = jest
.spyOn(sdk["client"], "request")
.mockResolvedValue({} as unknown);
await sdk.send(getNetworksString, {});
expect(fetchSpy).toHaveBeenCalledWith(getNetworksString, {});
});
});
describe("subscribe", () => {
it("should return a cleanup function", () => {
const next = jest.fn(), error = jest.fn(), complete = jest.fn()
const next = jest.fn(),
error = jest.fn(),
complete = jest.fn();
const sink = {
next,
error,
complete
}
jest.spyOn(sdk['wsClient'], 'subscribe').mockReturnValue(() => {})
const cleanup = sdk.subscribe(subscribePriceUpdatedString, { address: "0xtoken", networkId: 1 }, sink)
expect(typeof cleanup).toBe("function")
})
})
})
complete,
};
jest.spyOn(sdk["wsClient"], "subscribe").mockReturnValue(() => {});
const cleanup = sdk.subscribe(
subscribePriceUpdatedString,
{ address: "0xtoken", networkId: 1 },
sink,
);
expect(typeof cleanup).toBe("function");
});
});
});
Loading