[wallet] Add wallet_getChachedBalances method

This commit is contained in:
Roman Volosovskyi 2021-05-20 15:11:18 +03:00
parent 71f66f6806
commit d1dc5f1e27
No known key found for this signature in database
GPG Key ID: 0238A4B5ECEE70DE
5 changed files with 75 additions and 28 deletions

View File

@ -1 +1 @@
0.79.3 0.79.4

View File

@ -74,11 +74,11 @@ func LinkPreviewWhitelist() []Site {
Address: "youtu.be", Address: "youtu.be",
ImageSite: false, ImageSite: false,
}, },
Site{ // Site{
Title: "Tenor GIFs", // Title: "Tenor GIFs",
Address: "tenor.com", // Address: "tenor.com",
ImageSite: true, // ImageSite: true,
}, // },
Site{ Site{
Title: "GIPHY GIFs shortener", Title: "GIPHY GIFs shortener",
Address: "gph.is", Address: "gph.is",

View File

@ -97,28 +97,28 @@ func TestGetGiphyShortURLPreviewData(t *testing.T) {
require.Equal(t, bostonDynamicsEthGifData.Title, previewData.Title) require.Equal(t, bostonDynamicsEthGifData.Title, previewData.Title)
} }
func TestGetTenorPreviewData(t *testing.T) { // func TestGetTenorPreviewData(t *testing.T) {
validTenorLink := "https://tenor.com/view/robot-dance-do-you-love-me-boston-boston-dynamics-dance-gif-19998728" // validTenorLink := "https://tenor.com/view/robot-dance-do-you-love-me-boston-boston-dynamics-dance-gif-19998728"
previewData, err := GetTenorPreviewData(validTenorLink) // previewData, err := GetTenorPreviewData(validTenorLink)
gifData := LinkPreviewData{ // gifData := LinkPreviewData{
Site: "Tenor", // Site: "Tenor",
Title: "Annihere", // Title: "Annihere",
ThumbnailURL: "https://media.tenor.com/images/975f6b95d188c277ebba62d9b5511685/tenor.gif", // ThumbnailURL: "https://media.tenor.com/images/975f6b95d188c277ebba62d9b5511685/tenor.gif",
Height: 400, // Height: 400,
Width: 600, // Width: 600,
} // }
require.NoError(t, err) // require.NoError(t, err)
require.Equal(t, gifData.Site, previewData.Site) // require.Equal(t, gifData.Site, previewData.Site)
require.Equal(t, gifData.Title, previewData.Title) // require.Equal(t, gifData.Title, previewData.Title)
require.Equal(t, gifData.ThumbnailURL, previewData.ThumbnailURL) // require.Equal(t, gifData.ThumbnailURL, previewData.ThumbnailURL)
require.Equal(t, gifData.Height, previewData.Height) // require.Equal(t, gifData.Height, previewData.Height)
require.Equal(t, gifData.Width, previewData.Width) // require.Equal(t, gifData.Width, previewData.Width)
invalidTenorLink := "https://giphy.com/gifs/this-gif-does-not-exist-44444" // invalidTenorLink := "https://giphy.com/gifs/this-gif-does-not-exist-44444"
_, err = GetTenorPreviewData(invalidTenorLink) // _, err = GetTenorPreviewData(invalidTenorLink)
require.Error(t, err) // require.Error(t, err)
} // }
func TestStatusLinkPreviewData(t *testing.T) { func TestStatusLinkPreviewData(t *testing.T) {

View File

@ -238,3 +238,34 @@ func (api *API) CheckRecentHistory(ctx context.Context, addresses []common.Addre
addresses, addresses,
new(big.Int).SetUint64(api.s.db.network)) new(big.Int).SetUint64(api.s.db.network))
} }
type LastKnownBlockView struct {
Address common.Address `json:"address"`
Number *big.Int `json:"blockNumber"`
Balance BigInt `json:"balance"`
Nonce *int64 `json:"nonce"`
}
func blocksToViews(blocks map[common.Address]*LastKnownBlock) []LastKnownBlockView {
blocksViews := []LastKnownBlockView{}
for address, block := range blocks {
view := LastKnownBlockView{
Address: address,
Number: block.Number,
Balance: BigInt{block.Balance},
Nonce: block.Nonce,
}
blocksViews = append(blocksViews, view)
}
return blocksViews
}
func (api *API) GetCachedBalances(ctx context.Context, addresses []common.Address) ([]LastKnownBlockView, error) {
result, error := api.s.db.getLastKnownBalances(addresses)
if error != nil {
return nil, error
}
return blocksToViews(result), nil
}

View File

@ -433,6 +433,22 @@ func (db *Database) GetLastKnownBlockByAddress(address common.Address) (block *L
return nil, nil return nil, nil
} }
func (db *Database) getLastKnownBalances(addresses []common.Address) (map[common.Address]*LastKnownBlock, error) {
result := map[common.Address]*LastKnownBlock{}
for _, address := range addresses {
block, error := db.GetLastKnownBlockByAddress(address)
if error != nil {
return nil, error
}
if block != nil {
result[address] = block
}
}
return result, nil
}
func (db *Database) GetLastKnownBlockByAddresses(addresses []common.Address) (map[common.Address]*LastKnownBlock, []common.Address, error) { func (db *Database) GetLastKnownBlockByAddresses(addresses []common.Address) (map[common.Address]*LastKnownBlock, []common.Address, error) {
res := map[common.Address]*LastKnownBlock{} res := map[common.Address]*LastKnownBlock{}
accountsWithoutHistory := []common.Address{} accountsWithoutHistory := []common.Address{}
@ -785,8 +801,8 @@ func insertBlocksWithTransactions(creator statementCreator, account common.Addre
if err != nil { if err != nil {
return err return err
} }
updateTx, err := creator.Prepare(`UPDATE transfers updateTx, err := creator.Prepare(`UPDATE transfers
SET log = ? SET log = ?
WHERE network_id = ? AND address = ? AND hash = ?`) WHERE network_id = ? AND address = ? AND hash = ?`)
if err != nil { if err != nil {
return err return err