@@ -20,8 +20,8 @@ import (
20
20
)
21
21
22
22
type Transaction interface {
23
- Quote (d model.TransactionRequest ) (model.ExecutionRequest , error )
24
- Execute (e model.ExecutionRequest , userId string , deviceId string , ip string ) (model.TransactionReceipt , error )
23
+ Quote (d model.TransactionRequest ) (model.PrecisionSafeExecutionRequest , error )
24
+ Execute (e model.PrecisionSafeExecutionRequest , userId string , deviceId string , ip string ) (model.TransactionReceipt , error )
25
25
}
26
26
27
27
type TransactionRepos struct {
@@ -55,27 +55,28 @@ func NewTransaction(repos repository.Repositories, redis store.RedisStore, unit2
55
55
}
56
56
57
57
type transactionProcessingData struct {
58
- userId * string
59
- user * model.User
60
- deviceId * string
61
- ip * string
62
- executor * Executor
63
- processingFeeAsset * model.Asset
64
- transactionModel * model.Transaction
65
- chain * Chain
66
- executionRequest * model.ExecutionRequest
67
- cardAuthorization * AuthorizedCharge
68
- cardCapture * payments.CapturesResponse
69
- preBalance * float64
70
- recipientWalletId * string
71
- txId * string
72
- cumulativeValue * big.Int
73
- trueGas * uint64
58
+ userId * string
59
+ user * model.User
60
+ deviceId * string
61
+ ip * string
62
+ executor * Executor
63
+ processingFeeAsset * model.Asset
64
+ transactionModel * model.Transaction
65
+ chain * Chain
66
+ executionRequest * model.ExecutionRequest
67
+ precisionSafeExecutionRequest * model.PrecisionSafeExecutionRequest
68
+ cardAuthorization * AuthorizedCharge
69
+ cardCapture * payments.CapturesResponse
70
+ preBalance * float64
71
+ recipientWalletId * string
72
+ txId * string
73
+ cumulativeValue * big.Int
74
+ trueGas * uint64
74
75
}
75
76
76
- func (t transaction ) Quote (d model.TransactionRequest ) (model.ExecutionRequest , error ) {
77
+ func (t transaction ) Quote (d model.TransactionRequest ) (model.PrecisionSafeExecutionRequest , error ) {
77
78
// TODO: use prefab service to parse d and fill out known params
78
- res := model.ExecutionRequest {TransactionRequest : d }
79
+ res := model.PrecisionSafeExecutionRequest {TransactionRequest : d }
79
80
// chain, err := model.ChainInfo(uint64(d.ChainId))
80
81
chain , err := ChainInfo (uint64 (d .ChainId ), t .repos .Network , t .repos .Asset )
81
82
if err != nil {
@@ -91,7 +92,7 @@ func (t transaction) Quote(d model.TransactionRequest) (model.ExecutionRequest,
91
92
if err != nil {
92
93
return res , common .StringError (err )
93
94
}
94
- res .Quote = estimateUSD
95
+ res .PrecisionSafeQuote = common . QuoteToPrecise ( estimateUSD )
95
96
executor .Close ()
96
97
97
98
// Sign entire payload
@@ -108,9 +109,9 @@ func (t transaction) Quote(d model.TransactionRequest) (model.ExecutionRequest,
108
109
return res , nil
109
110
}
110
111
111
- func (t transaction ) Execute (e model.ExecutionRequest , userId string , deviceId string , ip string ) (res model.TransactionReceipt , err error ) {
112
+ func (t transaction ) Execute (e model.PrecisionSafeExecutionRequest , userId string , deviceId string , ip string ) (res model.TransactionReceipt , err error ) {
112
113
t .getStringInstrumentsAndUserId ()
113
- p := transactionProcessingData {executionRequest : & e , userId : & userId , deviceId : & deviceId , ip : & ip }
114
+ p := transactionProcessingData {precisionSafeExecutionRequest : & e , executionRequest : & model. ExecutionRequest {} , userId : & userId , deviceId : & deviceId , ip : & ip }
114
115
115
116
// Pre-flight transaction setup
116
117
p , err = t .transactionSetup (p )
@@ -153,7 +154,7 @@ func (t transaction) transactionSetup(p transactionProcessingData) (transactionP
153
154
p .user = & user
154
155
155
156
// Pull chain info needed for execution from repository
156
- chain , err := ChainInfo (uint64 ( p . executionRequest .ChainId ) , t .repos .Network , t .repos .Asset )
157
+ chain , err := ChainInfo (p . precisionSafeExecutionRequest .ChainId , t .repos .Network , t .repos .Asset )
157
158
if err != nil {
158
159
return p , common .StringError (err )
159
160
}
@@ -167,7 +168,7 @@ func (t transaction) transactionSetup(p transactionProcessingData) (transactionP
167
168
p .transactionModel = & transactionModel
168
169
169
170
updateDB := & model.TransactionUpdates {}
170
- processingFeeAsset , err := t .populateInitialTxModelData (* p .executionRequest , updateDB )
171
+ processingFeeAsset , err := t .populateInitialTxModelData (* p .precisionSafeExecutionRequest , updateDB )
171
172
p .processingFeeAsset = & processingFeeAsset
172
173
if err != nil {
173
174
return p , common .StringError (err )
@@ -196,7 +197,7 @@ func (t transaction) transactionSetup(p transactionProcessingData) (transactionP
196
197
197
198
func (t transaction ) safetyCheck (p transactionProcessingData ) (transactionProcessingData , error ) {
198
199
// Test the Tx and update model status
199
- estimateUSD , estimateETH , err := t .testTransaction (* p .executor , p .executionRequest .TransactionRequest , * p .chain , false )
200
+ estimateUSD , estimateETH , err := t .testTransaction (* p .executor , p .precisionSafeExecutionRequest .TransactionRequest , * p .chain , false )
200
201
if err != nil {
201
202
return p , common .StringError (err )
202
203
}
@@ -206,14 +207,15 @@ func (t transaction) safetyCheck(p transactionProcessingData) (transactionProces
206
207
}
207
208
208
209
// Verify the Quote and update model status
209
- _ , err = verifyQuote (* p .executionRequest , estimateUSD )
210
+ _ , err = verifyQuote (* p .precisionSafeExecutionRequest , estimateUSD )
210
211
if err != nil {
211
212
return p , common .StringError (err )
212
213
}
213
214
err = t .updateTransactionStatus ("Quote Verified" , p .transactionModel .Id )
214
215
if err != nil {
215
216
return p , common .StringError (err )
216
217
}
218
+ * p .executionRequest = common .ExecutionRequestToImprecise (* p .precisionSafeExecutionRequest )
217
219
218
220
// Get current balance of primary token
219
221
preBalance , err := (* p .executor ).GetBalance ()
@@ -442,7 +444,7 @@ func (t transaction) postProcess(p transactionProcessingData) {
442
444
}
443
445
}
444
446
445
- func (t transaction ) populateInitialTxModelData (e model.ExecutionRequest , m * model.TransactionUpdates ) (model.Asset , error ) {
447
+ func (t transaction ) populateInitialTxModelData (e model.PrecisionSafeExecutionRequest , m * model.TransactionUpdates ) (model.Asset , error ) {
446
448
txType := "fiat-to-crypto"
447
449
m .Type = & txType
448
450
// TODO populate transactionModel.Tags with key-val pairs for Unit21
@@ -510,7 +512,7 @@ func (t transaction) testTransaction(executor Executor, request model.Transactio
510
512
return res , eth , nil
511
513
}
512
514
513
- func verifyQuote (e model.ExecutionRequest , newEstimate model.Quote ) (bool , error ) {
515
+ func verifyQuote (e model.PrecisionSafeExecutionRequest , newEstimate model.Quote ) (bool , error ) {
514
516
// Null out values which have changed since payload was signed
515
517
dataToValidate := e
516
518
dataToValidate .Signature = ""
@@ -529,7 +531,11 @@ func verifyQuote(e model.ExecutionRequest, newEstimate model.Quote) (bool, error
529
531
if newEstimate .Timestamp - e .Timestamp > 20 {
530
532
return false , common .StringError (errors .New ("verifyQuote: quote expired" ))
531
533
}
532
- if newEstimate .TotalUSD > e .TotalUSD {
534
+ quotedTotal , err := strconv .ParseFloat (e .TotalUSD , 64 )
535
+ if err != nil {
536
+ return false , common .StringError (err )
537
+ }
538
+ if newEstimate .TotalUSD > quotedTotal {
533
539
return false , common .StringError (errors .New ("verifyQuote: price too volatile" ))
534
540
}
535
541
return true , nil
0 commit comments