Skip to content

Commit

Permalink
Add blockNumberTag
Browse files Browse the repository at this point in the history
  • Loading branch information
TimDaub committed Jul 7, 2022
1 parent b2af863 commit d0a20c8
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 7 deletions.
4 changes: 4 additions & 0 deletions src/assetId.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ export interface AssetIdParams {
chainId: string | ChainIdParams;
assetName: string | AssetNameParams;
tokenId: string;
blockNumberTag: string;
}

export class AssetId {
Expand All @@ -27,6 +28,7 @@ export class AssetId {
public chainId: ChainId;
public assetName: AssetName;
public tokenId: string;
public blockNumberTag: string;

constructor(params: AssetIdParams | string) {
if (typeof params === "string") {
Expand All @@ -36,6 +38,7 @@ export class AssetId {
this.chainId = new ChainId(params.chainId);
this.assetName = new AssetName(params.assetName);
this.tokenId = params.tokenId;
this.blockNumberTag = params.blockNumberTag;
}

public toString(): string {
Expand All @@ -47,6 +50,7 @@ export class AssetId {
chainId: this.chainId.toJSON(),
assetName: this.assetName.toJSON(),
tokenId: this.tokenId,
blockNumberTag: this.blockNumberTag,
};
}
}
28 changes: 27 additions & 1 deletion src/spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -87,12 +87,38 @@ const CAIP19AssetId: IdentifierSpec = {
},
};

const BlockNumberTag: IdentifierSpec = {
name: "blockNumberTag",
regex: "[0-9]{1,78}|latest",
parameters: {
delimiter: "",
values: {
0: {
name: "blockNumberTag",
regex: "[0-9]{1,78}|latest",
},
},
},
};

const CAIP19BlockNumberTag: IdentifierSpec = {
name: "blockNumberTag",
regex: ".*",
parameters: {
delimiter: "#",
values: {
0: CAIP19AssetId,
2: BlockNumberTag,
},
},
};

export const CAIP = {
"2": CAIP2,
"10": CAIP10,
"19": {
assetName: AssetName,
assetType: CAIP19AssetType,
assetId: CAIP19AssetId,
assetId: CAIP19BlockNumberTag,
},
};
10 changes: 5 additions & 5 deletions src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ export function getParams<T>(id: string, spec: IdentifierSpec): T {

export function joinParams(params: Params, spec: IdentifierSpec): string {
return Object.values(spec.parameters.values)
.map(parameter => {
.map((parameter) => {
const param = params[parameter.name];
return typeof param === "string"
? param
Expand All @@ -40,10 +40,10 @@ export function isValidId(id: string, spec: IdentifierSpec): boolean {
// console.log("after length");
// console.log("before matches");
const matches = params
.map((param, index) =>
new RegExp(spec.parameters.values[index].regex).test(param)
)
.filter(x => !!x);
.map((param, index) => {
return new RegExp(spec.parameters.values[index].regex).test(param);
})
.filter((x) => !!x);
if (matches.length !== params.length) return false;
// console.log("after matches");
return true;
Expand Down
5 changes: 4 additions & 1 deletion test/data/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,16 +50,19 @@ export const ASSET_TYPE_NESTED_PARAMS: AssetTypeParams = {
};

// AssetType Data Points
export const BLOCK_NUMBER_TAG = "123";
export const TOKEN_ID = "1";
export const ASSET_ID_STRING = `${ASSET_TYPE_STRING}/${TOKEN_ID}`;
export const ASSET_ID_STRING = `${ASSET_TYPE_STRING}/${TOKEN_ID}#${BLOCK_NUMBER_TAG}`;
export const ASSET_ID_PARAMS: AssetIdParams = {
chainId: CHAIN_ID_STRING,
assetName: ASSET_NAME_STRING,
tokenId: TOKEN_ID,
blockNumberTag: BLOCK_NUMBER_TAG,
};

export const ASSET_ID_NESTED_PARAMS: AssetIdParams = {
chainId: CHAIN_ID_PARAMS,
assetName: ASSET_NAME_PARAMS,
tokenId: TOKEN_ID,
blockNumberTag: BLOCK_NUMBER_TAG,
};

0 comments on commit d0a20c8

Please sign in to comment.