Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add functionality to zNS SDK to support zNA resolution #120

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
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
20 changes: 20 additions & 0 deletions src/api/dataStoreApi/actions/getDomainResourceAssociations.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import { Maybe } from "../../../types";
import { ResourceAssociation } from "../types";
import { makeApiCall } from "../../helpers";

export const getDomainResourceAssociations = async (
apiUri: string,
domainId: string
): Promise<ResourceAssociation[]> => {
let response: Maybe<ResourceAssociation[]>;
try {
response = await makeApiCall<ResourceAssociation[]>(
`${apiUri}v1/domains/resource/${domainId}`,
"GET"
);
} catch (e) {
throw Error(`Failed to get domain resource associations: ${domainId}`);
}

return response;
};
20 changes: 20 additions & 0 deletions src/api/dataStoreApi/actions/getResourceRegistry.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import { Maybe } from "../../../types";
import { ResourceRegistry } from "../types";
import { makeApiCall } from "../../helpers";

export const getResourceRegistry = async (
apiUri: string,
resourceType: string
): Promise<Maybe<ResourceRegistry>> => {
let response: Maybe<ResourceRegistry>;
try {
response = await makeApiCall<ResourceRegistry>(
`${apiUri}v1/resource-registry/get/${resourceType}`,
"GET"
);
} catch (e) {
throw Error(`Failed to get resource registry: ${resourceType}`);
}

return response;
};
2 changes: 2 additions & 0 deletions src/api/dataStoreApi/actions/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,5 @@ export * from "./getMostRecentSubdomainsById";
export * from "./getDomainsByOwner";
export * from "./getDomainById";
export * from "./getSubdomainsByIdDeep";
export * from "./getDomainResourceAssociations";
export * from "./getResourceRegistry";
32 changes: 31 additions & 1 deletion src/api/dataStoreApi/client.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
import { Domain, Maybe } from "../../types";
import * as actions from "./actions";
import { getLogger } from "../../utilities";
import { DomainSortOptions } from "./types";
import {
DomainSortOptions,
ResourceAssociation,
ResourceRegistry
} from "./types";

const logger = getLogger("api:client");

Expand Down Expand Up @@ -32,6 +36,12 @@ export interface DataStoreApiClient {
limit: number,
skip: number
) => Promise<Domain[]>;
getDomainResourceAssociations: (
domainId: string
) => Promise<ResourceAssociation[]>;
getResourceRegistry: (
resourceType: string
) => Promise<Maybe<ResourceRegistry>>;
}

export const createDataStoreApiClient = (
Expand Down Expand Up @@ -115,6 +125,26 @@ export const createDataStoreApiClient = (

return domains;
},

getDomainResourceAssociations: async (
domainId: string
): Promise<ResourceAssociation[]> => {
logger.debug("Calling to getDomainResourceAssociations");
const resourceAssociations: ResourceAssociation[] =
await actions.getDomainResourceAssociations(apiUri, domainId);

return resourceAssociations;
},

getResourceRegistry: async (
resourceType: string
): Promise<Maybe<ResourceRegistry>> => {
logger.debug("Calling to getResourceRegistry");
const resourceRegistry: Maybe<ResourceRegistry> =
await actions.getResourceRegistry(apiUri, resourceType);

return resourceRegistry;
},
};

return apiClient;
Expand Down
1 change: 1 addition & 0 deletions src/api/dataStoreApi/helpers/datastoreDomainToDomain.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ export function datastoreDomainToDomain(d: DataStoreDomain): Domain {
contract: d.registrar.toLowerCase(),
isRoot: d.isRoot,
buyNow: buyNow,
resources: d.resources,
};
return domain;
}
14 changes: 14 additions & 0 deletions src/api/dataStoreApi/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ export interface DataStoreDomain {
buyNow: BuyNow;
locked: boolean; // Older domains may not have these properties
lockedBy: string;
resources: ResourceAssociations;
}

