Skip to content

Commit 893fe93

Browse files
author
=
committed
rf: move abi cache to abi util
1 parent 5d55354 commit 893fe93

File tree

3 files changed

+19
-27
lines changed

3 files changed

+19
-27
lines changed

internal/common/abi.go

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,21 @@ import (
1111

1212
var contractApi string = "https://contract.thirdweb.com"
1313

14+
func GetABIForContractWithCache(chainId string, contract string, abiCache map[string]*abi.ABI) *abi.ABI {
15+
abi, ok := abiCache[contract]
16+
if !ok {
17+
abiResult, err := GetABIForContract(chainId, contract)
18+
if err != nil {
19+
abiCache[contract] = nil
20+
return nil
21+
} else {
22+
abiCache[contract] = abiResult
23+
abi = abiResult
24+
}
25+
}
26+
return abi
27+
}
28+
1429
func GetABIForContract(chainId string, contract string) (*abi.ABI, error) {
1530
url := fmt.Sprintf("%s/abi/%s/%s", contractApi, chainId, contract)
1631

internal/common/log.go

Lines changed: 2 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -42,22 +42,11 @@ type DecodedLog struct {
4242

4343
func DecodeLogs(chainId string, logs []Log) []*DecodedLog {
4444
decodedLogs := make([]*DecodedLog, len(logs))
45-
abis := make(map[string]*abi.ABI)
45+
abiCache := make(map[string]*abi.ABI)
4646

4747
decodeLogFunc := func(eventLog *Log) *DecodedLog {
4848
decodedLog := DecodedLog{Log: *eventLog}
49-
abi, ok := abis[eventLog.Address]
50-
if !ok {
51-
abiResult, err := GetABIForContract(chainId, eventLog.Address)
52-
if err != nil {
53-
abis[eventLog.Address] = nil
54-
return &decodedLog
55-
} else {
56-
abis[eventLog.Address] = abiResult
57-
}
58-
abi = abiResult
59-
}
60-
49+
abi := GetABIForContractWithCache(chainId, eventLog.Address, abiCache)
6150
if abi == nil {
6251
return &decodedLog
6352
}

internal/common/transaction.go

Lines changed: 2 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -55,22 +55,10 @@ type DecodedTransaction struct {
5555

5656
func DecodeTransactions(chainId string, txs []Transaction) []*DecodedTransaction {
5757
decodedTxs := make([]*DecodedTransaction, len(txs))
58-
abis := make(map[string]*abi.ABI)
59-
58+
abiCache := make(map[string]*abi.ABI)
6059
decodeTxFunc := func(transaction *Transaction) *DecodedTransaction {
6160
decodedTransaction := DecodedTransaction{Transaction: *transaction}
62-
abi, ok := abis[transaction.ToAddress]
63-
if !ok {
64-
abiResult, err := GetABIForContract(chainId, transaction.ToAddress)
65-
if err != nil {
66-
abis[transaction.ToAddress] = nil
67-
return &decodedTransaction
68-
} else {
69-
abis[transaction.ToAddress] = abiResult
70-
}
71-
abi = abiResult
72-
}
73-
61+
abi := GetABIForContractWithCache(chainId, transaction.ToAddress, abiCache)
7462
if abi == nil {
7563
return &decodedTransaction
7664
}

0 commit comments

Comments
 (0)