Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,12 @@
import { CONTRACT_DEPLOYER_ADDRESS } from "../../../utils/any-evm/zksync/constants.js";
import type { Hex } from "../../../utils/encoding/hex.js";
import type { ClientAndChainAndAccount } from "../../../utils/types.js";
import { zkDeployContractDeterministic } from "./zkDeployDeterministic.js";
import { prepareZkDeployContractDeterministicTransaction } from "./zkDeployDeterministic.js";

/**
* @internal
*/
export async function zkDeployContract(
export async function prepareZkDeployContractTransaction(

Check warning on line 16 in packages/thirdweb/src/contract/deployment/zksync/zkDeployContract.ts

View check run for this annotation

Codecov / codecov/patch

packages/thirdweb/src/contract/deployment/zksync/zkDeployContract.ts#L16

Added line #L16 was not covered by tests
options: ClientAndChainAndAccount & {
abi: Abi;
bytecode: Hex;
Expand All @@ -22,11 +22,6 @@
deploymentType?: "create" | "create2";
},
) {
if (options.salt !== undefined) {
// if a salt is provided, use the deterministic deployer
return zkDeployContractDeterministic(options);
}

const data = encodeDeployData({
abi: options.abi,
bytecode: options.bytecode,
Expand All @@ -37,18 +32,39 @@
),
});

return prepareTransaction({
chain: options.chain,
client: options.client,
to: CONTRACT_DEPLOYER_ADDRESS,
data,
eip712: {
factoryDeps: [options.bytecode],

Check warning on line 41 in packages/thirdweb/src/contract/deployment/zksync/zkDeployContract.ts

View check run for this annotation

Codecov / codecov/patch

packages/thirdweb/src/contract/deployment/zksync/zkDeployContract.ts#L35-L41

Added lines #L35 - L41 were not covered by tests
// TODO (zksync): allow passing in a paymaster
},
});
}

Check warning on line 45 in packages/thirdweb/src/contract/deployment/zksync/zkDeployContract.ts

View check run for this annotation

Codecov / codecov/patch

packages/thirdweb/src/contract/deployment/zksync/zkDeployContract.ts#L43-L45

Added lines #L43 - L45 were not covered by tests

