Skip to content

Commit ea7037d

Browse files
committed
Add some real-life encoding tests
1 parent d7f2df3 commit ea7037d

File tree

2 files changed

+125
-34
lines changed

2 files changed

+125
-34
lines changed

src/Eth/Abi/Encode.elm

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -46,19 +46,6 @@ import String.UTF8 as UTF8
4646
{-| -}
4747

4848

49-
50-
-- type Encoding
51-
-- = AddressE Address
52-
-- | UintE BigInt
53-
-- | IntE BigInt
54-
-- | BoolE Bool
55-
-- | DBytesE Hex
56-
-- | BytesE Hex
57-
-- | StringE String
58-
-- | ListE (List Encoding)
59-
-- | CustomE String
60-
61-
6249
functionCall : String -> List Encoding -> Result String Hex
6350
functionCall abiSig args =
6451
abiEncodeList_ args

tests/EncodeAbi.elm

Lines changed: 125 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
module EncodeAbi exposing (..)
22

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)
77
import Expect
88
import String exposing (join)
99
import String.Extra exposing (wrapWith)
@@ -47,9 +47,9 @@ pointers =
4747
]
4848

4949
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
5353
]
5454
in
5555
expectHex exp encoded
@@ -69,8 +69,8 @@ pointers =
6969
]
7070

7171
encoded =
72-
Abi.abiEncodeList
73-
[ Abi.list [ Abi.uint <| BigInt.fromInt 2 ]
72+
E.abiEncodeList
73+
[ E.list [ E.uint <| BigInt.fromInt 2 ]
7474
]
7575
in
7676
expectHex exp encoded
@@ -91,9 +91,9 @@ pointers =
9191
]
9292

9393
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 ]
9797
]
9898
in
9999
expectHex exp encoded
@@ -115,24 +115,128 @@ pointers =
115115
]
116116

117117
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
122122
]
123123
in
124124
expectHex exp encoded
125125
]
126126

127127

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+
128227
expectHex : List String -> Result String Hex -> Expect.Expectation
129228
expectHex expected result =
130229
case result of
131230
Err e ->
132231
Expect.fail e
133232

134233
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"
136240

137241

138242

@@ -144,23 +248,23 @@ encodeInt =
144248
describe "Int Encoding"
145249
[ test "-120" <|
146250
\_ ->
147-
Abi.abiEncode (Abi.int <| BigInt.fromInt -120)
251+
E.abiEncode (E.int <| BigInt.fromInt -120)
148252
|> Result.map Eth.Utils.hexToString
149253
|> Expect.equal (Ok "0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff88")
150254
, test "120" <|
151255
\_ ->
152-
Abi.abiEncode (Abi.int <| BigInt.fromInt 120)
256+
E.abiEncode (E.int <| BigInt.fromInt 120)
153257
|> Result.map Eth.Utils.hexToString
154258
|> Expect.equal (Ok "0x0000000000000000000000000000000000000000000000000000000000000078")
155259
, test "max positive int256" <|
156260
\_ ->
157261
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)
159263
|> Expect.equal (Just (Ok "0x7fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff"))
160264
, test "max negative int256" <|
161265
\_ ->
162266
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)
164268
|> Expect.equal (Just (Ok "0x8000000000000000000000000000000000000000000000000000000000000000"))
165269
]
166270

0 commit comments

Comments
 (0)