@@ -22,11 +22,12 @@ type TraceProcessor struct {
2222}
2323
2424type TraceOperation struct {
25- dbBlock * model.Block
26- minerAddr * model.Address
27- traceArr []* model.Trace
28- txArr []* model.Tx
29- Err error
25+ dbBlock * model.Block
26+ minerAddr * model.Address
27+ traceArr []* model.Trace
28+ txArr []* model.Tx
29+ contractLifecycleArr []* model.ContractLifecycle
30+ Err error
3031}
3132
3233func newErrOperation (err error ) * TraceOperation {
@@ -47,7 +48,9 @@ func (o *TraceOperation) Exec(tx *gorm.DB) error {
4748 o .dbBlock .MinerID = addrPre .ID
4849
4950 //save block
50- tx .Create (& o .dbBlock )
51+ if err := tx .Create (& o .dbBlock ).Error ; err != nil {
52+ return errors .Wrap (err , "create block failed" )
53+ }
5154
5255 // update block id for tx record
5356 for _ , txPO := range o .txArr {
@@ -71,6 +74,15 @@ func (o *TraceOperation) Exec(tx *gorm.DB) error {
7174 }
7275 }
7376
77+ if len (o .contractLifecycleArr ) > 0 {
78+ for _ , lifecycle := range o .contractLifecycleArr {
79+ lifecycle .TxId = o .txArr [lifecycle .TransactionPosition ].ID
80+ }
81+ if err := tx .Create (& o .contractLifecycleArr ).Error ; err != nil {
82+ return errors .Wrap (err , "create contract lifecycle" )
83+ }
84+ }
85+
7486 return nil
7587}
7688
@@ -150,10 +162,11 @@ func (t *TraceProcessor) Process(data evmUtil.BlockData) dbUtil.Operation {
150162 Model : model.Model {
151163 CreatedAt : dbBlock .CreatedAt ,
152164 },
153- TxSenderId : txArr [* trace .TransactionPosition ].FromId ,
154- ContractId : dbTrace .ToId ,
155- Value : decimal .NewFromBigInt (create .Value , - 18 ),
156- Event : model .ContractLifecycleCreate ,
165+ TransactionPosition : * trace .TransactionPosition ,
166+ TxSenderId : txArr [* trace .TransactionPosition ].FromId ,
167+ ContractId : dbTrace .ToId ,
168+ Value : decimal .NewFromBigInt (create .Value , - 18 ),
169+ Event : model .ContractLifecycleCreate ,
157170 })
158171 case types .TRACE_SUICIDE :
159172 suicide , _ := trace .Action .(types.Suicide )
@@ -185,10 +198,11 @@ func (t *TraceProcessor) Process(data evmUtil.BlockData) dbUtil.Operation {
185198 }
186199
187200 return & TraceOperation {
188- dbBlock : & dbBlock ,
189- minerAddr : & dbAddr ,
190- traceArr : traceArr ,
191- txArr : txArr ,
201+ dbBlock : & dbBlock ,
202+ minerAddr : & dbAddr ,
203+ traceArr : traceArr ,
204+ txArr : txArr ,
205+ contractLifecycleArr : contractLifecycleArr ,
192206 }
193207}
194208
@@ -199,7 +213,11 @@ func buildTxFromReceipt(db *gorm.DB, receipts []*types.Receipt, blockTime time.T
199213 if err != nil {
200214 return nil , err
201215 }
202- toAddr , err := model .MakeAddrId (db , receipt .To .Hex (), blockTime )
216+ to := receipt .To
217+ if to == nil {
218+ to = receipt .ContractAddress
219+ }
220+ toAddr , err := model .MakeAddrId (db , to .Hex (), blockTime )
203221 if err != nil {
204222 return nil , err
205223 }
0 commit comments