/**
* @internal
*/
export async function zkDeployContract(
options: ClientAndChainAndAccount & {

Check warning on line 51 in packages/thirdweb/src/contract/deployment/zksync/zkDeployContract.ts

View check run for this annotation

Codecov / codecov/patch

packages/thirdweb/src/contract/deployment/zksync/zkDeployContract.ts#L50-L51

Added lines #L50 - L51 were not covered by tests
abi: Abi;
bytecode: Hex;
params?: Record<string, unknown>;
salt?: string;
deploymentType?: "create" | "create2";
},
) {

Check warning on line 58 in packages/thirdweb/src/contract/deployment/zksync/zkDeployContract.ts

View check run for this annotation

Codecov / codecov/patch

packages/thirdweb/src/contract/deployment/zksync/zkDeployContract.ts#L58

Added line #L58 was not covered by tests
// if a salt is provided, use the deterministic deployer
const transaction =
options.salt !== undefined
? await prepareZkDeployContractDeterministicTransaction(options)
: await prepareZkDeployContractTransaction(options);

Check warning on line 63 in packages/thirdweb/src/contract/deployment/zksync/zkDeployContract.ts

View check run for this annotation

Codecov / codecov/patch

packages/thirdweb/src/contract/deployment/zksync/zkDeployContract.ts#L60-L63

Added lines #L60 - L63 were not covered by tests

const receipt = await sendAndConfirmTransaction({
account: options.account,
transaction: prepareTransaction({
chain: options.chain,
client: options.client,
to: CONTRACT_DEPLOYER_ADDRESS,
data,
eip712: {
factoryDeps: [options.bytecode],
// TODO (zksync): allow passing in a paymaster
},
}),
transaction,

Check warning on line 67 in packages/thirdweb/src/contract/deployment/zksync/zkDeployContract.ts

View check run for this annotation

Codecov / codecov/patch

packages/thirdweb/src/contract/deployment/zksync/zkDeployContract.ts#L67

Added line #L67 was not covered by tests
});

const events = parseEventLogs({
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,30 +12,14 @@
import { toWei } from "../../../utils/units.js";
import { privateKeyToAccount } from "../../../wallets/private-key.js";
import { getWalletBalance } from "../../../wallets/utils/getWalletBalance.js";
import { zkDeployContract } from "./zkDeployContract.js";
import { prepareZkDeployContractTransaction } from "./zkDeployContract.js";

/**
* @internal
*/
export async function zkDeployCreate2Factory(
async function prepareZkDeployCreate2FactoryTransaction(

Check warning on line 20 in packages/thirdweb/src/contract/deployment/zksync/zkDeployCreate2Factory.ts

View check run for this annotation

Codecov / codecov/patch

packages/thirdweb/src/contract/deployment/zksync/zkDeployCreate2Factory.ts#L20

Added line #L20 was not covered by tests
options: ClientAndChainAndAccount,
) {
const isDeployed = await isContractDeployed({
address: ZKSYNC_SINGLETON_FACTORY,
chain: options.chain,
client: options.client,
});

if (isDeployed) {
return ZKSYNC_SINGLETON_FACTORY;
}

if (!PUBLISHED_PRIVATE_KEY) {
throw new Error(
`Unable to deploy create2 factory on chain ${options.chain.id} - please contact us via https://thirdweb.com/support to enable this chain`,
);
}

const create2Signer = privateKeyToAccount({
client: options.client,
privateKey: PUBLISHED_PRIVATE_KEY,
Expand All @@ -49,25 +33,52 @@
});

if (balance.value < valueToSend) {
await sendAndConfirmTransaction({
account: options.account,
transaction: prepareTransaction({
chain: options.chain,
client: options.client,
to: create2Signer.address,
value: valueToSend,
}),
return prepareTransaction({
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this tx should be done before the second one, so ideally we return 2 transactions in an array here? and we execute them one by one

chain: options.chain,
client: options.client,
to: create2Signer.address,
value: valueToSend,

Check warning on line 40 in packages/thirdweb/src/contract/deployment/zksync/zkDeployCreate2Factory.ts

View check run for this annotation

Codecov / codecov/patch

packages/thirdweb/src/contract/deployment/zksync/zkDeployCreate2Factory.ts#L36-L40

Added lines #L36 - L40 were not covered by tests
});
}

await zkDeployContract({
return prepareZkDeployContractTransaction({

Check warning on line 44 in packages/thirdweb/src/contract/deployment/zksync/zkDeployCreate2Factory.ts

View check run for this annotation

Codecov / codecov/patch

packages/thirdweb/src/contract/deployment/zksync/zkDeployCreate2Factory.ts#L44

Added line #L44 was not covered by tests
client: options.client,
chain: options.chain,
account: create2Signer,
abi: parseAbi(singletonFactoryAbi),
bytecode: singletonFactoryBytecode,
deploymentType: "create2",
});
}

Check warning on line 52 in packages/thirdweb/src/contract/deployment/zksync/zkDeployCreate2Factory.ts

View check run for this annotation

Codecov / codecov/patch

packages/thirdweb/src/contract/deployment/zksync/zkDeployCreate2Factory.ts#L52

Added line #L52 was not covered by tests

/**
* @internal
*/
export async function zkDeployCreate2Factory(
options: ClientAndChainAndAccount,
) {
const isDeployed = await isContractDeployed({
address: ZKSYNC_SINGLETON_FACTORY,
chain: options.chain,
client: options.client,
});

Check warning on line 64 in packages/thirdweb/src/contract/deployment/zksync/zkDeployCreate2Factory.ts

View check run for this annotation

Codecov / codecov/patch

packages/thirdweb/src/contract/deployment/zksync/zkDeployCreate2Factory.ts#L57-L64

Added lines #L57 - L64 were not covered by tests

if (isDeployed) {
return ZKSYNC_SINGLETON_FACTORY;
}

Check warning on line 68 in packages/thirdweb/src/contract/deployment/zksync/zkDeployCreate2Factory.ts

View check run for this annotation

Codecov / codecov/patch

packages/thirdweb/src/contract/deployment/zksync/zkDeployCreate2Factory.ts#L66-L68

Added lines #L66 - L68 were not covered by tests

if (!PUBLISHED_PRIVATE_KEY) {
throw new Error(
`Unable to deploy create2 factory on chain ${options.chain.id} - please contact us via https://thirdweb.com/support to enable this chain`,
);
}

Check warning on line 74 in packages/thirdweb/src/contract/deployment/zksync/zkDeployCreate2Factory.ts

View check run for this annotation

Codecov / codecov/patch

packages/thirdweb/src/contract/deployment/zksync/zkDeployCreate2Factory.ts#L70-L74

Added lines #L70 - L74 were not covered by tests

const transaction = await prepareZkDeployCreate2FactoryTransaction(options);

Check warning on line 76 in packages/thirdweb/src/contract/deployment/zksync/zkDeployCreate2Factory.ts

View check run for this annotation

Codecov / codecov/patch

packages/thirdweb/src/contract/deployment/zksync/zkDeployCreate2Factory.ts#L76

Added line #L76 was not covered by tests

await sendAndConfirmTransaction({
account: options.account,
transaction,
});

Check warning on line 81 in packages/thirdweb/src/contract/deployment/zksync/zkDeployCreate2Factory.ts

View check run for this annotation

Codecov / codecov/patch

packages/thirdweb/src/contract/deployment/zksync/zkDeployCreate2Factory.ts#L78-L81

Added lines #L78 - L81 were not covered by tests

return ZKSYNC_SINGLETON_FACTORY;
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,72 @@
import { zkDeployContract } from "./zkDeployContract.js";
import { zkDeployCreate2Factory } from "./zkDeployCreate2Factory.js";

/**
* @internal
*/
export async function prepareZkDeployContractDeterministicTransaction(
options: ClientAndChainAndAccount & {

Check warning on line 26 in packages/thirdweb/src/contract/deployment/zksync/zkDeployDeterministic.ts

View check run for this annotation

Codecov / codecov/patch

packages/thirdweb/src/contract/deployment/zksync/zkDeployDeterministic.ts#L25-L26

Added lines #L25 - L26 were not covered by tests
abi: Abi;
bytecode: Hex;
params?: Record<string, unknown>;
salt?: string;
},
) {
const constructorAbi = options.abi.find(
(x) => "type" in x && x.type === "constructor",
) || { inputs: [] };
const encodedArgs = encodeAbiParameters(
constructorAbi.inputs,
normalizeFunctionParams(constructorAbi as AbiConstructor, options.params),
);
const create2FactoryAddress = await zkDeployCreate2Factory({
client: options.client,
chain: options.chain,
account: options.account,
});
const bytecode = ensureBytecodePrefix(options.bytecode);
const bytecodeHash = uint8ArrayToHex(hashBytecode(bytecode));

Check warning on line 46 in packages/thirdweb/src/contract/deployment/zksync/zkDeployDeterministic.ts

View check run for this annotation

Codecov / codecov/patch

packages/thirdweb/src/contract/deployment/zksync/zkDeployDeterministic.ts#L32-L46

Added lines #L32 - L46 were not covered by tests

// check if bytecodehash is known
const knownCodesStorageContract = getContract({
address: KNOWN_CODES_STORAGE,
chain: options.chain,
client: options.client,
});
const marker = await readContract({
contract: knownCodesStorageContract,
method: "function getMarker(bytes32 _hash) view returns (uint256 marker)",
params: [bytecodeHash],
});

Check warning on line 58 in packages/thirdweb/src/contract/deployment/zksync/zkDeployDeterministic.ts

View check run for this annotation

Codecov / codecov/patch

packages/thirdweb/src/contract/deployment/zksync/zkDeployDeterministic.ts#L49-L58

Added lines #L49 - L58 were not covered by tests
// if not known, publish the bytecodehash
if (marker !== 1n) {
await zkDeployContract({
client: options.client,
chain: options.chain,
account: options.account,
abi: options.abi,
bytecode,
params: options.params,
});
}

Check warning on line 69 in packages/thirdweb/src/contract/deployment/zksync/zkDeployDeterministic.ts

View check run for this annotation

Codecov / codecov/patch

packages/thirdweb/src/contract/deployment/zksync/zkDeployDeterministic.ts#L60-L69

Added lines #L60 - L69 were not covered by tests

// deploy with create2 factory
const factory = getContract({
address: create2FactoryAddress,
chain: options.chain,
client: options.client,
abi: parseAbi(singletonFactoryAbi),
});

Check warning on line 77 in packages/thirdweb/src/contract/deployment/zksync/zkDeployDeterministic.ts

View check run for this annotation

Codecov / codecov/patch

packages/thirdweb/src/contract/deployment/zksync/zkDeployDeterministic.ts#L72-L77

Added lines #L72 - L77 were not covered by tests

const salt = options?.salt ? keccakId(options.salt) : keccakId("thirdweb");

Check warning on line 79 in packages/thirdweb/src/contract/deployment/zksync/zkDeployDeterministic.ts

View check run for this annotation

Codecov / codecov/patch

packages/thirdweb/src/contract/deployment/zksync/zkDeployDeterministic.ts#L79

Added line #L79 was not covered by tests

return prepareContractCall({
contract: factory,
method: "deploy",
params: [salt, bytecodeHash, encodedArgs],
});
}

Check warning on line 86 in packages/thirdweb/src/contract/deployment/zksync/zkDeployDeterministic.ts

View check run for this annotation

Codecov / codecov/patch

packages/thirdweb/src/contract/deployment/zksync/zkDeployDeterministic.ts#L81-L86

Added lines #L81 - L86 were not covered by tests

/**
* @internal
*/
Expand All @@ -30,6 +96,7 @@
salt?: string;
},
) {
// We have to keep the full address prediction logic in this function to preserve the behavior of just returning the address if the contract is already deployed.
const constructorAbi = options.abi.find(
(x) => "type" in x && x.type === "constructor",
) || { inputs: [] };
Expand Down Expand Up @@ -79,27 +146,12 @@
});
}

console.log(
`deploying contract via create2 factory at: ${predictedAddress}`,
);

// deploy with create2 factory
const factory = getContract({
address: create2FactoryAddress,
chain: options.chain,
client: options.client,
abi: parseAbi(singletonFactoryAbi),
});

const salt = options?.salt ? keccakId(options.salt) : keccakId("thirdweb");
const transaction =
await prepareZkDeployContractDeterministicTransaction(options);

Check warning on line 150 in packages/thirdweb/src/contract/deployment/zksync/zkDeployDeterministic.ts

View check run for this annotation

Codecov / codecov/patch

packages/thirdweb/src/contract/deployment/zksync/zkDeployDeterministic.ts#L149-L150

Added lines #L149 - L150 were not covered by tests

await sendAndConfirmTransaction({
account: options.account,
transaction: prepareContractCall({
contract: factory,
method: "deploy",
params: [salt, bytecodeHash, encodedArgs],
}),
transaction,

Check warning on line 154 in packages/thirdweb/src/contract/deployment/zksync/zkDeployDeterministic.ts

View check run for this annotation

Codecov / codecov/patch

packages/thirdweb/src/contract/deployment/zksync/zkDeployDeterministic.ts#L154

Added line #L154 was not covered by tests
});
}

Expand Down