neo-go/pkg/rpc/response/result/unclaimed_gas.go
Elizaveta Chichindaeva 28908aa3cf [#2442] English Check
Signed-off-by: Elizaveta Chichindaeva <elizaveta@nspcc.ru>
2022-05-04 19:48:27 +03:00

50 lines
1.1 KiB
Go

package result
import (
"encoding/json"
"errors"
"math/big"
"github.com/nspcc-dev/neo-go/pkg/encoding/address"
"github.com/nspcc-dev/neo-go/pkg/util"
)
// UnclaimedGas response wrapper.
type UnclaimedGas struct {
Address util.Uint160
Unclaimed big.Int
}
// unclaimedGas is an auxiliary struct for JSON marshalling.
type unclaimedGas struct {
Address string `json:"address"`
Unclaimed string `json:"unclaimed"`
}
// MarshalJSON implements the json.Marshaler interface.
func (g UnclaimedGas) MarshalJSON() ([]byte, error) {
gas := &unclaimedGas{
Address: address.Uint160ToString(g.Address),
Unclaimed: g.Unclaimed.String(),
}
return json.Marshal(gas)
}
// UnmarshalJSON implements the json.Unmarshaler interface.
func (g *UnclaimedGas) UnmarshalJSON(data []byte) error {
gas := new(unclaimedGas)
if err := json.Unmarshal(data, gas); err != nil {
return err
}
uncl, ok := new(big.Int).SetString(gas.Unclaimed, 10)
if !ok {
return errors.New("failed to convert unclaimed gas")
}
g.Unclaimed = *uncl
addr, err := address.StringToUint160(gas.Address)
if err != nil {
return err
}
g.Address = addr
return nil
}