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