-
Notifications
You must be signed in to change notification settings - Fork 25
API 54/search types #1679
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
API 54/search types #1679
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
8742e0f
refactor: move search handler funcs into `searchTypeMap`
LuccaBitfly cfc3fc4
feat: add search for address, transaction, block, epoch and token
LuccaBitfly 2934d32
feat: add search ts types for address, transaction, block, epoch and …
LuccaBitfly 48cef6b
feat: sort user dashboards in response
LuccaBitfly File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -2,11 +2,15 @@ package dataaccess | |
|
|
||
| import ( | ||
| "context" | ||
| "database/sql" | ||
| "fmt" | ||
| "strings" | ||
|
|
||
| "github.com/doug-martin/goqu/v9" | ||
| "github.com/ethereum/go-ethereum/common/hexutil" | ||
| t "github.com/gobitfly/beaconchain/pkg/api/types" | ||
| "github.com/gobitfly/beaconchain/pkg/commons/db" | ||
| "github.com/pkg/errors" | ||
| ) | ||
|
|
||
| type SearchRepository interface { | ||
|
|
@@ -18,6 +22,11 @@ type SearchRepository interface { | |
| GetSearchValidatorsByWithdrawalEnsName(ctx context.Context, chainId uint64, ensName string) (*t.SearchValidatorsByWithdrawalCredential, error) | ||
| GetSearchValidatorsByGraffiti(ctx context.Context, chainId uint64, graffiti string) (*t.SearchValidatorsByGraffiti, error) | ||
| GetSearchValidatorsByGraffitiHex(ctx context.Context, chainId uint64, graffiti []byte) (*t.SearchValidatorsByGraffiti, error) | ||
| GetSearchAddress(ctx context.Context, chainId uint64, address []byte) (*t.SearchAddress, error) | ||
| GetSearchTransaction(ctx context.Context, chainId uint64, transactionHash []byte) (*t.SearchTransaction, error) | ||
| GetSearchBlock(ctx context.Context, chainId uint64, blockNumber uint64) (*t.SearchBlock, error) | ||
| GetSearchEpoch(ctx context.Context, chainId uint64, epoch uint64) (*t.SearchEpoch, error) | ||
| GetSearchToken(ctx context.Context, chainId uint64, address []byte) (*t.SearchToken, error) | ||
| } | ||
|
|
||
| func (d *DataAccessService) GetSearchValidatorByIndex(ctx context.Context, chainId, index uint64) (*t.SearchValidator, error) { | ||
|
|
@@ -131,3 +140,91 @@ func (d *DataAccessService) GetSearchValidatorsByGraffitiHex(ctx context.Context | |
| } | ||
| return ret, nil | ||
| } | ||
|
|
||
| func (d *DataAccessService) GetSearchAddress(ctx context.Context, chainId uint64, address []byte) (*t.SearchAddress, error) { | ||
| eth1AddressSearchItem, err := d.bigtable.SearchForAddress(address, 1) | ||
| if err != nil { | ||
| return nil, fmt.Errorf("failed to search for address %s: %w", hexutil.Encode(address), err) | ||
| } | ||
| if len(eth1AddressSearchItem) == 0 || eth1AddressSearchItem[0] == nil { | ||
| return nil, ErrNotFound | ||
| } | ||
| foundAddress := *eth1AddressSearchItem[0] | ||
| return &t.SearchAddress{ | ||
| Address: t.Address{ | ||
| Hash: t.Hash("0x" + foundAddress.Address), | ||
| // TODO: implement finding contract status / name | ||
| }, | ||
| }, nil | ||
| } | ||
|
|
||
| func (d *DataAccessService) GetSearchTransaction(ctx context.Context, chainId uint64, transactionHash []byte) (*t.SearchTransaction, error) { | ||
| tx, err := db.BigtableClient.GetIndexedEth1Transaction(transactionHash) | ||
| if err != nil { | ||
| return nil, fmt.Errorf("failed to search for transaction %s: %w", hexutil.Encode(transactionHash), err) | ||
| } | ||
| if tx == nil { | ||
| return nil, ErrNotFound | ||
| } | ||
| return &t.SearchTransaction{ | ||
| TransactionHash: t.Hash(hexutil.Encode(tx.Hash)), | ||
| }, nil | ||
| } | ||
|
|
||
| func (d *DataAccessService) GetSearchBlock(ctx context.Context, chainId uint64, blockNumber uint64) (*t.SearchBlock, error) { | ||
| block, err := db.BigtableClient.GetBlockFromBlocksTable(blockNumber) | ||
| if err != nil { | ||
| if err == db.ErrBlockNotFound { | ||
| return nil, ErrNotFound | ||
| } | ||
| return nil, fmt.Errorf("failed to search for block %d: %w", blockNumber, err) | ||
| } | ||
| if block == nil { | ||
| return nil, fmt.Errorf("nil block returned for block number %d", blockNumber) | ||
| } | ||
| return &t.SearchBlock{ | ||
| BlockNumber: block.Number, | ||
| }, nil | ||
| } | ||
|
|
||
| func (d *DataAccessService) GetSearchEpoch(ctx context.Context, chainId uint64, epoch uint64) (*t.SearchEpoch, error) { | ||
| ds := goqu.Dialect("postgres"). | ||
| From("epochs"). | ||
| Select(goqu.I("epoch")). | ||
| Where(goqu.I("epoch").Eq(epoch)) | ||
|
|
||
| foundEpoch, err := runQuery[uint64](ctx, d.readerDb, ds) | ||
| if err != nil { | ||
| if errors.Is(err, sql.ErrNoRows) { | ||
| return nil, ErrNotFound | ||
| } | ||
| return nil, err | ||
| } | ||
| return &t.SearchEpoch{ | ||
| Epoch: foundEpoch, | ||
| }, nil | ||
| } | ||
|
|
||
| func (d *DataAccessService) GetSearchToken(ctx context.Context, chainId uint64, address []byte) (*t.SearchToken, error) { | ||
| // TODO: find erc721 and erc1155 tokens | ||
| // currently only erc20 tokens supported | ||
| eth1AddressSearchItem, err := d.bigtable.SearchForAddress(address, 1) | ||
| if err != nil { | ||
| return nil, fmt.Errorf("failed to search for address %s: %w", hexutil.Encode(address), err) | ||
| } | ||
| if len(eth1AddressSearchItem) == 0 || eth1AddressSearchItem[0] == nil { | ||
| return nil, ErrNotFound | ||
| } | ||
| foundAddress := *eth1AddressSearchItem[0] | ||
| if foundAddress.Token == "" { | ||
| return nil, ErrNotFound | ||
| } | ||
| return &t.SearchToken{ | ||
| Address: t.Address{ | ||
| Hash: t.Hash("0x" + foundAddress.Address), | ||
| IsContract: true, | ||
| Label: foundAddress.Name, | ||
| }, | ||
| Token: foundAddress.Token, | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nitpick: The bigtable func will return
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. fixed, ptal |
||
| }, nil | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.