Created
June 9, 2016 23:33
-
-
Save ChrisCalderon/2818e6ba0214c25962b16c02e761dc26 to your computer and use it in GitHub Desktop.
Demos delegatecall in serpent
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import serpent | |
from ethereum import tester as t | |
code = '''\ | |
def foo(x, flag): | |
with prefix = 0: | |
if flag == "double": | |
prefix = 0x6ffa1caa*2**248 | |
else: | |
prefix = 0x70cd69ae*2**248 | |
delegate_calldata = alloc(36) | |
out = alloc(32) | |
mstore(ref(delegate_calldata), prefix / 2**248) | |
prefix *= 2**8 | |
mstore8(ref(delegate_calldata) + 1, prefix / 2**248) | |
prefix *= 2**8 | |
mstore8(ref(delegate_calldata) + 2, prefix / 2**248) | |
prefix *= 2**8 | |
mstore8(ref(delegate_calldata) + 3, prefix / 2**248) | |
mstore(ref(delegate_calldata) + 4, x) | |
delegatecall(msg.gas, self, 128, 36, 128 + 36, 32) | |
return(out[0]) | |
def double(x): | |
return(2*x) | |
def half(x): | |
return(x/2)''' | |
print serpent.compile_to_lll(code) | |
s = t.state() | |
c = s.abi_contract(code) | |
double = int('double'.encode('hex').ljust(64, '0'), 16) | |
print c.foo(1, double) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment