Skip to content

Commit f78b0a2

Browse files
authored
Feature: predictAddress token function (#7870)
1 parent e0ab3d5 commit f78b0a2

File tree

3 files changed

+54
-0
lines changed

3 files changed

+54
-0
lines changed

.changeset/hot-showers-throw.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"thirdweb": patch
3+
---
4+
5+
Adds predictAddress token extension function

packages/thirdweb/src/exports/tokens.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ export {
1313
getDeployedEntrypointERC20,
1414
} from "../tokens/get-entrypoint-erc20.js";
1515
export { isPoolRouterEnabled } from "../tokens/is-router-enabled.js";
16+
export { predictAddress } from "../tokens/predict-address.js";
1617
export {
1718
generateSalt,
1819
SaltFlag,
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
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

Comments
 (0)