Skip to content

Commit

Permalink
Add cache to GetContractCurrency (#107)
Browse files Browse the repository at this point in the history
  • Loading branch information
xiangliu-cb committed Oct 27, 2023
1 parent a9e6a46 commit 18f7c95
Showing 1 changed file with 17 additions and 6 deletions.
23 changes: 17 additions & 6 deletions services/block_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,16 +104,27 @@ func (s *BlockAPIService) PopulateTransaction(
if tx.Receipt != nil {
receiptLogs = tx.Receipt.Logs
}

// Compute tx operations via tx.Receipt logs for ERC20 transfer, mint and burn
var contractCurrencyMap = make(map[string]*client.ContractCurrency)
for _, log := range receiptLogs {
// if Filter == false, we record every ERC20 tokens
if !s.client.GetRosettaConfig().FilterTokens || (s.client.GetRosettaConfig().FilterTokens &&
client.IsValidERC20Token(s.client.GetRosettaConfig().TokenWhiteList, log.Address.String())) {
if !s.client.GetRosettaConfig().FilterTokens ||
(s.client.GetRosettaConfig().FilterTokens &&
client.IsValidERC20Token(s.client.GetRosettaConfig().TokenWhiteList, log.Address.String())) {

switch len(log.Topics) {
case TopicsInErc20DepositOrWithdrawal, TopicsInErc20Transfer:
currency, err := s.client.GetContractCurrency(log.Address, true)
if err != nil {
return nil, err
addressStr := log.Address.String()
var currency *client.ContractCurrency
var err error

if cachedCurrency, found := contractCurrencyMap[addressStr]; found {
currency = cachedCurrency
} else {
if currency, err = s.client.GetContractCurrency(log.Address, true); err != nil {
return nil, err
}
contractCurrencyMap[addressStr] = currency
}

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

0 comments on commit 18f7c95

Please sign in to comment.