|
| 1 | +import { bytesToHex, randomBytes } from "@noble/hashes/utils"; |
| 2 | +import type { Hex } from "viem"; |
| 3 | +import { predictAddress as _predictAddress } from "../extensions/tokens/__generated__/ERC20Entrypoint/read/predictAddress.js"; |
| 4 | +import { padHex, toHex } from "../utils/encoding/hex.js"; |
| 5 | +import { DEFAULT_DEVELOPER_ADDRESS } from "./constants.js"; |
| 6 | +import { getDeployedEntrypointERC20 } from "./get-entrypoint-erc20.js"; |
| 7 | +import { |
| 8 | + encodeInitParams, |
| 9 | + encodePoolConfig, |
| 10 | + generateSalt, |
| 11 | +} from "./token-utils.js"; |
| 12 | +import type { CreateTokenOptions } from "./types.js"; |
| 13 | + |
| 14 | +export async function predictAddress(options: CreateTokenOptions) { |
| 15 | + const { client, account, params, launchConfig } = options; |
| 16 | + |
| 17 | + const creator = params.owner || account.address; |
| 18 | + const encodedInitData = await encodeInitParams({ |
| 19 | + client, |
| 20 | + creator, |
| 21 | + params, |
| 22 | + }); |
| 23 | + |
| 24 | + const salt: Hex = generateSalt(options.salt || bytesToHex(randomBytes(31))); |
| 25 | + |
| 26 | + const entrypoint = await getDeployedEntrypointERC20(options); |
| 27 | + |
| 28 | + let hookData: Hex = "0x"; |
| 29 | + let contractId = padHex(toHex("ERC20Asset"), { size: 32 }); |
| 30 | + if (launchConfig?.kind === "pool") { |
| 31 | + hookData = encodePoolConfig(launchConfig.config); |
| 32 | + contractId = padHex(toHex("ERC20Asset_Pool"), { size: 32 }); |
| 33 | + } |
| 34 | + |
| 35 | + const address = await _predictAddress({ |
| 36 | + contract: entrypoint, |
| 37 | + contractId, |
| 38 | + params: { |
| 39 | + data: encodedInitData, |
| 40 | + hookData, |
| 41 | + developer: options.developerAddress || DEFAULT_DEVELOPER_ADDRESS, |
| 42 | + salt, |
| 43 | + }, |
| 44 | + creator, |
| 45 | + }); |
| 46 | + |
| 47 | + return address; |
| 48 | +} |
0 commit comments