github.com/fibonacci-chain/fbc@v0.0.0-20231124064014-c7636198c1e9/x/infura/types/types.go (about) 1 package types 2 3 import ( 4 "time" 5 6 "github.com/ethereum/go-ethereum/common/hexutil" 7 evm "github.com/fibonacci-chain/fbc/x/evm/watcher" 8 "gorm.io/gorm" 9 ) 10 11 type EngineData struct { 12 TransactionReceipts []*TransactionReceipt 13 Block *Block 14 ContractCodes []*ContractCode 15 } 16 17 type StreamData struct { 18 TransactionReceipts []evm.TransactionReceipt 19 Block evm.Block 20 Transactions []evm.Transaction 21 ContractCodes map[string][]byte 22 } 23 24 func (sd StreamData) ConvertEngineData() EngineData { 25 return EngineData{ 26 TransactionReceipts: convertTransactionReceipts(sd.TransactionReceipts), 27 Block: convertBlocks(sd.Block, sd.Transactions), 28 ContractCodes: convertContractCodes(sd.ContractCodes, int64(sd.Block.Number)), 29 } 30 } 31 32 func convertTransactionReceipts(trs []evm.TransactionReceipt) []*TransactionReceipt { 33 transactionReceipts := make([]*TransactionReceipt, len(trs)) 34 for i, t := range trs { 35 // convert TransactionReceipt 36 receipt := &TransactionReceipt{ 37 Status: uint64(t.Status), 38 CumulativeGasUsed: uint64(t.CumulativeGasUsed), 39 TransactionHash: t.GetHash(), 40 GasUsed: uint64(t.GasUsed), 41 BlockHash: t.GetBlockHash(), 42 BlockNumber: int64(t.BlockNumber), 43 TransactionIndex: uint64(t.TransactionIndex), 44 From: t.GetFrom(), 45 } 46 if t.ContractAddress != nil { 47 receipt.ContractAddress = t.ContractAddress.String() 48 } 49 to := t.GetTo() 50 if to != nil { 51 receipt.To = to.String() 52 } 53 54 // convert TransactionLog 55 transactionLogs := make([]TransactionLog, len(t.Logs)) 56 for i, l := range t.Logs { 57 log := TransactionLog{ 58 Address: l.Address.String(), 59 Data: hexutil.Encode(l.Data), 60 TransactionHash: receipt.TransactionHash, 61 TransactionIndex: receipt.TransactionIndex, 62 LogIndex: uint64(l.Index), 63 BlockNumber: receipt.BlockNumber, 64 BlockHash: receipt.BlockHash, 65 } 66 67 // convert LogTopic 68 logTopics := make([]LogTopic, len(l.Topics)) 69 for i, topic := range l.Topics { 70 logTopics[i] = LogTopic{ 71 Topic: topic.String(), 72 } 73 } 74 log.Topics = logTopics 75 76 transactionLogs[i] = log 77 } 78 79 receipt.Logs = transactionLogs 80 transactionReceipts[i] = receipt 81 } 82 return transactionReceipts 83 } 84 85 func convertBlocks(evmBlock evm.Block, evmTransactions []evm.Transaction) *Block { 86 block := &Block{ 87 Number: int64(evmBlock.Number), 88 Hash: evmBlock.Hash.String(), 89 ParentHash: evmBlock.ParentHash.String(), 90 TransactionsRoot: evmBlock.TransactionsRoot.String(), 91 StateRoot: evmBlock.StateRoot.String(), 92 Miner: evmBlock.Miner.String(), 93 Size: uint64(evmBlock.Size), 94 GasLimit: uint64(evmBlock.GasLimit), 95 GasUsed: evmBlock.GasUsed.ToInt().Uint64(), 96 Timestamp: uint64(evmBlock.Timestamp), 97 } 98 99 transactions := make([]*Transaction, len(evmTransactions)) 100 for i, t := range evmTransactions { 101 tx := &Transaction{ 102 BlockHash: t.BlockHash.String(), 103 BlockNumber: t.BlockNumber.ToInt().Int64(), 104 From: t.From.String(), 105 Gas: uint64(t.Gas), 106 GasPrice: t.GasPrice.String(), 107 Hash: t.Hash.String(), 108 Input: t.Input.String(), 109 Nonce: uint64(t.Nonce), 110 Index: uint64(*t.TransactionIndex), 111 Value: t.Value.String(), 112 V: t.V.String(), 113 R: t.R.String(), 114 S: t.S.String(), 115 } 116 if t.To != nil { 117 tx.To = t.To.String() 118 } 119 transactions[i] = tx 120 } 121 block.Transactions = transactions 122 return block 123 } 124 125 func convertContractCodes(codes map[string][]byte, height int64) []*ContractCode { 126 contractCodes := make([]*ContractCode, 0, len(codes)) 127 for k, v := range codes { 128 contractCodes = append(contractCodes, &ContractCode{ 129 Address: k, 130 Code: hexutil.Encode(v), 131 BlockNumber: height, 132 }) 133 } 134 return contractCodes 135 } 136 137 type TransactionReceipt struct { 138 gorm.Model 139 Status uint64 `gorm:"type:tinyint(4)"` 140 CumulativeGasUsed uint64 `gorm:"type:int(11)"` 141 TransactionHash string `gorm:"type:varchar(66);index;not null"` 142 ContractAddress string `gorm:"type:varchar(42)"` 143 GasUsed uint64 `gorm:"type:int(11)"` 144 BlockHash string `gorm:"type:varchar(66)"` 145 BlockNumber int64 146 TransactionIndex uint64 `gorm:"type:int(11)"` 147 From string `gorm:"type:varchar(42)"` 148 To string `gorm:"type:varchar(42)"` 149 Logs []TransactionLog 150 } 151 152 type TransactionLog struct { 153 gorm.Model 154 Address string `gorm:"type:varchar(42);index;not null"` 155 Data string `gorm:"type:text"` 156 TransactionHash string `gorm:"type:varchar(66)"` 157 TransactionIndex uint64 `gorm:"type:int(11)"` 158 LogIndex uint64 `gorm:"type:int(11)"` 159 BlockHash string `gorm:"type:varchar(66);index;not null"` 160 BlockNumber int64 `gorm:"index;not null"` 161 TransactionReceiptID uint 162 Topics []LogTopic 163 } 164 165 type LogTopic struct { 166 gorm.Model 167 Topic string `gorm:"type:varchar(66)"` 168 TransactionLogID uint 169 } 170 171 type Block struct { 172 Number int64 `gorm:"primaryKey"` 173 Hash string `gorm:"type:varchar(66);index;not null"` 174 ParentHash string `gorm:"type:varchar(66)"` 175 TransactionsRoot string `gorm:"type:varchar(66)"` 176 StateRoot string `gorm:"type:varchar(66)"` 177 Miner string `gorm:"type:varchar(42)"` 178 Size uint64 `gorm:"type:int(11)"` 179 GasLimit uint64 180 GasUsed uint64 181 Timestamp uint64 `gorm:"type:int(11)"` 182 Transactions []*Transaction 183 CreatedAt time.Time 184 UpdatedAt time.Time 185 DeletedAt gorm.DeletedAt `gorm:"index"` 186 } 187 188 type Transaction struct { 189 gorm.Model 190 BlockHash string `gorm:"type:varchar(66)"` 191 BlockNumber int64 192 From string `gorm:"type:varchar(42)"` 193 Gas uint64 `gorm:"type:int(11)"` 194 GasPrice string `gorm:"type:varchar(66)"` 195 Hash string `gorm:"type:varchar(66)"` 196 Input string `gorm:"type:text"` 197 Nonce uint64 `gorm:"type:int(11)"` 198 To string `gorm:"type:varchar(42)"` 199 Index uint64 `gorm:"type:int(11)"` 200 Value string `gorm:"type:varchar(255)"` 201 V string `gorm:"type:varchar(255)"` 202 R string `gorm:"type:varchar(255)"` 203 S string `gorm:"type:varchar(255)"` 204 } 205 206 type ContractCode struct { 207 gorm.Model 208 Address string `gorm:"type:varchar(42);index:unique_address,unique;not null"` 209 Code string 210 BlockNumber int64 211 }