Skip to content

Commit

Permalink
chore: remove bigint package
Browse files Browse the repository at this point in the history
  • Loading branch information
islishude committed Aug 9, 2023
1 parent 62e844c commit 6260314
Show file tree
Hide file tree
Showing 8 changed files with 110 additions and 60 deletions.
9 changes: 3 additions & 6 deletions flags.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
package main

import (
"encoding/json"
"fmt"

"github.com/islishude/bigint"
"github.com/islishude/web3-cli/internal/utils"
"github.com/urfave/cli/v2"
)
Expand Down Expand Up @@ -72,11 +70,10 @@ var (
Usage: "gas limit for contract call",
EnvVars: []string{"WEB3_CLI_CONTRACT_CALL_GAS"},
Action: func(ctx *cli.Context, s string) error {
var v bigint.Int
if err := json.Unmarshal([]byte(s), &v); err != nil {
return fmt.Errorf("invalid call-gas number: %s", s)
if v := utils.ToBigInt(s); v != nil {
return ctx.Set("call-gas", v.String())
}
return ctx.Set("call-gas", v.ToInt().String())
return fmt.Errorf("invalid number: %s", s)
},
}

Expand Down
1 change: 0 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ go 1.20

require (
github.com/ethereum/go-ethereum v1.12.0
github.com/islishude/bigint v1.3.3
github.com/urfave/cli/v2 v2.25.7
)

Expand Down
2 changes: 0 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,6 @@ github.com/gorilla/websocket v1.5.0 h1:PPwGk2jz7EePpoHN/+ClbZu8SPxiqlu12wZP/3sWm
github.com/gorilla/websocket v1.5.0/go.mod h1:YR8l580nyteQvAITg2hZ9XVh4b55+EU/adAjf1fMHhE=
github.com/holiman/uint256 v1.2.3 h1:K8UWO1HUJpRMXBxbmaY1Y8IAMZC/RsKB+ArEnnK4l5o=
github.com/holiman/uint256 v1.2.3/go.mod h1:SC8Ryt4n+UBbPbIBKaG9zbbDlp4jOru9xFZmPzLUTxw=
github.com/islishude/bigint v1.3.3 h1:h74XOzCuDfwWP2yTpvANXh0MmgxYpug3uViDtj9yv0M=
github.com/islishude/bigint v1.3.3/go.mod h1:fAzpZ5cMJoavMeO+oh36vemVmyGB+4aSbQqL5A03m5g=
github.com/klauspost/compress v1.15.15 h1:EF27CXIuDsYJ6mmvtBRlEuB2UVOqHG1tAXgZ7yIO+lw=
github.com/kr/pretty v0.3.1 h1:flRD4NNwYAUpkphVc1HcthR4KEIFJ65n8Mw5qdRn3LE=
github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY=
Expand Down
21 changes: 5 additions & 16 deletions internal/abis/pack.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,10 @@ import (
"fmt"
"reflect"
"strconv"
"strings"

"github.com/ethereum/go-ethereum/accounts/abi"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/common/hexutil"
"github.com/islishude/bigint"
"github.com/islishude/web3-cli/internal/utils"
)

Expand Down Expand Up @@ -170,14 +168,6 @@ func nonDynamicEncode(abiArg *abi.Type, arg any) (any, error) {
switch v := arg.(type) {
case string:
rawArg = v
if abiArg.T == abi.IntTy || abiArg.T == abi.UintTy {
if !strings.HasPrefix(rawArg, `"`) {
rawArg = `"` + rawArg
}
if !strings.HasSuffix(rawArg, `"`) {
rawArg = rawArg + `"`
}
}
case json.RawMessage:
if abiArg.T != abi.IntTy && abiArg.T != abi.UintTy {
v = bytes.TrimLeft(v, `"`)
Expand All @@ -188,12 +178,11 @@ func nonDynamicEncode(abiArg *abi.Type, arg any) (any, error) {

switch abiArg.T {
case abi.IntTy, abi.UintTy:
var i bigint.Int
if err := json.Unmarshal([]byte(rawArg), &i); err != nil {
return nil, fmt.Errorf("param %s should be a valid number type: %s", arg, err)
val := utils.ToBigInt(rawArg)
if val == nil {
return nil, fmt.Errorf("invalid number: %s", rawArg)
}

val := i.ToInt()
if abiArg.T == abi.IntTy {
switch abiArg.Size {
case 8:
Expand All @@ -205,7 +194,7 @@ func nonDynamicEncode(abiArg *abi.Type, arg any) (any, error) {
case 64:
return val.Int64(), nil
}
return i.ToInt(), nil
return val, nil
}

switch abiArg.Size {
Expand All @@ -218,7 +207,7 @@ func nonDynamicEncode(abiArg *abi.Type, arg any) (any, error) {
case 64:
return val.Uint64(), nil
}
return i.ToInt(), nil
return val, nil
case abi.BoolTy:
val, err := strconv.ParseBool(rawArg)
if err != nil {
Expand Down
22 changes: 6 additions & 16 deletions internal/utils/actions.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
package utils

import (
"encoding/json"
"fmt"
"strconv"
"strings"

"github.com/ethereum/go-ethereum/common/hexutil"
"github.com/islishude/bigint"
"github.com/urfave/cli/v2"
)

Expand All @@ -29,11 +27,10 @@ func EthCallHeightFlagAction(ctx *cli.Context, s string) error {
case "safe", "finalized", "latest", "earliest", "pending":
return nil
default:
var v bigint.Int
if err := json.Unmarshal([]byte(s), &v); err != nil {
return fmt.Errorf("invalid decimal number: %s", s)
if v := ToBigInt(s); v != nil {
return ctx.Set(flagName, v.String())
}
return ctx.Set(flagName, v.ToInt().String())
return fmt.Errorf("invalid number: %s", s)
}
}

Expand All @@ -46,17 +43,10 @@ func EthCallValueFlagAction(ctx *cli.Context, s string) error {
return fmt.Errorf("invalid ether value: %s", s)
}
return ctx.Set(flagName, (*hexutil.Big)(ToWei(v)).String())
case strings.HasSuffix(s, "gwei"):
v, err := strconv.ParseFloat(strings.TrimLeft(s, "gwei"), 64)
if err != nil {
return fmt.Errorf("invalid gwei value: %s", s)
}
return ctx.Set(flagName, (*hexutil.Big)(ToGWei(v)).String())
default:
var v bigint.Int
if err := json.Unmarshal([]byte(s), &v); err != nil {
return fmt.Errorf("invalid decimal number: %s", s)
if v := ToBigInt(s); v != nil {
return ctx.Set(flagName, v.String())
}
return ctx.Set(flagName, v.ToInt().String())
return fmt.Errorf("invalid number: %s", s)
}
}
58 changes: 40 additions & 18 deletions internal/utils/convert.go
Original file line number Diff line number Diff line change
@@ -1,31 +1,53 @@
package utils

import (
"fmt"
"math/big"
"strings"

"github.com/ethereum/go-ethereum/params"
)

func ToWei(amount float64) *big.Int {
eth := big.NewFloat(amount)
truncInt, _ := eth.Int(nil)
truncInt = new(big.Int).Mul(truncInt, big.NewInt(params.Ether))
fracStr := strings.Split(fmt.Sprintf("%.18f", eth), ".")[1]
fracStr += strings.Repeat("0", 18-len(fracStr))
fracInt, _ := new(big.Int).SetString(fracStr, 10)
wei := new(big.Int).Add(truncInt, fracInt)
eth = eth.Mul(eth, big.NewFloat(1e18))
wei, _ := eth.Int(new(big.Int))
return wei
}

func ToGWei(amount float64) *big.Int {
eth := big.NewFloat(amount)
truncInt, _ := eth.Int(nil)
truncInt = new(big.Int).Mul(truncInt, big.NewInt(params.Ether))
fracStr := strings.Split(fmt.Sprintf("%.9f", eth), ".")[1]
fracStr += strings.Repeat("0", 9-len(fracStr))
fracInt, _ := new(big.Int).SetString(fracStr, 10)
wei := new(big.Int).Add(truncInt, fracInt)
return wei
func ToBigInt(s string) *big.Int {
// trim quotation mark
s = strings.Trim(s, `"`)

isNeg := strings.HasPrefix(s, "-")
if isNeg {
return nil
}

s = strings.ToLower(s)
if strings.HasPrefix(s, "0x") {
s = strings.TrimPrefix(s, "0x")
i, _ := new(big.Int).SetString(s, 16)
return i
}

if strings.HasPrefix(s, "0b") {
s = strings.TrimPrefix(s, "0b")
i, _ := new(big.Int).SetString(s, 2)
return i
}

if strings.HasPrefix(s, "0o") {
s = strings.TrimPrefix(s, "0o")
i, _ := new(big.Int).SetString(s, 8)
return i
}

if strings.Contains(s, "e") {
flt, _, err := big.ParseFloat(s, 10, 0, big.ToNearestEven)
if err == nil {
i, _ := flt.Int(new(big.Int))
return i
}
}

i, _ := new(big.Int).SetString(s, 10)
return i
}
55 changes: 55 additions & 0 deletions internal/utils/convert_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
package utils

import (
"math/big"
"reflect"
"testing"
)

func TestToBigInt(t *testing.T) {
type args struct {
s string
}
tests := []struct {
name string
args args
want *big.Int
}{
{`invalid`, args{`xyz`}, nil},
{`1e4`, args{"1e4"}, big.NewInt(1e4)},
{`1e18`, args{"1e18"}, big.NewInt(1e18)},
{`1`, args{"1"}, big.NewInt(1)},
{`-1`, args{`"-1"`}, nil},
{`0x1`, args{`0x1`}, big.NewInt(1)},
{`0b1`, args{`0b1`}, big.NewInt(1)},
{`0o1`, args{`0o1`}, big.NewInt(1)},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := ToBigInt(tt.args.s); !reflect.DeepEqual(got, tt.want) {
t.Errorf("ToBigInt() = %v, want %v", got, tt.want)
}
})
}
}

func TestToWei(t *testing.T) {
type args struct {
amount float64
}
tests := []struct {
name string
args args
want *big.Int
}{
{"0.1", args{0.1}, big.NewInt(1e17)},
{"0.01", args{0.01}, big.NewInt(1e16)},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := ToWei(tt.args.amount); !reflect.DeepEqual(got, tt.want) {
t.Errorf("ToWei() = %v, want %v", got, tt.want)
}
})
}
}
2 changes: 1 addition & 1 deletion internal/utils/validate.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ func IsAddress(v string) bool {
return addregexp.MatchString(v)
}

var numberRx = regexp.MustCompile(`^[0-9]+$`)
var numberRx = regexp.MustCompile(`^(-)?[0-9]+$`)

func IsNumber(p string) bool {
return numberRx.MatchString(p)
Expand Down

0 comments on commit 6260314

Please sign in to comment.