This repository has been archived by the owner on Aug 21, 2021. It is now read-only.
/
order_validate.go
181 lines (172 loc) · 5.93 KB
/
order_validate.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
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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
package funds
import (
"encoding/hex"
"fmt"
"github.com/notegio/openrelay/config"
"github.com/notegio/openrelay/types"
"github.com/notegio/openrelay/channels"
"github.com/notegio/openrelay/funds/balance"
"log"
"math/big"
)
type OrderValidator interface {
ValidateOrder(order *types.Order) (bool, error)
}
type orderValidator struct {
balanceChecker balance.BalanceChecker
feeToken config.FeeToken
tokenProxy config.TokenProxy
}
type boolOrErr struct {
success bool
err error
}
func (funds *orderValidator) checkBalance(assetData types.AssetData, userAddress *types.Address, required []byte, respond chan boolOrErr) {
requiredInt := new(big.Int)
requiredInt.SetBytes(required[:])
if requiredInt.Cmp(big.NewInt(0)) == 0 {
// If the required amount is 0, there's no point in looking up the balance
respond <- boolOrErr{true, nil}
return
}
balance, err := funds.balanceChecker.GetBalance(assetData, userAddress)
if err != nil {
log.Printf("'%v': '%v', '%v'", err.Error(), hex.EncodeToString(assetData[:]), hex.EncodeToString(userAddress[:]))
respond <- boolOrErr{false, err}
return
}
respond <- boolOrErr{(requiredInt.Cmp(balance) <= 0), nil}
}
func (funds *orderValidator) checkAllowance(assetData types.AssetData, userAddress, proxyAddress *types.Address, required []byte, respond chan boolOrErr) {
requiredInt := new(big.Int)
requiredInt.SetBytes(required[:])
if requiredInt.Cmp(big.NewInt(0)) == 0 {
// If the required amount is 0, there's no point in looking up the allowance
respond <- boolOrErr{true, nil}
return
}
balance, err := funds.balanceChecker.GetAllowance(assetData, userAddress, proxyAddress)
if err != nil {
log.Printf("'%v': '%v', '%v'", err.Error(), hex.EncodeToString(assetData[:]), hex.EncodeToString(userAddress[:]))
respond <- boolOrErr{false, err}
return
}
respond <- boolOrErr{(requiredInt.Cmp(balance) <= 0), nil}
}
func getRemainingAmount(numerator, denominator, target []byte) []byte {
numInt := new(big.Int)
denomInt := new(big.Int)
targetInt := new(big.Int)
numInt.SetBytes(numerator)
denomInt.SetBytes(denominator)
targetInt.SetBytes(target)
mulInt := new(big.Int).Mul(numInt, targetInt)
return new(big.Int).Sub(targetInt, new(big.Int).Div(mulInt, denomInt)).Bytes()
}
// ValidateOrder makes sure that the maker of an order has sufficient funds to
// fill the order and pay makerFees. This assumes that TakerAmountFilled and
// TakerAmountCancelled reflect
func (funds *orderValidator) ValidateOrder(order *types.Order) (bool, error) {
feeToken, err := funds.feeToken.Get(order)
if err != nil {
log.Printf("Error getting fee token '%v'", err.Error())
return false, err
}
makerProxyAddress, err := funds.tokenProxy.Get(order)
if err != nil {
log.Printf("Error getting token proxy address '%v'", err.Error())
return false, err
}
feeProxyAddress, err := funds.tokenProxy.GetById(order, types.ERC20ProxyID)
if err != nil {
log.Printf("Error getting fee token proxy address '%v'", err.Error())
return false, err
}
makerChan := make(chan boolOrErr)
feeChan := make(chan boolOrErr)
makerAllowanceChan := make(chan boolOrErr)
feeAllowanceChan := make(chan boolOrErr)
unavailableAmount := order.TakerAssetAmountFilled.Big()
go funds.checkBalance(
order.MakerAssetData,
order.Maker,
getRemainingAmount(unavailableAmount.Bytes(), order.TakerAssetAmount[:], order.MakerAssetAmount[:]),
makerChan,
)
go funds.checkBalance(
feeToken,
order.Maker,
getRemainingAmount(unavailableAmount.Bytes(), order.TakerAssetAmount[:], order.MakerFee[:]),
feeChan,
)
go funds.checkAllowance(
order.MakerAssetData,
order.Maker,
makerProxyAddress,
getRemainingAmount(unavailableAmount.Bytes(), order.TakerAssetAmount[:], order.MakerAssetAmount[:]),
makerAllowanceChan,
)
go funds.checkAllowance(
feeToken,
order.Maker,
feeProxyAddress,
getRemainingAmount(unavailableAmount.Bytes(), order.TakerAssetAmount[:], order.MakerFee[:]),
feeAllowanceChan,
)
result := true
if chanResult := <-makerChan; !chanResult.success {
log.Printf("Insufficient maker token funds")
if chanResult.err != nil {
if chanResult.err.Error() == "no contract code at given address" {
return false, chanResult.err
}
panic(fmt.Sprintf("RPC Communication Failed: '%v'", chanResult.err.Error()))
}
result = false
}
if chanResult := <-feeChan; !chanResult.success {
log.Printf("Insufficient fee token allowance")
if chanResult.err != nil {
if chanResult.err.Error() == "no contract code at given address" {
return false, chanResult.err
}
panic(fmt.Sprintf("RPC Communication Failed: '%v'", chanResult.err.Error()))
}
result = false
}
if chanResult := <-makerAllowanceChan; !chanResult.success {
log.Printf("Insufficient makers token allowance")
if chanResult.err != nil {
if chanResult.err.Error() == "no contract code at given address" {
return false, chanResult.err
}
panic(fmt.Sprintf("RPC Communication Failed: '%v'", chanResult.err.Error()))
}
result = false
}
if chanResult := <-feeAllowanceChan; !chanResult.success {
log.Printf("Insufficient fee token allowance")
if chanResult.err != nil {
if chanResult.err.Error() == "no contract code at given address" {
return false, chanResult.err
}
panic(fmt.Sprintf("RPC Communication Failed: '%v'", chanResult.err.Error()))
}
result = false
}
return result, nil
}
func NewRpcOrderValidator(rpcUrl string, feeToken config.FeeToken, tokenProxy config.TokenProxy, invalidationChannel channels.ConsumerChannel) (OrderValidator, error) {
if checker, err := balance.NewRpcRoutingBalanceChecker(rpcUrl); err == nil {
if invalidationChannel != nil {
invalidationChannel.AddConsumer(checker)
invalidationChannel.StartConsuming()
}
return &orderValidator{checker, feeToken, tokenProxy}, nil
} else {
return nil, err
}
}
func NewOrderValidator(checker balance.BalanceChecker, feeToken config.FeeToken, tokenProxy config.TokenProxy) OrderValidator {
return &orderValidator{checker, feeToken, tokenProxy}
}