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
0d443a70
Commit
0d443a70
authored
Aug 06, 2023
by
Hamdi Allam
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
api module changes
parent
4acb9efa
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
52 additions
and
80 deletions
+52
-80
api.go
indexer/api/api.go
+13
-19
api_test.go
indexer/api/api_test.go
+39
-61
No files found.
indexer/api/api.go
View file @
0d443a70
...
...
@@ -16,17 +16,15 @@ type PaginationResponse struct {
HasNextPage
bool
`json:"hasNextPage"`
}
func
(
a
*
Api
)
DepositsHandler
(
w
http
.
ResponseWriter
,
r
*
http
.
Request
)
{
bv
:=
a
.
bridgeView
func
(
a
*
Api
)
L1DepositsHandler
(
w
http
.
ResponseWriter
,
r
*
http
.
Request
)
{
bv
:=
a
.
BridgeTransfersView
address
:=
common
.
HexToAddress
(
chi
.
URLParam
(
r
,
"address"
))
// limit := getIntFromQuery(r, "limit", 10)
// cursor := r.URL.Query().Get("cursor")
// sortDirection := r.URL.Query().Get("sortDirection")
deposits
,
err
:=
bv
.
DepositsByAddress
(
address
)
deposits
,
err
:=
bv
.
L1BridgeDepositsByAddress
(
address
)
if
err
!=
nil
{
http
.
Error
(
w
,
err
.
Error
(),
http
.
StatusInternalServerError
)
return
...
...
@@ -43,17 +41,15 @@ func (a *Api) DepositsHandler(w http.ResponseWriter, r *http.Request) {
jsonResponse
(
w
,
response
,
http
.
StatusOK
)
}
func
(
a
*
Api
)
WithdrawalsHandler
(
w
http
.
ResponseWriter
,
r
*
http
.
Request
)
{
bv
:=
a
.
bridgeView
func
(
a
*
Api
)
L2WithdrawalsHandler
(
w
http
.
ResponseWriter
,
r
*
http
.
Request
)
{
bv
:=
a
.
BridgeTransfersView
address
:=
common
.
HexToAddress
(
chi
.
URLParam
(
r
,
"address"
))
// limit := getIntFromQuery(r, "limit", 10)
// cursor := r.URL.Query().Get("cursor")
// sortDirection := r.URL.Query().Get("sortDirection")
withdrawals
,
err
:=
bv
.
WithdrawalsByAddress
(
address
)
withdrawals
,
err
:=
bv
.
L2BridgeWithdrawalsByAddress
(
address
)
if
err
!=
nil
{
http
.
Error
(
w
,
err
.
Error
(),
http
.
StatusInternalServerError
)
return
...
...
@@ -83,22 +79,20 @@ func jsonResponse(w http.ResponseWriter, data interface{}, statusCode int) {
}
type
Api
struct
{
Router
*
chi
.
Mux
bridgeView
database
.
Bridge
View
Router
*
chi
.
Mux
BridgeTransfersView
database
.
BridgeTransfers
View
}
func
NewApi
(
bv
database
.
BridgeView
)
*
Api
{
func
NewApi
(
bv
database
.
Bridge
Transfers
View
)
*
Api
{
r
:=
chi
.
NewRouter
()
api
:=
&
Api
{
Router
:
r
,
bridgeView
:
bv
,
}
api
:=
&
Api
{
Router
:
r
,
BridgeTransfersView
:
bv
}
// these regex are .+ because I wasn't sure what they should be
// don't want a regex for addresses because would prefer to validate the address
// with go-ethereum and throw a friendly error message
r
.
Get
(
"/api/v0/deposits/{address:.+}"
,
api
.
DepositsHandler
)
r
.
Get
(
"/api/v0/withdrawals/{address:.+}"
,
api
.
WithdrawalsHandler
)
r
.
Get
(
"/api/v0/deposits/{address:.+}"
,
api
.
L1
DepositsHandler
)
r
.
Get
(
"/api/v0/withdrawals/{address:.+}"
,
api
.
L2
WithdrawalsHandler
)
r
.
Get
(
"/healthz"
,
api
.
HealthzHandler
)
return
api
...
...
indexer/api/api_test.go
View file @
0d443a70
...
...
@@ -12,89 +12,67 @@ import (
"github.com/stretchr/testify/assert"
)
// MockBridge
View mocks the Bridge
View interface
type
MockBridgeView
struct
{}
// MockBridge
TransfersView mocks the BridgeTransfers
View interface
type
MockBridge
Transfers
View
struct
{}
const
(
guid1
=
"8408b6d2-7c90-4cfc-8604-b2204116cb6a"
guid2
=
"8408b6d2-7c90-4cfc-8604-b2204116cb6b"
)
// DepositsByAddress mocks returning deposits by an address
func
(
mbv
*
MockBridgeView
)
DepositsByAddress
(
address
common
.
Address
)
([]
*
database
.
DepositWithTransactionHashes
,
error
)
{
return
[]
*
database
.
DepositWithTransactionHashes
{
{
Deposit
:
database
.
Deposit
{
GUID
:
uuid
.
MustParse
(
guid1
),
InitiatedL1EventGUID
:
uuid
.
MustParse
(
guid2
),
Tx
:
database
.
Transaction
{},
TokenPair
:
database
.
TokenPair
{},
},
L1TransactionHash
:
common
.
HexToHash
(
"0x123"
),
},
},
nil
}
// DepositsByAddress mocks returning deposits by an address
func
(
mbv
*
MockBridgeView
)
DepositByMessageNonce
(
nonce
*
big
.
Int
)
(
*
database
.
Deposit
,
error
)
{
return
&
database
.
Deposit
{
var
(
deposit
=
database
.
L1BridgeDeposit
{
GUID
:
uuid
.
MustParse
(
guid1
),
InitiatedL1EventGUID
:
uuid
.
MustParse
(
guid2
),
Tx
:
database
.
Transaction
{},
TokenPair
:
database
.
TokenPair
{},
},
nil
}
withdrawal
=
database
.
L2BridgeWithdrawal
{
GUID
:
uuid
.
MustParse
(
guid2
),
InitiatedL2EventGUID
:
uuid
.
MustParse
(
guid1
),
WithdrawalHash
:
common
.
HexToHash
(
"0x456"
),
Tx
:
database
.
Transaction
{},
TokenPair
:
database
.
TokenPair
{},
}
)
func
(
mbv
*
MockBridgeTransfersView
)
L1BridgeDeposit
(
hash
common
.
Hash
)
(
*
database
.
L1BridgeDeposit
,
error
)
{
return
&
deposit
,
nil
}
// LatestDepositMessageNonce mocks returning the latest cross domain message nonce for a deposit
func
(
mbv
*
MockBridgeView
)
LatestDepositMessageNonce
()
(
*
big
.
Int
,
error
)
{
return
big
.
NewInt
(
0
),
nil
func
(
mbv
*
MockBridgeTransfersView
)
L1BridgeDepositByCrossDomainMessengerNonce
(
nonce
*
big
.
Int
)
(
*
database
.
L1BridgeDeposit
,
error
)
{
return
&
deposit
,
nil
}
// WithdrawalsByAddress mocks returning withdrawals by an address
func
(
mbv
*
MockBridgeView
)
WithdrawalsByAddress
(
address
common
.
Address
)
([]
*
database
.
WithdrawalWithTransactionHashes
,
error
)
{
return
[]
*
database
.
WithdrawalWithTransactionHashes
{
func
(
mbv
*
MockBridgeTransfersView
)
L1BridgeDepositsByAddress
(
address
common
.
Address
)
([]
*
database
.
L1BridgeDepositWithTransactionHashes
,
error
)
{
return
[]
*
database
.
L1BridgeDepositWithTransactionHashes
{
{
Withdrawal
:
database
.
Withdrawal
{
GUID
:
uuid
.
MustParse
(
guid2
),
InitiatedL2EventGUID
:
uuid
.
MustParse
(
guid1
),
WithdrawalHash
:
common
.
HexToHash
(
"0x456"
),
Tx
:
database
.
Transaction
{},
TokenPair
:
database
.
TokenPair
{},
},
L2TransactionHash
:
common
.
HexToHash
(
"0x789"
),
L1BridgeDeposit
:
deposit
,
L1TransactionHash
:
common
.
HexToHash
(
"0x123"
),
},
},
nil
}
// WithdrawalsByMessageNonce mocks returning withdrawals by a withdrawal hash
func
(
mbv
*
MockBridgeView
)
WithdrawalByMessageNonce
(
nonce
*
big
.
Int
)
(
*
database
.
Withdrawal
,
error
)
{
return
&
database
.
Withdrawal
{
GUID
:
uuid
.
MustParse
(
guid2
),
InitiatedL2EventGUID
:
uuid
.
MustParse
(
guid1
),
WithdrawalHash
:
common
.
HexToHash
(
"0x456"
),
Tx
:
database
.
Transaction
{},
TokenPair
:
database
.
TokenPair
{},
},
nil
func
(
mbv
*
MockBridgeTransfersView
)
L2BridgeWithdrawalByWithdrawalHash
(
address
common
.
Hash
)
(
*
database
.
L2BridgeWithdrawal
,
error
)
{
return
&
withdrawal
,
nil
}
// WithdrawalsByHash mocks returning withdrawals by a withdrawal hash
func
(
mbv
*
MockBridgeView
)
WithdrawalByHash
(
address
common
.
Hash
)
(
*
database
.
Withdrawal
,
error
)
{
return
&
database
.
Withdrawal
{
GUID
:
uuid
.
MustParse
(
guid2
),
InitiatedL2EventGUID
:
uuid
.
MustParse
(
guid1
),
WithdrawalHash
:
common
.
HexToHash
(
"0x456"
),
Tx
:
database
.
Transaction
{},
TokenPair
:
database
.
TokenPair
{},
},
nil
func
(
mbv
*
MockBridgeTransfersView
)
L2BridgeWithdrawalByCrossDomainMessengerNonce
(
nonce
*
big
.
Int
)
(
*
database
.
L2BridgeWithdrawal
,
error
)
{
return
&
withdrawal
,
nil
}
// LatestWithdrawalMessageNonce mocks returning the latest cross domain message nonce for a withdrawal
func
(
mbv
*
MockBridgeView
)
LatestWithdrawalMessageNonce
()
(
*
big
.
Int
,
error
)
{
return
big
.
NewInt
(
0
),
nil
func
(
mbv
*
MockBridgeTransfersView
)
L2BridgeWithdrawalsByAddress
(
address
common
.
Address
)
([]
*
database
.
L2BridgeWithdrawalWithTransactionHashes
,
error
)
{
return
[]
*
database
.
L2BridgeWithdrawalWithTransactionHashes
{
{
L2BridgeWithdrawal
:
withdrawal
,
L2TransactionHash
:
common
.
HexToHash
(
"0x789"
),
},
},
nil
}
func
TestHealthz
(
t
*
testing
.
T
)
{
api
:=
NewApi
(
&
MockBridgeView
{})
api
:=
NewApi
(
&
MockBridge
Transfers
View
{})
request
,
err
:=
http
.
NewRequest
(
"GET"
,
"/healthz"
,
nil
)
assert
.
Nil
(
t
,
err
)
...
...
@@ -104,8 +82,8 @@ func TestHealthz(t *testing.T) {
assert
.
Equal
(
t
,
http
.
StatusOK
,
responseRecorder
.
Code
)
}
func
TestDepositsHandler
(
t
*
testing
.
T
)
{
api
:=
NewApi
(
&
MockBridgeView
{})
func
Test
L1Bridge
DepositsHandler
(
t
*
testing
.
T
)
{
api
:=
NewApi
(
&
MockBridge
Transfers
View
{})
request
,
err
:=
http
.
NewRequest
(
"GET"
,
"/api/v0/deposits/0x123"
,
nil
)
assert
.
Nil
(
t
,
err
)
...
...
@@ -115,8 +93,8 @@ func TestDepositsHandler(t *testing.T) {
assert
.
Equal
(
t
,
http
.
StatusOK
,
responseRecorder
.
Code
)
}
func
Test
Withdrawal
sHandler
(
t
*
testing
.
T
)
{
api
:=
NewApi
(
&
MockBridgeView
{})
func
Test
L2BridgeWithdrawalsByAddres
sHandler
(
t
*
testing
.
T
)
{
api
:=
NewApi
(
&
MockBridge
Transfers
View
{})
request
,
err
:=
http
.
NewRequest
(
"GET"
,
"/api/v0/withdrawals/0x123"
,
nil
)
assert
.
Nil
(
t
,
err
)
...
...
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