Skip to content

Commit a041415

Browse files
committed
deploy with stylus constructor
1 parent 11149a3 commit a041415

File tree

2 files changed

+76
-36
lines changed

2 files changed

+76
-36
lines changed

packages/thirdweb/src/contract/deployment/deploy-with-abi.ts

Lines changed: 6 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,8 @@
11
import type { Abi, AbiConstructor } from "abitype";
22
import { parseEventLogs } from "../../event/actions/parse-logs.js";
3-
import { FN_SELECTOR } from "../../extensions/stylus/__generated__/IStylusConstructor/write/stylus_constructor.js";
43
import { contractDeployedEvent } from "../../extensions/stylus/__generated__/IStylusDeployer/events/ContractDeployed.js";
5-
import { deploy } from "../../extensions/stylus/__generated__/IStylusDeployer/write/deploy.js";
64
import { activateStylusContract } from "../../extensions/stylus/write/activateStylusContract.js";
7-
import { eth_blockNumber } from "../../rpc/actions/eth_blockNumber.js";
8-
import { getRpcClient } from "../../rpc/rpc.js";
5+
import { deployWithStylusConstructor } from "../../extensions/stylus/write/deployWithStylusConstructor.js";
96
import { sendAndConfirmTransaction } from "../../transaction/actions/send-and-confirm-transaction.js";
107
import { sendTransaction } from "../../transaction/actions/send-transaction.js";
118
import { prepareTransaction } from "../../transaction/prepare-transaction.js";
@@ -17,7 +14,7 @@ import { isZkSyncChain } from "../../utils/any-evm/zksync/isZkSyncChain.js";
1714
import { isContractDeployed } from "../../utils/bytecode/is-contract-deployed.js";
1815
import { ensureBytecodePrefix } from "../../utils/bytecode/prefix.js";
1916
import { concatHex } from "../../utils/encoding/helpers/concat-hex.js";
20-
import { type Hex, isHex, toHex } from "../../utils/encoding/hex.js";
17+
import { type Hex, isHex } from "../../utils/encoding/hex.js";
2118
import type { Prettify } from "../../utils/type-utils.js";
2219
import type { ClientAndChain } from "../../utils/types.js";
2320
import type { Account } from "../../wallets/interfaces/wallet.js";
@@ -178,39 +175,12 @@ export async function deployContract(
178175
}),
179176
});
180177
} else if (options.isStylus && options.constructorParams) {
181-
const STYLUS_DEPLOYER = "0xCeCbA2F1dC234F70Dd89f2041029807F8D03A990";
182-
const stylusDeployer = getContract({
183-
address: STYLUS_DEPLOYER,
178+
const deployTx = deployWithStylusConstructor({
179+
abi: options.abi,
180+
bytecode: options.bytecode,
184181
chain: options.chain,
185182
client: options.client,
186-
});
187-
188-
const constructorAbi = options.abi.find(
189-
(abi) => abi.type === "constructor",
190-
) as AbiConstructor | undefined;
191-
const constructorCalldata = (FN_SELECTOR +
192-
encodeAbiParameters(
193-
constructorAbi?.inputs || [], // Leave an empty array if there's no constructor
194-
normalizeFunctionParams(
195-
constructorAbi,
196-
options.constructorParams,
197-
).slice(2),
198-
)) as `${typeof FN_SELECTOR}${string}`;
199-
200-
const rpcRequest = getRpcClient({
201-
...options,
202-
});
203-
const blockNumber = await eth_blockNumber(rpcRequest);
204-
const salt = toHex(blockNumber, {
205-
size: 32,
206-
});
207-
208-
const deployTx = deploy({
209-
bytecode: options.bytecode,
210-
contract: stylusDeployer,
211-
initData: constructorCalldata,
212-
initValue: 0n,
213-
salt,
183+
constructorParams: options.constructorParams,
214184
});
215185

216186
const receipt = await sendAndConfirmTransaction({
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
import type { Abi, AbiConstructor } from "abitype";
2+
import type { Chain } from "../../../chains/types.js";
3+
import type { ThirdwebClient } from "../../../client/client.js";
4+
import { getContract } from "../../../contract/contract.js";
5+
import { FN_SELECTOR } from "../../../extensions/stylus/__generated__/IStylusConstructor/write/stylus_constructor.js";
6+
import { encodeAbiParameters } from "../../../utils/abi/encodeAbiParameters.js";
7+
import { normalizeFunctionParams } from "../../../utils/abi/normalizeFunctionParams.js";
8+
import { toHex } from "../../../utils/encoding/hex.js";
9+
import { deploy } from "../__generated__/IStylusDeployer/write/deploy.js";
10+
11+
const STYLUS_DEPLOYER = "0xcEcba2F1DC234f70Dd89F2041029807F8D03A990";
12+
13+
export type DeployWithStylusConstructorOptions = {
14+
chain: Chain;
15+
client: ThirdwebClient;
16+
bytecode: `0x${string}`;
17+
constructorParams: Record<string, unknown>;
18+
abi: Abi;
19+
};
20+
21+
/**
22+
* Deploy stylus contract with constructor params
23+
* @param options - The options deploying contract with constructor
24+
* @returns Prepared transaction to call stylus deployer
25+
* @example
26+
* ```ts
27+
* import { deployWithStylusConstructor } from "thirdweb/stylus";
28+
* const transaction = deployWithStylusConstructor({
29+
* client,
30+
* chain,
31+
* bytecode,
32+
* constructorParams,
33+
* abi
34+
* });
35+
* await sendTransaction({ transaction, account });
36+
* ```
37+
*/
38+
export function deployWithStylusConstructor(
39+
options: DeployWithStylusConstructorOptions,
40+
) {
41+
const { chain, client, constructorParams, abi, bytecode } = options;
42+
const bytecodeHex = bytecode.startsWith("0x")
43+
? bytecode
44+
: (`0x${bytecode}` as `0x${string}`);
45+
46+
const stylusDeployer = getContract({
47+
address: STYLUS_DEPLOYER,
48+
chain,
49+
client,
50+
});
51+
52+
const constructorAbi = abi.find((a) => a.type === "constructor") as
53+
| AbiConstructor
54+
| undefined;
55+
56+
const normalized = normalizeFunctionParams(constructorAbi, constructorParams);
57+
const constructorCalldata = (FN_SELECTOR +
58+
encodeAbiParameters(
59+
constructorAbi?.inputs || [], // Leave an empty array if there's no constructor
60+
normalized,
61+
).slice(2)) as `${typeof FN_SELECTOR}${string}`;
62+
63+
return deploy({
64+
bytecode: bytecodeHex,
65+
contract: stylusDeployer,
66+
initData: constructorCalldata,
67+
initValue: 0n,
68+
salt: toHex(0, { size: 32 }),
69+
});
70+
}

0 commit comments

Comments
 (0)