1+ import { getContract } from "src/contract/contract.js" ;
12import type { Hex } from "viem" ;
2- import type { ThirdwebClient } from "../client/client.js" ;
33import { NATIVE_TOKEN_ADDRESS , ZERO_ADDRESS } from "../constants/addresses.js" ;
44import { parseEventLogs } from "../event/actions/parse-logs.js" ;
55import { assetCreatedEvent } from "../extensions/assets/__generated__/AssetEntrypointERC20/events/AssetCreated.js" ;
66import { createAssetByImplementationConfig } from "../extensions/assets/__generated__/AssetEntrypointERC20/write/createAssetByImplementationConfig.js" ;
7- import { encodeInitialize } from "../extensions/assets/__generated__/ERC20Asset/write/initialize .js" ;
7+ import { decimals } from "../extensions/erc20/read/decimals .js" ;
88import { eth_blockNumber } from "../rpc/actions/eth_blockNumber.js" ;
99import { getRpcClient } from "../rpc/rpc.js" ;
10- import { upload } from "../storage/upload.js" ;
11- import type { FileOrBufferOrString } from "../storage/upload/types.js" ;
1210import { sendAndConfirmTransaction } from "../transaction/actions/send-and-confirm-transaction.js" ;
13- import { encodeAbiParameters } from "../utils/abi/encodeAbiParameters.js" ;
1411import { keccakId } from "../utils/any-evm/keccak-id.js" ;
1512import { toHex } from "../utils/encoding/hex.js" ;
16- import type { ClientAndChainAndAccount } from "../utils/types.js" ;
1713import {
1814 CreateHook ,
1915 DEFAULT_MAX_SUPPLY_ERC20 ,
20- DEFAULT_POOL_FEE ,
21- DEFAULT_POOL_INITIAL_TICK ,
2216 ImplementationType ,
2317} from "./constants.js" ;
2418import { getOrDeployEntrypointERC20 } from "./get-entrypoint-erc20.js" ;
2519import { getOrDeployERC20AssetImpl } from "./get-erc20-asset-impl.js" ;
26-
27- type TokenParams = {
28- name : string ;
29- description ?: string ;
30- image ?: FileOrBufferOrString ;
31- external_link ?: string ;
32- social_urls ?: Record < string , string > ;
33- symbol ?: string ;
34- contractURI ?: string ;
35- maxSupply ?: bigint ;
36- owner ?: string ;
37- } ;
38-
39- type PoolConfig = {
40- amount : bigint ;
41- currency ?: string ;
42- fee ?: number ;
43- initialTick ?: number ;
44- } ;
45-
46- type CreateTokenOptions = ClientAndChainAndAccount & {
47- salt ?: string ;
48- params : TokenParams ;
49- poolConfig ?: PoolConfig ;
50- } ;
20+ import {
21+ encodeInitParams ,
22+ encodeMarketConfig ,
23+ encodePoolConfig ,
24+ } from "./token-utils.js" ;
25+ import type { CreateTokenOptions } from "./types.js" ;
5126
5227export async function createTokenByImplConfig ( options : CreateTokenOptions ) {
53- const { client, account, params, poolConfig } = options ;
28+ const { client, chain , account, params, launchConfig } = options ;
5429
5530 const creator = params . owner || account . address ;
5631
@@ -75,7 +50,33 @@ export async function createTokenByImplConfig(options: CreateTokenOptions) {
7550 const entrypoint = await getOrDeployEntrypointERC20 ( options ) ;
7651 const tokenImpl = await getOrDeployERC20AssetImpl ( options ) ;
7752
78- const hookData = poolConfig ? encodePoolConfig ( poolConfig ) : "0x" ;
53+ let hookData : Hex = "0x" ;
54+
55+ if ( launchConfig ?. kind === "pool" ) {
56+ hookData = encodePoolConfig ( launchConfig . config ) ;
57+ } else if ( launchConfig ?. kind === "market" ) {
58+ const currencyContract =
59+ launchConfig . config . tokenOut &&
60+ launchConfig . config . tokenOut !== NATIVE_TOKEN_ADDRESS
61+ ? getContract ( {
62+ client,
63+ chain,
64+ address : launchConfig . config . tokenOut ,
65+ } )
66+ : null ;
67+ const currencyDecimals = launchConfig . config . priceDenominator
68+ ? launchConfig . config . priceDenominator
69+ : currencyContract
70+ ? await decimals ( {
71+ contract : currencyContract ,
72+ } )
73+ : 18 ;
74+
75+ hookData = encodeMarketConfig ( {
76+ ...launchConfig . config ,
77+ decimals : currencyDecimals ,
78+ } ) ;
79+ }
7980
8081 const transaction = createAssetByImplementationConfig ( {
8182 contract : entrypoint ,
@@ -84,7 +85,14 @@ export async function createTokenByImplConfig(options: CreateTokenOptions) {
8485 contractId : keccakId ( "ERC20Asset" ) ,
8586 implementation : tokenImpl . address ,
8687 implementationType : ImplementationType . ERC1967 ,
87- createHook : poolConfig ? CreateHook . CREATE_POOL : CreateHook . NONE ,
88+ createHook :
89+ launchConfig ?. kind === "pool"
90+ ? CreateHook . CREATE_POOL
91+ : launchConfig ?. kind === "market"
92+ ? CreateHook . CREATE_MARKET
93+ : launchConfig ?. kind === "distribute"
94+ ? CreateHook . DISTRIBUTE
95+ : CreateHook . NONE ,
8896 createHookData : hookData ,
8997 } ,
9098 params : {
@@ -111,64 +119,3 @@ export async function createTokenByImplConfig(options: CreateTokenOptions) {
111119
112120 return decodedEvent [ 0 ] ?. args . asset ;
113121}
114-
115- async function encodeInitParams ( options : {
116- client : ThirdwebClient ;
117- params : TokenParams ;
118- creator : string ;
119- } ) : Promise < Hex > {
120- const { client, params, creator } = options ;
121-
122- const contractURI =
123- options . params . contractURI ||
124- ( await upload ( {
125- client,
126- files : [
127- {
128- name : params . name ,
129- description : params . description ,
130- symbol : params . symbol ,
131- image : params . image ,
132- external_link : params . external_link ,
133- social_urls : params . social_urls ,
134- } ,
135- ] ,
136- } ) ) ||
137- "" ;
138-
139- return encodeInitialize ( {
140- name : params . name ,
141- symbol : params . symbol || params . name ,
142- contractURI,
143- maxSupply : params . maxSupply || DEFAULT_MAX_SUPPLY_ERC20 ,
144- owner : creator ,
145- } ) ;
146- }
147-
148- function encodePoolConfig ( poolConfig : PoolConfig ) : Hex {
149- const POOL_PARAMS = [
150- {
151- type : "address" ,
152- name : "currency" ,
153- } ,
154- {
155- type : "uint256" ,
156- name : "amount" ,
157- } ,
158- {
159- type : "uint24" ,
160- name : "fee" ,
161- } ,
162- {
163- type : "uint24" ,
164- name : "initialTick" ,
165- } ,
166- ] as const ;
167-
168- return encodeAbiParameters ( POOL_PARAMS , [
169- poolConfig . currency || NATIVE_TOKEN_ADDRESS ,
170- poolConfig . amount ,
171- poolConfig . fee || DEFAULT_POOL_FEE ,
172- poolConfig . initialTick || DEFAULT_POOL_INITIAL_TICK ,
173- ] ) ;
174- }
0 commit comments