/
storage.go
105 lines (81 loc) · 2.17 KB
/
storage.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
package belt
import (
"fmt"
"html/template"
"time"
"github.com/jinzhu/gorm"
blackfriday "gopkg.in/russross/blackfriday.v2"
"vallon.me/shortening"
)
type FAK uint64
func (f FAK) String() string {
return fmt.Sprintf("%g", float64(f)/100000000)
}
func (f FAK) Int() uint64 {
return uint64(f)
}
type Belt struct {
gorm.Model
Title string `json:"title" gorm:"unique_index;not null"`
Message string `json:"message"`
EndTime time.Time `json:"endtime"`
Options []Option `json:"options"`
}
func (b Belt) Info() template.HTML {
return template.HTML(
blackfriday.Run([]byte(b.Message),
blackfriday.WithExtensions(
blackfriday.HardLineBreak|
blackfriday.NoEmptyLineBeforeBlock|
blackfriday.CommonExtensions)))
}
func (b Belt) ShortID() string {
return string(shortening.Encode(uint64(b.ID)))
}
func (b Belt) Ended() bool {
return b.EndTime.Before(time.Now())
}
type Option struct {
ID uint `gorm:"primary_key"`
BeltID uint `json:"-" gorm:"unique_index:unique_belt_option"`
Name string `json:"name" gorm:"unique_index:unique_belt_option;not null"`
Image string `json:"image"`
Votes uint `json:"votes"`
Bets []Bet
}
func (o Option) Value(minConf int) FAK {
var val FAK
for _, bet := range o.Bets {
val += bet.Value(minConf)
}
return val
}
type Bet struct {
ID uint `json:"-" gorm:"primary_key"`
CreatedAt time.Time `json:"-"`
OptionID uint `json:"-"`
WIFKey string `json:"-"`
WatchAddr string `json:"watchaddr" gorm:"size:35;unique_index;not null"`
PayAddr string `json:"payaddr" gorm:"size:35"`
Transactions []BetTx `json:"txs"`
}
func (b Bet) Value(minConf int) FAK {
var val FAK
for _, tx := range b.Transactions {
if minConf <= tx.Confirmations {
val += tx.Value
}
}
return val
}
type BetTx struct {
ID uint `json:"-" gorm:"primary_key"`
CreatedAt time.Time `json:"-"`
BetID uint `json:"-"`
Hash string `json:"hash" gorm:"size:64;unique_index;not null"`
Index uint32 `json:"index" gorm:"not null"`
Value FAK `json:"value" gorm:"not null"`
Confirmations int `json:"confirmations" gorm:"default:0;not null"`
}
// const HashSize = 32
// const MaxHashStringSize = HashSize * 2