OSDN Git Service

doc(federation): add /list-crosschain-txs docs (#222)
[bytom/vapor.git] / federation / database / orm / cross_transaction.go
1 package orm
2
3 import (
4         "database/sql"
5         "encoding/json"
6
7         "github.com/vapor/errors"
8         "github.com/vapor/federation/common"
9         "github.com/vapor/federation/types"
10 )
11
12 type CrossTransaction struct {
13         ID                   uint64 `gorm:"primary_key"`
14         ChainID              uint64
15         SourceBlockHeight    uint64
16         SourceBlockHash      string
17         SourceTxIndex        uint64
18         SourceMuxID          string
19         SourceTxHash         string
20         SourceRawTransaction string
21         DestBlockHeight      sql.NullInt64  `sql:"default:null"`
22         DestBlockHash        sql.NullString `sql:"default:null"`
23         DestTxIndex          sql.NullInt64  `sql:"default:null"`
24         DestTxHash           sql.NullString `sql:"default:null"`
25         Status               uint8
26         CreatedAt            types.Timestamp
27         UpdatedAt            types.Timestamp
28
29         Chain *Chain `gorm:"foreignkey:ChainID"`
30         Reqs  []*CrossTransactionReq
31 }
32
33 func (c *CrossTransaction) MarshalJSON() ([]byte, error) {
34         var status string
35         switch c.Status {
36         case common.CrossTxPendingStatus:
37                 status = common.CrossTxPendingStatusLabel
38         case common.CrossTxCompletedStatus:
39                 status = common.CrossTxCompletedStatusLabel
40         default:
41                 return nil, errors.New("unknown cross-chain tx status")
42         }
43
44         return json.Marshal(&struct {
45                 FromChain         string                 `json:"from_chain"`
46                 SourceBlockHeight uint64                 `json:"source_block_height"`
47                 SourceBlockHash   string                 `json:"source_block_hash"`
48                 SourceTxIndex     uint64                 `json:"source_tx_index"`
49                 SourceTxHash      string                 `json:"source_tx_hash"`
50                 DestBlockHeight   uint64                 `json:"dest_block_height"`
51                 DestBlockHash     string                 `json:"dest_block_hash"`
52                 DestTxIndex       uint64                 `json:"dest_tx_index"`
53                 DestTxHash        string                 `json:"dest_tx_hash"`
54                 Status            string                 `json:"status"`
55                 Reqs              []*CrossTransactionReq `json:"crosschain_requests"`
56         }{
57                 FromChain:         c.Chain.Name,
58                 SourceBlockHeight: c.SourceBlockHeight,
59                 SourceBlockHash:   c.SourceBlockHash,
60                 SourceTxIndex:     c.SourceTxIndex,
61                 SourceTxHash:      c.SourceTxHash,
62                 DestBlockHeight:   uint64(c.DestBlockHeight.Int64),
63                 DestBlockHash:     c.DestBlockHash.String,
64                 DestTxIndex:       uint64(c.DestTxIndex.Int64),
65                 DestTxHash:        c.DestTxHash.String,
66                 Status:            status,
67                 Reqs:              c.Reqs,
68         })
69 }