📜 [專欄新文章] Reason Why You Should Use EIP1167 Proxy Contract. (With Tutorial)
✍️ Ping Chen
📥 歡迎投稿: https://medium.com/taipei-ethereum-meetup #徵技術分享文 #使用心得 #教學文 #medium
EIP1167 minimal proxy contract is a standardized, gas-efficient way to deploy a bunch of contract clones from a factory.
1. Who may consider using EIP1167
For some DApp that are creating clones of a contract for its users, a “factory pattern” is usually introduced. Users simply interact with the factory to get a copy. For example, Gnosis Multisig Wallet has a factory. So, instead of copy-and-paste the source code to Remix, compile, key in some parameters, and deploy it by yourself, you can just ask the factory to create a wallet for you since the contract code has already been on-chain.
The problem is: we need standalone contract instances for each user, but then we’ll have many copies of the same bytecode on the blockchain, which seems redundant. Take multisig wallet as an example, different multisig wallet instances have separate addresses to receive assets and store the wallet’s owners’ addresses, but they can share the same program logic by referring to the same library. We call them ‘proxy contracts’.
One of the most famous proxy contract users is Uniswap. It also has a factory pattern to create exchanges for each ERC20 tokens. Different from Gnosis Multisig, Uniswap only has one exchange instance that contains full bytecode as the program logic, and the remainders are all proxies. So, when you go to Etherscan to check out the code, you’ll see a short bytecode, which is unlikely an implementation of an exchange.
0x3660006000376110006000366000732157a7894439191e520825fe9399ab8655e0f7085af41558576110006000f3
What it does is blindly relay every incoming transaction to the reference contract 0x2157a7894439191e520825fe9399ab8655e0f708by delegatecall.
Every proxy is a 100% replica of that contract but serving for different tokens.
The length of the creation code of Uniswap exchange implementation is 12468 bytes. A proxy contract, however, has only 46 bytes, which is much more gas efficient. So, if your DApp is in a scenario of creating copies of a contract, no matter for each user, each token, or what else, you may consider using proxy contracts to save gas.
2. Why use EIP1167
According to the proposal, EIP is a “minimal proxy contract”. It is currently the known shortest(in bytecode) and lowest gas consumption overhead implementation of proxy contract. Though most ERCs are protocols or interfaces, EIP1167 is the “best practice” of a proxy contract. It uses some EVM black magic to optimize performance.
EIP1167 not only minimizes length, but it is also literally a “minimal” proxy that does nothing but proxying. It minimizes trust. Unlike other upgradable proxy contracts that rely on the honesty of their administrator (who can change the implementation), address in EIP1167 is hardcoded in bytecode and remain unchangeable.
That brings convenience to the community.
Etherscan automatically displays code for EIP1167 proxies.
When you see an EIP1167 proxy, you can definitely regard it as the contract that it points to. For instance, if Etherscan finds a contract meets the format of EIP1167, and the reference implementation’s code has been published, it will automatically use that code for the proxy contract. Unfortunately, non-standard EIP1167 proxies like Uniswap will not benefit from this kind of network effect.
3. How to upgrade a contract to EIP1167 compatible
*Please read all the steps before use, otherwise there might have problems.
A. Build a clone factory
For Vyper, there’s a function create_with_code_of(address)that creates a proxy and returns its address. For Solidity, you may find a reference implementation here.
function createClone(address target) internal returns (address result){ bytes20 targetBytes = bytes20(target); assembly { let clone := mload(0x40) mstore(clone, 0x3d602d80600a3d3981f3363d3d373d3d3d363d73000000000000000000000000) mstore(add(clone, 0x14), targetBytes) mstore(add(clone, 0x28), 0x5af43d82803e903d91602b57fd5bf30000000000000000000000000000000000) result := create(0, clone, 0x37) }}
You can either deploy the implementation contract first or deploy it with the factory’s constructor. I’ll suggest the former, so you can optimize it with higher runs.
contract WalletFactory is CloneFactory { address Template = "0xc0ffee"; function createWallet() external returns (address newWallet) { newWallet = createClone(Template); }}
B. Replace constructor with initializer
When it comes to a contract, there are two kinds of code: creation code and runtime code. Runtime code is the actual business logic stored in the contract’s code slot. Creation code, on the other hand, is runtime code plus an initialization process. When you compile a solidity source code, the output bytecode you get is creation code. And the permanent bytecode you can find on the blockchain is runtime code.
For EIP1167 proxies, we say it ‘clones’ a contract. It actually clones a contract’s runtime code. But if the contract that it is cloning has a constructor, the clone is not 100% precise. So, we need to slightly modify our implementation contract. Replace the constructor with an ‘initializer’, which is part of the permanent code but can only be called once.
// constructorconstructor(address _owner) external { owner = _owner;}// initializerfunction set(address _owner) external { require(owner == address(0)); owner = _owner;}
Mind that initializer is not a constructor, so theoretically it can be called multiple times. You need to maintain the edge case by yourself. Take the code above as an example, when the contract is initialized, the owner must never be set to 0, or anyone can modify it.
C. Don’t assign value outside a function
As mentioned, a creation code contains runtime code and initialization process. A so-called “initialization process” is not only a constructor but also all the variable assignments outside a function. If an EIP1167 proxy points to a contract that assigns value outside a function, it will again have different behavior. We need to remove them.
There are two approaches to solve this problem. The first one is to turn all the variables that need to be assigned to constant. By doing so, they are no longer a variable written in the contract’s storage, but a constant value that hardcoded everywhere it is used.
bytes32 public constant symbol = "4441490000000000000000000000000000000000000000000000000000000000";uint256 public constant decimals = 18;
Second, if you really want to assign a non-constant variable while initializing, then just add it to the initializer.
mapping(address => bool) public isOwner;uint public dailyWithdrawLimit;uint public signaturesRequired;
function set(address[] _owner, uint limit, uint required) external { require(dailyWithdrawLimit == 0 && signaturesRequired == 0); dailyWithdrawLimit = limit; signaturesRequired = required; //DO SOMETHING ELSE}
Our ultimate goal is to eliminate the difference between runtime code and creation code, so EIP1167 proxy can 100% imitate its implementation.
D. Put them all together
A proxy contract pattern splits the deployment process into two. But the factory can combine two steps into one, so users won’t feel different.
contract multisigWallet { //wallet interfaces function set(address[] owners, uint required, uint limit) external;}contract walletFactory is cloneFactory { address constant template = "0xdeadbeef"; function create(address[] owners, uint required, uint limit) external returns (address) { address wallet = createClone(template); multisigWallet(wallet).set(owners, required, limit); return wallet; }}
Since both the factory and the clone/proxy has exactly the same interface, no modification is required for all the existing DApp, webpage, and tools, just enjoy the benefit of proxy contracts!
4. Drawbacks
Though proxy contract can lower the storage fee of deploying multiple clones, it will slightly increase the gas cost of each operation in the future due to the usage of delegatecall. So, if the contract is not so long(in bytes), and you expect it’ll be called millions of times, it’ll eventually be more efficient to not use EIP1167 proxies.
In addition, proxy pattern also introduces a different attack vector to the system. For EIP1167 proxies, trust is minimized since the address they point to is hardcoded in bytecode. But, if the reference contract is not permanent, some problems may happen.
You might ever hear of parity multisig wallet hack. There are multiple proxies(not EIP1167) that refer to the same implementation. However, the wallet has a self-destruct function, which empties both the storage and the code of a contract. Unfortunately, there was a bug in Parity wallet’s access control and someone accidentally gained the ownership of the original implementation. That did not directly steal assets from other parity wallets, but then the hacker deleted the original implementation, making all the remaining wallets a shell without functionality, and lock assets in it forever.
https://cointelegraph.com/news/parity-multisig-wallet-hacked-or-how-come
Conclusion
In brief, the proxy factory pattern helps you to deploy a bunch of contract clones with a considerably lower gas cost. EIP1167 defines a bytecode format standard for minimal proxy and it is supported by Etherscan.
To upgrade a contract to EIP1167 compatible, you have to remove both constructor and variable assignment outside a function. So that runtime code will contain all business logic that proxies may need.
Here’s a use case of EIP1167 proxy contract: create adapters for ERC1155 tokens to support ERC20 interface.
pelith/erc-1155-adapter
References
https://eips.ethereum.org/EIPS/eip-1167
https://blog.openzeppelin.com/on-the-parity-wallet-multisig-hack-405a8c12e8f7/
Donation:
pingchen.eth
0xc1F9BB72216E5ecDc97e248F65E14df1fE46600a
Reason Why You Should Use EIP1167 Proxy Contract. (With Tutorial) was originally published in Taipei Ethereum Meetup on Medium, where people are continuing the conversation by highlighting and responding to this story.
👏 歡迎轉載分享鼓掌
同時也有3部Youtube影片,追蹤數超過193萬的網紅GU ZAP,也在其Youtube影片中提到,Specification รายละเอียดของไมค์ BOYA BY-M1 หรือ สเปค Transducer : Electret Condenser Polar pattern : Omni-directional Frequency Range : 65Hz ~ 18KHz S...
「adapter pattern」的推薦目錄:
- 關於adapter pattern 在 Taipei Ethereum Meetup Facebook 的最佳貼文
- 關於adapter pattern 在 ADBIG Facebook 的最佳貼文
- 關於adapter pattern 在 ADBIG Facebook 的最佳貼文
- 關於adapter pattern 在 GU ZAP Youtube 的最佳解答
- 關於adapter pattern 在 ADBIG Youtube 的最佳貼文
- 關於adapter pattern 在 OverclockZoneTV Youtube 的最佳解答
- 關於adapter pattern 在 適配器模式| Adapter Pattern - Ian Tsai 的評價
- 關於adapter pattern 在 Design-pattrns/Adapter-Pattern - GitHub 的評價
- 關於adapter pattern 在 Difference between Bridge pattern and Adapter pattern - Stack ... 的評價
- 關於adapter pattern 在 The Adapter Pattern - fjp.github.io 的評價
- 關於adapter pattern 在 What is the difference between Adapter and Delegation ... 的評價
adapter pattern 在 ADBIG Facebook 的最佳貼文
หวด FAR CRY 5 เกมดีๆ ภาพสวย เนื้อเรื่องแน่น กับหูฟัง Beyerdynamic MMX300 Gaming
ดาวน์โหลดเกมได้เลยที่ https://uplay.ubi.com/
หูฟังซื้อได้ที่ https://www.facebook.com/beyerdynamicth/
สเปค Minimum
OS: Windows 10 (64-bit)
CPU: Intel Core i5-2400 @ 3.1 GHz
Video Card: Nvidia GeForce GTX 670 or AMD R9 270
RAM: 8GB
Resolution: 720p
Video Preset: Low
สเปค Recommended (60 FPS)
OS: Windows 10 (64-bit )
CPU: Intel Core i7-4770 @ 3.4 GHz or AMD Ryzen 5 1600 @ 3.2 GHz
Video Card:: Nvidia GeForce GTX 970 or AMD R9 290X
RAM: 8GB
Resolution: 1080p
Video Preset: High
================
Beyerdynamic MMX 300
ชุดหูฟังสำหรับการเล่นเกมระดับพรีเมี่ยม ด้วยเทคโนโลยีชั้นสูงให้เสียงชัดเจนสามมิติและทรงพลังและมอบความสบายสูงสุด ให้คุณได้เปรียบเหนือคู่แข่ง ไมโครโฟนแคปซูลคุณภาพสูงให้คุณสื่อสารได้อย่างราบรื่น ถ้วยครอบหูทำจากไมโครฟิล์มนุ่มพิเศษเพื่อการสวมใส่ได้นานหลายชั่วโมง
รายละเอียด
TRANSMISSION TYPE : Wired
REMOTE : Universal 1-button remote
NOMINAL IMPEDANCE HEADPHONES : 32 ohms
HEADPHONE FREQUENCY RESPONSE : 5 Hz - 35,000 Hz
NOMINAL SOUND PRESSURE LEVEL : 96 dB
POLAR PATTERN FOR MICROPHONE : Cardioid
TRANSDUCER TYPE FOR MICROPHONE : Condenser - Cardioid
CONSTRUCTION : Circumaural (around the ear)
CABLE & PLUG : Stereo jack plug 3.5 mm (1/8") & ¼“ adapter (6.35 mm)
================
สเปค
: Core i3-8100 - 4050
: MSI H310M PRO-VD DDR4 LGA1151 - 2190
: GTX1050 Ti 4GB - 6990
: WD BLUE 1TB - 1290
: ADATA DDR4 8GB 2400 - 3090
: CORSAIR VS550 REV.2 - 1750
: AEROCOOL AERO-300 FAW Black - 1250
เสริม
: CORSAIR K63
: CORSAIR HARPOON
: PREDATOR XB272 240Hz
สเปคสตรีม
: Intel Core i7-8700K @ 4.7GHz
: WATERCOOL HEATKILLER CPU Block
: EK CoolStream 360 Radiator
: MSI Z370 TOMAHAWK
: Corsair Dominator Platinum DDR4 3000 16GB
: ZOTAC GeForce GTX 1080Ti 11GB GDDR5X Artic Strom
: Corsair MP500 240GB NVMe
: Corsair HX850i
: Corsair Graphite 760T ADBIG Edition
: HRC Custom kit
: Galaxy PC Cable Full-Modular
เกมมิ่งเกียร์
: Elgato HD 60 Plus
: Elgato Stream Deck
adapter pattern 在 ADBIG Facebook 的最佳貼文
หวด FAR CRY 5 เกมดีๆ ภาพสวย เนื้อเรื่องแน่น กับหูฟัง Beyerdynamic MMX300 Gaming
ดาวน์โหลดเกมได้เลยที่ https://uplay.ubi.com/
หูฟังซื้อได้ที่ https://www.facebook.com/beyerdynamicth/
สเปค Minimum
OS: Windows 10 (64-bit)
CPU: Intel Core i5-2400 @ 3.1 GHz
Video Card: Nvidia GeForce GTX 670 or AMD R9 270
RAM: 8GB
Resolution: 720p
Video Preset: Low
สเปค Recommended (60 FPS)
OS: Windows 10 (64-bit )
CPU: Intel Core i7-4770 @ 3.4 GHz or AMD Ryzen 5 1600 @ 3.2 GHz
Video Card:: Nvidia GeForce GTX 970 or AMD R9 290X
RAM: 8GB
Resolution: 1080p
Video Preset: High
================
Beyerdynamic MMX 300
ชุดหูฟังสำหรับการเล่นเกมระดับพรีเมี่ยม ด้วยเทคโนโลยีชั้นสูงให้เสียงชัดเจนสามมิติและทรงพลังและมอบความสบายสูงสุด ให้คุณได้เปรียบเหนือคู่แข่ง ไมโครโฟนแคปซูลคุณภาพสูงให้คุณสื่อสารได้อย่างราบรื่น ถ้วยครอบหูทำจากไมโครฟิล์มนุ่มพิเศษเพื่อการสวมใส่ได้นานหลายชั่วโมง
รายละเอียด
TRANSMISSION TYPE : Wired
REMOTE : Universal 1-button remote
NOMINAL IMPEDANCE HEADPHONES : 32 ohms
HEADPHONE FREQUENCY RESPONSE : 5 Hz - 35,000 Hz
NOMINAL SOUND PRESSURE LEVEL : 96 dB
POLAR PATTERN FOR MICROPHONE : Cardioid
TRANSDUCER TYPE FOR MICROPHONE : Condenser - Cardioid
CONSTRUCTION : Circumaural (around the ear)
CABLE & PLUG : Stereo jack plug 3.5 mm (1/8") & ¼“ adapter (6.35 mm)
================
สเปค
: Core i3-8100 - 4050
: MSI H310M PRO-VD DDR4 LGA1151 - 2190
: GTX1050 Ti 4GB - 6990
: WD BLUE 1TB - 1290
: ADATA DDR4 8GB 2400 - 3090
: CORSAIR VS550 REV.2 - 1750
: AEROCOOL AERO-300 FAW Black - 1250
เสริม
: CORSAIR K63
: CORSAIR HARPOON
: PREDATOR XB272 240Hz
สเปคสตรีม
: Intel Core i7-8700K @ 4.7GHz
: WATERCOOL HEATKILLER CPU Block
: EK CoolStream 360 Radiator
: MSI Z370 TOMAHAWK
: Corsair Dominator Platinum DDR4 3000 16GB
: ZOTAC GeForce GTX 1080Ti 11GB GDDR5X Artic Strom
: Corsair MP500 240GB NVMe
: Corsair HX850i
: Corsair Graphite 760T ADBIG Edition
: HRC Custom kit
: Galaxy PC Cable Full-Modular
เกมมิ่งเกียร์
: Elgato HD 60 Plus
: Elgato Stream Deck
adapter pattern 在 GU ZAP Youtube 的最佳解答
Specification รายละเอียดของไมค์ BOYA BY-M1 หรือ สเปค
Transducer : Electret Condenser
Polar pattern : Omni-directional
Frequency Range : 65Hz ~ 18KHz
Signal/Noise : 74dB SPL
Sensitivity : -30dB +/- 3dB / 0dB=1V/Pa, 1kHz
Output Impedance : 1000 Ohm or less
Connector : 3.5mm (1/8”) 4-pole gold plug
Accessories Furnished : Lapel Clip, LR44 battery, foam windscreen, 1/4” adapter
Battery Type : LR44
Dimensions : 18.00mm (H) x 8.30mm (W) x 8.30mm (D) Cable : 6.0m
Weight : Microphone : 2.5g / Power Module : 18g
แฟนเพจ : https://www.facebook.com/GuzapPage/
Website : http://www.guzap.com
ติดต่อ : zapmobilephone@gmail.com
ซื้อสินค้า : http://www.zapsmp.com
--------------------------
IG : tump_witthawat
IG : igguzap
adapter pattern 在 ADBIG Youtube 的最佳貼文
หวด FAR CRY 5 เกมดีๆ ภาพสวย เนื้อเรื่องแน่น กับหูฟัง Beyerdynamic MMX300 Gaming
ดาวน์โหลดเกมได้เลยที่ https://uplay.ubi.com/
หูฟังซื้อได้ที่ https://www.facebook.com/beyerdynamicth/
สเปค Minimum
OS: Windows 10 (64-bit)
CPU: Intel Core i5-2400 @ 3.1 GHz
Video Card: Nvidia GeForce GTX 670 or AMD R9 270
RAM: 8GB
Resolution: 720p
Video Preset: Low
สเปค Recommended (60 FPS)
OS: Windows 10 (64-bit )
CPU: Intel Core i7-4770 @ 3.4 GHz or AMD Ryzen 5 1600 @ 3.2 GHz
Video Card:: Nvidia GeForce GTX 970 or AMD R9 290X
RAM: 8GB
Resolution: 1080p
Video Preset: High
================
Beyerdynamic MMX 300
ชุดหูฟังสำหรับการเล่นเกมระดับพรีเมี่ยม ด้วยเทคโนโลยีชั้นสูงให้เสียงชัดเจนสามมิติและทรงพลังและมอบความสบายสูงสุด ให้คุณได้เปรียบเหนือคู่แข่ง ไมโครโฟนแคปซูลคุณภาพสูงให้คุณสื่อสารได้อย่างราบรื่น ถ้วยครอบหูทำจากไมโครฟิล์มนุ่มพิเศษเพื่อการสวมใส่ได้นานหลายชั่วโมง
รายละเอียด
TRANSMISSION TYPE : Wired
REMOTE : Universal 1-button remote
NOMINAL IMPEDANCE HEADPHONES : 32 ohms
HEADPHONE FREQUENCY RESPONSE : 5 Hz - 35,000 Hz
NOMINAL SOUND PRESSURE LEVEL : 96 dB
POLAR PATTERN FOR MICROPHONE : Cardioid
TRANSDUCER TYPE FOR MICROPHONE : Condenser - Cardioid
CONSTRUCTION : Circumaural (around the ear)
CABLE & PLUG : Stereo jack plug 3.5 mm (1/8") & ¼“ adapter (6.35 mm)
================
สเปค
: Core i3-8100 - 4050
: MSI H310M PRO-VD DDR4 LGA1151 - 2190
: GTX1050 Ti 4GB - 6990
: WD BLUE 1TB - 1290
: ADATA DDR4 8GB 2400 - 3090
: CORSAIR VS550 REV.2 - 1750
: AEROCOOL AERO-300 FAW Black - 1250
เสริม
: CORSAIR K63
: CORSAIR HARPOON
: PREDATOR XB272 240Hz
สเปคสตรีม
: Intel Core i7-8700K @ 4.7GHz
: WATERCOOL HEATKILLER CPU Block
: EK CoolStream 360 Radiator
: MSI Z370 TOMAHAWK
: Corsair Dominator Platinum DDR4 3000 16GB
: ZOTAC GeForce GTX 1080Ti 11GB GDDR5X Artic Strom
: Corsair MP500 240GB NVMe
: Corsair HX850i
: Corsair Graphite 760T ADBIG Edition
: HRC Custom kit
: Galaxy PC Cable Full-Modular
เกมมิ่งเกียร์
: Elgato HD 60 Plus
: Elgato Stream Deck
adapter pattern 在 OverclockZoneTV Youtube 的最佳解答
สรุปข่าวประจำสัปดาห์ 11/08/2013 กับ OverclockZone IT NEWS
- ยอดดาวน์โหลด Windows Phone Store เพิ่มหลักพันล้านภายใน 6 และทะลุกว่า 2 พันล้าน
- คาดว่า Galaxy Note III จะเปิดตัวในวันที่ 4 กันยายนนี้
- Google ยื่นจดสิทธิบัตร Pattern Unlock แบบใหม่ที่รันแอพพลิเคชันได้เลยในตัวแล้ว
- Apple เปิดให้แลก Adapter แท้ เพื่อแก้ปัญหาเครื่องระเบิด
- ชาวเน็ตจีนพบภาพกล่อง iPhone5C! หลุด
- Notebook พลังแสงอาทิตย์ ใช้งานต่อเนื่องได้ 10 ชม.ที่เรียกกันว่า Sol
- Google Maps คือแอพพลิเคชันอันดับหนึ่งบนสมาร์ทโฟนทั่วโลก
เล่าข่าวโดย นาย...อั๊ยซ์ , อุ้ย , น้องออย
http://www.overclockzone.com
FB: https://www.facebook.com/overclockzonefanpage
TWITTER: @OverclockZoneTH
ติดต่อโฆษณา ae@overclockzone.com
adapter pattern 在 Design-pattrns/Adapter-Pattern - GitHub 的推薦與評價
Adapter design pattern is one of the structural design pattern and it's used so that two unrelated interfaces can work together. The object that joins these ... ... <看更多>
adapter pattern 在 Difference between Bridge pattern and Adapter pattern - Stack ... 的推薦與評價
... <看更多>
adapter pattern 在 適配器模式| Adapter Pattern - Ian Tsai 的推薦與評價
Object Adapter & Class Adapter. Adapter Pattern內有分兩種模式,一種是物件結構型模式,另一種是類別結構型模式。類別 ... ... <看更多>