Skip to content

Commit dceb5fa

Browse files
committed
Created prepareCreateToken
1 parent 3915624 commit dceb5fa

File tree

4 files changed

+60
-11
lines changed

4 files changed

+60
-11
lines changed

packages/thirdweb/src/exports/tokens.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ export {
66
DEFAULT_DEVELOPER_ADDRESS,
77
DEFAULT_DEVELOPER_REWARD_BPS,
88
} from "../tokens/constants.js";
9-
export { createToken } from "../tokens/create-token.js";
9+
export { createToken, prepareCreateToken } from "../tokens/create-token.js";
1010
export { distributeToken } from "../tokens/distribute-token.js";
1111
export {
1212
getDeployedContractFactory,

packages/thirdweb/src/tokens/create-token.ts

Lines changed: 54 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
import { bytesToHex, randomBytes } from "@noble/hashes/utils";
22
import type { Hex } from "viem";
3+
import { parseEventLogs } from "../event/actions/parse-logs.js";
4+
import { createdEvent } from "../extensions/tokens/__generated__/ERC20Entrypoint/events/Created.js";
35
import { createById } from "../extensions/tokens/__generated__/ERC20Entrypoint/write/createById.js";
6+
import { sendAndConfirmTransaction } from "../transaction/actions/send-and-confirm-transaction.js";
47
import { padHex, toHex } from "../utils/encoding/hex.js";
58
import { DEFAULT_DEVELOPER_ADDRESS } from "./constants.js";
69
import { getDeployedEntrypointERC20 } from "./get-entrypoint-erc20.js";
@@ -12,9 +15,58 @@ import {
1215
import type { CreateTokenOptions } from "./types.js";
1316

1417
export async function createToken(options: CreateTokenOptions) {
15-
const { client, params, launchConfig } = options;
18+
const { client, account, params, launchConfig } = options;
1619

17-
const creator = params.owner;
20+
const creator = params.owner || account.address;
21+
const encodedInitData = await encodeInitParams({
22+
client,
23+
creator,
24+
params,
25+
});
26+
27+
const salt: Hex = generateSalt(options.salt || bytesToHex(randomBytes(31)));
28+
29+
const entrypoint = await getDeployedEntrypointERC20(options);
30+
31+
let hookData: Hex = "0x";
32+
let contractId = padHex(toHex("ERC20Asset"), { size: 32 });
33+
if (launchConfig?.kind === "pool") {
34+
hookData = encodePoolConfig(launchConfig.config);
35+
contractId = padHex(toHex("ERC20Asset_Pool"), { size: 32 });
36+
}
37+
38+
const transaction = createById({
39+
contract: entrypoint,
40+
contractId,
41+
params: {
42+
data: encodedInitData,
43+
hookData,
44+
developer: options.developerAddress || DEFAULT_DEVELOPER_ADDRESS,
45+
salt,
46+
},
47+
creator,
48+
});
49+
50+
const receipt = await sendAndConfirmTransaction({ account, transaction });
51+
const assetEvent = createdEvent();
52+
const decodedEvent = parseEventLogs({
53+
events: [assetEvent],
54+
logs: receipt.logs,
55+
});
56+
57+
if (decodedEvent.length === 0 || !decodedEvent[0]) {
58+
throw new Error(
59+
`No AssetCreated event found in transaction: ${receipt.transactionHash}`,
60+
);
61+
}
62+
63+
return decodedEvent[0]?.args.asset;
64+
}
65+
66+
export async function prepareCreateToken(options: CreateTokenOptions) {
67+
const { client, params, account, launchConfig } = options;
68+
69+
const creator = params.owner || account.address;
1870
const encodedInitData = await encodeInitParams({
1971
client,
2072
creator,

packages/thirdweb/src/tokens/predict-address.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,9 @@ import {
1212
import type { CreateTokenOptions } from "./types.js";
1313

1414
export async function predictAddress(options: CreateTokenOptions) {
15-
const { client, params, launchConfig } = options;
15+
const { client, params, launchConfig, account } = options;
1616

17-
const creator = params.owner;
17+
const creator = params.owner || account.address;
1818
const encodedInitData = await encodeInitParams({
1919
client,
2020
creator,

packages/thirdweb/src/tokens/types.ts

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,6 @@
11
import type { Hex } from "viem";
22
import type { FileOrBufferOrString } from "../storage/upload/types.js";
3-
import type {
4-
ClientAndChain,
5-
ClientAndChainAndAccount,
6-
} from "../utils/types.js";
3+
import type { ClientAndChainAndAccount } from "../utils/types.js";
74

85
export type TokenParams = {
96
name: string;
@@ -14,7 +11,7 @@ export type TokenParams = {
1411
symbol?: string;
1512
contractURI?: string;
1613
maxSupply: bigint;
17-
owner: string;
14+
owner?: string;
1815
};
1916

2017
export type PoolConfig = {
@@ -38,7 +35,7 @@ type LaunchConfig =
3835
| { kind: "pool"; config: PoolConfig }
3936
| { kind: "distribute"; config: DistributeConfig };
4037

41-
export type CreateTokenOptions = ClientAndChain & {
38+
export type CreateTokenOptions = ClientAndChainAndAccount & {
4239
salt?: Hex;
4340
params: TokenParams;
4441
launchConfig?: LaunchConfig;

0 commit comments

Comments
 (0)