r/ethdev • u/[deleted] • Feb 20 '18
Solidity libraries suck; use fallback proxies instead
Generally, the goal with libraries is to save gas on new contracts by reusing code. Using libraries, however, requires you to declare a bunch of interfaces, like so:
contract C {
using L for L.Storage;
L.Storage lib;
function a() {
lib.a();
}
function b() {
lib.b();
}
function c() {
lib.c();
}
}Each method requires overhead, and individually contributes more and more code that proxies into the same library. Also, libraries cannot refer to their own data; they must receive a storage reference. It would be better if there was one method that handled all of this. Fortunately, that's what the fallback function is for. Starting in Metropolis, you can proxy almost every method with the fallback function using solidity assembly.
contract Proxy {
function () external {
// return Real(0x0000005E1CBE78009143B44D717423cb01a002B7).delegatecall(msg.data);
assembly {
calldatacopy(0, 0, calldatasize)
let _retVal := delegatecall(sub(gas,740), 0x0000005E1CBE78009143B44D717423cb01a002B7, 0, calldatasize, 0, 32)
switch _retVal case 0 { revert(0,returndatasize) } default { return(0, returndatasize) }
}
}
}Switching from libraries to the proxy model saved me about 70% on CREATE operations. The delegatecall overhead is comparable to using a library. Depending on your use case (multisig wallets, etc), you can benefit from using fallback proxies instead. Proxying also works recursively, and the proxy address can be updated with an upgraded library.
2
6
u/fergarrui Feb 20 '18
Doing that you just need to be careful about things like this:
https://github.com/fergarrui/ethereum-security/tree/master/contracts/delegatecall
https://github.com/fergarrui/ethereum-security/blob/master/test/delegatecall/delegateCallTest.js
→ More replies (1)
1
Feb 27 '18
Vitalik and chriseth also recommend this: https://www.reddit.com/r/ethereum/comments/6c1jui/delegatecall_forwarders_how_to_save_5098_on/
The output of solc --clone is smaller than the contract generated by the above assembly.
1
Feb 27 '18
https://github.com/yarrumretep/clone-factory/blob/master/contracts/CloneFactory.sol
This is an even smaller clone than
solc --clone
1
u/augusto-teixeira Apr 13 '18
How does one interact with this fallback from web3 or truffle? One needs to mess with the ABI?
1
0
Feb 23 '18
[deleted]
1
Feb 23 '18
I have a factory contract that creates and manages privileged proxies. The factory guarantees the integrity of its offspring and lets them consume a utility token.
2
u/[deleted] Feb 20 '18
This gas savings is enabled by the ability to see the return argument count in the latest EVM?