-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathpegin_quote.go
More file actions
150 lines (132 loc) · 6.44 KB
/
pegin_quote.go
File metadata and controls
150 lines (132 loc) · 6.44 KB
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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
package quote
import (
"context"
"time"
"github.com/rsksmart/liquidity-provider-server/internal/entities"
"github.com/rsksmart/liquidity-provider-server/internal/entities/utils"
)
const (
AcceptedPeginQuoteEventId entities.EventId = "AcceptedPeginQuote"
CallForUserCompletedEventId entities.EventId = "CallForUserCompleted"
RegisterPeginCompletedEventId entities.EventId = "RegisterPeginCompleted"
)
type PeginState string
const (
PeginStateWaitingForDeposit PeginState = "WaitingForDeposit"
PeginStateWaitingForDepositConfirmations PeginState = "WaitingForDepositConfirmations"
PeginStateTimeForDepositElapsed PeginState = "TimeForDepositElapsed"
PeginStateCallForUserSucceeded PeginState = "CallForUserSucceeded"
PeginStateCallForUserFailed PeginState = "CallForUserFailed"
PeginStateRegisterPegInSucceeded PeginState = "RegisterPegInSucceeded"
PeginStateRegisterPegInFailed PeginState = "RegisterPegInFailed"
)
type PeginQuoteRepository interface {
InsertQuote(ctx context.Context, quote CreatedPeginQuote) error
GetQuote(ctx context.Context, hash string) (*PeginQuote, error)
GetPeginCreationData(ctx context.Context, hash string) PeginCreationData
GetQuotes(ctx context.Context, hashes []string) ([]PeginQuote, error)
GetRetainedQuote(ctx context.Context, hash string) (*RetainedPeginQuote, error)
InsertRetainedQuote(ctx context.Context, quote RetainedPeginQuote) error
UpdateRetainedQuote(ctx context.Context, quote RetainedPeginQuote) error
GetRetainedQuoteByState(ctx context.Context, states ...PeginState) ([]RetainedPeginQuote, error)
// DeleteQuotes deletes both regular and retained quotes
DeleteQuotes(ctx context.Context, quotes []string) (uint, error)
}
type CreatedPeginQuote struct {
Hash string
Quote PeginQuote
CreationData PeginCreationData
}
type PeginCreationData struct {
GasPrice *entities.Wei `json:"gasPrice" bson:"gas_price" validate:"required"`
FeePercentage *utils.BigFloat `json:"feePercentage" bson:"gte=0,lt=100,max_decimal_places=2" validate:"required"`
FixedFee *entities.Wei `json:"fixedFee" bson:"fixed_fee" validate:"required"`
}
func PeginCreationDataZeroValue() PeginCreationData {
return PeginCreationData{
GasPrice: entities.NewWei(0),
FeePercentage: utils.NewBigFloat64(0),
FixedFee: entities.NewWei(0),
}
}
type PeginQuote struct {
FedBtcAddress string `json:"fedBTCAddress" bson:"fed_address" validate:"required"`
LbcAddress string `json:"lbcAddress" bson:"lbc_address" validate:"required"`
LpRskAddress string `json:"lpRskAddress" bson:"lp_rsk_address" validate:"required"`
BtcRefundAddress string `json:"btcRefundAddress" bson:"btc_refund_address" validate:"required"`
RskRefundAddress string `json:"rskRefundAddress" bson:"rsk_refund_address" validate:"required"`
LpBtcAddress string `json:"lpBtcAddress" bson:"lp_btc_address" validate:"required"`
CallFee *entities.Wei `json:"callFee" bson:"call_fee" validate:"required"`
PenaltyFee *entities.Wei `json:"penaltyFee" bson:"penalty_fee" validate:"required"`
ContractAddress string `json:"contractAddress" bson:"contract_address" validate:"required"`
Data string `json:"data" bson:"data" validate:""`
GasLimit uint32 `json:"gasLimit,omitempty" bson:"gas_limit" validate:"required"`
Nonce int64 `json:"nonce" bson:"nonce" validate:"required"`
Value *entities.Wei `json:"value" bson:"value" validate:"required"`
AgreementTimestamp uint32 `json:"agreementTimestamp" bson:"agreement_timestamp" validate:"required"`
TimeForDeposit uint32 `json:"timeForDeposit" bson:"time_for_deposit" validate:"required"`
LpCallTime uint32 `json:"lpCallTime" bson:"lp_call_time" validate:"required"`
Confirmations uint16 `json:"confirmations" bson:"confirmations" validate:"required"`
CallOnRegister bool `json:"callOnRegister" bson:"call_on_register"`
GasFee *entities.Wei `json:"gasFee" bson:"gas_fee" validate:"required"`
ProductFeeAmount uint64 `json:"productFeeAmount" bson:"product_fee_amount" validate:""`
}
func (quote *PeginQuote) ExpireTime() time.Time {
return time.Unix(int64(quote.AgreementTimestamp+quote.TimeForDeposit), 0)
}
func (quote *PeginQuote) IsExpired() bool {
return time.Now().After(quote.ExpireTime())
}
func (quote *PeginQuote) Total() *entities.Wei {
if quote.Value == nil {
quote.Value = entities.NewWei(0)
}
if quote.CallFee == nil {
quote.CallFee = entities.NewWei(0)
}
if quote.GasFee == nil {
quote.GasFee = entities.NewWei(0)
}
total := new(entities.Wei)
total.Add(total, quote.Value)
total.Add(total, quote.CallFee)
total.Add(total, entities.NewUWei(quote.ProductFeeAmount))
total.Add(total, quote.GasFee)
return total
}
type RetainedPeginQuote struct {
QuoteHash string `json:"quoteHash" bson:"quote_hash" validate:"required"`
DepositAddress string `json:"depositAddress" bson:"deposit_address" validate:"required"`
Signature string `json:"signature" bson:"signature" validate:"required"`
RequiredLiquidity *entities.Wei `json:"requiredLiquidity" bson:"required_liquidity" validate:"required"`
State PeginState `json:"state" bson:"state" validate:"required"`
UserBtcTxHash string `json:"userBtcTxHash" bson:"user_btc_tx_hash"`
CallForUserTxHash string `json:"callForUserTxHash" bson:"call_for_user_tx_hash"`
RegisterPeginTxHash string `json:"registerPeginTxHash" bson:"register_pegin_tx_hash"`
}
type WatchedPeginQuote struct {
PeginQuote PeginQuote
RetainedQuote RetainedPeginQuote
CreationData PeginCreationData
}
func NewWatchedPeginQuote(peginQuote PeginQuote, retainedQuote RetainedPeginQuote, creationData PeginCreationData) WatchedPeginQuote {
return WatchedPeginQuote{PeginQuote: peginQuote, RetainedQuote: retainedQuote, CreationData: creationData}
}
type AcceptedPeginQuoteEvent struct {
entities.Event
Quote PeginQuote
RetainedQuote RetainedPeginQuote
CreationData PeginCreationData
}
type CallForUserCompletedEvent struct {
entities.Event
PeginQuote PeginQuote
RetainedQuote RetainedPeginQuote
CreationData PeginCreationData
Error error
}
type RegisterPeginCompletedEvent struct {
entities.Event
RetainedQuote RetainedPeginQuote
Error error
}