Skip to content

Commit 2d69d81

Browse files
committed
reorg, encode market config
1 parent 8e8bcb2 commit 2d69d81

File tree

7 files changed

+255
-200
lines changed

7 files changed

+255
-200
lines changed

packages/thirdweb/src/assets/bootstrap.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -171,7 +171,7 @@ export async function deployFeeManager(options: ClientAndChainAndAccount) {
171171
});
172172
}
173173

174-
async function deployAssetFactory(options: ClientAndChainAndAccount) {
174+
export async function deployAssetFactory(options: ClientAndChainAndAccount) {
175175
// create2 factory
176176
const create2Factory = await getDeployedCreate2Factory(options);
177177
if (!create2Factory) {
Lines changed: 44 additions & 97 deletions
Original file line numberDiff line numberDiff line change
@@ -1,56 +1,31 @@
1+
import { getContract } from "src/contract/contract.js";
12
import type { Hex } from "viem";
2-
import type { ThirdwebClient } from "../client/client.js";
33
import { NATIVE_TOKEN_ADDRESS, ZERO_ADDRESS } from "../constants/addresses.js";
44
import { parseEventLogs } from "../event/actions/parse-logs.js";
55
import { assetCreatedEvent } from "../extensions/assets/__generated__/AssetEntrypointERC20/events/AssetCreated.js";
66
import { 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";
88
import { eth_blockNumber } from "../rpc/actions/eth_blockNumber.js";
99
import { getRpcClient } from "../rpc/rpc.js";
10-
import { upload } from "../storage/upload.js";
11-
import type { FileOrBufferOrString } from "../storage/upload/types.js";
1210
import { sendAndConfirmTransaction } from "../transaction/actions/send-and-confirm-transaction.js";
13-
import { encodeAbiParameters } from "../utils/abi/encodeAbiParameters.js";
1411
import { keccakId } from "../utils/any-evm/keccak-id.js";
1512
import { toHex } from "../utils/encoding/hex.js";
16-
import type { ClientAndChainAndAccount } from "../utils/types.js";
1713
import {
1814
CreateHook,
1915
DEFAULT_MAX_SUPPLY_ERC20,
20-
DEFAULT_POOL_FEE,
21-
DEFAULT_POOL_INITIAL_TICK,
2216
ImplementationType,
2317
} from "./constants.js";
2418
import { getOrDeployEntrypointERC20 } from "./get-entrypoint-erc20.js";
2519
import { 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

5227
export 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-
}

packages/thirdweb/src/assets/create-token.test.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ describe.runIf(process.env.TW_SECRET_KEY)("create token by impl config", () => {
1414
account: TEST_ACCOUNT_A,
1515
params: {
1616
name: "Test",
17+
maxSupply: 10_000_000_000n,
1718
},
1819
salt: "salt123",
1920
});
Lines changed: 37 additions & 99 deletions
Original file line numberDiff line numberDiff line change
@@ -1,53 +1,26 @@
11
import type { Hex } from "viem";
2-
import type { ThirdwebClient } from "../client/client.js";
32
import { NATIVE_TOKEN_ADDRESS, ZERO_ADDRESS } from "../constants/addresses.js";
3+
import { getContract } from "../contract/contract.js";
44
import { parseEventLogs } from "../event/actions/parse-logs.js";
55
import { assetCreatedEvent } from "../extensions/assets/__generated__/AssetEntrypointERC20/events/AssetCreated.js";
66
import { createAsset } from "../extensions/assets/__generated__/AssetEntrypointERC20/write/createAsset.js";
7-
import { encodeInitialize } from "../extensions/assets/__generated__/ERC20Asset/write/initialize.js";
7+
import { decimals } from "../extensions/erc20/read/decimals.js";
88
import { eth_blockNumber } from "../rpc/actions/eth_blockNumber.js";
99
import { getRpcClient } from "../rpc/rpc.js";
10-
import { upload } from "../storage/upload.js";
11-
import type { FileOrBufferOrString } from "../storage/upload/types.js";
1210
import { sendAndConfirmTransaction } from "../transaction/actions/send-and-confirm-transaction.js";
13-
import { encodeAbiParameters } from "../utils/abi/encodeAbiParameters.js";
1411
import { keccakId } from "../utils/any-evm/keccak-id.js";
1512
import { toHex } from "../utils/encoding/hex.js";
16-
import type { ClientAndChainAndAccount } from "../utils/types.js";
17-
import {
18-
DEFAULT_MAX_SUPPLY_ERC20,
19-
DEFAULT_POOL_FEE,
20-
DEFAULT_POOL_INITIAL_TICK,
21-
} from "./constants.js";
13+
import { DEFAULT_MAX_SUPPLY_ERC20 } from "./constants.js";
2214
import { getOrDeployEntrypointERC20 } from "./get-entrypoint-erc20.js";
23-
24-
export type TokenParams = {
25-
name: string;
26-
description?: string;
27-
image?: FileOrBufferOrString;
28-
external_link?: string;
29-
social_urls?: Record<string, string>;
30-
symbol?: string;
31-
contractURI?: string;
32-
maxSupply: bigint;
33-
owner?: string;
34-
};
35-
36-
type PoolConfig = {
37-
amount: bigint;
38-
currency?: string;
39-
fee?: number;
40-
initialTick?: number;
41-
};
42-
43-
export type CreateTokenOptions = ClientAndChainAndAccount & {
44-
salt?: string;
45-
params: TokenParams;
46-
poolConfig?: PoolConfig;
47-
};
15+
import {
16+
encodeInitParams,
17+
encodeMarketConfig,
18+
encodePoolConfig,
19+
} from "./token-utils.js";
20+
import type { CreateTokenOptions } from "./types.js";
4821

4922
export async function createToken(options: CreateTokenOptions) {
50-
const { client, account, params, poolConfig } = options;
23+
const { client, chain, account, params, launchConfig } = options;
5124

5225
const creator = params.owner || account.address;
5326

@@ -71,7 +44,33 @@ export async function createToken(options: CreateTokenOptions) {
7144

7245
const entrypoint = await getOrDeployEntrypointERC20(options);
7346

74-
const hookData = poolConfig ? encodePoolConfig(poolConfig) : "0x";
47+
let hookData: Hex = "0x";
48+
49+
if (launchConfig?.kind === "pool") {
50+
hookData = encodePoolConfig(launchConfig.config);
51+
} else if (launchConfig?.kind === "market") {
52+
const currencyContract =
53+
launchConfig.config.tokenOut &&
54+
launchConfig.config.tokenOut !== NATIVE_TOKEN_ADDRESS
55+
? getContract({
56+
client,
57+
chain,
58+
address: launchConfig.config.tokenOut,
59+
})
60+
: null;
61+
const currencyDecimals = launchConfig.config.priceDenominator
62+
? launchConfig.config.priceDenominator
63+
: currencyContract
64+
? await decimals({
65+
contract: currencyContract,
66+
})
67+
: 18;
68+
69+
hookData = encodeMarketConfig({
70+
...launchConfig.config,
71+
decimals: currencyDecimals,
72+
});
73+
}
7574

7675
const transaction = createAsset({
7776
contract: entrypoint,
@@ -100,64 +99,3 @@ export async function createToken(options: CreateTokenOptions) {
10099

101100
return decodedEvent[0]?.args.asset;
102101
}
103-
104-
async function encodeInitParams(options: {
105-
client: ThirdwebClient;
106-
params: TokenParams;
107-
creator: string;
108-
}): Promise<Hex> {
109-
const { client, params, creator } = options;
110-
111-
const contractURI =
112-
options.params.contractURI ||
113-
(await upload({
114-
client,
115-
files: [
116-
{
117-
name: params.name,
118-
description: params.description,
119-
symbol: params.symbol,
120-
image: params.image,
121-
external_link: params.external_link,
122-
social_urls: params.social_urls,
123-
},
124-
],
125-
})) ||
126-
"";
127-
128-
return encodeInitialize({
129-
name: params.name,
130-
symbol: params.symbol || params.name,
131-
contractURI,
132-
maxSupply: params.maxSupply || DEFAULT_MAX_SUPPLY_ERC20,
133-
owner: creator,
134-
});
135-
}
136-
137-
function encodePoolConfig(poolConfig: PoolConfig): Hex {
138-
const POOL_PARAMS = [
139-
{
140-
type: "address",
141-
name: "currency",
142-
},
143-
{
144-
type: "uint256",
145-
name: "amount",
146-
},
147-
{
148-
type: "uint24",
149-
name: "fee",
150-
},
151-
{
152-
type: "uint24",
153-
name: "initialTick",
154-
},
155-
] as const;
156-
157-
return encodeAbiParameters(POOL_PARAMS, [
158-
poolConfig.currency || NATIVE_TOKEN_ADDRESS,
159-
poolConfig.amount,
160-
poolConfig.fee || DEFAULT_POOL_FEE,
161-
poolConfig.initialTick || DEFAULT_POOL_INITIAL_TICK,
162-
]);
163-
}

packages/thirdweb/src/assets/get-entrypoint-erc20.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import type {
1111
ClientAndChain,
1212
ClientAndChainAndAccount,
1313
} from "../utils/types.js";
14-
import { getDeployedAssetFactory } from "./bootstrap.js";
14+
import { deployAssetFactory, getDeployedAssetFactory } from "./bootstrap.js";
1515
import {
1616
DEFAULT_INFRA_ADMIN,
1717
DEFAULT_SALT,
@@ -33,9 +33,9 @@ export async function getOrDeployEntrypointERC20(
3333
});
3434
}
3535

36-
const assetFactory = await getDeployedAssetFactory(options);
36+
let assetFactory = await getDeployedAssetFactory(options);
3737
if (!assetFactory) {
38-
throw new Error(`Asset factory not found for chain: ${options.chain.id}`);
38+
assetFactory = await deployAssetFactory(options);
3939
}
4040

4141
const entrypointImpl = await getOrDeployInfraContract({

0 commit comments

Comments
 (0)