Skip to content

Commit

Permalink
Add ERC721 util class with tokenUriToJson method
Browse files Browse the repository at this point in the history
  • Loading branch information
JunichiSugiura committed Oct 8, 2024
1 parent 8d9ae55 commit 08841aa
Show file tree
Hide file tree
Showing 7 changed files with 1,763 additions and 11 deletions.
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@
"profil": "pnpm --filter @cartridge/profile",
"utils": "pnpm --filter @cartridge/utils",
"example:next": "pnpm --filter starknet-react-next",
"test": "pnpm keychain test",
"test:ci": "pnpm keychain test:ci",
"test": "pnpm test",
"test:ci": "pnpm test:ci",
"test:storybook": "pnpm turbo build:deps test:storybook"
},
"devDependencies": {
Expand Down
10 changes: 10 additions & 0 deletions packages/utils/jest.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { Config } from "jest";

const config: Config = {
preset: "ts-jest",
roots: ["<rootDir>/src"],
testMatch: ["**/*.test.ts"],
moduleFileExtensions: ["js", "ts"],
};

export default config;
7 changes: 6 additions & 1 deletion packages/utils/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@
"scripts": {
"build:deps": "tsc",
"format": "prettier --write \"src/**/*.ts\"",
"format:check": "prettier --check \"src/**/*.ts\""
"format:check": "prettier --check \"src/**/*.ts\"",
"test": "jest --watch",
"test:ci": "jest --ci"
},
"files": [
"dist"
Expand All @@ -18,7 +20,10 @@
},
"devDependencies": {
"@cartridge/tsconfig": "workspace:^",
"@jest/globals": "^29.7.0",
"@types/node": "^20.6.0",
"jest": "^29.3.1",
"ts-jest": "^29.2.5",
"typescript": "^5.4.5"
}
}
1,486 changes: 1,486 additions & 0 deletions packages/utils/src/erc721.test.ts

Large diffs are not rendered by default.

52 changes: 52 additions & 0 deletions packages/utils/src/erc721.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
import { shortString } from "starknet";

export class ERC721 {
private address: string;
// private provider: Provider;

constructor({
address,
}: // provider
{
address: string;
// provider: Provider
}) {
this.address = address;
// this.provdier = provider;
}

async tokenUriToJson(result: string[]): Promise<Record<string, any>> {
const dataUri = result.map(shortString.decodeShortString).join("");
switch (this.address) {
// impl contract specific logic here if necessary
// Starknet.id
case "0x05dbdedc203e92749e2e746e2d40a768d966bd243df04a6b712e222bc040a9af": {
const url = new URL(
result.slice(1, -1).map(shortString.decodeShortString).join(""),
);
const res = await fetch(url);
return res.json();
}
// Realms: Loot Survivor
case "0x018108b32cea514a78ef1b0e4a0753e855cdf620bc0565202c02456f618c4dc4": {
break;
}
default: {
if (dataUri.includes("data:application/json;base64,")) {
const [, encodedData] = dataUri.split(",");
const decodedData = atob(encodedData);
return JSON.parse(decodedData);
}
if (
dataUri.includes("data:application/json;utf8,") ||
dataUri.includes("data:application/json,")
) {
const data = dataUri.split(",").slice(1).join(",");
return JSON.parse(data);
}
}
}

throw new Error(`Unsupported data URI format: ${dataUri}`);
}
}
Loading

0 comments on commit 08841aa

Please sign in to comment.