-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #44 from MVPWorkshop/fix/gettokenlist
Fix/gettokenlist
- Loading branch information
Showing
8 changed files
with
152 additions
and
138 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,20 +1,54 @@ | ||
import { TheaNetwork, TokenListResponsePayload, TokenResponseFromRaribleAPI } from "src/types"; | ||
import { consts, TOKEN_LIST_FETCHING_URL } from "src/utils/consts"; | ||
import { GraphqlQuery, QueryError, QueryErrorResponse, QueryResponse, TheaNetwork, TokenInfo } from "src/types"; | ||
import { TheaSubgraphError } from "src/utils"; | ||
import { consts } from "src/utils/consts"; | ||
import { HttpClient } from "../shared"; | ||
|
||
export const tokenInfo: GraphqlQuery = { | ||
query: `{ | ||
tokens(orderBy: projectId) { | ||
id | ||
tokenURI | ||
projectId | ||
vintage | ||
activeAmount | ||
mintedAmount | ||
retiredAmount | ||
unwrappedAmount | ||
} | ||
}` | ||
}; | ||
export class GetTokenList { | ||
readonly httpClient: HttpClient; | ||
readonly network: TheaNetwork; | ||
|
||
constructor(network: TheaNetwork) { | ||
this.network = network; | ||
this.httpClient = new HttpClient(TOKEN_LIST_FETCHING_URL); | ||
this.httpClient = new HttpClient(consts[`${network}`].subGraphUrl); | ||
} | ||
async getTokenList() { | ||
const response = await this.httpClient.get<TokenListResponsePayload>("/items/byCollection", { | ||
collection: consts[`${this.network}`].networkName + ":" + consts[`${this.network}`].theaERC1155Contract | ||
}); | ||
const tokenIdsList = response.items.map((item: TokenResponseFromRaribleAPI) => item.tokenId); | ||
return tokenIdsList; | ||
const response = await this.httpClient.post< | ||
GraphqlQuery, | ||
QueryResponse<{ tokens: TokenInfo[] }> | QueryErrorResponse | ||
>("", tokenInfo); | ||
|
||
const tokens = this.handleResponse<{ tokens: TokenInfo[] }, TokenInfo[]>(response, "tokens"); | ||
return tokens.reduce((acc, token: TokenInfo) => { | ||
const projectId = token.projectId; | ||
if (acc[`${projectId}`]) { | ||
acc[`${projectId}`].push(token); | ||
} else { | ||
acc[`${projectId}`] = [token]; | ||
} | ||
return acc; | ||
}, {} as Record<string, TokenInfo[]>); | ||
} | ||
private handleResponse<T, Response>( | ||
response: QueryResponse<T> | QueryErrorResponse, | ||
responseProperty: keyof T | ||
): Response { | ||
if ("errors" in response) throw new TheaSubgraphError("Subgraph call error", response.errors as QueryError[]); | ||
|
||
// eslint-disable-next-line security/detect-object-injection | ||
return response.data[responseProperty] as Response; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,27 +1,40 @@ | ||
import { TheaNetwork, GetTokenList, consts } from "../../../src"; | ||
import { itemsByCollection } from "../../mocks"; | ||
import { TheaNetwork, GetTokenList, tokenInfo, TheaSubgraphError, QueryError } from "../../../src"; | ||
import { subgraphResponse } from "../../mocks"; | ||
|
||
jest.mock("../../../src/modules/shared/httpClient", () => { | ||
return { | ||
HttpClient: jest.fn().mockImplementation(() => { | ||
return { | ||
get: jest.fn().mockReturnValue(itemsByCollection) | ||
post: jest.fn().mockReturnValue(subgraphResponse) | ||
}; | ||
}) | ||
}; | ||
}); | ||
|
||
describe("GetTokenList", () => { | ||
const getTokenList: GetTokenList = new GetTokenList(TheaNetwork.GANACHE); | ||
const httpGetSpy = jest.spyOn(getTokenList.httpClient, "get"); | ||
const httpPostSpy = jest.spyOn(getTokenList.httpClient, "post"); | ||
|
||
it("should throw error with list of query errors", async () => { | ||
const expectedResult = { errors: [{ error: "indexing_error" }] }; | ||
jest.spyOn(getTokenList.httpClient, "post").mockResolvedValueOnce(expectedResult); | ||
|
||
await expect(getTokenList.getTokenList()).rejects.toThrow( | ||
new TheaSubgraphError("Subgraph call error", [{ error: "indexing_error" }] as QueryError[]) | ||
); | ||
}); | ||
|
||
describe("getTokenList", () => { | ||
it("should return a token ID list", async () => { | ||
const result = await getTokenList.getTokenList(); | ||
expect(result).toEqual([itemsByCollection.items[0].tokenId]); | ||
expect(httpGetSpy).toBeCalledWith("/items/byCollection", { | ||
collection: consts[TheaNetwork.GANACHE].networkName + ":" + consts[TheaNetwork.GANACHE].theaERC1155Contract | ||
expect(result).toEqual({ | ||
[`${subgraphResponse.data.tokens[0].projectId}`]: [subgraphResponse.data.tokens[0]], | ||
[`${subgraphResponse.data.tokens[1].projectId}`]: [ | ||
subgraphResponse.data.tokens[1], | ||
subgraphResponse.data.tokens[2] | ||
] | ||
}); | ||
expect(httpPostSpy).toBeCalledWith("", tokenInfo); | ||
}); | ||
}); | ||
}); |
Oops, something went wrong.