1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
mod ganache;
pub use ganache::{Ganache, GanacheInstance};
mod solc;
pub use solc::{CompiledContract, Solc};
mod hash;
pub use hash::{hash_message, id, keccak256, serialize};
pub use rlp;
use crate::types::{Address, Bytes, U256};
use std::convert::TryInto;
pub const WEI_IN_ETHER: usize = 1000000000000000000;
pub fn format_ether<T: Into<U256>>(amount: T) -> U256 {
amount.into() / WEI_IN_ETHER
}
pub fn format_units<T: Into<U256>>(amount: T, decimals: usize) -> U256 {
amount.into() / decimals
}
pub fn parse_ether<S>(eth: S) -> Result<U256, S::Error>
where
S: TryInto<U256>,
{
Ok(eth.try_into()? * WEI_IN_ETHER)
}
pub fn parse_units<S>(eth: S, decimals: usize) -> Result<U256, S::Error>
where
S: TryInto<U256>,
{
Ok(eth.try_into()? * decimals)
}
pub fn get_contract_address(sender: impl Into<Address>, nonce: impl Into<U256>) -> Address {
let mut stream = rlp::RlpStream::new();
stream.begin_list(2);
stream.append(&sender.into());
stream.append(&nonce.into());
let hash = keccak256(&stream.out());
let mut bytes = [0u8; 20];
bytes.copy_from_slice(&hash[12..]);
Address::from(bytes)
}
pub fn get_create2_address(
from: impl Into<Address>,
salt: impl Into<Bytes>,
init_code: impl Into<Bytes>,
) -> Address {
let bytes = [
&[0xff],
from.into().as_bytes(),
salt.into().as_ref(),
&keccak256(init_code.into().as_ref()),
]
.concat();
let hash = keccak256(&bytes);
let mut bytes = [0u8; 20];
bytes.copy_from_slice(&hash[12..]);
Address::from(bytes)
}
#[cfg(test)]
mod tests {
use super::*;
use rustc_hex::FromHex;
#[test]
fn contract_address() {
let from = "6ac7ea33f8831ea9dcc53393aaa88b25a785dbf0"
.parse::<Address>()
.unwrap();
for (nonce, expected) in [
"cd234a471b72ba2f1ccf0a70fcaba648a5eecd8d",
"343c43a37d37dff08ae8c4a11544c718abb4fcf8",
"f778b86fa74e846c4f0a1fbd1335fe81c00a0c91",
"fffd933a0bc612844eaf0c6fe3e5b8e9b6c1d19c",
]
.iter()
.enumerate()
{
let address = get_contract_address(from, nonce);
assert_eq!(address, expected.parse::<Address>().unwrap());
}
}
#[test]
fn create2_address() {
for (from, salt, init_code, expected) in &[
(
"0000000000000000000000000000000000000000",
"0000000000000000000000000000000000000000000000000000000000000000",
"00",
"4D1A2e2bB4F88F0250f26Ffff098B0b30B26BF38",
),
(
"deadbeef00000000000000000000000000000000",
"0000000000000000000000000000000000000000000000000000000000000000",
"00",
"B928f69Bb1D91Cd65274e3c79d8986362984fDA3",
),
(
"deadbeef00000000000000000000000000000000",
"000000000000000000000000feed000000000000000000000000000000000000",
"00",
"D04116cDd17beBE565EB2422F2497E06cC1C9833",
),
(
"0000000000000000000000000000000000000000",
"0000000000000000000000000000000000000000000000000000000000000000",
"deadbeef",
"70f2b2914A2a4b783FaEFb75f459A580616Fcb5e",
),
(
"00000000000000000000000000000000deadbeef",
"00000000000000000000000000000000000000000000000000000000cafebabe",
"deadbeef",
"60f3f640a8508fC6a86d45DF051962668E1e8AC7",
),
(
"00000000000000000000000000000000deadbeef",
"00000000000000000000000000000000000000000000000000000000cafebabe",
"deadbeefdeadbeefdeadbeefdeadbeefdeadbeefdeadbeefdeadbeefdeadbeefdeadbeefdeadbeefdeadbeef",
"1d8bfDC5D46DC4f61D6b6115972536eBE6A8854C",
),
(
"0000000000000000000000000000000000000000",
"0000000000000000000000000000000000000000000000000000000000000000",
"",
"E33C0C7F7df4809055C3ebA6c09CFe4BaF1BD9e0",
),
] {
let from = from.parse::<Address>().unwrap();
let salt = salt.from_hex::<Vec<u8>>().unwrap();
let init_code = init_code.from_hex::<Vec<u8>>().unwrap();
let expected = expected.parse::<Address>().unwrap();
assert_eq!(expected, get_create2_address(from, salt, init_code))
}
}
}