📜 [專欄新文章] 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.
👏 歡迎轉載分享鼓掌
同時也有9部Youtube影片,追蹤數超過92萬的網紅ochikeron,也在其Youtube影片中提到,These tasty, crispy, and buttery cookies are made with only 3 Ingredients! Kids love them! What I used is Hot Cake Mix. It is a very popular and vers...
「pattern library」的推薦目錄:
- 關於pattern library 在 Taipei Ethereum Meetup Facebook 的精選貼文
- 關於pattern library 在 A Happy Mum Facebook 的最佳貼文
- 關於pattern library 在 紀老師程式教學網 Facebook 的最佳解答
- 關於pattern library 在 ochikeron Youtube 的最佳貼文
- 關於pattern library 在 ochikeron Youtube 的最讚貼文
- 關於pattern library 在 ochikeron Youtube 的精選貼文
- 關於pattern library 在 UI: Pattern Library - Pinterest 的評價
- 關於pattern library 在 edx/ux-pattern-library: The (working) Visual, UI, and ... - GitHub 的評價
- 關於pattern library 在 About - USPTO UI Design Library 的評價
- 關於pattern library 在 Design Systems, Pattern Libraries & Style Guides... Oh My! 的評價
pattern library 在 A Happy Mum Facebook 的最佳貼文
*This giveaway has ended. Thanks to everyone who joined and congrats to the winners!*
{Giveaway} Even if we don't get to travel often, we never stop learning about the world.
Thanks to Lonely Planet Kids, we are loving these latest additions to our library which expose the kids to new langagues and make them excited to learn about the culture, history and geography of cities around the globe.
Inspired by the popular First Words book series, First Words Flashcards are the perfect tools for kids to get started on foreign languages. Each pack contains 50 colourful, glossy cards featuring everyday words with an illustration on one side and text on the reverse. My girls are thoroughly enjoying learning new words daily and love to test themselves and each other too. It's fun and challenging, even for adults. See my stories for more.
Which airport sells the most chocolate? Where is the tallest builiding on the planet located? Which city is called 'Pearl of the Orient'? The Cities Activity Book lets kids learn about fun facts and famous monuments through a series of drawing, puzzle and sticker activites.
Let the little ones be inspired by signature styles and crafts from global cultures. Around the World Craft & Design Book provides step-by-step instructions on how to make an African Djembe drum, Native Amerian dreamcatcher, Wayang Puppet and kids can also design their own Matryoshka nesting dolls, Totem pole, Rosemaling pattern and more. We can't wait to sit down and start crafting away!
Thank you, Lonely Planet Kids! For more information on their full range of books, visit lonelyplanetkids.com.
**GIVEAWAY!**
I am giving away TWO sets of
- 1x The Cities Activity Book
- 1 x Around the World Craft and Design Book
- 1x Flashcard set of your choice (Spanish/Italian/French/Japanese/Mandarin/English)
To join the giveaway,
1) Like A Happy Mum and Lonely Planet Kids
2) Like and share this post
3) Comment and tell me your favourite city
4) Tag 5 of your friends in your comments
This giveaway ends on 31 July 2359hrs and is not endorsed by or affiliated with Instagram. I have 1 more set to give away on IG so hop over for another chance to win. Good luck!
pattern library 在 紀老師程式教學網 Facebook 的最佳解答
[外掛推薦] 34 個最棒的 Python 開源函式庫推薦(2019 年版)
英文說明文章: https://goo.gl/hHMxCv
簡中說明文章: https://goo.gl/87juhA
--------(本文開始)--------
寫程式最需要的,就是好用的外掛。那種不用寫一個字,就能擁有強大、穩定功能的方便感,就如同一個不會做菜的人可以不用研究食譜,直接走到樓下便利商店填飽肚子的重要性一樣。今天要推薦的,是 34 款由網友評選出來,2018 年最重要的 Python 開源函式庫。
為了各位能快速掌握這 34 款函式庫是什麼,我幫各位整理在下面。詳情可以點進上方英文或簡中說明文章觀看:
Part 1:Python 小工具(Python Toolkits)
01. pipenv:為人類提供 Python 開發流程的函式庫
02. pyxel:簡單就能做出復古小遊戲的 Python 函式庫
03. PyTest:Python 測試用函式庫
04. poetry:Python 套件相依關係管理函式庫。當你裝某個函式庫時,會自動幫你裝相依的函式庫,不會裝完後還缺東缺西。
05. Loguru:Python 日誌函式庫。
06. Faust:Python 串流應用程式(Streaming Applications)構建函式庫
07. Pampy:「模式匹配」(Pattern Match)用函式庫
08. Pyre-check:高效率的拼寫檢查(Type-Checking)函式庫
09. Delorean:讓你撰寫時區(Time-zone)相關程式更簡單的 Python 函式庫
10. Cirq:一套讓你撰寫「量子電腦」專用程式的 Python 函式庫
11. python-nubia:一套讓你輕鬆寫出命令列網路程式的函式庫
Part 2:網頁函式庫
12. Requests-HTML:讓你輕鬆解析網路爬蟲爬回來的 HTML 語法之 Python 函式庫
13. Bokeh:以視覺化呈現資料(餅圖、長條圖...)的 Python 函式庫
14. Vibora:一個「主從式(Client-Server)」 Python 網頁框架
15. PyWebView:讓你可以在自己的程式內,輕鬆顯示遠方網頁內容的函式庫(Web View Library)
16. WhatWaf:一個能偵測網頁應用程式防火牆、並想辦法繞過去的函式庫
17. Molten:一個小型的網站後台框架(如果你不想用 Django 或 Flask 這麼肥的框架,可以用這個來取代)
Part 3:命令列工具
18. TermToSVG:可以把命令列發生的過程,記錄成動畫,以 SVG 格式儲存(寫網誌示範執行流程時很好用)
19. Asciinema:與前一款 TermToSVG 一樣,也是可以記錄命令列執行過程的函式庫。
20. Termgraph:可以在命令列視窗繪製長條圖等簡單圖形的函式庫
Part 4:原始碼編輯
21. Black:可以幫你把 Python 原始碼排列得漂漂亮亮的工具
22. Algojammer:可以讓你把一個 Python 寫的演算法,運作過程視覺化出來
23. Bowler:一個 Python 重構(Re-factoring)工具
Part 5:除錯用工具
24. Py-spy:一個測速工具。可以讓你找出執行緩慢的瓶頸是發生在哪一段程式碼
25. Birdseye:一個 Python 除錯工具
26. Icecream:可以利用螢幕輸出(print 指令)來除錯的工具
Part 6:編譯器/轉譯器
27. Transcrypt:可以把 Python 寫成的程式碼,轉成 JavaScript 的工具
28. Pyodide:可以把 Python 轉譯成 WebAssembly 語言
Part 7:資料處理用函式庫
29. Voluptuous:檢查用 JSON、YAML 等格式表示的資料,是否符合該格式語法的檢查函式庫
30. Botflow:可以用「管道串接(Pipeline)」來執行資料處理(網路爬蟲...等)的函式庫
31. Fast-Pandas:用來測試 Pandas(一個資料處理用函式庫)執行瓶頸的函式庫
Part 8:繪圖用工具
32. A Tour in the Wonderland of Math with Python:一個用來繪製各種美觀數學圖形的函式庫
33. Chartify:用來繪製各種統計圖表的函式庫
34. Hypertools:用來取得高維度資料的視覺化圖形之函式庫
希望大家喜歡今天的分享!請大家多多按讚鼓勵、或追蹤我的 FB、YouTube、以及 Instagram 喔~
---------------
Facebook 粉絲頁(歡迎追蹤): https://goo.gl/N1z9JB
YouTube 頻道(歡迎訂閱): https://goo.gl/pQsdCt
Instagram 日常生活: https://goo.gl/nBHzXC
pattern library 在 ochikeron Youtube 的最佳貼文
These tasty, crispy, and buttery cookies are made with only 3 Ingredients! Kids love them!
What I used is Hot Cake Mix. It is a very popular and versatile Japanese pancake mix. You can make cakes, cookies, muffins, or any kind of sweets.
Hot Cake Mix comes in many brands. In this video I used Morinaga, which is the most recognized one.
https://amzn.to/3BstXQf
---------------------------------
3-Ingredient Icebox Cookies
Difficulty: Easy
Time: 1hr
Number of servings: 15 cookies (1 pattern)
Ingredients:
50g (1.8oz.) butter *room temperature
100g (3.5oz.) Hot Cake Mix
1 tsp. cocoa powder
Directions:
1. Plain Dough: Cream half of the butter (25g) in a bowl with a spatula. Add half of the Hot Cake Mix (50g) and mix well with a spatula. When the dough starts to crumble, use your hands to mix tougher until formed.
2. Chocolate Dough: Cream half of the butter (25g) in a bowl with a spatula. Add half of the Hot Cake Mix (50g) and cocoa powder, then mix well with a spatula. When the dough starts to crumble, use your hands to mix tougher until formed.
3. Wrap in plastic wrap and cool in the fridge for about 30 minutes to set.
4. Please see the video for how to make Checkerboard, Pinwheel, and Marble shapes. Wrap in plastic wrap again and cool and harden in the fridge or freezer for about 15 minutes. *you can keep the dough in the freezer up to a week.
5. Preheat the oven to 170C (340F). Slice the dough into 7mm (0.3inch) thick. Place them on a baking sheet lined with parchment paper.
6. Bake at 170C (340F) for about 15 minutes. You can cool the cookies on the baking sheet!
レシピ(日本語)
https://cooklabo.blogspot.com/2021/07/Icebox-Cookies.html
---------------------------------
Music by
YouTube Audio Library
Follow me on social media. If you have recreated any of my food, you can share some pictures #ochikeron. I am always happy to see them.
♥FOLLOW ME HERE♥
http://instagram.com/ochikeron/
https://www.facebook.com/ochikeron
https://plus.google.com/+ochikeron
http://twitter.com/ochikeron
♥My COOKBOOK available on Amazon Kindle♥
http://amzn.to/2EwR3ah
NO MORE hard copies... those who got one are lucky!
♥More Written Recipes are on my BLOG♥
http://createeathappy.blogspot.com/
♥My Recipe Posts in Japanese♥
http://cooklabo.blogspot.jp/
http://cookpad.com/ami
http://twitter.com/alohaforever
♥and of course PLEASE SUBSCRIBE♥
http://www.youtube.com/user/ochikeron?sub_confirmation=1
pattern library 在 ochikeron Youtube 的最讚貼文
No-fuss family dish kids and adults will love 😋
You can arrange this dish with regular taro potatoes, or regular potatoes or a radish kind.
Perfect for everyday meal and Bento box!
Because its shape and pattern are like a shrimp, we call this taro potato Ebi-Imo (shrimp potato). It is in season from November through January. My neighbor gave me some beautiful Ebi-Imo, so I decided to make this dish for my family. I hope this idea helps 👍
---------------------------------
Chicken Mince Simmered Ebi-Imo (Shrimp-Shaped Taro Potatoes) Recipe
Difficulty: Easy
Time: 30min
Number of servings: 4
Ingredients
600g (1.3lb.) Ebi-Imo (shrimp potato)
300g (0.7lb.) minced chicken
3 tbsp. green peas (cooked/frozen/canned)
salt
400ml Dashi broth
A
* 4 tbsp. soy sauce
* 2 tbsp. Sake
* 2 tbsp. sugar
* 1 tbsp. ginger juice
1 tbsp. cooking oil
B
* 1 tbsp. corn/potato starch
* 1 tbsp. water
Directions:
1. Peel and cut Ebi-Imo in bite-size chunks. Parboil in salted water to remove bitter taste then rinse to remove slimy texture. *When you use regular potato, this procedure is not necessary but it improves flavor.
2. Heat cooking oil in a pot, and cook minced chicken until no longer pink.
3. Add Ebi-Imo and Dashi stock, and bring to a boil. When it comes to a boil, remove the foam.
4. Add the seasonings (A), and cook until the potatoes are soft for about 10~15 minutes.
5. Stop the heat, add starch diluted with water, then heat again until thick.
6. Lastly, add green peas, and cook until hot.
レシピ(日本語)
https://cooklabo.blogspot.com/2021/02/Chicken-Mince-Simmered-Potatoes.html
---------------------------------
Music by
YouTube Audio Library
Follow me on social media. If you have recreated any of my food, you can share some pictures #ochikeron. I am always happy to see them.
♥FOLLOW ME HERE♥
http://instagram.com/ochikeron/
https://www.facebook.com/ochikeron
https://plus.google.com/+ochikeron
http://twitter.com/ochikeron
♥My COOKBOOK available on Amazon Kindle♥
http://amzn.to/2EwR3ah
NO MORE hard copies... those who got one are lucky!
♥More Written Recipes are on my BLOG♥
http://createeathappy.blogspot.com/
♥My Recipe Posts in Japanese♥
http://cooklabo.blogspot.jp/
http://cookpad.com/ami
http://twitter.com/alohaforever
♥and of course PLEASE SUBSCRIBE♥
http://www.youtube.com/user/ochikeron?sub_confirmation=1
pattern library 在 ochikeron Youtube 的精選貼文
3 different colors all strawberry flavor but my kids said the sweetness differs. haha I found this kit earlier this year, so maybe it is no longer available. Well, I hope you get an idea of this product since it comes out every year with new Precure series 😉
Healin' Good♥Precure (Japanese: ヒーリングっど♥プリキュア) is a Japanese magical girl anime series by Toei Animation. It is my daughter's favorite program followed by Star☆Twinkle PreCure last year 💖 https://youtu.be/0ZFZVybYINk https://www.youtube.com/watch?v=WMLol36BrRU
FYI, Healin' Good♥Precure Dress Jelly Kit (ヒーリングっどプリキュアドレスゼリー)
https://amzn.to/3mDRMx1
Pink: Cure Grace (キュアグレース, Kyua Gurēsu) / Nodoka Hanadera (花寺のどか, Hanadera Nodoka)
Blue: Cure Fontaine (キュアフォンテーヌ, Kyua Fontēnu) / Chiyu Sawaizumi (沢泉ちゆ, Sawaizumi Chiyu)
Yellow: Cure Sparkle (キュアスパークル, Kyua Supākuru) Hinata Hiramitsu / (平光ひなた, Hiramitsu Hinata)
---------------------------------
Precure Dress Jelly Dolls (Healin' Good Pretty Cure DIY Kit)
Difficulty: Very Easy
Time: 5min + 2hrs cooling time
Calories: 16kcal each
Directions:
1. Put each jelly powder in mugs.
2. Add one tray cup of water into each mug and mix well.
3. Microwave each mug on medium (500w) for 1 minute, then mix well.
4. Put each into the right dress mold (see the pattern) then cool in the fridge for 2 hours.
5. Trace the jelly with a toothpick, remove from the mold, and place the dress picks on top.
レシピ(日本語)
https://cooklabo.blogspot.com/2020/09/Precure-Dress-Jelly.html
---------------------------------
Music by
YouTube Audio Library
Follow me on social media. If you have recreated any of my food, you can share some pictures #ochikeron. I am always happy to see them.
♥FOLLOW ME HERE♥
http://instagram.com/ochikeron/
https://www.facebook.com/ochikeron
https://plus.google.com/+ochikeron
http://twitter.com/ochikeron
♥My COOKBOOK available on Amazon Kindle♥
http://amzn.to/2EwR3ah
NO MORE hard copies... those who got one are lucky!
♥More Written Recipes are on my BLOG♥
http://createeathappy.blogspot.com/
♥My Recipe Posts in Japanese♥
http://cooklabo.blogspot.jp/
http://cookpad.com/ami
http://twitter.com/alohaforever
♥and of course PLEASE SUBSCRIBE♥
http://www.youtube.com/user/ochikeron?sub_confirmation=1
pattern library 在 edx/ux-pattern-library: The (working) Visual, UI, and ... - GitHub 的推薦與評價
The use and compilation of Sass into CSS using perferrably LibSass (if using the Sass method for including the pattern library). Third Party Dependencies. Also, ... ... <看更多>
pattern library 在 About - USPTO UI Design Library 的推薦與評價
An open source UI style guide, pattern library, and Bootstrap theme. Our docs are packed full of usability guidelines, examples, and source code. ... <看更多>
pattern library 在 UI: Pattern Library - Pinterest 的推薦與評價
Apr 21, 2016 - User interface pattern library. See more ideas about user interface, pattern library, style guide ui. ... <看更多>