2018-04-16 20:15:30 +00:00
|
|
|
package core
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"encoding/binary"
|
|
|
|
"io"
|
|
|
|
|
|
|
|
"github.com/CityOfZion/neo-go/pkg/core/storage"
|
|
|
|
"github.com/CityOfZion/neo-go/pkg/core/transaction"
|
2019-08-27 13:29:42 +00:00
|
|
|
"github.com/CityOfZion/neo-go/pkg/crypto/keys"
|
2018-04-16 20:15:30 +00:00
|
|
|
"github.com/CityOfZion/neo-go/pkg/util"
|
|
|
|
)
|
|
|
|
|
|
|
|
const feeMode = 0x0
|
|
|
|
|
|
|
|
// Assets is mapping between AssetID and the AssetState.
|
|
|
|
type Assets map[util.Uint256]*AssetState
|
|
|
|
|
|
|
|
func (a Assets) commit(b storage.Batch) error {
|
|
|
|
buf := new(bytes.Buffer)
|
|
|
|
for hash, state := range a {
|
|
|
|
if err := state.EncodeBinary(buf); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
key := storage.AppendPrefix(storage.STAsset, hash.Bytes())
|
|
|
|
b.Put(key, buf.Bytes())
|
|
|
|
buf.Reset()
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-02-09 15:53:58 +00:00
|
|
|
// AssetState represents the state of an NEO registered Asset.
|
2018-04-16 20:15:30 +00:00
|
|
|
type AssetState struct {
|
|
|
|
ID util.Uint256
|
|
|
|
AssetType transaction.AssetType
|
|
|
|
Name string
|
|
|
|
Amount util.Fixed8
|
|
|
|
Available util.Fixed8
|
|
|
|
Precision uint8
|
|
|
|
FeeMode uint8
|
2018-11-26 21:12:33 +00:00
|
|
|
FeeAddress util.Uint160
|
2019-08-27 13:29:42 +00:00
|
|
|
Owner *keys.PublicKey
|
2018-04-16 20:15:30 +00:00
|
|
|
Admin util.Uint160
|
|
|
|
Issuer util.Uint160
|
|
|
|
Expiration uint32
|
|
|
|
IsFrozen bool
|
|
|
|
}
|
|
|
|
|
|
|
|
// DecodeBinary implements the Payload interface.
|
|
|
|
func (a *AssetState) DecodeBinary(r io.Reader) error {
|
|
|
|
if err := binary.Read(r, binary.LittleEndian, &a.ID); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if err := binary.Read(r, binary.LittleEndian, &a.AssetType); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
var err error
|
|
|
|
a.Name, err = util.ReadVarString(r)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := binary.Read(r, binary.LittleEndian, &a.Amount); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if err := binary.Read(r, binary.LittleEndian, &a.Available); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if err := binary.Read(r, binary.LittleEndian, &a.Precision); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if err := binary.Read(r, binary.LittleEndian, &a.FeeMode); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2018-11-26 21:12:33 +00:00
|
|
|
if err := binary.Read(r, binary.LittleEndian, &a.FeeAddress); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2018-04-16 20:15:30 +00:00
|
|
|
|
2019-08-27 13:29:42 +00:00
|
|
|
a.Owner = &keys.PublicKey{}
|
2018-04-16 20:15:30 +00:00
|
|
|
if err := a.Owner.DecodeBinary(r); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if err := binary.Read(r, binary.LittleEndian, &a.Admin); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if err := binary.Read(r, binary.LittleEndian, &a.Issuer); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if err := binary.Read(r, binary.LittleEndian, &a.Expiration); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return binary.Read(r, binary.LittleEndian, &a.IsFrozen)
|
|
|
|
}
|
|
|
|
|
|
|
|
// EncodeBinary implements the Payload interface.
|
|
|
|
func (a *AssetState) EncodeBinary(w io.Writer) error {
|
|
|
|
if err := binary.Write(w, binary.LittleEndian, a.ID); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if err := binary.Write(w, binary.LittleEndian, a.AssetType); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if err := util.WriteVarUint(w, uint64(len(a.Name))); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if err := binary.Write(w, binary.LittleEndian, []byte(a.Name)); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if err := binary.Write(w, binary.LittleEndian, a.Amount); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if err := binary.Write(w, binary.LittleEndian, a.Available); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if err := binary.Write(w, binary.LittleEndian, a.Precision); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if err := binary.Write(w, binary.LittleEndian, a.FeeMode); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2018-11-26 21:12:33 +00:00
|
|
|
|
|
|
|
if err := binary.Write(w, binary.LittleEndian, a.FeeAddress); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2018-04-16 20:15:30 +00:00
|
|
|
if err := a.Owner.EncodeBinary(w); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if err := binary.Write(w, binary.LittleEndian, a.Admin); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if err := binary.Write(w, binary.LittleEndian, a.Issuer); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if err := binary.Write(w, binary.LittleEndian, a.Expiration); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return binary.Write(w, binary.LittleEndian, a.IsFrozen)
|
|
|
|
}
|
2018-11-26 21:12:33 +00:00
|
|
|
|
2019-01-22 12:14:52 +00:00
|
|
|
// GetName returns the asset name based on its type.
|
2018-11-26 21:12:33 +00:00
|
|
|
func (a *AssetState) GetName() string {
|
|
|
|
|
|
|
|
if a.AssetType == transaction.GoverningToken {
|
|
|
|
return "NEO"
|
|
|
|
} else if a.AssetType == transaction.UtilityToken {
|
|
|
|
return "NEOGas"
|
|
|
|
}
|
|
|
|
|
|
|
|
return a.Name
|
|
|
|
}
|