bobcat_proxy/
lib.rs

1#![no_std]
2
3use array_concat::concat_arrays;
4
5pub type Address = [u8; 20];
6
7pub const fn make_minimal_proxy(addr: Address) -> [u8; 18 + 16 + 20] {
8    // I can't remember where this comes from (TODO), but we use this in
9    // 9lives for our share proxies.
10    concat_arrays!(
11        [
12            0x60, 0x2d, 0x5f, 0x81, 0x60, 0x09, 0x5f, 0x39, 0xf3, 0x5f, 0x5f, 0x36, 0x5f, 0x5f,
13            0x37, 0x36, 0x5f, 0x73,
14        ],
15        addr,
16        [
17            0x5a, 0xf4, 0x3d, 0x5f, 0x5f, 0x3e, 0x60, 0x29, 0x57, 0x3d, 0x5f, 0xfd, 0x5b, 0x3d,
18            0x5f, 0xf3,
19        ]
20    )
21}
22
23macro_rules! unpack_arr {
24    ($b:expr, $l:expr) => {
25        match const_hex::const_decode_to_array::<$l>($b) {
26            Ok(v) => v,
27            Err(_) => panic!("bad code"),
28        }
29    };
30}
31
32/// Make a EIP1967 proxy that reads from the standard storage slot.
33pub const fn make_eip1967_proxy(logic: Address) -> [u8; 1 + 20 + 102] {
34    // Created from eip1967.huff .
35    concat_arrays!(
36      [0x73],
37      logic,
38      unpack_arr!(
39          b"7f360894a13ba1a3210667c828492db98dca3e2076cc3735a920a3ca505d382bbc55603a8060403d393df3365f5f375f5f365f7f360894a13ba1a3210667c828492db98dca3e2076cc3735a920a3ca505d382bbc545af45f3d5f5f3e3d9161003857fd5bf3",
40          101
41      )
42    )
43}
44
45/// Make beacon proxy that calls "implementation()(address)" on a
46/// hardcoded beacon address to get the logic address to delegate to.
47pub const fn make_beacon_proxy(beacon: Address) -> [u8; 20 + 110] {
48    // Created from beacon-proxy.huff .
49    concat_arrays!(
50        unpack_arr!(
51            b"60438060093d393df3365f5f375f5f365f635c60da1b602052602060206004603c73",
52            34
53        ),
54        beacon,
55        unpack_arr!(
56          b"60438060093d393df3365f5f375f5f365f635c60da1b602052602060206004603c7390b75e378f50a871e4125ba9d1ef9a3826cedf415afa506020515af45f3d5f5f3e3d9161004157fd5bf3",
57          76
58        )
59    )
60}
61
62/// Make a beacon proxy that loads from the beacon slot to get the
63/// "implementation()(address)" to get the logic address to delegate to.
64pub const fn make_upgradeable_beacon_proxy(beacon: Address) -> [u8; 1 + 20 + 123] {
65    // Created from beacon-proxy.huff .
66    concat_arrays!(
67        [0x73],
68        beacon,
69        unpack_arr!(
70            b"7fa3f0ad74e5423aebfd80d3ef4346578335a9a72aeaee59ff6cb3582b35133d505560508060403d393df3365f5f375f5f365f635c60da1b602052602060206004603c7fa3f0ad74e5423aebfd80d3ef4346578335a9a72aeaee59ff6cb3582b35133d50545afa506020515af45f3d5f5f3e3d9161004e57fd5bf3",
71            123
72        )
73    )
74}
75
76pub const fn make_multi3_proxy(
77    one: Address,
78    two: Address,
79    three: Address,
80    all: Address,
81) -> [u8; 46 + 20 + 7 + 20 + 7 + 20 + 6 + 20 + 21] {
82    // Created from multi3-proxy.huff .
83    concat_arrays!(
84        unpack_arr!(
85            b"609e8060093d393df3365f5f375f5f365f6002355f1a8060011461003d5780600214610058576003146100735773",
86            46
87        ),
88        all,
89        unpack_arr!(b"61008d565b5073", 7),
90        one,
91        unpack_arr!(b"61008d565b5073", 7),
92        two,
93        unpack_arr!(b"61008d565b73", 6),
94        three,
95        unpack_arr!(b"61008d565b5af45f3d5f5f3e3d9161009c57fd5bf3", 21)
96    )
97}
98
99/// Create a proxy that calls "implementation(bytes4)" on the beacon given
100/// to figure out where to delegatecall its calldata to.
101pub const fn make_beacon_sel_proxy(beacon: Address) -> [u8; 42 + 20 + 33] {
102    // Created from sel-beacon-proxy.huff .
103    concat_arrays!(
104        unpack_arr!(
105            b"60568060093d393df33660046020355f5f366020630d7415775f5260045f602037602060246024601c73",
106            42
107        ),
108        beacon,
109        unpack_arr!(
110            b"5afa15610054576024513660046024375af43d5f5f3e5f3d911561005457f35bfd",
111            33
112        )
113    )
114}