github.com/nspcc-dev/neo-go@v0.105.2-0.20240517133400-6be757af3eba/pkg/neorpc/result/unclaimed_gas.go (about)

     1  package result
     2  
     3  import (
     4  	"encoding/json"
     5  	"errors"
     6  	"math/big"
     7  
     8  	"github.com/nspcc-dev/neo-go/pkg/encoding/address"
     9  	"github.com/nspcc-dev/neo-go/pkg/util"
    10  )
    11  
    12  // UnclaimedGas response wrapper.
    13  type UnclaimedGas struct {
    14  	Address   util.Uint160
    15  	Unclaimed big.Int
    16  }
    17  
    18  // unclaimedGas is an auxiliary struct for JSON marshalling.
    19  type unclaimedGas struct {
    20  	Address   string `json:"address"`
    21  	Unclaimed string `json:"unclaimed"`
    22  }
    23  
    24  // MarshalJSON implements the json.Marshaler interface.
    25  func (g UnclaimedGas) MarshalJSON() ([]byte, error) {
    26  	gas := &unclaimedGas{
    27  		Address:   address.Uint160ToString(g.Address),
    28  		Unclaimed: g.Unclaimed.String(),
    29  	}
    30  	return json.Marshal(gas)
    31  }
    32  
    33  // UnmarshalJSON implements the json.Unmarshaler interface.
    34  func (g *UnclaimedGas) UnmarshalJSON(data []byte) error {
    35  	gas := new(unclaimedGas)
    36  	if err := json.Unmarshal(data, gas); err != nil {
    37  		return err
    38  	}
    39  	uncl, ok := new(big.Int).SetString(gas.Unclaimed, 10)
    40  	if !ok {
    41  		return errors.New("failed to convert unclaimed gas")
    42  	}
    43  	g.Unclaimed = *uncl
    44  	addr, err := address.StringToUint160(gas.Address)
    45  	if err != nil {
    46  		return err
    47  	}
    48  	g.Address = addr
    49  	return nil
    50  }