Skip to content

Commit

Permalink
do not allow amounts bigger than num decimals
Browse files Browse the repository at this point in the history
  • Loading branch information
kroggen committed Nov 1, 2023
1 parent bff4528 commit bede2d2
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 1 deletion.
4 changes: 3 additions & 1 deletion contract/vm_callback.go
Original file line number Diff line number Diff line change
Expand Up @@ -1045,7 +1045,8 @@ func parseDecimalAmount(str string, digits int) string {
if to_add > 0 {
p2 = p2 + strings.Repeat("0", to_add)
} else if to_add < 0 {
p2 = p2[0:digits]
//p2 = p2[0:digits]
return "error"
}
str = p1 + p2

Expand All @@ -1068,6 +1069,7 @@ func transformAmount(amountStr string, forkVersion int32) (*big.Int, error) {
if strings.Contains(amountStr,".") && strings.HasSuffix(amountStr,"aergo") {
// Extract the part before the unit
decimalAmount := strings.TrimSuffix(amountStr, "aergo")
decimalAmount = strings.TrimRight(decimalAmount, " ")
// Parse the decimal amount
decimalAmount = parseDecimalAmount(decimalAmount, 18)
if decimalAmount == "error" {
Expand Down
37 changes: 37 additions & 0 deletions contract/vm_callback_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -162,8 +162,45 @@ func TestTransformAmount(t *testing.T) {
{4, ".01aergo", bigIntFromString("10000000000000000"), nil},
{4, "0.01aergo", bigIntFromString("10000000000000000"), nil},
{4, "0.0000000001aergo", bigIntFromString("100000000"), nil},
{4, "0.000000000000000001aergo", bigIntFromString("1"), nil},
{4, "0.000000000000000123aergo", bigIntFromString("123"), nil},
{4, "0.000000000000000000aergo", bigIntFromString("0"), nil},
{4, "0.000000000000123000aergo", bigIntFromString("123000"), nil},
{4, "0.100000000000000123aergo", bigIntFromString("100000000000000123"), nil},
{4, "1.000000000000000123aergo", bigIntFromString("1000000000000000123"), nil},
{4, "123.456000000000000789aergo", bigIntFromString("123456000000000000789"), nil},

{4, "123.456 aergo", bigIntFromString("123456000000000000000"), nil},
{4, "123.4 aergo", bigIntFromString("123400000000000000000"), nil},
{4, "123. aergo", bigIntFromString("123000000000000000000"), nil},
{4, "100. aergo", bigIntFromString("100000000000000000000"), nil},
{4, "10. aergo", bigIntFromString("10000000000000000000"), nil},
{4, "1. aergo", bigIntFromString("1000000000000000000"), nil},
{4, "100.0 aergo", bigIntFromString("100000000000000000000"), nil},
{4, "10.0 aergo", bigIntFromString("10000000000000000000"), nil},
{4, "1.0 aergo", bigIntFromString("1000000000000000000"), nil},
{4, ".1 aergo", bigIntFromString("100000000000000000"), nil},
{4, "0.1 aergo", bigIntFromString("100000000000000000"), nil},
{4, ".01 aergo", bigIntFromString("10000000000000000"), nil},
{4, "0.01 aergo", bigIntFromString("10000000000000000"), nil},
{4, "0.0000000001 aergo", bigIntFromString("100000000"), nil},
{4, "0.000000000000000001 aergo", bigIntFromString("1"), nil},
{4, "0.000000000000000123 aergo", bigIntFromString("123"), nil},
{4, "0.000000000000000000 aergo", bigIntFromString("0"), nil},
{4, "0.000000000000123000 aergo", bigIntFromString("123000"), nil},
{4, "0.100000000000000123 aergo", bigIntFromString("100000000000000123"), nil},
{4, "1.000000000000000123 aergo", bigIntFromString("1000000000000000123"), nil},
{4, "123.456000000000000789 aergo", bigIntFromString("123456000000000000789"), nil},

{4, "0.0000000000000000001aergo", nil, errors.New("converting error for BigNum: 0.0000000000000000001aergo")},
{4, "0.000000000000000000000000001aergo", nil, errors.New("converting error for BigNum: 0.000000000000000000000000001aergo")},
{4, "0.000000000000000123000aergo", nil, errors.New("converting error for BigNum: 0.000000000000000123000aergo")},
{4, "0.0000000000000000000000aergo", nil, errors.New("converting error for BigNum: 0.0000000000000000000000aergo")},

{4, "0.0000000000000000001 aergo", nil, errors.New("converting error for BigNum: 0.0000000000000000001 aergo")},
{4, "0.000000000000000000000000001 aergo", nil, errors.New("converting error for BigNum: 0.000000000000000000000000001 aergo")},
{4, "0.000000000000000123000 aergo", nil, errors.New("converting error for BigNum: 0.000000000000000123000 aergo")},
{4, "0.0000000000000000000000 aergo", nil, errors.New("converting error for BigNum: 0.0000000000000000000000 aergo")},
}

for _, tt := range decimal_tests {
Expand Down

0 comments on commit bede2d2

Please sign in to comment.