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
29a29ac1
Commit
29a29ac1
authored
Aug 22, 2023
by
Mark Tyneway
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
op-chain-ops: cleanup
parent
fdd2761f
Changes
3
Show whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
87 additions
and
11 deletions
+87
-11
batch_file.go
op-chain-ops/safe/batch_file.go
+44
-0
batch_file_test.go
op-chain-ops/safe/batch_file_test.go
+1
-1
batch_helpers.go
op-chain-ops/safe/batch_helpers.go
+42
-10
No files found.
op-chain-ops/safe/batch_file.go
View file @
29a29ac1
...
...
@@ -170,6 +170,8 @@ type BatchTransaction struct {
// An error is defined by:
// - incorrectly encoded calldata
// - mismatch in number of arguments
// It does not currently work on structs, will return no error if a "tuple"
// is used as an argument. Need to find a generic way to work with structs.
func
(
bt
*
BatchTransaction
)
Check
()
error
{
if
len
(
bt
.
Method
.
Inputs
)
!=
len
(
bt
.
InputValues
)
{
return
fmt
.
Errorf
(
"expected %d inputs but got %d"
,
len
(
bt
.
Method
.
Inputs
),
len
(
bt
.
InputValues
))
...
...
@@ -184,6 +186,32 @@ func (bt *BatchTransaction) Check() error {
if
!
bytes
.
Equal
(
bt
.
Data
[
0
:
4
],
selector
)
{
return
fmt
.
Errorf
(
"data does not match signature"
)
}
// Check the calldata
values
:=
make
([]
any
,
len
(
bt
.
Method
.
Inputs
))
for
i
,
input
:=
range
bt
.
Method
.
Inputs
{
value
,
ok
:=
bt
.
InputValues
[
input
.
Name
]
if
!
ok
{
return
fmt
.
Errorf
(
"missing input %s"
,
input
.
Name
)
}
// Need to figure out better way to handle tuples in a generic way
if
input
.
Type
==
"tuple"
{
return
nil
}
arg
,
err
:=
unstringifyArg
(
value
,
input
.
Type
)
if
err
!=
nil
{
return
err
}
values
[
i
]
=
arg
}
calldata
,
err
:=
bt
.
Arguments
()
.
PackValues
(
values
)
if
err
!=
nil
{
return
err
}
if
!
bytes
.
Equal
(
bt
.
Data
[
4
:
],
calldata
)
{
return
fmt
.
Errorf
(
"calldata does not match inputs, expected %s, got %s"
,
hexutil
.
Encode
(
bt
.
Data
[
4
:
]),
hexutil
.
Encode
(
calldata
))
}
}
return
nil
}
...
...
@@ -197,6 +225,22 @@ func (bt *BatchTransaction) Signature() string {
return
fmt
.
Sprintf
(
"%s(%s)"
,
bt
.
Method
.
Name
,
strings
.
Join
(
types
,
","
))
}
func
(
bt
*
BatchTransaction
)
Arguments
()
abi
.
Arguments
{
arguments
:=
make
(
abi
.
Arguments
,
len
(
bt
.
Method
.
Inputs
))
for
i
,
input
:=
range
bt
.
Method
.
Inputs
{
serialized
,
err
:=
json
.
Marshal
(
input
)
if
err
!=
nil
{
panic
(
err
)
}
var
arg
abi
.
Argument
if
err
:=
json
.
Unmarshal
(
serialized
,
&
arg
);
err
!=
nil
{
panic
(
err
)
}
arguments
[
i
]
=
arg
}
return
arguments
}
// UnmarshalJSON will unmarshal a BatchTransaction from JSON.
func
(
b
*
BatchTransaction
)
UnmarshalJSON
(
data
[]
byte
)
error
{
var
bt
batchTransactionMarshaling
...
...
op-chain-ops/safe/batch_file_test.go
View file @
29a29ac1
...
...
@@ -71,7 +71,7 @@ func TestBatchAddCallFinalizeWithdrawalTransaction(t *testing.T) {
}
// TestBatchAddCallDespostTransaction ensures that simple calls can be serialized correctly.
func
TestBatchAddCallDespostTransaction
(
t
*
testing
.
T
)
{
func
TestBatchAddCallDespos
i
tTransaction
(
t
*
testing
.
T
)
{
file
,
err
:=
os
.
ReadFile
(
"testdata/portal-abi.json"
)
require
.
NoError
(
t
,
err
)
portalABI
,
err
:=
abi
.
JSON
(
bytes
.
NewReader
(
file
))
...
...
op-chain-ops/safe/batch_helpers.go
View file @
29a29ac1
...
...
@@ -88,18 +88,50 @@ func stringifyArg(argument any) (string, error) {
}
}
// countArgs will recursively count the number of arguments in an abi.Argument.
func
countArgs
(
total
*
int
,
input
abi
.
Argument
)
error
{
for
i
,
elem
:=
range
input
.
Type
.
TupleElems
{
e
:=
*
elem
*
total
++
arg
:=
abi
.
Argument
{
Name
:
input
.
Type
.
TupleRawNames
[
i
],
Type
:
e
,
}
return
countArgs
(
total
,
arg
)
// unstringifyArg converts a string to a Go type.
func
unstringifyArg
(
arg
string
,
typ
string
)
(
any
,
error
)
{
switch
typ
{
case
"address"
:
return
common
.
HexToAddress
(
arg
),
nil
case
"bool"
:
return
strconv
.
ParseBool
(
arg
)
case
"uint8"
:
val
,
err
:=
strconv
.
ParseUint
(
arg
,
10
,
8
)
return
uint8
(
val
),
err
case
"uint16"
:
val
,
err
:=
strconv
.
ParseUint
(
arg
,
10
,
16
)
return
uint16
(
val
),
err
case
"uint32"
:
val
,
err
:=
strconv
.
ParseUint
(
arg
,
10
,
32
)
return
uint32
(
val
),
err
case
"uint64"
:
val
,
err
:=
strconv
.
ParseUint
(
arg
,
10
,
64
)
return
val
,
err
case
"int8"
:
val
,
err
:=
strconv
.
ParseInt
(
arg
,
10
,
8
)
return
val
,
err
case
"int16"
:
val
,
err
:=
strconv
.
ParseInt
(
arg
,
10
,
16
)
return
val
,
err
case
"int32"
:
val
,
err
:=
strconv
.
ParseInt
(
arg
,
10
,
32
)
return
val
,
err
case
"int64"
:
val
,
err
:=
strconv
.
ParseInt
(
arg
,
10
,
64
)
return
val
,
err
case
"uint256"
,
"int256"
:
val
,
ok
:=
new
(
big
.
Int
)
.
SetString
(
arg
,
10
)
if
!
ok
{
return
nil
,
fmt
.
Errorf
(
"failed to parse %s as big.Int"
,
arg
)
}
return
val
,
nil
case
"string"
:
return
arg
,
nil
case
"bytes"
:
return
hexutil
.
Decode
(
arg
)
default
:
return
nil
,
fmt
.
Errorf
(
"unknown type: %s"
,
typ
)
}
return
nil
}
// createContractInput converts an abi.Argument to one or more ContractInputs.
...
...
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