Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Demos delegatecall in serpent
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
You can’t perform that action at this time.