Skip to content

Commit d519cec

Browse files
fedekunze0xstepitMalteHerrmann
authored
imp(erc20): types (evmos#1976)
* imp(erc20): types * Apply suggestions from code review Co-authored-by: stepit <48993133+0xstepit@users.noreply.github.com> Co-authored-by: MalteHerrmann <42640438+MalteHerrmann@users.noreply.github.com> --------- Co-authored-by: stepit <48993133+0xstepit@users.noreply.github.com> Co-authored-by: MalteHerrmann <42640438+MalteHerrmann@users.noreply.github.com>
1 parent b899cf0 commit d519cec

File tree

1 file changed

+104
-0
lines changed

1 file changed

+104
-0
lines changed

precompiles/erc20/types.go

Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
// Copyright Tharsis Labs Ltd.(Evmos)
2+
// SPDX-License-Identifier:ENCL-1.0(https://github.com/evmos/evmos/blob/main/LICENSE)
3+
4+
package erc20
5+
6+
import (
7+
"fmt"
8+
"math/big"
9+
10+
"github.com/ethereum/go-ethereum/common"
11+
)
12+
13+
// ParseTransferArgs parses the arguments from the transfer method and returns
14+
// the destination address (to) and amount.
15+
func ParseTransferArgs(args []interface{}) (
16+
to common.Address, amount *big.Int, err error,
17+
) {
18+
if len(args) != 2 {
19+
return common.Address{}, nil, fmt.Errorf("invalid number of arguments; expected 2; got: %d", len(args))
20+
}
21+
22+
to, ok := args[0].(common.Address)
23+
if !ok {
24+
return common.Address{}, nil, fmt.Errorf("invalid to address: %v", args[0])
25+
}
26+
27+
amount, ok = args[1].(*big.Int)
28+
if !ok {
29+
return common.Address{}, nil, fmt.Errorf("invalid amount: %v", args[1])
30+
}
31+
32+
return to, amount, nil
33+
}
34+
35+
// ParseTransferFromArgs parses the arguments from the transferFrom method and returns
36+
// the sender address (from), destination address (to) and amount.
37+
func ParseTransferFromArgs(args []interface{}) (
38+
from, to common.Address, amount *big.Int, err error,
39+
) {
40+
if len(args) != 3 {
41+
return common.Address{}, common.Address{}, nil, fmt.Errorf("invalid number of arguments; expected 3; got: %d", len(args))
42+
}
43+
44+
from, ok := args[0].(common.Address)
45+
if !ok {
46+
return common.Address{}, common.Address{}, nil, fmt.Errorf("invalid from address: %v", args[0])
47+
}
48+
49+
to, ok = args[1].(common.Address)
50+
if !ok {
51+
return common.Address{}, common.Address{}, nil, fmt.Errorf("invalid to address: %v", args[1])
52+
}
53+
54+
amount, ok = args[2].(*big.Int)
55+
if !ok {
56+
return common.Address{}, common.Address{}, nil, fmt.Errorf("invalid amount: %v", args[2])
57+
}
58+
59+
return from, to, amount, nil
60+
}
61+
62+
// ParseApproveArgs parses the approval arguments and returns the spender address
63+
// and amount.
64+
func ParseApproveArgs(args []interface{}) (
65+
spender common.Address, amount *big.Int, err error,
66+
) {
67+
if len(args) != 2 {
68+
return common.Address{}, nil, fmt.Errorf("invalid number of arguments; expected 2; got: %d", len(args))
69+
}
70+
71+
spender, ok := args[0].(common.Address)
72+
if !ok {
73+
return common.Address{}, nil, fmt.Errorf("invalid spender address: %v", args[0])
74+
}
75+
76+
amount, ok = args[1].(*big.Int)
77+
if !ok {
78+
return common.Address{}, nil, fmt.Errorf("invalid amount: %v", args[1])
79+
}
80+
81+
return spender, amount, nil
82+
}
83+
84+
// ParseAllowanceArgs parses the allowance arguments and returns the owner and
85+
// the spender addresses.
86+
func ParseAllowanceArgs(args []interface{}) (
87+
owner, spender common.Address, err error,
88+
) {
89+
if len(args) != 2 {
90+
return common.Address{}, common.Address{}, fmt.Errorf("invalid number of arguments; expected 2; got: %d", len(args))
91+
}
92+
93+
owner, ok := args[0].(common.Address)
94+
if !ok {
95+
return common.Address{}, common.Address{}, fmt.Errorf("invalid owner address: %v", args[0])
96+
}
97+
98+
spender, ok = args[1].(common.Address)
99+
if !ok {
100+
return common.Address{}, common.Address{}, fmt.Errorf("invalid spender address: %v", args[1])
101+
}
102+
103+
return owner, spender, nil
104+
}

0 commit comments

Comments
 (0)