github.com/annchain/OG@v0.0.9/arefactor_core/core/state/token_object.go (about)

     1  package state
     2  
     3  import (
     4  	"fmt"
     5  	ogtypes "github.com/annchain/OG/arefactor/og_interface"
     6  	"github.com/annchain/OG/common/math"
     7  	"github.com/annchain/commongo/marshaller"
     8  	"math/big"
     9  )
    10  
    11  const (
    12  	MaxTokenName   = 20
    13  	MaxTokenSymbol = 5
    14  )
    15  
    16  //go:generate msgp
    17  
    18  //msgp:tuple TokenObject
    19  type TokenObject struct {
    20  	TokenID    int32
    21  	Name       string
    22  	Symbol     string
    23  	Issuer     ogtypes.Address
    24  	ReIssuable bool
    25  
    26  	Issues    []*math.BigInt
    27  	Destroyed bool
    28  
    29  	db StateDBInterface
    30  }
    31  
    32  func NewTokenObject(tokenID int32, issuer ogtypes.Address, name, symbol string, reIssuable bool, fstIssue *math.BigInt, db StateDBInterface) *TokenObject {
    33  
    34  	if len(name) > MaxTokenName {
    35  		name = name[:MaxTokenName]
    36  	}
    37  	if len(symbol) > MaxTokenSymbol {
    38  		symbol = symbol[:MaxTokenSymbol]
    39  	}
    40  
    41  	t := &TokenObject{}
    42  
    43  	t.TokenID = tokenID
    44  	t.Issuer = issuer
    45  	t.Name = name
    46  	t.Symbol = symbol
    47  	t.ReIssuable = reIssuable
    48  	t.Issues = []*math.BigInt{math.NewBigIntFromBigInt(fstIssue.Value)}
    49  	t.Destroyed = false
    50  
    51  	t.db = db
    52  
    53  	return t
    54  }
    55  
    56  func (t *TokenObject) GetID() int32 {
    57  	return t.TokenID
    58  }
    59  
    60  func (t *TokenObject) GetName() string {
    61  	return t.Name
    62  }
    63  
    64  func (t *TokenObject) GetSymbol() string {
    65  	return t.Symbol
    66  }
    67  
    68  func (t *TokenObject) CanReIssue() bool {
    69  	return t.ReIssuable
    70  }
    71  
    72  func (t *TokenObject) AllIssues() []*math.BigInt {
    73  	return t.Issues
    74  }
    75  
    76  func (t *TokenObject) OneIssue(term int) *math.BigInt {
    77  	if len(t.Issues) <= term {
    78  		return math.NewBigInt(0)
    79  	}
    80  	return t.Issues[term]
    81  }
    82  
    83  /**
    84  Setters
    85  */
    86  
    87  func (t *TokenObject) ReIssue(amount *math.BigInt) error {
    88  	if t.Destroyed {
    89  		return fmt.Errorf("token has been destroyed")
    90  	}
    91  	if !t.ReIssuable {
    92  		return fmt.Errorf("token is not able to reissue")
    93  	}
    94  	t.db.AppendJournal(&reIssueChange{
    95  		tokenID: t.TokenID,
    96  	})
    97  	t.Issues = append(t.Issues, amount)
    98  	return nil
    99  }
   100  
   101  func (t *TokenObject) Destroy() {
   102  	t.db.AppendJournal(&destroyChange{
   103  		tokenID:       t.TokenID,
   104  		prevDestroyed: t.Destroyed,
   105  	})
   106  	t.Destroyed = true
   107  }
   108  
   109  func (t *TokenObject) CopyRaw(tObj *TokenObject) {
   110  	t.TokenID = tObj.TokenID
   111  	t.Name = tObj.Name
   112  	t.Symbol = tObj.Symbol
   113  	t.Issuer = tObj.Issuer
   114  	t.ReIssuable = tObj.ReIssuable
   115  	t.Issues = tObj.Issues
   116  	t.Destroyed = tObj.Destroyed
   117  }
   118  
   119  func (t *TokenObject) Encode() ([]byte, error) {
   120  	return t.MarshalMsg()
   121  }
   122  
   123  func (t *TokenObject) Decode(b []byte) error {
   124  	_, err := t.UnmarshalMsg(b)
   125  	return err
   126  }
   127  
   128  /**
   129  Marshaller part
   130  */
   131  
   132  func (t *TokenObject) MarshalMsg() ([]byte, error) {
   133  	var err error
   134  	b := make([]byte, marshaller.HeaderSize)
   135  
   136  	// int32 TokenID
   137  	b = marshaller.AppendInt32(b, t.TokenID)
   138  	// string Name
   139  	b = marshaller.AppendString(b, t.Name)
   140  	// string Symbol
   141  	b = marshaller.AppendString(b, t.Symbol)
   142  	// Address
   143  	b, err = marshaller.AppendIMarshaller(b, t.Issuer)
   144  	if err != nil {
   145  		return b, err
   146  	}
   147  	// bool ReIssuable
   148  	b = marshaller.AppendBool(b, t.ReIssuable)
   149  	// []math.BigInt issues
   150  	b = marshaller.AppendHeader(b, len(t.Issues))
   151  	for _, bi := range t.Issues {
   152  		b = marshaller.AppendBigInt(b, bi.Value)
   153  	}
   154  	// bool Destroyed
   155  	b = marshaller.AppendBool(b, t.Destroyed)
   156  
   157  	b = marshaller.FillHeaderData(b)
   158  	return b, nil
   159  }
   160  
   161  func (t *TokenObject) UnmarshalMsg(b []byte) ([]byte, error) {
   162  	b, _, err := marshaller.DecodeHeader(b)
   163  	if err != nil {
   164  		return nil, err
   165  	}
   166  
   167  	// TokenID
   168  	t.TokenID, b, err = marshaller.ReadInt32(b)
   169  	if err != nil {
   170  		return nil, err
   171  	}
   172  	// Name
   173  	t.Name, b, err = marshaller.ReadString(b)
   174  	if err != nil {
   175  		return nil, err
   176  	}
   177  	// Symbol
   178  	t.Symbol, b, err = marshaller.ReadString(b)
   179  	if err != nil {
   180  		return nil, err
   181  	}
   182  	// Issuer
   183  	t.Issuer, b, err = ogtypes.UnmarshalAddress(b)
   184  	if err != nil {
   185  		return nil, err
   186  	}
   187  	// ReIssuable
   188  	t.ReIssuable, b, err = marshaller.ReadBool(b)
   189  	if err != nil {
   190  		return nil, err
   191  	}
   192  	// issues
   193  	b, sz, err := marshaller.DecodeHeader(b)
   194  	if err != nil {
   195  		return nil, err
   196  	}
   197  	t.Issues = make([]*math.BigInt, sz)
   198  	for i := range t.Issues {
   199  		var bi *big.Int
   200  		bi, b, err = marshaller.ReadBigInt(b)
   201  		if err != nil {
   202  			return nil, err
   203  		}
   204  		t.Issues[i] = math.NewBigIntFromBigInt(bi)
   205  	}
   206  
   207  	// Destroyed
   208  	t.Destroyed, b, err = marshaller.ReadBool(b)
   209  	if err != nil {
   210  		return nil, err
   211  	}
   212  
   213  	return b, err
   214  }
   215  
   216  func (t *TokenObject) MsgSize() int {
   217  	size := 0
   218  
   219  	size += marshaller.Int32Size + // TokenID
   220  		marshaller.CalStringSize(t.Name) + // Name
   221  		marshaller.CalStringSize(t.Symbol) + // Symbol
   222  		marshaller.CalIMarshallerSize(t.Issuer.MsgSize()) + // Issuer
   223  		1 // ReIssuable
   224  
   225  	// issues
   226  	var sz int
   227  	for _, issue := range t.Issues {
   228  		sz += marshaller.CalIMarshallerSize(len(issue.Value.Bytes()))
   229  	}
   230  	size += marshaller.CalIMarshallerSize(sz)
   231  
   232  	// Destroyed
   233  	size += 1
   234  
   235  	return size
   236  }