Skip to content

Commit

Permalink
Custom create2 factory deployment for skale chains (#5693)
Browse files Browse the repository at this point in the history
TOOL-2779 (XPROT-931)

## Problem solved

Short description of the bug fixed or feature added

<!-- start pr-codex -->

---

## PR-Codex overview
This PR introduces a custom `create2` factory deployment for SKALE chains, enhancing gas management and testing functionalities.

### Detailed summary
- Added tests for `create2` factory address computation and deployment in `create-2-factory.test.ts`.
- Introduced `Create2FactoryDeploymentInfo` type in `create-2-factory.ts`.
- Implemented custom gas price and limit handling for specific SKALE chains.
- Updated `deployCreate2Factory` to utilize custom gas settings.

> ✨ Ask PR-Codex anything about this PR by commenting with `/codex {your question}`

<!-- end pr-codex -->
  • Loading branch information
kumaryash90 committed Dec 19, 2024
1 parent 00b6c2e commit 7c40fda
Show file tree
Hide file tree
Showing 3 changed files with 88 additions and 4 deletions.
5 changes: 5 additions & 0 deletions .changeset/funny-bobcats-explode.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"thirdweb": patch
---

Custom create2 factory deployment for skale chains
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import { describe, expect, it } from "vitest";
import { ANVIL_CHAIN } from "../../../../test/src/chains.js";
import { TEST_CLIENT } from "../../../../test/src/test-clients.js";
import { TEST_ACCOUNT_A } from "../../../../test/src/test-wallets.js";
import { defineChain } from "../../../chains/utils.js";
import {
computeCreate2FactoryAddress,
deployCreate2Factory,
getDeployedCreate2Factory,
} from "./create-2-factory.js";

describe.runIf(process.env.TW_SECRET_KEY)("create2 factory tests", () => {
it("should compute create2 factory address", async () => {
const addr = await computeCreate2FactoryAddress({
client: TEST_CLIENT,
chain: defineChain(1),
});

expect(addr).to.eq("0x4e59b44847b379578588920cA78FbF26c0B4956C");
});

it("should compute create2 factory address with custom gas", async () => {
const addr = await computeCreate2FactoryAddress({
client: TEST_CLIENT,
chain: defineChain(1564830818),
});

expect(addr).to.eq("0x50620b64D9524aC7dC8c967123E87e5b6dB98f0c");
});

it("should deploy create2 factory", async () => {
await deployCreate2Factory({
client: TEST_CLIENT,
account: TEST_ACCOUNT_A,
chain: ANVIL_CHAIN,
});

const create2Factory = await getDeployedCreate2Factory({
chain: ANVIL_CHAIN,
client: TEST_CLIENT,
});
expect(create2Factory).not.toBeNull();
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,13 @@ const SIGNATURE = {
s: "0x2222222222222222222222222222222222222222222222222222222222222222",
} as const;

type Create2FactoryDeploymentInfo = {
valueToSend: bigint;
predictedAddress: `0x${string}`;
signerAddress: string;
transaction: `0x${string}`;
};

/**
* Computes the address of the Create2 factory contract and checks if it is deployed.
* @param options - The options for retrieving the Create2 factory address.
Expand Down Expand Up @@ -155,15 +162,26 @@ export async function deployCreate2Factory(options: ClientAndChainAndAccount) {
chain,
});

const gasPriceFetched = await getGasPrice(options);
const bin = _getNearestGasPriceBin(gasPriceFetched);
let gasPrice: bigint | undefined;
let gasLimit: bigint | undefined;

if (CUSTOM_GAS_FOR_CHAIN[chainId]) {
gasPrice = CUSTOM_GAS_FOR_CHAIN[chainId.toString()]?.gasPrice;
gasLimit = CUSTOM_GAS_FOR_CHAIN[chainId.toString()]?.gasLimit;

Check warning on line 170 in packages/thirdweb/src/contract/deployment/utils/create-2-factory.ts

View check run for this annotation

Codecov / codecov/patch

packages/thirdweb/src/contract/deployment/utils/create-2-factory.ts#L169-L170

Added lines #L169 - L170 were not covered by tests
} else {
const gasPriceFetched = await getGasPrice(options);
gasPrice = _getNearestGasPriceBin(gasPriceFetched);
}

const deploymentInfo = await _getCreate2FactoryDeploymentInfo(eipChain, {
gasPrice: bin,
gasPrice,
gasLimit,
});

const balance = await eth_getBalance(rpcRequest, {
address: deploymentInfo.signerAddress,
});

if (balance < deploymentInfo.valueToSend) {
const transaction = prepareTransaction({
chain,
Expand Down Expand Up @@ -193,7 +211,7 @@ export async function deployCreate2Factory(options: ClientAndChainAndAccount) {
async function _getCreate2FactoryDeploymentInfo(
chainId: number,
gasOptions: { gasPrice?: bigint; gasLimit?: bigint },
) {
): Promise<Create2FactoryDeploymentInfo> {
// 100000 is default deployment gas limit and 100 gwei is default gas price for create2 factory deployment
// (See: https://github.com/Arachnid/deterministic-deployment-proxy?tab=readme-ov-file#deployment-gas-limit)
const gasPrice = gasOptions.gasPrice ? gasOptions.gasPrice : 100n * 10n ** 9n;
Expand Down Expand Up @@ -278,6 +296,23 @@ const CUSTOM_GAS_FOR_CHAIN: Record<string, CustomChain> = {
gasPrice: 2500n * 10n ** 9n,
gasLimit: 200000n,
},
// SKALE chains
"1350216234": {
name: "Titan",
gasPrice: 110000n,
},
"1482601649": {
name: "Nebula",
gasPrice: 110000n,
},
"1564830818": {
name: "Calypso",
gasPrice: 110000n,
},
"2046399126": {
name: "Europa",
gasPrice: 110000n,
},
};

const CUSTOM_GAS_BINS = [
Expand Down

0 comments on commit 7c40fda

Please sign in to comment.