github.com/annchain/OG@v0.0.9/arefactor_core/core/processor_tx.go (about) 1 package core 2 3 import ( 4 "fmt" 5 "github.com/annchain/OG/arefactor/types" 6 "strconv" 7 ) 8 9 type OgTxProcessor struct{} 10 11 func NewOgTxProcessor() *OgTxProcessor { 12 return &OgTxProcessor{} 13 } 14 15 func (tp *OgTxProcessor) Process(engine LedgerEngine, tx types.Txi) (*Receipt, error) { 16 curNonce := engine.GetNonce(tx.Sender()) 17 if tx.GetNonce() > curNonce { 18 engine.SetNonce(tx.Sender(), tx.GetNonce()) 19 } 20 21 if tx.GetType() == types.TxBaseTypeSequencer { 22 receipt := NewReceipt(tx.GetTxHash(), ReceiptStatusSuccess, "", emptyAddress) 23 return receipt, nil 24 } 25 if tx.GetType() == types.TxBaseAction { 26 actionTx := tx.(*types.ActionTx) 27 receipt, err := ActionTxProcessor(engine, actionTx) 28 if err != nil { 29 return receipt, fmt.Errorf("process action tx error: %v", err) 30 } 31 return receipt, nil 32 } 33 34 if tx.GetType() != types.TxBaseTypeNormal { 35 receipt := NewReceipt(tx.GetTxHash(), ReceiptStatusUnknownTxType, "", emptyAddress) 36 return receipt, nil 37 } 38 39 // transfer balance 40 txnormal := tx.(*types.Tx) 41 if txnormal.Value.Value.Sign() != 0 && !(txnormal.To.Cmp(emptyAddress) == 0) { 42 engine.SubTokenBalance(txnormal.Sender(), txnormal.TokenId, txnormal.Value) 43 engine.AddTokenBalance(txnormal.To, txnormal.TokenId, txnormal.Value) 44 } 45 46 receipt := NewReceipt(tx.GetTxHash(), ReceiptStatusSuccess, "", emptyAddress) 47 return receipt, nil 48 } 49 50 func ActionTxProcessor(engine LedgerEngine, actionTx *types.ActionTx) (*Receipt, error) { 51 52 switch actionTx.Action { 53 case types.ActionTxActionIPO, types.ActionTxActionSPO, types.ActionTxActionDestroy: 54 return processPublicOffering(engine, actionTx) 55 default: 56 return nil, fmt.Errorf("unkown tx action type: %x", actionTx.Action) 57 } 58 59 } 60 61 func processPublicOffering(engine LedgerEngine, actionTx *types.ActionTx) (*Receipt, error) { 62 63 switch offerProcess := actionTx.ActionData.(type) { 64 case *types.InitialOffering: 65 issuer := actionTx.Sender() 66 name := offerProcess.TokenName 67 reIssuable := offerProcess.EnableSPO 68 amount := offerProcess.Value 69 70 tokenID, err := engine.IssueToken(issuer, name, "", reIssuable, amount) 71 if err != nil { 72 receipt := NewReceipt(actionTx.GetTxHash(), ReceiptStatusFailed, err.Error(), emptyAddress) 73 return receipt, err 74 } 75 receipt := NewReceipt(actionTx.GetTxHash(), ReceiptStatusSuccess, strconv.Itoa(int(tokenID)), emptyAddress) 76 return receipt, nil 77 78 case *types.SecondaryOffering: 79 tokenID := offerProcess.TokenId 80 amount := offerProcess.Value 81 82 err := engine.ReIssueToken(tokenID, amount) 83 if err != nil { 84 receipt := NewReceipt(actionTx.GetTxHash(), ReceiptStatusFailed, err.Error(), emptyAddress) 85 return receipt, err 86 } 87 receipt := NewReceipt(actionTx.GetTxHash(), ReceiptStatusSuccess, strconv.Itoa(int(tokenID)), emptyAddress) 88 return receipt, nil 89 90 case *types.DestroyOffering: 91 tokenID := offerProcess.TokenId 92 93 err := engine.DestroyToken(tokenID) 94 if err != nil { 95 receipt := NewReceipt(actionTx.GetTxHash(), ReceiptStatusFailed, err.Error(), emptyAddress) 96 return receipt, err 97 } 98 receipt := NewReceipt(actionTx.GetTxHash(), ReceiptStatusSuccess, strconv.Itoa(int(tokenID)), emptyAddress) 99 return receipt, nil 100 101 default: 102 return nil, fmt.Errorf("unknown offerProcess: %v", actionTx.ActionData) 103 } 104 }