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

lmittmann / w3 / 7640376565

24 Jan 2024 12:46PM UTC coverage: 77.601%. Remained the same
7640376565

Pull #101

github

web-flow
build(deps): bump github.com/ethereum/go-ethereum in /examples

Bumps [github.com/ethereum/go-ethereum](https://github.com/ethereum/go-ethereum) from 1.13.8 to 1.13.11.
- [Release notes](https://github.com/ethereum/go-ethereum/releases)
- [Commits](https://github.com/ethereum/go-ethereum/compare/v1.13.8...v1.13.11)

---
updated-dependencies:
- dependency-name: github.com/ethereum/go-ethereum
  dependency-type: direct:production
  update-type: version-update:semver-patch
...

Signed-off-by: dependabot[bot] <support@github.com>
Pull Request #101: build(deps): bump github.com/ethereum/go-ethereum from 1.13.8 to 1.13.11 in /examples

1805 of 2326 relevant lines covered (77.6%)

1420.88 hits per line

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

74.55
/func.go
1
package w3
2

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

8
        "github.com/ethereum/go-ethereum/accounts/abi"
9
        "github.com/ethereum/go-ethereum/crypto"
10
        _abi "github.com/lmittmann/w3/internal/abi"
11
)
12

13
var (
14
        ErrInvalidABI       = errors.New("w3: invalid ABI")
15
        ErrArgumentMismatch = errors.New("w3: argument mismatch")
16
        ErrReturnsMismatch  = errors.New("w3: returns mismatch")
17
        ErrInvalidType      = errors.New("w3: invalid type")
18
        ErrEvmRevert        = errors.New("w3: evm reverted")
19

20
        revertSelector       = selector("Error(string)")
21
        outputSuccess        = B("0x0000000000000000000000000000000000000000000000000000000000000001")
22
        approveSelector      = selector("approve(address,uint256)")
23
        transferSelector     = selector("transfer(address,uint256)")
24
        transferFromSelector = selector("transferFrom(address,address,uint256)")
25
)
26

27
// Func represents a Smart Contract function ABI binding.
28
//
29
// Func implements the [w3types.Func] interface.
30
type Func struct {
31
        Signature string        // Function signature
32
        Selector  [4]byte       // 4-byte selector
33
        Args      abi.Arguments // Arguments (input)
34
        Returns   abi.Arguments // Returns (output)
35

36
        name string // Function name
37
}
38

39
// NewFunc returns a new Smart Contract function ABI binding from the given
40
// Solidity function signature and its returns.
41
//
42
// An error is returned if the signature or returns parsing fails.
43
func NewFunc(signature, returns string) (*Func, error) {
47✔
44
        name, args, err := _abi.ParseWithName(signature)
47✔
45
        if err != nil {
47✔
46
                return nil, fmt.Errorf("%w: %v", ErrInvalidABI, err)
×
47
        }
×
48
        if name == "" {
47✔
49
                return nil, fmt.Errorf("%w: missing function name", ErrInvalidABI)
×
50
        }
×
51

52
        returnArgs, err := _abi.Parse(returns)
47✔
53
        if err != nil {
47✔
54
                return nil, fmt.Errorf("%w: %v", ErrInvalidABI, err)
×
55
        }
×
56

57
        sig := args.SignatureWithName(name)
47✔
58
        return &Func{
47✔
59
                Signature: sig,
47✔
60
                Selector:  selector(sig),
47✔
61
                Args:      abi.Arguments(args),
47✔
62
                Returns:   abi.Arguments(returnArgs),
47✔
63
                name:      name,
47✔
64
        }, nil
47✔
65
}
66

67
// MustNewFunc is like [NewFunc] but panics if the signature or returns parsing
68
// fails.
69
func MustNewFunc(signature, returns string) *Func {
43✔
70
        fn, err := NewFunc(signature, returns)
43✔
71
        if err != nil {
43✔
72
                panic(err)
×
73
        }
74
        return fn
43✔
75
}
76

77
// EncodeArgs ABI-encodes the given args and prepends the Func's 4-byte
78
// selector.
79
func (f *Func) EncodeArgs(args ...any) ([]byte, error) {
16✔
80
        return _abi.Arguments(f.Args).EncodeWithSelector(f.Selector, args...)
16✔
81
}
16✔
82

83
// DecodeArgs ABI-decodes the given input to the given args.
84
func (f *Func) DecodeArgs(input []byte, args ...any) error {
22✔
85
        if len(input) < 4 {
23✔
86
                return errors.New("w3: insufficient input length")
1✔
87
        }
1✔
88
        if !bytes.Equal(input[:4], f.Selector[:]) {
22✔
89
                return errors.New("w3: input does not match selector")
1✔
90
        }
1✔
91
        return _abi.Arguments(f.Args).Decode(input[4:], args...)
20✔
92
}
93

94
// DecodeReturns ABI-decodes the given output to the given returns.
95
func (f *Func) DecodeReturns(output []byte, returns ...any) error {
8✔
96
        // check the output for a revert reason
8✔
97
        if bytes.HasPrefix(output, revertSelector[:]) {
8✔
98
                if reason, err := abi.UnpackRevert(output); err != nil {
×
99
                        return err
×
100
                } else {
×
101
                        return fmt.Errorf("%w: %s", ErrEvmRevert, reason)
×
102
                }
×
103
        }
104

105
        // Gracefully handle uncompliant ERC20 returns
106
        if len(returns) == 1 && len(output) == 0 &&
8✔
107
                (f.Selector == approveSelector ||
8✔
108
                        f.Selector == transferSelector ||
8✔
109
                        f.Selector == transferFromSelector) {
8✔
110
                output = outputSuccess
×
111
        }
×
112

113
        return _abi.Arguments(f.Returns).Decode(output, returns...)
8✔
114
}
115

116
// selector returns the 4-byte selector of the given signature.
117
func selector(signature string) (selector [4]byte) {
51✔
118
        copy(selector[:], crypto.Keccak256([]byte(signature)))
51✔
119
        return
51✔
120
}
51✔
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

© 2025 Coveralls, Inc