📜 [專欄新文章] 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.
👏 歡迎轉載分享鼓掌
同時也有1部Youtube影片,追蹤數超過9萬的網紅Smart Travel,也在其Youtube影片中提到,#bangkoklockdowns #thailandlockdowns #曼谷通關 #泰國通關 #曼谷自由行 #最新曼谷交通攻略 #bangkoktransportation #bangkokairportsuvarnabhumi #motorbikebangkok #SaphanTaksin #...
「c# token example」的推薦目錄:
- 關於c# token example 在 Taipei Ethereum Meetup Facebook 的最讚貼文
- 關於c# token example 在 Nana Mahazan Facebook 的最佳貼文
- 關於c# token example 在 Nana Mahazan Facebook 的最佳貼文
- 關於c# token example 在 Smart Travel Youtube 的最讚貼文
- 關於c# token example 在 How do I get an OAuth 2.0 authentication token in C# - Stack ... 的評價
- 關於c# token example 在 Token Authentication C# Console Application - Sample - gists ... 的評價
- 關於c# token example 在 Manually Build a Login Flow - Facebook for Developers 的評價
- 關於c# token example 在 Get Facebook C# Api Access Token - CodeSamplez.com 的評價
- 關於c# token example 在 Web API Token Authentication C# - Adadev 的評價
c# token example 在 Nana Mahazan Facebook 的最佳貼文
Memang seronok menghias makanan dengan Althea ni.. Tapi yg penting nanti makan pun berselera.. Korang pun boleh join #100funmeals dari Sustagen Malaysia ni.. Cepat, tarikh tutup 31 Disember ni.. so cepat submit gambar korang!
The top 100 submissions will receive a 1.3kg Sustagen Pack along with a mystery token!
More Info:
https://www.facebook.com/sustagenmalaysia/photos/a.1485577378342582.1073741828.1445490559017931/1571743553059297/?type=1&theater
https://www.facebook.com/notes/sustagen-malaysia/how-to-join-100funmeals/1582067852026867
More Example From Samantha Lee:
https://www.facebook.com/iamleesamantha/photos/a.122571231246462.1073741835.112111622292423/219586928211558/?type=1&theater
T&C:
https://www.facebook.com/notes/sustagen-malaysia/sustagen-100funmeals-official-rules-terms-conditions/1574027936164192
c# token example 在 Nana Mahazan Facebook 的最佳貼文
Budak2 ni kalau makan, presentation tu penting.. Baru selera kan? Althea memang suka kalau bab mencantikkan makanan.. Hias hias.. Kreatif! Good job munchkin.. 😘 Owh, saya join #100funmeals dari Sustagen Malaysia ni.. Jom join sbb campaign ni sampai 31 Disember tau, so cepat!
The top 100 submissions will receive a 1.3kg Sustagen Pack along with a mystery token!
More Info:
https://www.facebook.com/sustagenmalaysia/photos/a.1485577378342582.1073741828.1445490559017931/1571743553059297/?type=1&theater
More Example From Samantha Lee:
https://www.facebook.com/iamleesamantha/photos/a.122571231246462.1073741835.112111622292423/219586928211558/?type=1&theater
T&C:
https://www.facebook.com/notes/sustagen-malaysia/sustagen-100funmeals-official-rules-terms-conditions/1574027936164192
c# token example 在 Smart Travel Youtube 的最讚貼文
#bangkoklockdowns #thailandlockdowns #曼谷通關 #泰國通關 #曼谷自由行 #最新曼谷交通攻略
#bangkoktransportation #bangkokairportsuvarnabhumi #motorbikebangkok #SaphanTaksin #中文字幕 #CC #englishsubtitles
Hello大家好, 我叫阿Tsar,
相信亞洲最快解封的就是泰國
因為公司已經委派任務給我, 六月尾我會去泰國
除非到時有突發事件發生, 要取消航班
所以今日同大家分享是去曼谷機場出市區
除了搭的士, 就有更多便宜的選擇, 就是機場快線
繼而可以轉乘空鐵BTS, 或是地鐵MRT
除了講曼谷機場交通之外
亦都會講旅遊時候在市內的交通, 最常見幾種交通工具
例如有取代了 Uber的grab、 Tuktuk、電單車等等
曼谷的士最多可以載五個人, 有些會較大型, 但較少量
泰國的士顏色代表不同車行公司管理,
沒什麼特別意義,不需特別拘泥於顏色選擇。
但車頂上面的英文「Meter」招牌, 就一定要認住
因為是用咪錶正路的的士, 有些不正路的司機, 會開天殺價
那麼你就不應該上車, 例如如果曼谷新機場Suvarnabhumi Airport,
搭的士出市區, 大約400THB 大概是100hkd,
如果他們不跟咪錶, 價錢可以任由他說, 分分鐘要double
如果你是三個人或以上一起搭的話, 我當然會建議你搭的士吧
假使你一兩個人, 我建議你搭機場快線ARL出市區
但如果你是搭廉航的話, 請你睇清楚自己是否在新機場到境
因為好多廉航著陸在舊機場, 是沒有機場快線,這個是溫馨提示
溫馨提示還有兩件事, 第一就是每個入境泰國的人
需要攜帶 2 萬泰銖、家庭則需要 4 萬泰銖。
或可用其他國家等值貨幣代替
假設香港人, 每個人是要帶接近5000hkd, 或家庭的10000hkd
所以如果你帶不夠錢, 實在有機會被人懷疑你是黑工
分分鐘拒絕你入境,
海關雖然不是每個人都查, 但是都會抽查
如遇上抽查又沒有足夠現金,可試向入境人員解釋會在當地提款機付款,
再提供酒店住宿、回程機票等資料,也有望獲通融。
另一個溫馨提示就是, 任何人士在境內購買、使用吸食、進出口電子煙皆一律違法。
最高可判5年監禁或不超過50萬泰銖(約12萬港元)罰款,或兩者並罰。
當你拿回行李, 出到到境大堂, 就見指示牌去BF
ARL機場快線買車票的時候, 你首先要決定,
之後你想轉乘BTS 抑或 MRT 去你的酒店
因為ARL 落車的站 BTS 和 MRT是有所不同的
如果你住的酒店是 BTS站 附近,就搭到 Phaya Thai站轉 BTS 空鐵去到你酒店,
如果你住的酒店是 MRT站 附近,就搭到 Makkasan站轉 MRT 地鐵去到你酒店,
曼谷機場快線的站是A1,
如果要轉MRT, 就搭到去 A6站, 35THB=9hkd, 22分鐘
機場快綫A6 Makkasan站落車,再轉地鐵MRT
如果要轉BTS, 就搭到去 A8站, 45THB=11hkd, 26分鐘
我記得由機場快線落車去Phaya Thai站,
再轉搭BTS, 都叫做Phraya Thai站
但我記得在Praya Thai站有好多樓梯要行的
如果你去的時候已經有好多手提行李, 那麼你自己想想是否太麻煩
所以大家在香港book酒店的時候要清楚這一點
究竟你的酒店近BTS或MRT,
如果你在離境當日, 已經買了很多東西,
我建議你由酒店一程車去機場, 這些錢就不好慳啦
當你在機場快線ARL售票機的時候, 首先選擇語言
第二步你要選擇落車地點, 就好似是Phaya Thai站抑或Makkasan站
然後再選買幾多張飛, 一個人一張,如此類推
售票機的右手面即時顯示, 你要落車的地方和價錢
售賣機是接受硬幣和紙幣, 那麼你就可以入錢
然後售賣機就會吐出一個token塑膠幣, 就是你一程的車票
然後就可以入閘, 你不需要看我在哪裏上車
因為當天上車, 我是在 Hua Mak A4上車的,
Hua Mak 即相等於香港的東涌或青衣啦,
我在那裏上車匙因為公司book的酒店是在這裏附近
如果你們在曼谷機場是要從A1上車的,ok!
機場快線車廂裏面都好乾淨, 望落跟香港的地鐵差不多
所以大家如果是精明消費的朋友, 機場快線除了的士之外的另類選擇
由於當日我要搭地鐵MRT, 這裏路牌指示都非常清楚
一落車如果覺得口渴, 周圍都有大家好掛住的泰式奶茶
如果你轉乘MRT, 這個地鐵站名叫做 Phetchaburi站
下一個站就是Sukhumvit地鐵站, 即是Terminal.21附近
在BTS叫Asok站, 大家相連的
我意思是說如果你住在Terminal 21附近的酒店
你出街要搭BTS的時候, 都可以在這裏搭車去其他地方
非常之方便, 落到去MRT買票, 都好像香港搭地鐵一樣啦
如果你就要去翟道翟, 搭MRT,
翟道翟地鐵站名叫kamphaeng phet
好車站裏面都有Super rich換錢
千萬不要在機場換super rich, 會貴很多
當然你在香港出門之前會有兌換少少泰幣啦
到步當晚, 可能你在酒店附近逛街shopping或按摩啦
第二日的交通費, 我這裏有些少貼士同大家分享
Rabbit Card兔子卡, 相等於我們香港的八達通
兔子卡(Rabbit Card)只能用在曼谷BTS搭乘,
並不能使用在曼谷地鐵(MRT)系統,
Rabbit Card初次買卡費用為200THB,
含有100泰銖搭乘車資儲值,使用期限是5年,
增值金額最少為100泰銖,最多4千泰銖,
除了泰國BTS外,也能在部分快餐店或者連鎖超市付款,非常方便!
此外,兔子卡(Rabbit Card)也有分為大人卡、小童、老年人、學生卡
外一種就是BTS的一日券, 必須要在售票處購買,
價錢是140THB, 某些公司都有代購
當日任何時間用直至當晚凌晨12點為止,
如果你一日之內會用到三四次已經回本
除了因為可以節省搭車費之外, 另外最重要解決的問題
就在繁忙時間不需要排隊買票,
所以大家可以考慮下,
如果你買單程車票,買的步驟好簡單啦
首先看看地圖你要去的地方收費
然後在買票機按你個銀碼,
再投入硬幣或紙幣,然後攞飛, 就可以搭車啦
BTS 有兩條主要路線,分別是深綠色的Silom Line和淺綠色的Sukhumvit Line。
2020曼谷空鐵 BTS 沿線熱門景點, BTS Sukhumvit 蘇坤蔚線
有以下站名, Siam、Chit Lom、Mo Chit、Asok站
Siam(暹羅站)為曼谷百貨公司聚集地,
聚集了非常多美食、精品、咖啡廳、流行服飾、運動用品店
高檔的有Siam Paragon、中價的Siam Square
和Siam Center、Discovery Center等購物商場,
而且這個站有bts可連結Chit Lom站,可通往Central World、四面佛等。
霓虹夜市Talad Neon Night Market、Big C Supercenter、曼谷水門市場等等
BTS 深綠色的Silom缐, 主要站有 National Stadium、Sala Daeng、Saphan Taksin站
National Stadium站可通往BACC曼谷藝術及文化中心、
也可以從這裡通往TOKYU東急百貨、MBK Center、Siam Square。
Sala Daeng站最知名的景點莫過於Patpong Night Market了!
是曼谷夜生活的景點
Saphan Taksin站附近有石龍軍路食街, 我曾經介紹過
亦都有曼谷中央碼頭,可以在此搭船
玩曼谷前往大皇宮、鄭王廟、臥佛寺、Asiatique碼頭夜市等,
好多泰國曼谷知名景點都要從這邊轉搭船前往!
至於交通圖的深藍色線就是MRT地鐵
看完我影片之後, 你們就不用每次搭的士
除了空鐵和地鐵都會比搭的士便宜之外
曼谷搭的士有兩個問題, 第一可能的士司機會兜路
第二曼谷的士非常塞車嘅
除了的士、BTS、MRT以外, 曼谷市內亦都可以搭Grab
即是之前的Uber, 下載跟用的介面都差不多
電單車每2公里40Bath, 這個方法香港人好少用
適合短程, 我自己就搭過兩次, 其實都安全的
而且泰國政府指定穿著橙色背心的電單車司機要經過考核
但是講到底, 香港人條命都是矜貴些, 所以輕輕介紹一下
另外一種交通工具是Tuktuk, 相信好多人都搭過
Tuktuk和電單車一樣, 都只適合短程, 所以發應該多過50bath
如果貴過50bath, 我建議你寧願搭回的士或地鐵較好
如果想知曼谷有什麼值得平價吃、買、玩
那麼你就要看我的曼谷播放清單,
這些影片的留言在置頂, 會有一條link,
就會有我的曼谷播放清單, 超過50條影片, 陸續更新
希望大家多多支持啦, 今次講住咁多先,
星期六會直播,我最近看過的書籍,跟大家分享
暫時不會再講鬼故啦, 盡可能做到知識共享
如果你們對某些話題有興趣, 可以隨時留言給我
我會找相關的書籍跟大家分享, 今次講住咁多先啦,88
I believe Thailand is the earliest to set for the lift of all business and activity lockdowns .
Because my company has delegated tasks to me, I will go to Thailand at the end of June.
Unless there is an emergency at that time, the flight will be cancelled.So today I’m going to share with you that cheapest way to get to city from Bangkok Airport .Other than taxi, there are chaer options, Airport Rail Link ARL.Then you can transfer to the skytrain BTS or subway MRT .
Besides Bangkok airport traffic, I also talk about the most common types of transportation in the city.For example, there are Grab, Tuktuk n motorcycles.Bangkok taxis can carry up to five people, some will be larger, but not many.Thai taxi colors represent management of different car companies,
, .................
成為這個頻道的會員並獲得獎勵:
https://www.youtube.com/channel/UCIuNPxqDGG08p3EqCwY0XIg/join
請用片右下角調4K睇片。
c# token example 在 Token Authentication C# Console Application - Sample - gists ... 的推薦與評價
Token Authentication C# Console Application - Sample - AuthToken.cs. ... <看更多>
c# token example 在 Manually Build a Login Flow - Facebook for Developers 的推薦與評價
For example, if your login request looks like: ... This will return control flow back to your app with an access token on success, or error on failure. ... <看更多>
c# token example 在 How do I get an OAuth 2.0 authentication token in C# - Stack ... 的推薦與評價
... <看更多>
相關內容