transaction: fix asset type JSONization

It's stringy in C#.
This commit is contained in:
Roman Khimov 2021-02-12 23:02:51 +03:00
parent 90a06ed7a5
commit c3b6405f0f
2 changed files with 71 additions and 1 deletions

View file

@ -1,5 +1,10 @@
package transaction
import (
"errors"
"fmt"
)
// AssetType represents a NEO asset type.
type AssetType uint8
@ -14,3 +19,68 @@ const (
Invoice AssetType = DutyFlag | 0x18
Token AssetType = CreditFlag | 0x20
)
// String implements Stringer interface.
func (a AssetType) String() string {
switch a {
case CreditFlag:
return "CreditFlag"
case DutyFlag:
return "DutyFlag"
case GoverningToken:
return "GoverningToken"
case UtilityToken:
return "UtilityToken"
case Currency:
return "Currency"
case Share:
return "Share"
case Invoice:
return "Invoice"
case Token:
return "Token"
default:
return fmt.Sprintf("Unknonwn (%d)", a)
}
}
// MarshalJSON implements the json marshaller interface.
func (a AssetType) MarshalJSON() ([]byte, error) {
return []byte(`"` + a.String() + `"`), nil
}
// UnmarshalJSON implements the json.Unmarshaler interface.
func (a *AssetType) UnmarshalJSON(data []byte) error {
l := len(data)
if l < 2 || data[0] != '"' || data[l-1] != '"' {
return errors.New("wrong format")
}
var err error
*a, err = AssetTypeFromString(string(data[1 : l-1]))
return err
}
// AssetTypeFromString converts string into AssetType.
func AssetTypeFromString(s string) (AssetType, error) {
switch s {
case "CreditFlag":
return CreditFlag, nil
case "DutyFlag":
return DutyFlag, nil
case "GoverningToken":
return GoverningToken, nil
case "UtilityToken":
return UtilityToken, nil
case "Currency":
return Currency, nil
case "Share":
return Share, nil
case "Invoice":
return Invoice, nil
case "Token":
return Token, nil
default:
return 0, errors.New("bad asset type")
}
}

View file

@ -178,7 +178,7 @@ var rpcClientTestCases = map[string][]rpcClientTestCase{
invoke: func(c *Client) (interface{}, error) {
return c.GetAssetState(util.Uint256{})
},
serverResponse: `{"id":1,"jsonrpc":"2.0","result":{"id":"0xc56f33fc6ecfcd0c225c4ab356fee59390af8560be0e930faebe74a6daff7c9b","type":0,"name":"NEO","amount":"100000000","available":"100000000","precision":0,"owner":"00","admin":"Abf2qMs1pzQb8kYk9RuxtUb9jtRKJVuBJt","issuer":"AFmseVrdL9f9oyCzZefL9tG6UbvhPbdYzM","expiration":4000000,"frozen":false}}`,
serverResponse: `{"id":1,"jsonrpc":"2.0","result":{"id":"0xc56f33fc6ecfcd0c225c4ab356fee59390af8560be0e930faebe74a6daff7c9b","type":"GoverningToken","name":"NEO","amount":"100000000","available":"100000000","precision":0,"owner":"00","admin":"Abf2qMs1pzQb8kYk9RuxtUb9jtRKJVuBJt","issuer":"AFmseVrdL9f9oyCzZefL9tG6UbvhPbdYzM","expiration":4000000,"frozen":false}}`,
result: func(c *Client) interface{} {
return &result.AssetState{
ID: core.GoverningTokenID(),