-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement block number tag as TokenURI
- Loading branch information
Showing
5 changed files
with
121 additions
and
0 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
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 |
---|---|---|
@@ -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, | ||
}; | ||
} | ||
} |
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 |
---|---|---|
@@ -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)); | ||
}); | ||
}); |