-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Addition of FlexibleCosmosIntAmount to support int and str
- Loading branch information
Showing
4 changed files
with
67 additions
and
26 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,48 @@ | ||
package lib | ||
|
||
import ( | ||
"encoding/json" | ||
"fmt" | ||
"strconv" | ||
|
||
cosmossdk_io_math "cosmossdk.io/math" | ||
) | ||
|
||
type Address = string | ||
type Allo = int64 | ||
type BlockHeight = int64 | ||
|
||
// FlexibleCosmosIntAmount represents amounts of tokens, where the amount can be specified or as a string | ||
type FlexibleCosmosIntAmount struct { | ||
Number cosmossdk_io_math.Int | ||
} | ||
|
||
func (fv *FlexibleCosmosIntAmount) String() string { | ||
fmt.Println("fv.Number", fv.Number) | ||
return fv.Number.String() | ||
} | ||
|
||
func (fv *FlexibleCosmosIntAmount) UnmarshalJSON(data []byte) error { | ||
// Handle number | ||
if data[0] >= '0' && data[0] <= '9' { | ||
var num int64 | ||
if err := json.Unmarshal(data, &num); err != nil { | ||
return fmt.Errorf("failed to parse number: %w", err) | ||
} | ||
fv.Number = cosmossdk_io_math.NewInt(num) | ||
return nil | ||
} | ||
|
||
// Handle string | ||
var str string | ||
if err := json.Unmarshal(data, &str); err == nil { | ||
num, err := strconv.ParseInt(str, 10, 64) | ||
if err != nil { | ||
return fmt.Errorf("failed to convert string to number: %w", err) | ||
} | ||
fv.Number = cosmossdk_io_math.NewInt(num) | ||
return nil | ||
} | ||
|
||
return fmt.Errorf("invalid value: %s", string(data)) | ||
} |