-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
retrieving address details (label, ens, ...) from single method
- Loading branch information
1 parent
2dc7f45
commit 873d06e
Showing
5 changed files
with
85 additions
and
26 deletions.
There are no files selected for viewing
This file contains 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 |
---|---|---|
@@ -0,0 +1,47 @@ | ||
package dataaccess | ||
|
||
import ( | ||
"context" | ||
|
||
"github.com/ethereum/go-ethereum/common/hexutil" | ||
"github.com/gobitfly/beaconchain/pkg/api/types" | ||
"github.com/gobitfly/beaconchain/pkg/commons/db" | ||
) | ||
|
||
func (d *DataAccessService) GetLabelsAndEnsForAddresses(ctx context.Context, addressMap map[string]*types.Address) error { | ||
addresses := make([][]byte, 0, len(addressMap)) | ||
ensMapping := make(map[string]string, len(addressMap)) | ||
for address, data := range addressMap { | ||
ensMapping[address] = "" | ||
add, err := hexutil.Decode(address) | ||
if err != nil { | ||
return err | ||
} | ||
addresses = append(addresses, add) | ||
if data == nil { | ||
addressMap[address] = &types.Address{Hash: types.Hash(address)} | ||
} | ||
} | ||
// determine ENS names | ||
if err := db.GetEnsNamesForAddresses(ensMapping); err != nil { | ||
return err | ||
} | ||
for address, ens := range ensMapping { | ||
addressMap[address].Ens = ens | ||
} | ||
|
||
// determine tags | ||
tags := []struct { | ||
Address []byte `db:"address"` | ||
Tag string `db:"tag"` | ||
}{} | ||
err := d.alloyReader.SelectContext(ctx, &tags, `SELECT address, tag FROM address_tags WHERE address = ANY($1)`, addresses) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
for _, tag := range tags { | ||
addressMap[hexutil.Encode(tag.Address)].Label = tag.Tag | ||
} | ||
return nil | ||
} |
This file contains 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 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 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
15 changes: 15 additions & 0 deletions
15
backend/pkg/commons/db/migrations/postgres/20240822134034_add_address_tags.sql
This file contains 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 |
---|---|---|
@@ -0,0 +1,15 @@ | ||
-- +goose Up | ||
-- +goose StatementBegin | ||
SELECT('up SQL query - create address_tags table'); | ||
CREATE TABLE address_tags ( | ||
address bytea NOT NULL UNIQUE, | ||
tag CHARACTER VARYING(100) NOT NULL, | ||
PRIMARY KEY (address, tag) | ||
); | ||
-- +goose StatementEnd | ||
|
||
-- +goose Down | ||
-- +goose StatementBegin | ||
SELECT('down SQL query - drop address_tags table'); | ||
DROP TABLE execution_payloads; | ||
-- +goose StatementEnd |