Skip to content

Commit

Permalink
including token scanning status on list tokens endpoint response
Browse files Browse the repository at this point in the history
  • Loading branch information
lucasmenendez committed Nov 7, 2023
1 parent 52ccb1d commit 22391d8
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 20 deletions.
42 changes: 39 additions & 3 deletions api/tokens.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ func (capi *census3API) getTokens(msg *api.APIdata, ctx *httprouter.HTTPContext)
// init response struct with the initial pagination information and empty
// list of tokens
tokensResponse := GetTokensResponse{
Tokens: []GetTokensItem{},
Tokens: []GetTokenResponse{},
Pagination: &Pagination{PageSize: pageSize},
}
rows, nextCursorRow, prevCursorRow := paginationToRequest(rows, dbPageSize, cursor, goForward)
Expand All @@ -105,16 +105,52 @@ func (capi *census3API) getTokens(msg *api.APIdata, ctx *httprouter.HTTPContext)
}
// parse results from database to the response format
for _, tokenData := range rows {
tokensResponse.Tokens = append(tokensResponse.Tokens, GetTokensItem{
// get last block with token information
atBlock, err := capi.db.QueriesRO.LastBlockByTokenID(internalCtx, tokenData.ID)
if err != nil {
if !errors.Is(err, sql.ErrNoRows) {
return ErrCantGetToken.WithErr(err)
}
atBlock = 0
}
// if the token is not synced, get the last block of the network to
// calculate the current scan progress
tokenProgress := 100
if !tokenData.Synced {
// get correct web3 uri provider
w3URI, exists := capi.w3p.URIByChainID(tokenData.ChainID)
if !exists {
return ErrChainIDNotSupported.With("chain ID not supported")
}
// get last block of the network, if something fails return progress 0
w3 := state.Web3{}
if err := w3.Init(internalCtx, w3URI, common.BytesToAddress(tokenData.ID),
state.TokenType(tokenData.TypeID)); err != nil {
return ErrInitializingWeb3.WithErr(err)
}
// fetch the last block header and calculate progress
lastBlockNumber, err := w3.LatestBlockNumber(internalCtx)
if err != nil {
return ErrCantGetLastBlockNumber.WithErr(err)
}
tokenProgress = int(float64(atBlock) / float64(lastBlockNumber) * 100)
}

tokensResponse.Tokens = append(tokensResponse.Tokens, GetTokenResponse{
ID: common.BytesToAddress(tokenData.ID).String(),
Type: state.TokenType(int(tokenData.TypeID)).String(),
Name: tokenData.Name,
StartBlock: tokenData.CreationBlock,
StartBlock: uint64(tokenData.CreationBlock),
Tags: tokenData.Tags,
Symbol: tokenData.Symbol,
ChainID: tokenData.ChainID,
ChainAddress: tokenData.ChainAddress,
ExternalID: tokenData.ExternalID,
Status: &GetTokenStatusResponse{
AtBlock: atBlock,
Synced: tokenData.Synced,
Progress: tokenProgress,
},
})
}
// encode the response and send it
Expand Down
16 changes: 2 additions & 14 deletions api/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,21 +49,9 @@ type GetTokenResponse struct {
ExternalID string `json:"externalID,omitempty"`
}

type GetTokensItem struct {
ID string `json:"ID"`
Type string `json:"type"`
StartBlock int64 `json:"startBlock"`
Name string `json:"name"`
Symbol string `json:"symbol"`
Tags string `json:"tags,omitempty"`
ChainID uint64 `json:"chainID"`
ChainAddress string `json:"chainAddress"`
ExternalID string `json:"externalID,omitempty"`
}

type GetTokensResponse struct {
Tokens []GetTokensItem `json:"tokens"`
Pagination *Pagination `json:"pagination"`
Tokens []GetTokenResponse `json:"tokens"`
Pagination *Pagination `json:"pagination"`
}

type TokenTypesResponse struct {
Expand Down
6 changes: 3 additions & 3 deletions scripts/initial-tokens/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,12 @@
**Create Python virtualenv and execute it**
```sh
python3 -m venv ./venv
source ./bin/activate
source ./venv/bin/activate
```

**Install requirements**
```sh
pip install -r requirements
pip install -r requirements.txt
```

**Execute the script**
Expand All @@ -26,7 +26,6 @@ Tokens (ERC20):
- UNI: 0x1f9840a85d5af5bf1d1762f925bdaddc4201f984 (mainnet)
- AAVE: 0x7Fc66500c84A76Ad7e9c93437bFc5Ac33E2DDaE9 (mainnet)
- COMP: 0xc00e94cb662c3520282e6f5717214004a7f26888 (mainnet)
- LAND: 0xF87E31492Faf9A91B02Ee0dEAAd50d51d56D5d4d (mainnet)
- ZRX: 0xe41d2489571d322189246dafa5ebde1f4699f498 (mainnet)
- MKR: 0x9f8f72aa9304c8b593d555f12ef6589cc3a579a2 (mainnet)
- FWB: 0x35bd01fc9d6d5d81ca9e055db88dc49aa2c699a8 (mainnet)
Expand All @@ -47,6 +46,7 @@ Tokens (ERC20):
NFTs:
- Decentraland (LAND) 0xF87E31492Faf9A91B02Ee0dEAAd50d51d56D5d4d (mainnet)
- BoredApeYachtClub (BAYC) 0xbc4ca0eda7647a8ab7c2061c2e118a18a936f13d
- CryptoPunks (Ͼ) 0xb47e3cd837dDF8e4c57F05d70Ab865de6e193BBB
- Azuki (AZUKI) 0xED5AF388653567Af2F388E6224dC7C4b3241C544
Expand Down
5 changes: 5 additions & 0 deletions scripts/initial-tokens/initial-tokens.json
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,11 @@
"type": "erc721",
"chainID": 1
},
{
"id": "0xF87E31492Faf9A91B02Ee0dEAAd50d51d56D5d4d",
"type": "erc721",
"chainID": 1
},
{
"type": "poap",
"chainID": 1,
Expand Down

0 comments on commit 22391d8

Please sign in to comment.