Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
N
nebula
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
exchain
nebula
Commits
194c739e
Unverified
Commit
194c739e
authored
Oct 30, 2023
by
Adrian Sutton
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
batching: Add more unit tests.
parent
7a28c35e
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
165 additions
and
21 deletions
+165
-21
call.go
op-service/sources/batching/call.go
+20
-0
call_test.go
op-service/sources/batching/call_test.go
+145
-0
multicall.go
op-service/sources/batching/multicall.go
+0
-21
No files found.
op-service/sources/batching/call.go
View file @
194c739e
...
...
@@ -66,6 +66,26 @@ func (c *ContractCall) Unpack(hex hexutil.Bytes) (*CallResult, error) {
return
&
CallResult
{
out
:
out
},
nil
}
func
toCallArg
(
msg
ethereum
.
CallMsg
)
interface
{}
{
arg
:=
map
[
string
]
interface
{}{
"from"
:
msg
.
From
,
"to"
:
msg
.
To
,
}
if
len
(
msg
.
Data
)
>
0
{
arg
[
"data"
]
=
hexutil
.
Bytes
(
msg
.
Data
)
}
if
msg
.
Value
!=
nil
{
arg
[
"value"
]
=
(
*
hexutil
.
Big
)(
msg
.
Value
)
}
if
msg
.
Gas
!=
0
{
arg
[
"gas"
]
=
hexutil
.
Uint64
(
msg
.
Gas
)
}
if
msg
.
GasPrice
!=
nil
{
arg
[
"gasPrice"
]
=
(
*
hexutil
.
Big
)(
msg
.
GasPrice
)
}
return
arg
}
type
CallResult
struct
{
out
[]
interface
{}
}
...
...
op-service/sources/batching/call_test.go
0 → 100644
View file @
194c739e
package
batching
import
(
"math/big"
"testing"
"github.com/ethereum-optimism/optimism/op-bindings/bindings"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/common/hexutil"
"github.com/stretchr/testify/require"
)
func
TestContractCall_ToCallArgs
(
t
*
testing
.
T
)
{
addr
:=
common
.
Address
{
0xbd
}
testAbi
,
err
:=
bindings
.
ERC20MetaData
.
GetAbi
()
require
.
NoError
(
t
,
err
)
call
:=
NewContractCall
(
testAbi
,
addr
,
"approve"
,
common
.
Address
{
0xcc
},
big
.
NewInt
(
1234444
))
args
,
err
:=
call
.
ToCallArgs
()
require
.
NoError
(
t
,
err
)
argMap
,
ok
:=
args
.
(
map
[
string
]
interface
{})
require
.
True
(
t
,
ok
)
require
.
Equal
(
t
,
argMap
[
"from"
],
common
.
Address
{})
require
.
Equal
(
t
,
argMap
[
"to"
],
&
addr
)
expectedData
,
err
:=
call
.
Pack
()
require
.
NoError
(
t
,
err
)
require
.
Equal
(
t
,
argMap
[
"data"
],
hexutil
.
Bytes
(
expectedData
))
require
.
NotContains
(
t
,
argMap
,
"value"
)
require
.
NotContains
(
t
,
argMap
,
"gas"
)
require
.
NotContains
(
t
,
argMap
,
"gasPrice"
)
}
func
TestContractCall_Pack
(
t
*
testing
.
T
)
{
addr
:=
common
.
Address
{
0xbd
}
testAbi
,
err
:=
bindings
.
ERC20MetaData
.
GetAbi
()
require
.
NoError
(
t
,
err
)
sender
:=
common
.
Address
{
0xcc
}
amount
:=
big
.
NewInt
(
1234444
)
call
:=
NewContractCall
(
testAbi
,
addr
,
"approve"
,
sender
,
amount
)
actual
,
err
:=
call
.
Pack
()
require
.
NoError
(
t
,
err
)
expected
,
err
:=
testAbi
.
Pack
(
"approve"
,
sender
,
amount
)
require
.
NoError
(
t
,
err
)
require
.
Equal
(
t
,
actual
,
expected
)
}
func
TestContractCall_PackInvalid
(
t
*
testing
.
T
)
{
addr
:=
common
.
Address
{
0xbd
}
testAbi
,
err
:=
bindings
.
ERC20MetaData
.
GetAbi
()
require
.
NoError
(
t
,
err
)
// Second arg should be a *big.Int so packing should fail
call
:=
NewContractCall
(
testAbi
,
addr
,
"approve"
,
common
.
Address
{
0xcc
},
uint32
(
123
))
_
,
err
=
call
.
Pack
()
require
.
Error
(
t
,
err
)
}
func
TestContractCall_Unpack
(
t
*
testing
.
T
)
{
addr
:=
common
.
Address
{
0xbd
}
testAbi
,
err
:=
bindings
.
ERC20MetaData
.
GetAbi
()
require
.
NoError
(
t
,
err
)
call
:=
NewContractCall
(
testAbi
,
addr
,
"balanceOf"
,
common
.
Address
{
0xcc
})
outputs
:=
testAbi
.
Methods
[
"balanceOf"
]
.
Outputs
expected
:=
big
.
NewInt
(
1234
)
packed
,
err
:=
outputs
.
Pack
(
expected
)
require
.
NoError
(
t
,
err
)
unpacked
,
err
:=
call
.
Unpack
(
packed
)
require
.
NoError
(
t
,
err
)
require
.
Equal
(
t
,
unpacked
.
GetBigInt
(
0
),
expected
)
}
func
TestContractCall_UnpackInvalid
(
t
*
testing
.
T
)
{
addr
:=
common
.
Address
{
0xbd
}
testAbi
,
err
:=
bindings
.
ERC20MetaData
.
GetAbi
()
require
.
NoError
(
t
,
err
)
call
:=
NewContractCall
(
testAbi
,
addr
,
"balanceOf"
,
common
.
Address
{
0xcc
})
// Input data is the wrong format and won't unpack successfully
inputPacked
,
err
:=
call
.
Pack
()
require
.
NoError
(
t
,
err
)
_
,
err
=
call
.
Unpack
(
inputPacked
)
require
.
Error
(
t
,
err
)
}
func
TestCallResult_GetValues
(
t
*
testing
.
T
)
{
tests
:=
[]
struct
{
name
string
getter
func
(
result
*
CallResult
,
i
int
)
interface
{}
expected
interface
{}
}{
{
name
:
"GetUint8"
,
getter
:
func
(
result
*
CallResult
,
i
int
)
interface
{}
{
return
result
.
GetUint8
(
i
)
},
expected
:
uint8
(
12
),
},
{
name
:
"GetUint32"
,
getter
:
func
(
result
*
CallResult
,
i
int
)
interface
{}
{
return
result
.
GetUint32
(
i
)
},
expected
:
uint32
(
12346
),
},
{
name
:
"GetUint64"
,
getter
:
func
(
result
*
CallResult
,
i
int
)
interface
{}
{
return
result
.
GetUint64
(
i
)
},
expected
:
uint64
(
12346
),
},
{
name
:
"GetBool"
,
getter
:
func
(
result
*
CallResult
,
i
int
)
interface
{}
{
return
result
.
GetBool
(
i
)
},
expected
:
true
,
},
{
name
:
"GetHash"
,
getter
:
func
(
result
*
CallResult
,
i
int
)
interface
{}
{
return
result
.
GetHash
(
i
)
},
expected
:
([
32
]
byte
)(
common
.
Hash
{
0xaa
,
0xbb
,
0xcc
}),
},
{
name
:
"GetBigInt"
,
getter
:
func
(
result
*
CallResult
,
i
int
)
interface
{}
{
return
result
.
GetBigInt
(
i
)
},
expected
:
big
.
NewInt
(
2398423
),
},
}
for
_
,
test
:=
range
tests
{
test
:=
test
t
.
Run
(
test
.
name
,
func
(
t
*
testing
.
T
)
{
callResult
:=
&
CallResult
{[]
interface
{}{
nil
,
0
,
"abc"
,
test
.
expected
,
"xyz"
,
3
,
nil
}}
actual
:=
test
.
getter
(
callResult
,
3
)
require
.
EqualValues
(
t
,
test
.
expected
,
actual
)
})
}
}
op-service/sources/batching/multicall.go
View file @
194c739e
...
...
@@ -5,7 +5,6 @@ import (
"fmt"
"io"
"github.com/ethereum/go-ethereum"
"github.com/ethereum/go-ethereum/common/hexutil"
"github.com/ethereum/go-ethereum/rpc"
)
...
...
@@ -80,23 +79,3 @@ func (m *MultiCaller) CallLatest(ctx context.Context, calls ...*ContractCall) ([
}
return
callResults
,
nil
}
func
toCallArg
(
msg
ethereum
.
CallMsg
)
interface
{}
{
arg
:=
map
[
string
]
interface
{}{
"from"
:
msg
.
From
,
"to"
:
msg
.
To
,
}
if
len
(
msg
.
Data
)
>
0
{
arg
[
"data"
]
=
hexutil
.
Bytes
(
msg
.
Data
)
}
if
msg
.
Value
!=
nil
{
arg
[
"value"
]
=
(
*
hexutil
.
Big
)(
msg
.
Value
)
}
if
msg
.
Gas
!=
0
{
arg
[
"gas"
]
=
hexutil
.
Uint64
(
msg
.
Gas
)
}
if
msg
.
GasPrice
!=
nil
{
arg
[
"gasPrice"
]
=
(
*
hexutil
.
Big
)(
msg
.
GasPrice
)
}
return
arg
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment