1- export type CreateTokenArgs = { }
1+ import type { Hex } from "viem" ;
2+ import type { ThirdwebClient } from "../client/client.js" ;
3+ import { ZERO_ADDRESS } from "../constants/addresses.js" ;
4+ import { getContract } from "../contract/contract.js" ;
5+ import { createAsset } from "../extensions/assets/__generated__/AssetEntrypointERC20/write/createAsset.js" ;
6+ import { encodeInitialize } from "../extensions/assets/__generated__/ERC20Asset/write/initialize.js" ;
7+ import { eth_blockNumber } from "../rpc/actions/eth_blockNumber.js" ;
8+ import { getRpcClient } from "../rpc/rpc.js" ;
9+ import { upload } from "../storage/upload.js" ;
10+ import type { FileOrBufferOrString } from "../storage/upload/types.js" ;
11+ import { sendTransaction } from "../transaction/actions/send-transaction.js" ;
12+ import { keccakId } from "../utils/any-evm/keccak-id.js" ;
13+ import { toHex } from "../utils/encoding/hex.js" ;
14+ import type { ClientAndChainAndAccount } from "../utils/types.js" ;
15+ import { DEFAULT_MAX_SUPPLY_ERC20 } from "./constants.js" ;
16+ import { getEntrypointERC20 } from "./get-entrypoint-erc20.js" ;
217
3- export function createToken ( params : CreateTokenArgs ) {
18+ export type TokenParams = {
19+ name : string ;
20+ description ?: string ;
21+ image ?: FileOrBufferOrString ;
22+ external_link ?: string ;
23+ social_urls ?: Record < string , string > ;
24+ symbol ?: string ;
25+ contractURI ?: string ;
26+ maxSupply ?: bigint ;
27+ owner ?: string ;
28+ } ;
429
5- }
30+ export type CreateTokenOptions = ClientAndChainAndAccount & {
31+ salt ?: string ;
32+ params : TokenParams ;
33+ } ;
34+
35+ export async function createToken ( options : CreateTokenOptions ) {
36+ const { chain, client, account, params } = options ;
37+
38+ const creator = params . owner || account . address ;
39+
40+ const encodedInitData = await encodeInitParams ( {
41+ client,
42+ params,
43+ creator,
44+ } ) ;
45+
46+ const rpcRequest = getRpcClient ( {
47+ ...options ,
48+ } ) ;
49+ const blockNumber = await eth_blockNumber ( rpcRequest ) ;
50+ const salt = options . salt
51+ ? options . salt . startsWith ( "0x" ) && options . salt . length === 66
52+ ? ( options . salt as `0x${string } `)
53+ : keccakId ( options . salt )
54+ : toHex ( blockNumber , {
55+ size : 32 ,
56+ } ) ;
57+
58+ const entrypointAddress = await getEntrypointERC20 ( chain ) ;
59+ const entrypoint = getContract ( {
60+ client,
61+ address : entrypointAddress ,
62+ chain,
63+ } ) ;
64+
65+ const transaction = createAsset ( {
66+ contract : entrypoint ,
67+ creator,
68+ createParams : {
69+ amount : params . maxSupply || DEFAULT_MAX_SUPPLY_ERC20 ,
70+ referrer : ZERO_ADDRESS ,
71+ salt,
72+ data : encodedInitData ,
73+ hookData : "0x" ,
74+ } ,
75+ } ) ;
76+
77+ return await sendTransaction ( { account, transaction } ) ;
78+ }
79+
80+ async function encodeInitParams ( options : {
81+ client : ThirdwebClient ;
82+ params : TokenParams ;
83+ creator : string ;
84+ } ) : Promise < Hex > {
85+ const { client, params, creator } = options ;
86+
87+ const contractURI =
88+ options . params . contractURI ||
89+ ( await upload ( {
90+ client,
91+ files : [
92+ {
93+ name : params . name ,
94+ description : params . description ,
95+ symbol : params . symbol ,
96+ image : params . image ,
97+ external_link : params . external_link ,
98+ social_urls : params . social_urls ,
99+ } ,
100+ ] ,
101+ } ) ) ||
102+ "" ;
103+
104+ return encodeInitialize ( {
105+ name : params . name ,
106+ symbol : params . symbol || params . name ,
107+ contractURI,
108+ maxSupply : params . maxSupply || DEFAULT_MAX_SUPPLY_ERC20 ,
109+ owner : creator ,
110+ } ) ;
111+ }
0 commit comments