Skip to content
Merged
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
5 changes: 5 additions & 0 deletions .changeset/popular-ligers-taste.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"thirdweb": patch
---

bytes32 salt for deterministic deployment
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import {
import { TEST_CLIENT } from "../../../test/src/test-clients.js";
import { TEST_ACCOUNT_A } from "../../../test/src/test-wallets.js";
import { simulateTransaction } from "../../transaction/actions/simulate.js";
import { computePublishedContractAddress } from "../../utils/any-evm/compute-published-contract-address.js";
import { keccakId } from "../../utils/any-evm/keccak-id.js";
import { ENTRYPOINT_ADDRESS_v0_6 } from "../../wallets/smart/lib/constants.js";
import { prepareDeterministicDeployTransaction } from "./deploy-deterministic.js";

Expand Down Expand Up @@ -51,6 +53,16 @@ describe.runIf(process.env.TW_SECRET_KEY)("deployFromMetadata", () => {
salt: "some-salt",
});
const tx2 = prepareDeterministicDeployTransaction({
chain: FORKED_ETHEREUM_CHAIN,
client: TEST_CLIENT,
contractId: "AccountFactory",
constructorParams: {
defaultAdmin: TEST_ACCOUNT_A.address,
entrypoint: ENTRYPOINT_ADDRESS_v0_6,
},
salt: keccakId("some-salt"),
});
const tx3 = prepareDeterministicDeployTransaction({
chain: FORKED_OPTIMISM_CHAIN,
client: TEST_CLIENT,
contractId: "AccountFactory",
Expand All @@ -59,11 +71,42 @@ describe.runIf(process.env.TW_SECRET_KEY)("deployFromMetadata", () => {
entrypoint: ENTRYPOINT_ADDRESS_v0_6,
},
});
const [tx1Result, tx2Result] = await Promise.all([
const [tx1Result, tx2Result, tx3Result] = await Promise.all([
simulateTransaction({ transaction: tx1 }),
simulateTransaction({ transaction: tx2 }),
simulateTransaction({ transaction: tx3 }),
]);
expect(tx1Result === tx2Result).toBe(true);
expect(tx1Result !== tx3Result).toBe(true);
});

it("computed address and deployed address should match", async () => {
const computedPromise = computePublishedContractAddress({
chain: FORKED_ETHEREUM_CHAIN,
client: TEST_CLIENT,
contractId: "AccountFactory",
constructorParams: {
defaultAdmin: TEST_ACCOUNT_A.address,
entrypoint: ENTRYPOINT_ADDRESS_v0_6,
},
salt: keccakId("some-salt"),
});
const tx = prepareDeterministicDeployTransaction({
chain: FORKED_ETHEREUM_CHAIN,
client: TEST_CLIENT,
contractId: "AccountFactory",
constructorParams: {
defaultAdmin: TEST_ACCOUNT_A.address,
entrypoint: ENTRYPOINT_ADDRESS_v0_6,
},
salt: keccakId("some-salt"),
});

const [computed, txResult] = await Promise.all([
computedPromise,
simulateTransaction({ transaction: tx }),
]);
expect(tx1Result !== tx2Result).toBe(true);
expect(computed === txResult).toBe(true);
});
// TODO: Replace these tests' live contracts with mocks
it("should deploy a published contract with no constructor", async () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,11 @@
} from "../../../utils/any-evm/zksync/constants.js";
import { isContractDeployed } from "../../../utils/bytecode/is-contract-deployed.js";
import { ensureBytecodePrefix } from "../../../utils/bytecode/prefix.js";
import { type Hex, uint8ArrayToHex } from "../../../utils/encoding/hex.js";
import {
type Hex,
isHex,
uint8ArrayToHex,
} from "../../../utils/encoding/hex.js";
import type { ClientAndChainAndAccount } from "../../../utils/types.js";
import { getContract } from "../../contract.js";
import { zkDeployContract } from "./zkDeployContract.js";
Expand Down Expand Up @@ -89,7 +93,11 @@
abi: parseAbi(singletonFactoryAbi),
});

const salt = options?.salt ? keccakId(options.salt) : keccakId("thirdweb");
const salt = options?.salt
? isHex(options.salt) && options.salt.length === 66
? options.salt
: keccakId(options.salt)
: keccakId("thirdweb");

Check warning on line 100 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#L96-L100

Added lines #L96 - L100 were not covered by tests

await sendAndConfirmTransaction({
account: options.account,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { type Hex, encodePacked } from "viem";
import { getAddress } from "../address.js";
import { ensureBytecodePrefix } from "../bytecode/prefix.js";
import { isHex } from "../encoding/hex.js";
import { keccak256 } from "../hashing/keccak256.js";
import { getSaltHash } from "./get-salt-hash.js";
import { keccakId } from "./keccak-id.js";
Expand Down Expand Up @@ -33,7 +34,9 @@ export function computeDeploymentAddress(
) {
const bytecode = ensureBytecodePrefix(options.bytecode);
const saltHash = options.salt
? keccakId(options.salt)
? isHex(options.salt) && options.salt.length === 66
? options.salt
: keccakId(options.salt)
: getSaltHash(bytecode);

// 1. create init bytecode hash with contract's bytecode and encoded args
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -96,5 +96,6 @@ export async function computeDeploymentInfoFromBytecode(args: {
initBytecodeWithsalt,
encodedArgs,
create2FactoryAddress,
salt,
};
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { encodePacked } from "viem/utils";
import { ensureBytecodePrefix } from "../bytecode/prefix.js";
import { type Hex, uint8ArrayToHex } from "../encoding/hex.js";
import { type Hex, isHex, uint8ArrayToHex } from "../encoding/hex.js";
import { getSaltHash } from "./get-salt-hash.js";
import { keccakId } from "./keccak-id.js";

Expand Down Expand Up @@ -29,8 +29,11 @@ export function getInitBytecodeWithSalt(
options: GetInitiBytecodeWithSaltOptions,
): Hex {
const bytecode = ensureBytecodePrefix(options.bytecode);

const saltHash = options.salt
? keccakId(options.salt)
? isHex(options.salt) && options.salt.length === 66
? options.salt
: keccakId(options.salt)
: getSaltHash(bytecode);

const encodedArgs =
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import type { Address } from "../../address.js";
import type { Hex } from "../../encoding/hex.js";
import { type Hex, isHex } from "../../encoding/hex.js";
import { keccakId } from "../keccak-id.js";
import { create2Address } from "./create2Address.js";

Expand All @@ -13,7 +13,11 @@
export function computeDeploymentAddress(
options: ComputeDeploymentAddressOptions,
) {
const saltHash = options.salt ? keccakId(options.salt) : keccakId("thirdweb");
const saltHash = options.salt
? isHex(options.salt) && options.salt.length === 66
? options.salt
: keccakId(options.salt)

Check warning on line 19 in packages/thirdweb/src/utils/any-evm/zksync/computeDeploymentAddress.ts

View check run for this annotation

Codecov / codecov/patch

packages/thirdweb/src/utils/any-evm/zksync/computeDeploymentAddress.ts#L17-L19

Added lines #L17 - L19 were not covered by tests
: keccakId("thirdweb");

return create2Address({
sender: options.create2FactoryAddress,
Expand Down
Loading