Skip to content

Commit

Permalink
move function
Browse files Browse the repository at this point in the history
  • Loading branch information
kroggen committed Nov 1, 2023
1 parent bede2d2 commit 4bc6f8c
Showing 1 changed file with 35 additions and 27 deletions.
62 changes: 35 additions & 27 deletions contract/vm_callback.go
Original file line number Diff line number Diff line change
Expand Up @@ -1030,33 +1030,6 @@ func luaCryptoKeccak256(data unsafe.Pointer, dataLen C.int) (unsafe.Pointer, int
}
}

func parseDecimalAmount(str string, digits int) string {
idx := strings.Index(str, ".")
if idx == -1 {
return str
}
p1 := str[0:idx]
p2 := str[idx+1:]
if strings.Index(p2, ".") != -1 {
return "error"
}

to_add := digits - len(p2)
if to_add > 0 {
p2 = p2 + strings.Repeat("0", to_add)
} else if to_add < 0 {
//p2 = p2[0:digits]
return "error"
}
str = p1 + p2

str = strings.TrimLeft(str, "0")
if str == "" {
str = "0"
}
return str
}

// transformAmount processes the input string to calculate the total amount,
// taking into account the different units ("aergo", "gaer", "aer")
func transformAmount(amountStr string, forkVersion int32) (*big.Int, error) {
Expand Down Expand Up @@ -1128,6 +1101,41 @@ func transformAmount(amountStr string, forkVersion int32) (*big.Int, error) {
return totalAmount, nil
}

// convert decimal amount into big integer string
func parseDecimalAmount(str string, num_decimals int) string {
// Get the integer and decimal parts
idx := strings.Index(str, ".")
if idx == -1 {
return str
}
p1 := str[0:idx]
p2 := str[idx+1:]

// Check for another decimal point
if strings.Index(p2, ".") != -1 {
return "error"
}

// Compute the amount of zero digits to add
to_add := num_decimals - len(p2)
if to_add > 0 {
p2 = p2 + strings.Repeat("0", to_add)
} else if to_add < 0 {
// Do not truncate decimal amounts
return "error"
}

// Join the integer and decimal parts
str = p1 + p2

// Remove leading zeros
str = strings.TrimLeft(str, "0")
if str == "" {
str = "0"
}
return str
}

// parseAndConvert is a helper function to parse the substring as a big integer
// and apply the necessary multiplier based on the unit.
func parseAndConvert(subStr, unit string, mulUnit *big.Int, fullStr string) (*big.Int, error) {
Expand Down

0 comments on commit 4bc6f8c

Please sign in to comment.