Skip to content

Commit 18f7c95

Browse files
committed
Add cache to GetContractCurrency (#107)
1 parent a9e6a46 commit 18f7c95

1 file changed

Lines changed: 17 additions & 6 deletions

File tree

services/block_service.go

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -104,16 +104,27 @@ func (s *BlockAPIService) PopulateTransaction(
104104
if tx.Receipt != nil {
105105
receiptLogs = tx.Receipt.Logs
106106
}
107+
107108
// Compute tx operations via tx.Receipt logs for ERC20 transfer, mint and burn
109+
var contractCurrencyMap = make(map[string]*client.ContractCurrency)
108110
for _, log := range receiptLogs {
109-
// if Filter == false, we record every ERC20 tokens
110-
if !s.client.GetRosettaConfig().FilterTokens || (s.client.GetRosettaConfig().FilterTokens &&
111-
client.IsValidERC20Token(s.client.GetRosettaConfig().TokenWhiteList, log.Address.String())) {
111+
if !s.client.GetRosettaConfig().FilterTokens ||
112+
(s.client.GetRosettaConfig().FilterTokens &&
113+
client.IsValidERC20Token(s.client.GetRosettaConfig().TokenWhiteList, log.Address.String())) {
114+
112115
switch len(log.Topics) {
113116
case TopicsInErc20DepositOrWithdrawal, TopicsInErc20Transfer:
114-
currency, err := s.client.GetContractCurrency(log.Address, true)
115-
if err != nil {
116-
return nil, err
117+
addressStr := log.Address.String()
118+
var currency *client.ContractCurrency
119+
var err error
120+
121+
if cachedCurrency, found := contractCurrencyMap[addressStr]; found {
122+
currency = cachedCurrency
123+
} else {
124+
if currency, err = s.client.GetContractCurrency(log.Address, true); err != nil {
125+
return nil, err
126+
}
127+
contractCurrencyMap[addressStr] = currency
117128
}
118129

119130
if currency.Symbol == client.UnknownERC20Symbol && !s.config.RosettaCfg.IndexUnknownTokens {

0 commit comments

Comments
 (0)