Skip to content

Commit 39c6fbc

Browse files
authored
feat(extensions): add generated is<Ext>Supported and encode<Ext> calls, add concatHex util fn (#2752)
1 parent 9453f74 commit 39c6fbc

File tree

380 files changed

+15004
-34
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

380 files changed

+15004
-34
lines changed

.changeset/happy-ads-march.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"thirdweb": minor
3+
---
4+
5+
add `concatHex` utility function

packages/thirdweb/scripts/generate/generate.ts

Lines changed: 92 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,8 @@ ${
120120
import { once } from "../../../../../utils/promise/once.js";`
121121
: ""
122122
}
123+
import type { ThirdwebContract } from "../../../../../contract/contract.js";
124+
import { detectMethod } from "../../../../../utils/bytecode/detectExtension.js";
123125
124126
125127
${
@@ -145,6 +147,24 @@ export const FN_SELECTOR = "${preparedMethod[0]}" as const;
145147
const FN_INPUTS = ${JSON.stringify(preparedMethod[1], null, 2)} as const;
146148
const FN_OUTPUTS = ${JSON.stringify(preparedMethod[2], null, 2)} as const;
147149
150+
/**
151+
* Checks if the \`${f.name}\` method is supported by the given contract.
152+
* @param contract The ThirdwebContract.
153+
* @returns A promise that resolves to a boolean indicating if the \`${f.name}\` method is supported.
154+
* @extension ERC721
155+
* @example
156+
* \`\`\`ts
157+
* import { is${uppercaseFirstLetter(
158+
f.name,
159+
)}Supported } from "thirdweb/extensions/${extensionName}";
160+
*
161+
* const supported = await is${uppercaseFirstLetter(f.name)}Supported(contract);
162+
* \`\`\`
163+
*/
164+
export async function is${uppercaseFirstLetter(f.name)}Supported(contract: ThirdwebContract<any>) {
165+
return detectMethod({contract, method: [FN_SELECTOR, FN_INPUTS, FN_OUTPUTS] as const});
166+
}
167+
148168
${
149169
f.inputs.length > 0
150170
? `/**
@@ -172,9 +192,31 @@ export function encode${uppercaseFirstLetter(
172192
`
173193
: ""
174194
}
175-
176-
177-
195+
${
196+
f.inputs.length > 0
197+
? `/**
198+
* Encodes the "${f.name}" function into a Hex string with its parameters.
199+
* @param options - The options for the ${f.name} function.
200+
* @returns The encoded hexadecimal string.
201+
* @extension ${extensionName.toUpperCase()}
202+
* @example
203+
* \`\`\`ts
204+
* import { encode${uppercaseFirstLetter(
205+
f.name,
206+
)} } "thirdweb/extensions/${extensionName}";
207+
* const result = encode${uppercaseFirstLetter(f.name)}({\n * ${f.inputs
208+
.map((x) => ` ${removeLeadingUnderscore(x.name)}: ...,`)
209+
.join("\n * ")}\n * });
210+
* \`\`\`
211+
*/
212+
export function encode${uppercaseFirstLetter(f.name)}(options: ${inputTypeName}) {
213+
\/\/ we do a "manual" concat here to avoid the overhead of the "concatHex" function
214+
\/\/ we can do this because we know the specific formats of the values
215+
return FN_SELECTOR + encode${uppercaseFirstLetter(f.name)}Params(options).slice(2) as \`\${typeof FN_SELECTOR}\${string}\`;
216+
}
217+
`
218+
: ""
219+
}
178220
/**
179221
* Calls the "${f.name}" function on the contract.
180222
* @param options - The options for the "${f.name}" function.
@@ -255,6 +297,8 @@ ${
255297
import type { Hex } from "../../../../../utils/encoding/hex.js";`
256298
: ""
257299
}
300+
import type { ThirdwebContract } from "../../../../../contract/contract.js";
301+
import { detectMethod } from "../../../../../utils/bytecode/detectExtension.js";
258302
259303
${
260304
f.inputs.length > 0
@@ -278,6 +322,24 @@ export const FN_SELECTOR = "${preparedMethod[0]}" as const;
278322
const FN_INPUTS = ${JSON.stringify(preparedMethod[1], null, 2)} as const;
279323
const FN_OUTPUTS = ${JSON.stringify(preparedMethod[2], null, 2)} as const;
280324
325+
/**
326+
* Checks if the \`${f.name}\` method is supported by the given contract.
327+
* @param contract The ThirdwebContract.
328+
* @returns A promise that resolves to a boolean indicating if the \`${f.name}\` method is supported.
329+
* @extension ERC721
330+
* @example
331+
* \`\`\`ts
332+
* import { is${uppercaseFirstLetter(
333+
f.name,
334+
)}Supported } from "thirdweb/extensions/${extensionName}";
335+
*
336+
* const supported = await is${uppercaseFirstLetter(f.name)}Supported(contract);
337+
* \`\`\`
338+
*/
339+
export async function is${uppercaseFirstLetter(f.name)}Supported(contract: ThirdwebContract<any>) {
340+
return detectMethod({contract, method: [FN_SELECTOR, FN_INPUTS, FN_OUTPUTS] as const});
341+
}
342+
281343
${
282344
f.inputs.length > 0
283345
? `/**
@@ -305,7 +367,33 @@ export function encode${uppercaseFirstLetter(
305367
`
306368
: ""
307369
}
308-
370+
${
371+
f.inputs.length > 0
372+
? `/**
373+
* Encodes the "${f.name}" function into a Hex string with its parameters.
374+
* @param options - The options for the ${f.name} function.
375+
* @returns The encoded hexadecimal string.
376+
* @extension ${extensionName.toUpperCase()}
377+
* @example
378+
* \`\`\`ts
379+
* import { encode${uppercaseFirstLetter(
380+
f.name,
381+
)} } "thirdweb/extensions/${extensionName}";
382+
* const result = encode${uppercaseFirstLetter(f.name)}({\n * ${f.inputs
383+
.map((x) => ` ${removeLeadingUnderscore(x.name)}: ...,`)
384+
.join("\n * ")}\n * });
385+
* \`\`\`
386+
*/
387+
export function encode${uppercaseFirstLetter(
388+
f.name,
389+
)}(options: ${uppercaseFirstLetter(f.name)}Params) {
390+
\/\/ we do a "manual" concat here to avoid the overhead of the "concatHex" function
391+
\/\/ we can do this because we know the specific formats of the values
392+
return FN_SELECTOR + encode${uppercaseFirstLetter(f.name)}Params(options).slice(2) as \`\${typeof FN_SELECTOR}\${string}\`;
393+
}
394+
`
395+
: ""
396+
}
309397
${
310398
f.outputs.length > 0
311399
? `/**

packages/thirdweb/src/abi/encode.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,9 @@ import type {
33
AbiParameter,
44
AbiParametersToPrimitiveTypes,
55
} from "abitype";
6-
import { concatHex, toFunctionSelector } from "viem";
6+
import { toFunctionSelector } from "viem";
77
import { encodeAbiParameters } from "../utils/abi/encodeAbiParameters.js";
8+
import { concatHex } from "../utils/encoding/helpers/concat-hex.js";
89

910
/**
1011
* Encodes an ABI function with its arguments into a hexadecimal string.

packages/thirdweb/src/contract/deployment/deploy-with-abi.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,10 @@ import type {
33
AbiParameter,
44
AbiParametersToPrimitiveTypes,
55
} from "abitype";
6-
import { concatHex } from "viem";
76
import { prepareTransaction } from "../../transaction/prepare-transaction.js";
87
import { encodeAbiParameters } from "../../utils/abi/encodeAbiParameters.js";
98
import { ensureBytecodePrefix } from "../../utils/bytecode/prefix.js";
9+
import { concatHex } from "../../utils/encoding/helpers/concat-hex.js";
1010
import { type Hex, isHex } from "../../utils/encoding/hex.js";
1111
import type { Prettify } from "../../utils/type-utils.js";
1212
import type { ClientAndChain } from "../../utils/types.js";

packages/thirdweb/src/exports/thirdweb.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -206,6 +206,7 @@ export {
206206
type ToHexParameters,
207207
type Uint8ArrayToHexOpts,
208208
} from "../utils/encoding/hex.js";
209+
export { concatHex } from "../utils/encoding/helpers/concat-hex.js";
209210

210211
// bytes
211212
// to

packages/thirdweb/src/exports/utils.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,7 @@ export {
7474
type ToHexParameters,
7575
type Uint8ArrayToHexOpts,
7676
} from "../utils/encoding/hex.js";
77+
export { concatHex } from "../utils/encoding/helpers/concat-hex.js";
7778

7879
// bytes
7980
// to

packages/thirdweb/src/extensions/common/__generated__/IClaimConditionsSinglePhase/write/setClaimConditions.ts

Lines changed: 46 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/thirdweb/src/extensions/common/__generated__/IContractMetadata/read/contractURI.ts

Lines changed: 21 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/thirdweb/src/extensions/common/__generated__/IContractMetadata/read/name.ts

Lines changed: 21 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/thirdweb/src/extensions/common/__generated__/IContractMetadata/read/symbol.ts

Lines changed: 21 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)