Skip to content

Commit 2afbf4e

Browse files
author
=
committed
wip: decode txs using contract api
1 parent 0cad690 commit 2afbf4e

File tree

3 files changed

+69
-1
lines changed

3 files changed

+69
-1
lines changed

internal/common/abi.go

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,30 @@ package common
22

33
import (
44
"fmt"
5+
"net/http"
56
"regexp"
67
"strings"
78

89
"github.com/ethereum/go-ethereum/accounts/abi"
910
)
1011

12+
var contractApi string = "https://contract.thirdweb.com"
13+
14+
func GetABIForContract(chainId string, contract string) (*abi.ABI, error) {
15+
url := fmt.Sprintf("%s/abi/%s/%s", contractApi, chainId, contract)
16+
17+
resp, err := http.Get(url)
18+
if err != nil {
19+
return nil, fmt.Errorf("failed to get contract abi: %v", err)
20+
}
21+
22+
abi, err := abi.JSON(resp.Body)
23+
if err != nil {
24+
return nil, fmt.Errorf("failed to load contract abi: %v", err)
25+
}
26+
return &abi, nil
27+
}
28+
1129
func ConstructEventABI(signature string) (*abi.Event, error) {
1230
// Regex to extract the event name and parameters
1331
regex := regexp.MustCompile(`^(\w+)\((.*)\)$`)

internal/common/transaction.go

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,55 @@ type DecodedTransaction struct {
5252
Decoded DecodedTransactionData `json:"decodedData"`
5353
}
5454

55+
func DecodeTransactions(chainId string, txs []Transaction) []*DecodedTransaction {
56+
decodedTxs := []*DecodedTransaction{}
57+
abis := make(map[string]*abi.ABI)
58+
59+
decodeTxFunc := func(transaction *Transaction) *DecodedTransaction {
60+
decodedTransaction := DecodedTransaction{Transaction: *transaction}
61+
abi, ok := abis[transaction.ToAddress]
62+
// ABI not found yet
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+
}
72+
73+
if abi == nil {
74+
return &decodedTransaction
75+
}
76+
77+
decodedData, err := hex.DecodeString(strings.TrimPrefix(transaction.Data, "0x"))
78+
if err != nil {
79+
return &decodedTransaction
80+
}
81+
82+
if len(decodedData) < 4 {
83+
return &decodedTransaction
84+
}
85+
methodID := decodedData[:4]
86+
method, err := abi.MethodById(methodID)
87+
if err != nil {
88+
log.Debug().Msgf("failed to get method by id: %v", err)
89+
return &decodedTransaction
90+
}
91+
if method == nil {
92+
return &decodedTransaction
93+
}
94+
return transaction.Decode(method)
95+
}
96+
97+
for _, transaction := range txs {
98+
decodedTx := decodeTxFunc(&transaction)
99+
decodedTxs = append(decodedTxs, decodedTx)
100+
}
101+
return decodedTxs
102+
}
103+
55104
func (t *Transaction) Decode(functionABI *abi.Method) *DecodedTransaction {
56105
decodedData, err := hex.DecodeString(strings.TrimPrefix(t.Data, "0x"))
57106
if err != nil {

internal/handlers/transactions_handlers.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -219,7 +219,8 @@ func handleTransactionsRequest(c *gin.Context, contractAddress, signature string
219219
}
220220
queryResult.Data = decodedTransactions
221221
} else {
222-
queryResult.Data = transactionsResult.Data
222+
decodedTransactions := common.DecodeTransactions(chainId.String(), transactionsResult.Data)
223+
queryResult.Data = decodedTransactions
223224
}
224225
queryResult.Meta.TotalItems = len(transactionsResult.Data)
225226
}

0 commit comments

Comments
 (0)