11import { bytesToHex , randomBytes } from "@noble/hashes/utils" ;
22import type { Hex } from "viem" ;
3+ import { parseEventLogs } from "../event/actions/parse-logs.js" ;
4+ import { createdEvent } from "../extensions/tokens/__generated__/ERC20Entrypoint/events/Created.js" ;
35import { createById } from "../extensions/tokens/__generated__/ERC20Entrypoint/write/createById.js" ;
6+ import { sendAndConfirmTransaction } from "../transaction/actions/send-and-confirm-transaction.js" ;
47import { padHex , toHex } from "../utils/encoding/hex.js" ;
58import { DEFAULT_DEVELOPER_ADDRESS } from "./constants.js" ;
69import { getDeployedEntrypointERC20 } from "./get-entrypoint-erc20.js" ;
@@ -12,9 +15,58 @@ import {
1215import type { CreateTokenOptions } from "./types.js" ;
1316
1417export 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,
0 commit comments