From cb3e4a648ec6cf0e21f9b4c38317e43de91d12c0 Mon Sep 17 00:00:00 2001 From: Ardis Lu Date: Mon, 13 Jul 2026 21:20:03 -0700 Subject: [PATCH] Optimize gas usage Switching from `DUP2` to `PUSH0` saves 1 gas. --- manual.md | 14 +++++++------- minimal-contract-deployment.eas | 4 ++-- 2 files changed, 9 insertions(+), 9 deletions(-) diff --git a/manual.md b/manual.md index 2bb0392..818f8b1 100644 --- a/manual.md +++ b/manual.md @@ -36,10 +36,10 @@ const factory = '0x8137a81A74E2a9d510898B2e3E307e7C33eBad8A'; const salt = '0x0000000000000000000000000000000000000000000000000000000000000123'; // Must be the entire creation bytecode, including constructor arguments -const bytecode = '0x385f818153f3'; // minimal-contract-deployment.eas, compiled +const bytecode = '0x385f815f53f3'; // minimal-contract-deployment.eas, compiled ethers.getCreate2Address(factory, salt, ethers.keccak256(bytecode)); -// 0xD7ada5e4De61a3c14994046d349d90F7ae693433 +// 0xd81613ac8a3EADbf8c88C5cf285E1fDb38541813 ``` And here's how to mine a specific smart contract address by testing different `salt` values: @@ -51,7 +51,7 @@ const ethers = await import('https://cdn.jsdelivr.net/npm/ethers@6.7.1/+esm'); const factory = '0x8137a81A74E2a9d510898B2e3E307e7C33eBad8A'; // Must be the entire creation bytecode for the smart contract you want to deploy, including constructor arguments -const bytecode = '0x385f818153f3'; // minimal-contract-deployment.eas, compiled +const bytecode = '0x385f815f53f3'; // minimal-contract-deployment.eas, compiled const hashedBytecode = ethers.keccak256(bytecode); // Hash it once and save for efficiency // Simple regex to match the desired CREATE2 address @@ -68,7 +68,7 @@ while (true) { } salt++; } -// {factory: '0x8137a81A74E2a9d510898B2e3E307e7C33eBad8A', paddedSalt: '0x000000000000000000000000000000000000000000000000000000000005c342', bytecode: '0x385f818153f3', addr: '0xbeef7906063D950306868AF5af36955015110f84'} +// {factory: '0x8137a81A74E2a9d510898B2e3E307e7C33eBad8A', paddedSalt: '0x0000000000000000000000000000000000000000000000000000000000063f86', bytecode: '0x385f815f53f3', addr: '0xbeefe9163827eaa19a74a33d57168867fd240F9e'} ``` ## Using the mined `salt` @@ -78,8 +78,8 @@ Once a desired `CREATE2` address has been mined, you can deploy it by sending th ```javascript // Same parameters from "Mine the desired CREATE2 address" const factory = '8137a81A74E2a9d510898B2e3E307e7C33eBad8A'; // Drop "0x" prefix -const salt = '000000000000000000000000000000000000000000000000000000000005c342'; // Drop "0x" prefix -const bytecode = '385f818153f3'; // Drop "0x" prefix +const salt = '0000000000000000000000000000000000000000000000000000000000063f86'; // Drop "0x" prefix +const bytecode = '385f815f53f3'; // Drop "0x" prefix await ethereum.request({ method: 'eth_sendTransaction', @@ -89,7 +89,7 @@ await ethereum.request({ data: `0x${salt}${bytecode}` }] }); -// Smart contract is deployed as 0xbeef7906063D950306868AF5af36955015110f84, as predicted +// Smart contract is deployed as 0xbeefe9163827eaa19a74a33d57168867fd240F9e, as predicted ``` Block explorers such as Etherscan may interpret this payload as a call to a Solidity function with the ABI selector `0x00000000`. As of July 2023, Etherscan's database has the `0x00000000` selector mapped to a function called `fulfillBasicOrder_efficient_6GL6yc(tuple parameters)`. diff --git a/minimal-contract-deployment.eas b/minimal-contract-deployment.eas index f61e895..c98bd82 100644 --- a/minimal-contract-deployment.eas +++ b/minimal-contract-deployment.eas @@ -2,12 +2,12 @@ CODESIZE ; [size] PUSH0 ; [offset, size] DUP2 ; [size, offset, size] -DUP2 ; [offset, size, offset, size] +PUSH0 ; [offset, size, offset, size] MSTORE8 ; [offset, size] RETURN ; [] ;;; This smart contract, compiled: -;;; 385f818153f3 +;;; 385f815f53f3 ;;; When sent to EVM, this smart contract will deploy a new smart contract with bytecode: ;;; 060000000000 -- 2.47.3