-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
110 additions
and
60 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters