1
1
module EncodeAbi exposing (..)
2
2
3
- import BigInt exposing (BigInt )
4
- import Eth.Abi.Encode as Abi
5
- import Eth.Types exposing (Hex )
6
- import Eth.Utils exposing (hexToString , remove0x )
3
+ import BigInt exposing (fromInt )
4
+ import Eth.Abi.Encode as E
5
+ import Eth.Types exposing (Address , Hex )
6
+ import Eth.Utils exposing (hexToString , remove0x , unsafeToAddress , unsafeToHex )
7
7
import Expect
8
8
import String exposing (join )
9
9
import String.Extra exposing (wrapWith )
@@ -47,9 +47,9 @@ pointers =
47
47
]
48
48
49
49
encoded =
50
- Abi . abiEncodeList
51
- [ Abi . uint <| BigInt . fromInt 1
52
- , Abi . uint <| BigInt . fromInt 2
50
+ E . abiEncodeList
51
+ [ E . uint <| BigInt . fromInt 1
52
+ , E . uint <| BigInt . fromInt 2
53
53
]
54
54
in
55
55
expectHex exp encoded
@@ -69,8 +69,8 @@ pointers =
69
69
]
70
70
71
71
encoded =
72
- Abi . abiEncodeList
73
- [ Abi . list [ Abi . uint <| BigInt . fromInt 2 ]
72
+ E . abiEncodeList
73
+ [ E . list [ E . uint <| BigInt . fromInt 2 ]
74
74
]
75
75
in
76
76
expectHex exp encoded
@@ -91,9 +91,9 @@ pointers =
91
91
]
92
92
93
93
encoded =
94
- Abi . abiEncodeList
95
- [ Abi . uint <| BigInt . fromInt 1
96
- , Abi . list [ Abi . uint <| BigInt . fromInt 2 ]
94
+ E . abiEncodeList
95
+ [ E . uint <| BigInt . fromInt 1
96
+ , E . list [ E . uint <| BigInt . fromInt 2 ]
97
97
]
98
98
in
99
99
expectHex exp encoded
@@ -115,24 +115,128 @@ pointers =
115
115
]
116
116
117
117
encoded =
118
- Abi . abiEncodeList
119
- [ Abi . uint <| BigInt . fromInt 1
120
- , Abi . list [ Abi . uint <| BigInt . fromInt 2 ]
121
- , Abi . uint <| BigInt . fromInt 3
118
+ E . abiEncodeList
119
+ [ E . uint <| BigInt . fromInt 1
120
+ , E . list [ E . uint <| BigInt . fromInt 2 ]
121
+ , E . uint <| BigInt . fromInt 3
122
122
]
123
123
in
124
124
expectHex exp encoded
125
125
]
126
126
127
127
128
+ someCallBody : List SomeStruct -> Result String Hex
129
+ someCallBody elts =
130
+ let
131
+ eltsEncoded =
132
+ elts |> List . map encodeSubStruct |> E . list
133
+
134
+ someToken =
135
+ unsafeToAddress " 0x0eb3a705fc54725037cc9e008bdede697f62f335"
136
+ in
137
+ E . abiEncodeList [ E . uint ( fromInt 8 ) , E . address someToken, eltsEncoded ]
138
+
139
+
140
+
141
+
142
+ -- struct SomeStruct {
143
+ -- bytes32 someBytes32Str;
144
+ -- address token;
145
+ -- bytes callData;
146
+ -- bool someBool;
147
+ -- }
148
+ type alias SomeStruct =
149
+ { someBytes32Str : String
150
+ , token : Address
151
+ , callData : Hex
152
+ , someBool : Bool
153
+ }
154
+
155
+
156
+ encodeSubStruct : SomeStruct -> E .Encoding
157
+ encodeSubStruct o =
158
+ let
159
+ nameEncoded =
160
+ o. someBytes32Str
161
+ |> E . stringToHex
162
+ |> unsafeToHex
163
+ |> E . staticBytes
164
+ in
165
+ E . tuple [ nameEncoded, E . address o. token, E . bytes o. callData, E . bool o. someBool ]
166
+
167
+
168
+ complexStruct : Test
169
+ complexStruct =
170
+ describe " Encoding complex struct"
171
+ [ test " Encode struct with empty array" <|
172
+ \ _ ->
173
+ let
174
+ encoded =
175
+ someCallBody []
176
+
177
+ expected =
178
+ [ " 0000000000000000000000000000000000000000000000000000000000000008" -- id
179
+ , " 0000000000000000000000000eb3a705fc54725037cc9e008bdede697f62f335" -- out otken
180
+ , " 0000000000000000000000000000000000000000000000000000000000000060" -- array pointer
181
+ , " 0000000000000000000000000000000000000000000000000000000000000000" -- array len
182
+ ]
183
+ in
184
+ Expect . equal ( Ok ( unsafeToHex <| join " " expected)) encoded
185
+ , test " Encode struct with array elements" <|
186
+ \ _ ->
187
+ let
188
+ encoded =
189
+ someCallBody
190
+ [ { someBytes32Str = " ZeroEx"
191
+ , token = otherToken
192
+ , callData = unsafeToHex " 0x11111111111111111111111111111111111111111111111111111111111111112222"
193
+ , someBool = True
194
+ }
195
+ ]
196
+
197
+ expected =
198
+ [
199
+ " 0000000000000000000000000000000000000000000000000000000000000008" -- id
200
+ , " 0000000000000000000000000eb3a705fc54725037cc9e008bdede697f62f335" -- token
201
+ , " 0000000000000000000000000000000000000000000000000000000000000060" -- array pointer
202
+ , " 0000000000000000000000000000000000000000000000000000000000000001" -- array len
203
+ , " 0000000000000000000000000000000000000000000000000000000000000020" -- first elt pointer
204
+ , " 5a65726f45780000000000000000000000000000000000000000000000000000" -- "ZeroEx"
205
+ , " 0000000000000000000000002170ed0880ac9a755fd29b2688956bd959f933f8" -- token
206
+ , " 0000000000000000000000000000000000000000000000000000000000000080" -- pointer to calldata (two lines below)
207
+ , " 0000000000000000000000000000000000000000000000000000000000000001" -- boolean "true"
208
+ , " 0000000000000000000000000000000000000000000000000000000000000022" -- calldata len
209
+ , " 1111111111111111111111111111111111111111111111111111111111111111" -- calldata
210
+ , " 2222000000000000000000000000000000000000000000000000000000000000" -- calldata (part 2)
211
+ ]
212
+ in
213
+ expectHex expected encoded
214
+ ]
215
+
216
+
217
+ coalesce : a -> Maybe a -> a
218
+ coalesce a ma =
219
+ case ma of
220
+ Nothing ->
221
+ a
222
+
223
+ Just v ->
224
+ v
225
+
226
+
128
227
expectHex : List String -> Result String Hex -> Expect .Expectation
129
228
expectHex expected result =
130
229
case result of
131
230
Err e ->
132
231
Expect . fail e
133
232
134
233
Ok hex ->
135
- Expect . equal ( expected |> join " " |> wrapWith 64 " " ) ( hex |> hexToString |> remove0x |> wrapWith 64 " " )
234
+ Expect . equal ( wrapWith 64 " " <| join " " expected) ( wrapWith 64 " " <| remove0x <| hexToString <| hex)
235
+
236
+
237
+ otherToken : Eth .Types .Address
238
+ otherToken =
239
+ unsafeToAddress " 0x2170ed0880ac9a755fd29b2688956bd959f933f8"
136
240
137
241
138
242
@@ -144,23 +248,23 @@ encodeInt =
144
248
describe " Int Encoding"
145
249
[ test " -120" <|
146
250
\ _ ->
147
- Abi . abiEncode ( Abi . int <| BigInt . fromInt - 120 )
251
+ E . abiEncode ( E . int <| BigInt . fromInt - 120 )
148
252
|> Result . map Eth . Utils . hexToString
149
253
|> Expect . equal ( Ok " 0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff88" )
150
254
, test " 120" <|
151
255
\ _ ->
152
- Abi . abiEncode ( Abi . int <| BigInt . fromInt 120 )
256
+ E . abiEncode ( E . int <| BigInt . fromInt 120 )
153
257
|> Result . map Eth . Utils . hexToString
154
258
|> Expect . equal ( Ok " 0x0000000000000000000000000000000000000000000000000000000000000078" )
155
259
, test " max positive int256" <|
156
260
\ _ ->
157
261
BigInt . fromIntString " 57896044618658097711785492504343953926634992332820282019728792003956564819967"
158
- |> Maybe . map ( Abi . int >> Abi . abiEncode >> Result . map Eth . Utils . hexToString)
262
+ |> Maybe . map ( E . int >> E . abiEncode >> Result . map Eth . Utils . hexToString)
159
263
|> Expect . equal ( Just ( Ok " 0x7fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff" ))
160
264
, test " max negative int256" <|
161
265
\ _ ->
162
266
BigInt . fromIntString " -57896044618658097711785492504343953926634992332820282019728792003956564819968"
163
- |> Maybe . map ( Abi . int >> Abi . abiEncode >> Result . map Eth . Utils . hexToString)
267
+ |> Maybe . map ( E . int >> E . abiEncode >> Result . map Eth . Utils . hexToString)
164
268
|> Expect . equal ( Just ( Ok " 0x8000000000000000000000000000000000000000000000000000000000000000" ))
165
269
]
166
270
0 commit comments