Skip to content

Commit

Permalink
push duplicated logic to helper
Browse files Browse the repository at this point in the history
  • Loading branch information
rianhughes committed Mar 8, 2024
1 parent e658126 commit ae86272
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 20 deletions.
13 changes: 3 additions & 10 deletions rpc/types_transaction.go
Original file line number Diff line number Diff line change
Expand Up @@ -255,16 +255,9 @@ func (txn *UnknownTransaction) UnmarshalJSON(data []byte) error {
return err
}
// BlockWithReceipts swrap transaction in the Transaction field.
if dec["Transaction"] != nil {
var decInner map[string]interface{}
txnInner, err := json.Marshal(dec["Transaction"])
if err != nil {
return err
}
if err := json.Unmarshal(txnInner, &decInner); err != nil {
return err
}
dec = decInner
dec, err := utils.UnwrapJSON(dec, "Transaction")
if err != nil {
return err
}

t, err := unmarshalTxn(dec)
Expand Down
14 changes: 4 additions & 10 deletions rpc/types_transaction_receipt.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"strconv"

"github.com/NethermindEth/juno/core/felt"
"github.com/NethermindEth/starknet.go/utils"
)

type FeePayment struct {
Expand Down Expand Up @@ -361,16 +362,9 @@ func (tr *UnknownTransactionReceipt) UnmarshalJSON(data []byte) error {
}

// BlockWithReceipts wrap receipts in the TransactionReceipt field.
if dec["TransactionReceipt"] != nil {
var decInner map[string]interface{}
recInner, err := json.Marshal(dec["TransactionReceipt"])
if err != nil {
return err
}
if err := json.Unmarshal(recInner, &decInner); err != nil {
return err
}
dec = decInner
dec, err := utils.UnwrapJSON(dec, "TransactionReceipt")
if err != nil {
return err
}

t, err := unmarshalTransactionReceipt(dec)
Expand Down
18 changes: 18 additions & 0 deletions utils/data.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package utils

import "encoding/json"

func UnwrapJSON(data map[string]interface{}, tag string) (map[string]interface{}, error) {
if data[tag] != nil {
var unwrappedData map[string]interface{}
dataInner, err := json.Marshal(data[tag])
if err != nil {
return nil, err
}
if err := json.Unmarshal(dataInner, &unwrappedData); err != nil {
return nil, err
}
return unwrappedData, nil
}
return data, nil
}

0 comments on commit ae86272

Please sign in to comment.