export interface BuyNowPriceListing {
Expand All @@ -50,6 +51,19 @@ export interface BuyNow {
isActive: boolean;
}

export interface ResourceRegistry {
resourceType: string;
resourceRegistry: string;
}

export interface ResourceAssociation extends ResourceRegistry {
resourceId: string;
}

export interface ResourceAssociations {
[resourceType: string]: ResourceAssociation;
}

type Show = 1;
type Hide = 0;
type OptionsValue = Show | Hide;
Expand Down
16 changes: 15 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,11 @@ import {
DomainPurchaserConfig,
NetworkDomainMintableConfig,
} from "./actions/minting/types";
import { DomainSortOptions } from "./api/dataStoreApi/types";
import {
DomainSortOptions,
ResourceAssociation,
ResourceRegistry,
} from "./api/dataStoreApi/types";

export * from "./types";
export { configuration };
Expand Down Expand Up @@ -189,6 +193,16 @@ export const createInstance = (config: Config): Instance => {
getAllDomains: subgraphClient.getAllDomains,
getDomainMetrics: async (domainIds: string[]) =>
getDomainMetrics(config.metricsUri, domainIds),
getDomainResourceAssociations: async (
domainId: string
): Promise<ResourceAssociation[]> => {
return await dataStoreApiClient.getDomainResourceAssociations(domainId);
},
getResourceRegistry: async (
resourceType: string
): Promise<Maybe<ResourceRegistry>> => {
return await dataStoreApiClient.getResourceRegistry(resourceType);
},
mintSubdomain: async (
params: SubdomainParams,
signer: ethers.Signer,
Expand Down
18 changes: 18 additions & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ import { Bid, BuyNowListing } from "./zAuction";
import {
BuyNowPriceListing,
DomainSortOptions,
ResourceAssociation,
ResourceAssociations,
ResourceRegistry,
} from "./api/dataStoreApi/types";

export type DexSubgraphUris = Map<string>;
Expand Down Expand Up @@ -244,6 +247,20 @@ export interface Instance {
*/
getDomainMetrics(domainIds: string[]): Promise<DomainMetricsCollection>;

/**
* Gets all the resource associations for a domain
* @param domainId Domain id to get resource associations for
*/
getDomainResourceAssociations(
domainId: string
): Promise<ResourceAssociation[]>;

/**
* Get resource registry information for a resource type
* @param resourceType Resource type to get resource registry for
*/
getResourceRegistry(resourceType: string): Promise<Maybe<ResourceRegistry>>;

/**
* Mints a new subdomain
* @param params The subdomain parameters
Expand Down Expand Up @@ -800,6 +817,7 @@ export interface Domain {
created?: Created;
isRoot?: boolean;
buyNow?: BuyNowPriceListing;
resources?: ResourceAssociations;
}

export interface DomainMetadata {
Expand Down
7 changes: 5 additions & 2 deletions test/e2e.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,11 +62,14 @@ describe("SDK test", () => {
const info = await sdk.zauction.getPaymentTokenInfo(tokenOnUniNotCG);
expect(info.symbol).to.eq("SHKOOBYSHNAX");
});
it("Reaches out to Sushiswap for a coin that's not on Uniswap", async () => {

// Sushiswap subgraph no longer exists, so these tests are disabled until a new backup for token info is found.
// See https://wilderworld.atlassian.net/browse/MUD-210
xit("Reaches out to Sushiswap for a coin that's not on Uniswap", async () => {
const info = await sdk.zauction.getPaymentTokenInfo(tokenOnSushiNotUni);
expect(info.symbol).to.eq("KING");
});
it("Fails when token is not found", async () => {
xit("Fails when token is not found", async () => {
return await sdk.zauction.getPaymentTokenInfo(randomToken)
.catch(error => {
const expectedMessage = `Token info with address ${randomToken} could not be found`;
Expand Down