From b01bdcd5e79ab0209d2fc73a7c8eff5026d0ee69 Mon Sep 17 00:00:00 2001 From: gregfromstl Date: Mon, 18 Aug 2025 17:07:32 -0700 Subject: [PATCH 1/3] fix: updated createToken to just return a prepared tx --- packages/thirdweb/src/tokens/create-token.ts | 22 +++----------------- packages/thirdweb/src/tokens/types.ts | 15 +++++++------ 2 files changed, 12 insertions(+), 25 deletions(-) diff --git a/packages/thirdweb/src/tokens/create-token.ts b/packages/thirdweb/src/tokens/create-token.ts index 581c3b342f0..933523a13b1 100644 --- a/packages/thirdweb/src/tokens/create-token.ts +++ b/packages/thirdweb/src/tokens/create-token.ts @@ -1,9 +1,6 @@ import { bytesToHex, randomBytes } from "@noble/hashes/utils"; import type { Hex } from "viem"; -import { parseEventLogs } from "../event/actions/parse-logs.js"; -import { createdEvent } from "../extensions/tokens/__generated__/ERC20Entrypoint/events/Created.js"; import { createById } from "../extensions/tokens/__generated__/ERC20Entrypoint/write/createById.js"; -import { sendAndConfirmTransaction } from "../transaction/actions/send-and-confirm-transaction.js"; import { padHex, toHex } from "../utils/encoding/hex.js"; import { DEFAULT_DEVELOPER_ADDRESS } from "./constants.js"; import { getDeployedEntrypointERC20 } from "./get-entrypoint-erc20.js"; @@ -15,9 +12,9 @@ import { import type { CreateTokenOptions } from "./types.js"; export async function createToken(options: CreateTokenOptions) { - const { client, account, params, launchConfig } = options; + const { client, params, launchConfig } = options; - const creator = params.owner || account.address; + const creator = params.owner; const encodedInitData = await encodeInitParams({ client, creator, @@ -47,18 +44,5 @@ export async function createToken(options: CreateTokenOptions) { creator, }); - const receipt = await sendAndConfirmTransaction({ account, transaction }); - const assetEvent = createdEvent(); - const decodedEvent = parseEventLogs({ - events: [assetEvent], - logs: receipt.logs, - }); - - if (decodedEvent.length === 0 || !decodedEvent[0]) { - throw new Error( - `No AssetCreated event found in transaction: ${receipt.transactionHash}`, - ); - } - - return decodedEvent[0]?.args.asset; + return transaction; } diff --git a/packages/thirdweb/src/tokens/types.ts b/packages/thirdweb/src/tokens/types.ts index 58b6cdb8b86..553c21fa838 100644 --- a/packages/thirdweb/src/tokens/types.ts +++ b/packages/thirdweb/src/tokens/types.ts @@ -1,6 +1,9 @@ import type { Hex } from "viem"; import type { FileOrBufferOrString } from "../storage/upload/types.js"; -import type { ClientAndChainAndAccount } from "../utils/types.js"; +import type { + ClientAndChainAndAccount, + ClientAndChain, +} from "../utils/types.js"; export type TokenParams = { name: string; @@ -11,7 +14,7 @@ export type TokenParams = { symbol?: string; contractURI?: string; maxSupply: bigint; - owner?: string; + owner: string; }; export type PoolConfig = { @@ -35,7 +38,7 @@ type LaunchConfig = | { kind: "pool"; config: PoolConfig } | { kind: "distribute"; config: DistributeConfig }; -export type CreateTokenOptions = ClientAndChainAndAccount & { +export type CreateTokenOptions = ClientAndChain & { salt?: Hex; params: TokenParams; launchConfig?: LaunchConfig; @@ -44,6 +47,6 @@ export type CreateTokenOptions = ClientAndChainAndAccount & { export type CreateTokenByImplementationConfigOptions = ClientAndChainAndAccount & - CreateTokenOptions & { - implementationAddress: string; - }; + CreateTokenOptions & { + implementationAddress: string; + }; From 3915624476abad6f2cc1d9c4a902e9d7b0302492 Mon Sep 17 00:00:00 2001 From: gregfromstl Date: Mon, 18 Aug 2025 17:12:19 -0700 Subject: [PATCH 2/3] feat: update create token to return a prepared tx --- .changeset/twenty-bees-sink.md | 5 +++++ packages/thirdweb/src/tokens/predict-address.ts | 4 ++-- packages/thirdweb/src/tokens/types.ts | 8 ++++---- 3 files changed, 11 insertions(+), 6 deletions(-) create mode 100644 .changeset/twenty-bees-sink.md diff --git a/.changeset/twenty-bees-sink.md b/.changeset/twenty-bees-sink.md new file mode 100644 index 00000000000..c5a79975384 --- /dev/null +++ b/.changeset/twenty-bees-sink.md @@ -0,0 +1,5 @@ +--- +"thirdweb": patch +--- + +Update createToken to return a prepared transaction diff --git a/packages/thirdweb/src/tokens/predict-address.ts b/packages/thirdweb/src/tokens/predict-address.ts index 21ca022b3ae..123a2f91477 100644 --- a/packages/thirdweb/src/tokens/predict-address.ts +++ b/packages/thirdweb/src/tokens/predict-address.ts @@ -12,9 +12,9 @@ import { import type { CreateTokenOptions } from "./types.js"; export async function predictAddress(options: CreateTokenOptions) { - const { client, account, params, launchConfig } = options; + const { client, params, launchConfig } = options; - const creator = params.owner || account.address; + const creator = params.owner; const encodedInitData = await encodeInitParams({ client, creator, diff --git a/packages/thirdweb/src/tokens/types.ts b/packages/thirdweb/src/tokens/types.ts index 553c21fa838..7f1e9f01fe0 100644 --- a/packages/thirdweb/src/tokens/types.ts +++ b/packages/thirdweb/src/tokens/types.ts @@ -1,8 +1,8 @@ import type { Hex } from "viem"; import type { FileOrBufferOrString } from "../storage/upload/types.js"; import type { - ClientAndChainAndAccount, ClientAndChain, + ClientAndChainAndAccount, } from "../utils/types.js"; export type TokenParams = { @@ -47,6 +47,6 @@ export type CreateTokenOptions = ClientAndChain & { export type CreateTokenByImplementationConfigOptions = ClientAndChainAndAccount & - CreateTokenOptions & { - implementationAddress: string; - }; + CreateTokenOptions & { + implementationAddress: string; + }; From dceb5fa197e60a04eb68d0ed03a7e59f97386fe8 Mon Sep 17 00:00:00 2001 From: gregfromstl Date: Wed, 20 Aug 2025 09:46:52 -0700 Subject: [PATCH 3/3] Created prepareCreateToken --- packages/thirdweb/src/exports/tokens.ts | 2 +- packages/thirdweb/src/tokens/create-token.ts | 56 ++++++++++++++++++- .../thirdweb/src/tokens/predict-address.ts | 4 +- packages/thirdweb/src/tokens/types.ts | 9 +-- 4 files changed, 60 insertions(+), 11 deletions(-) diff --git a/packages/thirdweb/src/exports/tokens.ts b/packages/thirdweb/src/exports/tokens.ts index d24b6dcc940..df7c63aeacf 100644 --- a/packages/thirdweb/src/exports/tokens.ts +++ b/packages/thirdweb/src/exports/tokens.ts @@ -6,7 +6,7 @@ export { DEFAULT_DEVELOPER_ADDRESS, DEFAULT_DEVELOPER_REWARD_BPS, } from "../tokens/constants.js"; -export { createToken } from "../tokens/create-token.js"; +export { createToken, prepareCreateToken } from "../tokens/create-token.js"; export { distributeToken } from "../tokens/distribute-token.js"; export { getDeployedContractFactory, diff --git a/packages/thirdweb/src/tokens/create-token.ts b/packages/thirdweb/src/tokens/create-token.ts index 933523a13b1..14b00b209cf 100644 --- a/packages/thirdweb/src/tokens/create-token.ts +++ b/packages/thirdweb/src/tokens/create-token.ts @@ -1,6 +1,9 @@ import { bytesToHex, randomBytes } from "@noble/hashes/utils"; import type { Hex } from "viem"; +import { parseEventLogs } from "../event/actions/parse-logs.js"; +import { createdEvent } from "../extensions/tokens/__generated__/ERC20Entrypoint/events/Created.js"; import { createById } from "../extensions/tokens/__generated__/ERC20Entrypoint/write/createById.js"; +import { sendAndConfirmTransaction } from "../transaction/actions/send-and-confirm-transaction.js"; import { padHex, toHex } from "../utils/encoding/hex.js"; import { DEFAULT_DEVELOPER_ADDRESS } from "./constants.js"; import { getDeployedEntrypointERC20 } from "./get-entrypoint-erc20.js"; @@ -12,9 +15,58 @@ import { import type { CreateTokenOptions } from "./types.js"; export async function createToken(options: CreateTokenOptions) { - const { client, params, launchConfig } = options; + const { client, account, params, launchConfig } = options; - const creator = params.owner; + const creator = params.owner || account.address; + const encodedInitData = await encodeInitParams({ + client, + creator, + params, + }); + + const salt: Hex = generateSalt(options.salt || bytesToHex(randomBytes(31))); + + const entrypoint = await getDeployedEntrypointERC20(options); + + let hookData: Hex = "0x"; + let contractId = padHex(toHex("ERC20Asset"), { size: 32 }); + if (launchConfig?.kind === "pool") { + hookData = encodePoolConfig(launchConfig.config); + contractId = padHex(toHex("ERC20Asset_Pool"), { size: 32 }); + } + + const transaction = createById({ + contract: entrypoint, + contractId, + params: { + data: encodedInitData, + hookData, + developer: options.developerAddress || DEFAULT_DEVELOPER_ADDRESS, + salt, + }, + creator, + }); + + const receipt = await sendAndConfirmTransaction({ account, transaction }); + const assetEvent = createdEvent(); + const decodedEvent = parseEventLogs({ + events: [assetEvent], + logs: receipt.logs, + }); + + if (decodedEvent.length === 0 || !decodedEvent[0]) { + throw new Error( + `No AssetCreated event found in transaction: ${receipt.transactionHash}`, + ); + } + + return decodedEvent[0]?.args.asset; +} + +export async function prepareCreateToken(options: CreateTokenOptions) { + const { client, params, account, launchConfig } = options; + + const creator = params.owner || account.address; const encodedInitData = await encodeInitParams({ client, creator, diff --git a/packages/thirdweb/src/tokens/predict-address.ts b/packages/thirdweb/src/tokens/predict-address.ts index 123a2f91477..71f55b1713e 100644 --- a/packages/thirdweb/src/tokens/predict-address.ts +++ b/packages/thirdweb/src/tokens/predict-address.ts @@ -12,9 +12,9 @@ import { import type { CreateTokenOptions } from "./types.js"; export async function predictAddress(options: CreateTokenOptions) { - const { client, params, launchConfig } = options; + const { client, params, launchConfig, account } = options; - const creator = params.owner; + const creator = params.owner || account.address; const encodedInitData = await encodeInitParams({ client, creator, diff --git a/packages/thirdweb/src/tokens/types.ts b/packages/thirdweb/src/tokens/types.ts index 7f1e9f01fe0..58b6cdb8b86 100644 --- a/packages/thirdweb/src/tokens/types.ts +++ b/packages/thirdweb/src/tokens/types.ts @@ -1,9 +1,6 @@ import type { Hex } from "viem"; import type { FileOrBufferOrString } from "../storage/upload/types.js"; -import type { - ClientAndChain, - ClientAndChainAndAccount, -} from "../utils/types.js"; +import type { ClientAndChainAndAccount } from "../utils/types.js"; export type TokenParams = { name: string; @@ -14,7 +11,7 @@ export type TokenParams = { symbol?: string; contractURI?: string; maxSupply: bigint; - owner: string; + owner?: string; }; export type PoolConfig = { @@ -38,7 +35,7 @@ type LaunchConfig = | { kind: "pool"; config: PoolConfig } | { kind: "distribute"; config: DistributeConfig }; -export type CreateTokenOptions = ClientAndChain & { +export type CreateTokenOptions = ClientAndChainAndAccount & { salt?: Hex; params: TokenParams; launchConfig?: LaunchConfig;