• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

unpackdev / solgo / 8465579594

28 Mar 2024 10:03AM UTC coverage: 68.339% (-0.3%) from 68.666%
8465579594

Pull #163

github

0x19
Introducing log, tx decoder including fixes for constructor
Pull Request #163: Bytecode package: Log, Tx decoder incl. fixes for contract

4 of 185 new or added lines in 4 files covered. (2.16%)

2 existing lines in 1 file now uncovered.

26677 of 39036 relevant lines covered (68.34%)

0.75 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

0.0
/bytecode/transaction.go
1
package bytecode
2

3
import (
4
        "bytes"
5
        "fmt"
6
        "strings"
7

8
        abi "github.com/ethereum/go-ethereum/accounts/abi"
9
        "github.com/unpackdev/solgo/utils"
10
)
11

12
// Transaction encapsulates a decoded Ethereum transaction, providing detailed information
13
// about the transaction's method, its arguments, and the associated ABI.
14
// This structured format makes it easier to work with Ethereum transactions programmatically.
15
type Transaction struct {
16
        Abi            string                      `json:"abi"`             // ABI string of the transaction's method.
17
        SignatureBytes []byte                      `json:"signature_bytes"` // Raw signature bytes of the transaction.
18
        Signature      string                      `json:"signature"`       // Human-readable signature of the transaction's method.
19
        Type           utils.TransactionMethodType `json:"type"`            // Type of the transaction, classified by its method name.
20
        Name           string                      `json:"name"`            // Name of the transaction's method.
21
        Method         *abi.Method                 `json:"-"`               // ABI method information, not serialized to JSON.
22
        Inputs         map[string]interface{}      `json:"inputs"`          // Decoded arguments passed to the transaction's method.
23
}
24

25
// DecodeTransactionFromAbi decodes an Ethereum transaction using the provided ABI.
26
// It extracts the method signature and arguments from the raw transaction data, constructing
27
// a Transaction object that includes this information along with the method's ABI.
28
//
29
// The function requires the raw transaction data (`data`) and the ABI of the smart contract
30
// (`abiData`) in JSON format. It returns a pointer to a Transaction object, populated with
31
// the decoded method information and its arguments, or an error if decoding fails.
32
//
33
// This function simplifies the process of interacting with raw Ethereum transactions, making
34
// it easier to analyze and use the transaction data programmatically.
NEW
35
func DecodeTransactionFromAbi(data []byte, abiData []byte) (*Transaction, error) {
×
NEW
36
        // The first 4 bytes of the data represent the ID of the method in the ABI.
×
NEW
37
        methodSigData := data[:4]
×
NEW
38

×
NEW
39
        contractABI, err := abi.JSON(bytes.NewReader(abiData))
×
NEW
40
        if err != nil {
×
NEW
41
                return nil, fmt.Errorf("failed to parse abi: %s", err)
×
NEW
42
        }
×
43

NEW
44
        method, err := contractABI.MethodById(methodSigData)
×
NEW
45
        if err != nil {
×
NEW
46
                return nil, fmt.Errorf("failed to get method by id: %s", err)
×
NEW
47
        }
×
48

NEW
49
        inputsSigData := data[4:]
×
NEW
50
        inputsMap := make(map[string]interface{})
×
NEW
51
        if err := method.Inputs.UnpackIntoMap(inputsMap, inputsSigData); err != nil {
×
NEW
52
                return nil, fmt.Errorf("failed to unpack inputs into map: %s", err)
×
NEW
53
        }
×
54

NEW
55
        txAbi, err := utils.MethodToABI(method)
×
NEW
56
        if err != nil {
×
NEW
57
                return nil, fmt.Errorf("failure to cast method into abi: %w", err)
×
NEW
58
        }
×
59

NEW
60
        return &Transaction{
×
NEW
61
                Abi:            txAbi,
×
NEW
62
                SignatureBytes: methodSigData,
×
NEW
63
                Signature:      method.String(),
×
NEW
64
                Name:           method.Name,
×
NEW
65
                Method:         method,
×
NEW
66
                Type:           utils.TransactionMethodType(strings.ToLower(method.Name)),
×
NEW
67
                Inputs:         inputsMap,
×
NEW
68
        }, nil
×
69
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc