Skip to content

Commit

Permalink
Implement block number tag as TokenURI
Browse files Browse the repository at this point in the history
  • Loading branch information
TimDaub committed Jul 7, 2022
1 parent b2af863 commit f9f14e6
Show file tree
Hide file tree
Showing 5 changed files with 121 additions and 0 deletions.
1 change: 1 addition & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@ export * from "./account";
export * from "./chain";
export * from "./assetType";
export * from "./assetId";
export * from "./tokenURI";
16 changes: 16 additions & 0 deletions src/spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -87,12 +87,28 @@ const CAIP19AssetId: IdentifierSpec = {
},
};

const CAIP19TokenURI: IdentifierSpec = {
name: "tokenURI",
regex: "[-:a-zA-Z0-9]{13,226}",
parameters: {
delimiter: "#",
values: {
0: CAIP19AssetId,
1: {
name: "blockNumberTag",
regex: "[0-9]{1,78}|latest",
},
},
},
};

export const CAIP = {
"2": CAIP2,
"10": CAIP10,
"19": {
assetName: AssetName,
assetType: CAIP19AssetType,
assetId: CAIP19AssetId,
tokenURI: CAIP19TokenURI,
},
};
47 changes: 47 additions & 0 deletions src/tokenURI.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
import { AssetId, AssetIdParams } from "./assetId";
import { CAIP } from "./spec";
import { IdentifierSpec } from "./types";
import { isValidId, joinParams, getParams } from "./utils";

export interface TokenURIParams {
assetId: string | AssetIdParams;
blockNumberTag: string;
}

export class TokenURI {
public static spec: IdentifierSpec = CAIP["19"].tokenURI;

public static parse(id: string): TokenURIParams {
if (!isValidId(id, this.spec)) {
throw new Error(`Invalid ${this.spec.name} provided: ${id}`);
}
return new TokenURI(getParams<TokenURIParams>(id, this.spec)).toJSON();
}

public static format(params: TokenURIParams): string {
return joinParams(params as any, this.spec);
}

public assetId: AssetId;
public blockNumberTag: string;

constructor(params: TokenURIParams | string) {
if (typeof params === "string") {
params = TokenURI.parse(params);
}

this.assetId = new AssetId(params.assetId);
this.blockNumberTag = params.blockNumberTag;
}

public toString(): string {
return TokenURI.format(this.toJSON());
}

public toJSON(): TokenURIParams {
return {
assetId: this.assetId,
blockNumberTag: this.blockNumberTag,
};
}
}
13 changes: 13 additions & 0 deletions test/data/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { AssetIdParams, AssetTypeParams } from "../../src";
import { TokenURIParams } from "../../src";
import { AssetNameParams } from "../../src/assetName";

// ChainId Data Points
Expand Down Expand Up @@ -63,3 +64,15 @@ export const ASSET_ID_NESTED_PARAMS: AssetIdParams = {
assetName: ASSET_NAME_PARAMS,
tokenId: TOKEN_ID,
};

export const BLOCK_NUMBER_TAG = "123";
export const TOKEN_URI_STRING = `${ASSET_ID_STRING}#${BLOCK_NUMBER_TAG}`;
export const TOKEN_URI_PARAMS: TokenURIParams = {
assetId: ASSET_ID_STRING,
blockNumberTag: BLOCK_NUMBER_TAG,
};

export const TOKEN_URI_NESTED_PARAMS: TokenURIParams = {
assetId: ASSET_ID_NESTED_PARAMS,
blockNumberTag: BLOCK_NUMBER_TAG,
};
44 changes: 44 additions & 0 deletions test/tokenURI.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import { TokenURI } from "../src";

import * as data from "./data";

function assertInterface(result: TokenURI) {
expect(result.assetId.toString()).toEqual(data.ASSET_ID_STRING);
expect(result.blockNumberTag).toEqual(data.BLOCK_NUMBER_TAG);
expect(result.toString()).toEqual(data.TOKEN_URI_STRING);
expect(result.toJSON()).toEqual(data.TOKEN_URI_NESTED_PARAMS);
}

describe("AssetId", () => {
it("should parse string", async () => {
const result = TokenURI.parse(data.TOKEN_URI_STRING);
expect(result).toEqual(data.TOKEN_URI_NESTED_PARAMS);
});

it("should format params", async () => {
const result = TokenURI.format(data.TOKEN_URI_PARAMS);
expect(result).toEqual(data.TOKEN_URI_STRING);
});

it("should instantiate from params", async () => {
const result = new TokenURI(data.TOKEN_URI_PARAMS);
assertInterface(result);
});

it("should instantiate from string", async () => {
const result = new TokenURI(data.TOKEN_URI_STRING);
assertInterface(result);
});

it("should instantiate from nested params", async () => {
const result = new TokenURI(data.TOKEN_URI_NESTED_PARAMS);
assertInterface(result);
});

it("should support JSON.stringify", async () => {
const result = new TokenURI(data.TOKEN_URI_PARAMS);
const str = JSON.stringify(result);
const json = JSON.parse(str);
assertInterface(new TokenURI(json));
});
});

0 comments on commit f9f14e6

Please sign in to comment.