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
6bace497
Unverified
Commit
6bace497
authored
Oct 30, 2023
by
Ethen Pociask
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
[indexer.api.add_withdrawal_hash] Added cross-domain msg hash to API response
parent
19fc0203
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
96 additions
and
30 deletions
+96
-30
models.go
indexer/api/models/models.go
+31
-0
models_test.go
indexer/api/models/models_test.go
+64
-0
withdrawals.go
indexer/api/routes/withdrawals.go
+1
-30
No files found.
indexer/api/models/models.go
View file @
6bace497
package
models
import
"github.com/ethereum-optimism/optimism/indexer/database"
// DepositItem ... Deposit item model for API responses
type
DepositItem
struct
{
Guid
string
`json:"guid"`
...
...
@@ -43,3 +45,32 @@ type WithdrawalResponse struct {
HasNextPage
bool
`json:"hasNextPage"`
Items
[]
WithdrawalItem
`json:"items"`
}
// FIXME make a pure function that returns a struct instead of newWithdrawalResponse
// newWithdrawalResponse ... Converts a database.L2BridgeWithdrawalsResponse to an api.WithdrawalResponse
func
CreateWithdrawalResponse
(
withdrawals
*
database
.
L2BridgeWithdrawalsResponse
)
WithdrawalResponse
{
items
:=
make
([]
WithdrawalItem
,
len
(
withdrawals
.
Withdrawals
))
for
i
,
withdrawal
:=
range
withdrawals
.
Withdrawals
{
item
:=
WithdrawalItem
{
Guid
:
withdrawal
.
L2BridgeWithdrawal
.
TransactionWithdrawalHash
.
String
(),
L2BlockHash
:
withdrawal
.
L2BlockHash
.
String
(),
Timestamp
:
withdrawal
.
L2BridgeWithdrawal
.
Tx
.
Timestamp
,
From
:
withdrawal
.
L2BridgeWithdrawal
.
Tx
.
FromAddress
.
String
(),
To
:
withdrawal
.
L2BridgeWithdrawal
.
Tx
.
ToAddress
.
String
(),
TransactionHash
:
withdrawal
.
L2TransactionHash
.
String
(),
Amount
:
withdrawal
.
L2BridgeWithdrawal
.
Tx
.
Amount
.
String
(),
MessageHash
:
withdrawal
.
L2BridgeWithdrawal
.
CrossDomainMessageHash
.
String
(),
ProofTransactionHash
:
withdrawal
.
ProvenL1TransactionHash
.
String
(),
ClaimTransactionHash
:
withdrawal
.
FinalizedL1TransactionHash
.
String
(),
L1TokenAddress
:
withdrawal
.
L2BridgeWithdrawal
.
TokenPair
.
RemoteTokenAddress
.
String
(),
L2TokenAddress
:
withdrawal
.
L2BridgeWithdrawal
.
TokenPair
.
LocalTokenAddress
.
String
(),
}
items
[
i
]
=
item
}
return
WithdrawalResponse
{
Cursor
:
withdrawals
.
Cursor
,
HasNextPage
:
withdrawals
.
HasNextPage
,
Items
:
items
,
}
}
indexer/api/models/models_test.go
0 → 100644
View file @
6bace497
package
models_test
import
(
"fmt"
"reflect"
"testing"
"github.com/ethereum-optimism/optimism/indexer/api/models"
"github.com/ethereum-optimism/optimism/indexer/database"
"github.com/ethereum/go-ethereum/common"
"github.com/stretchr/testify/require"
)
func
TestCreateWithdrawal
(
t
*
testing
.
T
)
{
// (1) Create a dummy database response object
cdh
:=
common
.
HexToHash
(
"0x2"
)
dbWithdrawals
:=
&
database
.
L2BridgeWithdrawalsResponse
{
Withdrawals
:
[]
database
.
L2BridgeWithdrawalWithTransactionHashes
{
{
L2BridgeWithdrawal
:
database
.
L2BridgeWithdrawal
{
TransactionWithdrawalHash
:
common
.
HexToHash
(
"0x1"
),
BridgeTransfer
:
database
.
BridgeTransfer
{
CrossDomainMessageHash
:
&
cdh
,
Tx
:
database
.
Transaction
{
FromAddress
:
common
.
HexToAddress
(
"0x3"
),
ToAddress
:
common
.
HexToAddress
(
"0x4"
),
Timestamp
:
5
,
},
TokenPair
:
database
.
TokenPair
{
LocalTokenAddress
:
common
.
HexToAddress
(
"0x6"
),
RemoteTokenAddress
:
common
.
HexToAddress
(
"0x7"
),
},
},
},
},
},
}
// (2) Create and validate response object
response
:=
models
.
CreateWithdrawalResponse
(
dbWithdrawals
)
require
.
NotEmpty
(
t
,
response
.
Items
)
require
.
Len
(
t
,
response
.
Items
,
1
)
// (3) Use reflection to check that all fields in WithdrawalItem are populated correctly
item
:=
response
.
Items
[
0
]
structType
:=
reflect
.
TypeOf
(
item
)
structVal
:=
reflect
.
ValueOf
(
item
)
fieldNum
:=
structVal
.
NumField
()
for
i
:=
0
;
i
<
fieldNum
;
i
++
{
field
:=
structVal
.
Field
(
i
)
fieldName
:=
structType
.
Field
(
i
)
.
Name
isSet
:=
field
.
IsValid
()
&&
!
field
.
IsZero
()
require
.
True
(
t
,
isSet
,
fmt
.
Sprintf
(
"%s in not set"
,
fieldName
))
}
}
indexer/api/routes/withdrawals.go
View file @
6bace497
...
...
@@ -4,38 +4,9 @@ import (
"net/http"
"github.com/ethereum-optimism/optimism/indexer/api/models"
"github.com/ethereum-optimism/optimism/indexer/database"
"github.com/go-chi/chi/v5"
)
// FIXME make a pure function that returns a struct instead of newWithdrawalResponse
// newWithdrawalResponse ... Converts a database.L2BridgeWithdrawalsResponse to an api.WithdrawalResponse
func
newWithdrawalResponse
(
withdrawals
*
database
.
L2BridgeWithdrawalsResponse
)
models
.
WithdrawalResponse
{
items
:=
make
([]
models
.
WithdrawalItem
,
len
(
withdrawals
.
Withdrawals
))
for
i
,
withdrawal
:=
range
withdrawals
.
Withdrawals
{
item
:=
models
.
WithdrawalItem
{
Guid
:
withdrawal
.
L2BridgeWithdrawal
.
TransactionWithdrawalHash
.
String
(),
L2BlockHash
:
withdrawal
.
L2BlockHash
.
String
(),
Timestamp
:
withdrawal
.
L2BridgeWithdrawal
.
Tx
.
Timestamp
,
From
:
withdrawal
.
L2BridgeWithdrawal
.
Tx
.
FromAddress
.
String
(),
To
:
withdrawal
.
L2BridgeWithdrawal
.
Tx
.
ToAddress
.
String
(),
TransactionHash
:
withdrawal
.
L2TransactionHash
.
String
(),
Amount
:
withdrawal
.
L2BridgeWithdrawal
.
Tx
.
Amount
.
String
(),
ProofTransactionHash
:
withdrawal
.
ProvenL1TransactionHash
.
String
(),
ClaimTransactionHash
:
withdrawal
.
FinalizedL1TransactionHash
.
String
(),
L1TokenAddress
:
withdrawal
.
L2BridgeWithdrawal
.
TokenPair
.
RemoteTokenAddress
.
String
(),
L2TokenAddress
:
withdrawal
.
L2BridgeWithdrawal
.
TokenPair
.
LocalTokenAddress
.
String
(),
}
items
[
i
]
=
item
}
return
models
.
WithdrawalResponse
{
Cursor
:
withdrawals
.
Cursor
,
HasNextPage
:
withdrawals
.
HasNextPage
,
Items
:
items
,
}
}
// L2WithdrawalsHandler ... Handles /api/v0/withdrawals/{address} GET requests
func
(
h
Routes
)
L2WithdrawalsHandler
(
w
http
.
ResponseWriter
,
r
*
http
.
Request
)
{
addressValue
:=
chi
.
URLParam
(
r
,
"address"
)
...
...
@@ -69,7 +40,7 @@ func (h Routes) L2WithdrawalsHandler(w http.ResponseWriter, r *http.Request) {
h
.
logger
.
Error
(
"Unable to read withdrawals from DB"
,
"err"
,
err
.
Error
())
return
}
response
:=
new
WithdrawalResponse
(
withdrawals
)
response
:=
models
.
Create
WithdrawalResponse
(
withdrawals
)
err
=
jsonResponse
(
w
,
response
,
http
.
StatusOK
)
if
err
!=
nil
{
...
...
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