Skip to content

Commit

Permalink
add /utils export
Browse files Browse the repository at this point in the history
  • Loading branch information
jnsdls committed Feb 7, 2024
1 parent 2bc20e2 commit c154956
Show file tree
Hide file tree
Showing 8 changed files with 277 additions and 156 deletions.
78 changes: 0 additions & 78 deletions packages/thirdweb/benchmark/contract-read.ts

This file was deleted.

26 changes: 16 additions & 10 deletions packages/thirdweb/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,6 @@
},
"author": "thirdweb eng <[email protected]>",
"type": "module",
"bin": {
"thirdweb": "node_modules/@thirdweb-dev/cli/dist/cli/index.js"
},
"main": "./dist/cjs/index.js",
"module": "./dist/esm/index.js",
"types": "./dist/types/index.d.ts",
Expand Down Expand Up @@ -54,6 +51,11 @@
"import": "./dist/esm/wallets/index.js",
"default": "./dist/cjs/wallets/index.js"
},
"./utils": {
"types": "./dist/types/utils/index.d.ts",
"import": "./dist/esm/utils/index.js",
"default": "./dist/cjs/utils/index.js"
},
"./wallets/*": {
"types": "./dist/types/wallets/*.d.ts",
"import": "./dist/esm/wallets/*.js",
Expand Down Expand Up @@ -91,6 +93,9 @@
"wallets": [
"./dist/types/wallets/index.d.ts"
],
"utils": [
"./dist/types/utils/index.d.ts"
],
"wallets/*": [
"./dist/types/wallets/*.d.ts"
],
Expand Down Expand Up @@ -127,30 +132,31 @@
"@radix-ui/react-popover": "^1.0.7",
"@radix-ui/react-tabs": "^1.0.4",
"@radix-ui/react-tooltip": "^1.0.7",
"@tanstack/react-query": "^5.18.0",
"@walletconnect/ethereum-provider": "^2.11.0",
"@tanstack/react-query": "^5.18.1",
"@walletconnect/ethereum-provider": "^2.11.1",
"abitype": "^1.0.0",
"bs58": "^5.0.0",
"cbor-x": "^1.5.8",
"fuse.js": "^7.0.0",
"mipd": "^0.0.5",
"qrcode": "^1.5.3",
"viem": "^2.7.1"
"viem": "^2.7.6"
},
"devDependencies": {
"@tanstack/eslint-plugin-query": "^5.18.0",
"@tanstack/eslint-plugin-query": "^5.18.1",
"@thirdweb-dev/sdk": "workspace:*",
"@types/bun": "^1.0.4",
"@types/bun": "^1.0.5",
"@types/qrcode": "^1.5.5",
"@types/react": "18.2.17",
"@viem/anvil": "^0.0.7",
"@vitest/coverage-v8": "^1.2.2",
"eslint-config-thirdweb": "workspace:*",
"eslint-plugin-better-tree-shaking": "0.0.4",
"eslint-plugin-jsdoc": "^48.0.4",
"eslint-plugin-jsdoc": "^48.0.6",
"ethers5": "npm:ethers@^5.0.0",
"ethers6": "npm:ethers@^6.0.0",
"react": "^18.2.0",
"rimraf": "^5.0.5",
"tinybench": "^2.6.0",
"typedoc-gen": "workspace:*",
"typescript": "^5.3.3",
"vitest": "^1.2.2"
Expand Down
2 changes: 1 addition & 1 deletion packages/thirdweb/src/extensions/erc20/read/decimals.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import type { ThirdwebContract } from "../../../contract/index.js";
import { readContract } from "../../../transaction/actions/read.js";
import type { TxOpts } from "../../../transaction/transaction.js";
import { detectMethod } from "../../../utils/extensions/detect.js";
import { detectMethod } from "../../../utils/bytecode/detectExtension.js";

const cache = new WeakMap<ThirdwebContract<any>, Promise<number>>();

Expand Down
38 changes: 38 additions & 0 deletions packages/thirdweb/src/utils/bytecode/extractIPFS.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import { describe, it, expect } from "vitest";
import { getByteCode } from "../../contract/index.js";
import {
DOODLES_CONTRACT,
USDC_CONTRACT,
} from "../../../test/src/test-contracts.js";
import { extractIPFSUri } from "./extractIPFS.js";

describe("extractIPFSUri", () => {
it("works if ipfs is there", async () => {
// get some bytecode
const bytecode = await getByteCode(DOODLES_CONTRACT);
// extract IPFS hash
const ipfsHash = extractIPFSUri(bytecode);

// DOODLES bytecode contains an IPFS hash
expect(ipfsHash).toMatchInlineSnapshot(
`"ipfs://QmRKWTE2aFc8VZdsHAZ8B7vMStPBFwn4AQr7WGUhjkuJWb"`,
);
});

it("does not work if ipfs is not there", async () => {
// get some bytecode
const bytecode = await getByteCode(USDC_CONTRACT);
// extract IPFS hash
const ipfsHash = extractIPFSUri(bytecode);

// USDC bytecode does not contain an IPFS hash
expect(ipfsHash).toMatchInlineSnapshot(`undefined`);
});

it("works with the weird mumbai contract", async () => {
const ipfsHash = extractIPFSUri(
"0x363d3d373d3d3d363d30545af43d82803e903d91601857fd5bf3",
);
expect(ipfsHash).toMatchInlineSnapshot(`undefined`);
});
});
40 changes: 40 additions & 0 deletions packages/thirdweb/src/utils/bytecode/extractIPFS.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import { hexToBytes } from "@noble/hashes/utils";
import { decode } from "cbor-x";
import { encode } from "bs58";

/**
* Extracts the IPFS URI from the given bytecode.
* @param bytecode - The bytecode to extract the IPFS URI from.
* @returns The IPFS URI if found, otherwise undefined.
* @example
* ```ts
* import { extractIPFSUri } from "thirdweb/utils/bytecode/extractIPFS";
* const bytecode = "0x363d3d373d3d3d363d30545af43d82803e903d91601857fd5bf3";
* const ipfsHash = extractIPFSUri(bytecode);
* console.log(ipfsHash);
* ```
*/
export function extractIPFSUri(bytecode: string): string | undefined {
const numericBytecode = hexToBytes(
bytecode.startsWith("0x") ? bytecode.slice(2) : bytecode,
);

const cborLength =
// @ts-expect-error - TS doesn't like this, but it's fine
numericBytecode[numericBytecode.length - 2] * 0x100 +
// @ts-expect-error - TS doesn't like this, but it's fine
numericBytecode[numericBytecode.length - 1];
const cborStart = numericBytecode.length - 2 - cborLength;
// if the cborStart is invalid, return undefined
if (cborStart < 0 || cborStart > numericBytecode.length) {
return undefined;
}
const bytecodeBuffer = numericBytecode.slice(cborStart, -2);

const cborData = decode(bytecodeBuffer);
if ("ipfs" in cborData) {
return `ipfs://${encode(cborData["ipfs"])}`;
}

return undefined;
}
13 changes: 13 additions & 0 deletions packages/thirdweb/src/utils/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
// bytecode
export { extractIPFSUri } from "./bytecode/extractIPFS.js";
export { detectMethod } from "./bytecode/detectExtension.js";

// units
export {
formatEther,
formatGwei,
formatUnits,
parseEther,
parseGwei,
parseUnits,
} from "./units.js";
Loading

0 comments on commit c154956

Please sign in to comment.