-
Notifications
You must be signed in to change notification settings - Fork 33
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #133 from eqlabs/latenssi/token-balance-fix
Refactor handling of a tokens balance
- Loading branch information
Showing
5 changed files
with
43 additions
and
17 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
package tokens | ||
|
||
import ( | ||
"encoding/json" | ||
|
||
"github.com/onflow/cadence" | ||
) | ||
|
||
type Balance struct { | ||
CadenceValue cadence.Value | ||
} | ||
|
||
func (b *Balance) MarshalJSON() ([]byte, error) { | ||
if b.CadenceValue == nil { | ||
// Not able to omit the balance field as it is in a "parent" struct | ||
// So using JSON null here | ||
return json.Marshal(nil) | ||
} | ||
|
||
// Only handle fixed point numbers differently, rest can use the default | ||
_, isUfix64 := b.CadenceValue.Type().(cadence.UFix64Type) | ||
_, isFix64 := b.CadenceValue.Type().(cadence.Fix64Type) | ||
if isUfix64 || isFix64 { | ||
return json.Marshal(b.CadenceValue.String()) | ||
} | ||
|
||
return json.Marshal(b.CadenceValue.ToGoValue()) | ||
} |
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