2018-04-16 20:15:30 +00:00
|
|
|
package core
|
|
|
|
|
|
|
|
import (
|
|
|
|
"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"
|
2019-09-16 09:18:13 +00:00
|
|
|
"github.com/CityOfZion/neo-go/pkg/io"
|
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 {
|
2019-09-16 09:18:13 +00:00
|
|
|
buf := io.NewBufBinWriter()
|
2018-04-16 20:15:30 +00:00
|
|
|
for hash, state := range a {
|
2019-09-16 09:18:13 +00:00
|
|
|
if err := state.EncodeBinary(buf.BinWriter); err != nil {
|
2018-04-16 20:15:30 +00:00
|
|
|
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.
|
2019-09-16 09:18:13 +00:00
|
|
|
func (a *AssetState) DecodeBinary(br *io.BinReader) error {
|
2019-08-28 16:27:06 +00:00
|
|
|
br.ReadLE(&a.ID)
|
|
|
|
br.ReadLE(&a.AssetType)
|
2018-04-16 20:15:30 +00:00
|
|
|
|
2019-08-28 16:27:06 +00:00
|
|
|
a.Name = br.ReadString()
|
2018-04-16 20:15:30 +00:00
|
|
|
|
2019-08-28 16:27:06 +00:00
|
|
|
br.ReadLE(&a.Amount)
|
|
|
|
br.ReadLE(&a.Available)
|
|
|
|
br.ReadLE(&a.Precision)
|
|
|
|
br.ReadLE(&a.FeeMode)
|
|
|
|
br.ReadLE(&a.FeeAddress)
|
2018-04-16 20:15:30 +00:00
|
|
|
|
2019-08-28 16:27:06 +00:00
|
|
|
if br.Err != nil {
|
|
|
|
return br.Err
|
|
|
|
}
|
2019-08-27 13:29:42 +00:00
|
|
|
a.Owner = &keys.PublicKey{}
|
2019-09-16 09:18:13 +00:00
|
|
|
if err := a.Owner.DecodeBinary(br); err != nil {
|
2018-04-16 20:15:30 +00:00
|
|
|
return err
|
|
|
|
}
|
2019-08-28 16:27:06 +00:00
|
|
|
br.ReadLE(&a.Admin)
|
|
|
|
br.ReadLE(&a.Issuer)
|
|
|
|
br.ReadLE(&a.Expiration)
|
|
|
|
br.ReadLE(&a.IsFrozen)
|
|
|
|
|
|
|
|
return br.Err
|
2018-04-16 20:15:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// EncodeBinary implements the Payload interface.
|
2019-09-16 09:18:13 +00:00
|
|
|
func (a *AssetState) EncodeBinary(bw *io.BinWriter) error {
|
2019-08-28 16:27:06 +00:00
|
|
|
bw.WriteLE(a.ID)
|
|
|
|
bw.WriteLE(a.AssetType)
|
|
|
|
bw.WriteString(a.Name)
|
|
|
|
bw.WriteLE(a.Amount)
|
|
|
|
bw.WriteLE(a.Available)
|
|
|
|
bw.WriteLE(a.Precision)
|
|
|
|
bw.WriteLE(a.FeeMode)
|
|
|
|
bw.WriteLE(a.FeeAddress)
|
|
|
|
|
|
|
|
if bw.Err != nil {
|
|
|
|
return bw.Err
|
2018-04-16 20:15:30 +00:00
|
|
|
}
|
2019-09-16 09:18:13 +00:00
|
|
|
if err := a.Owner.EncodeBinary(bw); err != nil {
|
2018-04-16 20:15:30 +00:00
|
|
|
return err
|
|
|
|
}
|
2019-08-28 16:27:06 +00:00
|
|
|
bw.WriteLE(a.Admin)
|
|
|
|
bw.WriteLE(a.Issuer)
|
|
|
|
bw.WriteLE(a.Expiration)
|
|
|
|
bw.WriteLE(a.IsFrozen)
|
|
|
|
return bw.Err
|
2018-04-16 20:15:30 +00:00
|
|
|
}
|
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
|
|
|
|
}
|