|
| 1 | +import { createAssetByImplementationConfig } from "src/extensions/assets/__generated__/AssetEntrypointERC20/write/createAssetByImplementationConfig.js"; |
| 2 | +import type { Hex } from "viem"; |
| 3 | +import type { ThirdwebClient } from "../client/client.js"; |
| 4 | +import { NATIVE_TOKEN_ADDRESS, ZERO_ADDRESS } from "../constants/addresses.js"; |
| 5 | +import { encodeInitialize } from "../extensions/assets/__generated__/ERC20Asset/write/initialize.js"; |
| 6 | +import { eth_blockNumber } from "../rpc/actions/eth_blockNumber.js"; |
| 7 | +import { getRpcClient } from "../rpc/rpc.js"; |
| 8 | +import { upload } from "../storage/upload.js"; |
| 9 | +import type { FileOrBufferOrString } from "../storage/upload/types.js"; |
| 10 | +import { sendTransaction } from "../transaction/actions/send-transaction.js"; |
| 11 | +import { encodeAbiParameters } from "../utils/abi/encodeAbiParameters.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 { |
| 16 | + CreateHook, |
| 17 | + DEFAULT_MAX_SUPPLY_ERC20, |
| 18 | + DEFAULT_POOL_FEE, |
| 19 | + DEFAULT_POOL_INITIAL_TICK, |
| 20 | + ImplementationType, |
| 21 | +} from "./constants.js"; |
| 22 | +import { getOrDeployEntrypointERC20 } from "./get-entrypoint-erc20.js"; |
| 23 | +import { getOrDeployERC20AssetImpl } from "./get-erc20-asset-impl.js"; |
| 24 | + |
| 25 | +export type TokenParams = { |
| 26 | + name: string; |
| 27 | + description?: string; |
| 28 | + image?: FileOrBufferOrString; |
| 29 | + external_link?: string; |
| 30 | + social_urls?: Record<string, string>; |
| 31 | + symbol?: string; |
| 32 | + contractURI?: string; |
| 33 | + maxSupply?: bigint; |
| 34 | + owner?: string; |
| 35 | +}; |
| 36 | + |
| 37 | +export type PoolConfig = { |
| 38 | + amount: bigint; |
| 39 | + currency?: string; |
| 40 | + fee?: number; |
| 41 | + initialTick?: number; |
| 42 | +}; |
| 43 | + |
| 44 | +export type CreateTokenOptions = ClientAndChainAndAccount & { |
| 45 | + salt?: string; |
| 46 | + params: TokenParams; |
| 47 | + poolConfig?: PoolConfig; |
| 48 | +}; |
| 49 | + |
| 50 | +export async function createTokenByImplConfig(options: CreateTokenOptions) { |
| 51 | + const { client, account, params, poolConfig } = options; |
| 52 | + |
| 53 | + const creator = params.owner || account.address; |
| 54 | + |
| 55 | + const encodedInitData = await encodeInitParams({ |
| 56 | + client, |
| 57 | + params, |
| 58 | + creator, |
| 59 | + }); |
| 60 | + |
| 61 | + const rpcRequest = getRpcClient({ |
| 62 | + ...options, |
| 63 | + }); |
| 64 | + const blockNumber = await eth_blockNumber(rpcRequest); |
| 65 | + const salt = options.salt |
| 66 | + ? options.salt.startsWith("0x") && options.salt.length === 66 |
| 67 | + ? (options.salt as `0x${string}`) |
| 68 | + : keccakId(options.salt) |
| 69 | + : toHex(blockNumber, { |
| 70 | + size: 32, |
| 71 | + }); |
| 72 | + |
| 73 | + const entrypoint = await getOrDeployEntrypointERC20(options); |
| 74 | + const tokenImpl = await getOrDeployERC20AssetImpl(options); |
| 75 | + |
| 76 | + const hookData = poolConfig ? encodePoolConfig(poolConfig) : "0x"; |
| 77 | + |
| 78 | + const transaction = createAssetByImplementationConfig({ |
| 79 | + contract: entrypoint, |
| 80 | + creator, |
| 81 | + config: { |
| 82 | + contractId: keccakId("ERC20Asset"), |
| 83 | + implementation: tokenImpl.address, |
| 84 | + implementationType: ImplementationType.ERC1967, |
| 85 | + createHook: poolConfig ? CreateHook.CREATE_POOL : CreateHook.NONE, |
| 86 | + createHookData: hookData, |
| 87 | + }, |
| 88 | + params: { |
| 89 | + amount: params.maxSupply || DEFAULT_MAX_SUPPLY_ERC20, |
| 90 | + referrer: ZERO_ADDRESS, |
| 91 | + salt, |
| 92 | + data: encodedInitData, |
| 93 | + hookData, |
| 94 | + }, |
| 95 | + }); |
| 96 | + |
| 97 | + return await sendTransaction({ account, transaction }); |
| 98 | +} |
| 99 | + |
| 100 | +async function encodeInitParams(options: { |
| 101 | + client: ThirdwebClient; |
| 102 | + params: TokenParams; |
| 103 | + creator: string; |
| 104 | +}): Promise<Hex> { |
| 105 | + const { client, params, creator } = options; |
| 106 | + |
| 107 | + const contractURI = |
| 108 | + options.params.contractURI || |
| 109 | + (await upload({ |
| 110 | + client, |
| 111 | + files: [ |
| 112 | + { |
| 113 | + name: params.name, |
| 114 | + description: params.description, |
| 115 | + symbol: params.symbol, |
| 116 | + image: params.image, |
| 117 | + external_link: params.external_link, |
| 118 | + social_urls: params.social_urls, |
| 119 | + }, |
| 120 | + ], |
| 121 | + })) || |
| 122 | + ""; |
| 123 | + |
| 124 | + return encodeInitialize({ |
| 125 | + name: params.name, |
| 126 | + symbol: params.symbol || params.name, |
| 127 | + contractURI, |
| 128 | + maxSupply: params.maxSupply || DEFAULT_MAX_SUPPLY_ERC20, |
| 129 | + owner: creator, |
| 130 | + }); |
| 131 | +} |
| 132 | + |
| 133 | +function encodePoolConfig(poolConfig: PoolConfig): Hex { |
| 134 | + const POOL_PARAMS = [ |
| 135 | + { |
| 136 | + type: "address", |
| 137 | + name: "currency", |
| 138 | + }, |
| 139 | + { |
| 140 | + type: "uint256", |
| 141 | + name: "amount", |
| 142 | + }, |
| 143 | + { |
| 144 | + type: "uint24", |
| 145 | + name: "fee", |
| 146 | + }, |
| 147 | + { |
| 148 | + type: "uint24", |
| 149 | + name: "initialTick", |
| 150 | + }, |
| 151 | + ] as const; |
| 152 | + |
| 153 | + return encodeAbiParameters(POOL_PARAMS, [ |
| 154 | + poolConfig.currency || NATIVE_TOKEN_ADDRESS, |
| 155 | + poolConfig.amount, |
| 156 | + poolConfig.fee || DEFAULT_POOL_FEE, |
| 157 | + poolConfig.initialTick || DEFAULT_POOL_INITIAL_TICK, |
| 158 | + ]); |
| 159 | +} |
0 commit comments