2020-01-13 10:21:44 +00:00
|
|
|
package result
|
2019-02-08 08:04:38 +00:00
|
|
|
|
|
|
|
import (
|
2019-02-13 18:01:52 +00:00
|
|
|
"bytes"
|
|
|
|
"sort"
|
|
|
|
|
2019-12-17 11:51:07 +00:00
|
|
|
"github.com/CityOfZion/neo-go/pkg/core/state"
|
2019-08-27 13:29:42 +00:00
|
|
|
"github.com/CityOfZion/neo-go/pkg/crypto/keys"
|
2019-02-08 08:04:38 +00:00
|
|
|
"github.com/CityOfZion/neo-go/pkg/util"
|
|
|
|
)
|
|
|
|
|
|
|
|
// AccountState wrapper used for the representation of
|
2019-11-28 16:06:09 +00:00
|
|
|
// state.Account on the RPC Server.
|
2019-02-08 08:04:38 +00:00
|
|
|
type AccountState struct {
|
2019-09-09 08:23:27 +00:00
|
|
|
Version uint8 `json:"version"`
|
|
|
|
ScriptHash util.Uint160 `json:"script_hash"`
|
|
|
|
IsFrozen bool `json:"frozen"`
|
2019-08-27 13:29:42 +00:00
|
|
|
Votes []*keys.PublicKey `json:"votes"`
|
2019-09-09 08:23:27 +00:00
|
|
|
Balances []Balance `json:"balances"`
|
2019-02-13 18:01:52 +00:00
|
|
|
}
|
|
|
|
|
2019-10-22 14:56:03 +00:00
|
|
|
// Balances type for sorting balances in rpc response.
|
2019-02-13 18:01:52 +00:00
|
|
|
type Balances []Balance
|
|
|
|
|
|
|
|
func (b Balances) Len() int { return len(b) }
|
|
|
|
func (b Balances) Less(i, j int) bool { return bytes.Compare(b[i].Asset[:], b[j].Asset[:]) != -1 }
|
|
|
|
func (b Balances) Swap(i, j int) { b[i], b[j] = b[j], b[i] }
|
|
|
|
|
2019-10-22 14:56:03 +00:00
|
|
|
// Balance response wrapper.
|
2019-02-13 18:01:52 +00:00
|
|
|
type Balance struct {
|
|
|
|
Asset util.Uint256 `json:"asset"`
|
|
|
|
Value util.Fixed8 `json:"value"`
|
2019-02-08 08:04:38 +00:00
|
|
|
}
|
|
|
|
|
2019-11-28 16:06:09 +00:00
|
|
|
// NewAccountState creates a new Account wrapper.
|
|
|
|
func NewAccountState(a *state.Account) AccountState {
|
2019-02-13 18:01:52 +00:00
|
|
|
balances := make(Balances, 0, len(a.Balances))
|
2019-11-15 17:35:09 +00:00
|
|
|
for k, v := range a.GetBalanceValues() {
|
2019-02-13 18:01:52 +00:00
|
|
|
balances = append(balances, Balance{
|
|
|
|
Asset: k,
|
|
|
|
Value: v,
|
|
|
|
})
|
2019-02-08 08:04:38 +00:00
|
|
|
}
|
|
|
|
|
2019-02-13 18:01:52 +00:00
|
|
|
sort.Sort(balances)
|
|
|
|
|
2019-02-08 08:04:38 +00:00
|
|
|
// reverse scriptHash to be consistent with other client
|
2020-02-15 16:23:11 +00:00
|
|
|
scriptHash := a.ScriptHash.Reverse()
|
2019-02-08 08:04:38 +00:00
|
|
|
|
|
|
|
return AccountState{
|
|
|
|
Version: a.Version,
|
|
|
|
ScriptHash: scriptHash,
|
|
|
|
IsFrozen: a.IsFrozen,
|
|
|
|
Votes: a.Votes,
|
|
|
|
Balances: balances,
|
|
|
|
}
|
|
|
|
}
|