2018-01-26 05:59:21 +00:00
|
|
|
package transactions
|
|
|
|
|
|
|
|
import (
|
|
|
|
"github.com/ethereum/go-ethereum/accounts/keystore"
|
|
|
|
"github.com/status-im/status-go/geth/signal"
|
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
// EventTransactionQueued is triggered when send transaction request is queued
|
|
|
|
EventTransactionQueued = "transaction.queued"
|
|
|
|
// EventTransactionFailed is triggered when send transaction request fails
|
|
|
|
EventTransactionFailed = "transaction.failed"
|
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
// SendTransactionNoErrorCode is sent when no error occurred.
|
|
|
|
SendTransactionNoErrorCode = iota
|
|
|
|
// SendTransactionDefaultErrorCode is every case when there is no special tx return code.
|
|
|
|
SendTransactionDefaultErrorCode
|
|
|
|
// SendTransactionPasswordErrorCode is sent when account failed verification.
|
|
|
|
SendTransactionPasswordErrorCode
|
|
|
|
// SendTransactionTimeoutErrorCode is sent when tx is timed out.
|
|
|
|
SendTransactionTimeoutErrorCode
|
|
|
|
// SendTransactionDiscardedErrorCode is sent when tx was discarded.
|
|
|
|
SendTransactionDiscardedErrorCode
|
|
|
|
)
|
|
|
|
|
|
|
|
var txReturnCodes = map[error]int{
|
2018-01-05 20:58:17 +00:00
|
|
|
nil: SendTransactionNoErrorCode,
|
|
|
|
keystore.ErrDecrypt: SendTransactionPasswordErrorCode,
|
|
|
|
ErrQueuedTxTimedOut: SendTransactionTimeoutErrorCode,
|
|
|
|
ErrQueuedTxDiscarded: SendTransactionDiscardedErrorCode,
|
2018-01-26 05:59:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// SendTransactionEvent is a signal sent on a send transaction request
|
|
|
|
type SendTransactionEvent struct {
|
2018-04-04 17:39:38 +00:00
|
|
|
ID string `json:"id"`
|
|
|
|
Args SendTxArgs `json:"args"`
|
|
|
|
MessageID string `json:"message_id"`
|
2018-01-26 05:59:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// NotifyOnEnqueue returns handler that processes incoming tx queue requests
|
2018-04-04 17:39:38 +00:00
|
|
|
func NotifyOnEnqueue(queuedTx *QueuedTx) {
|
2018-01-26 05:59:21 +00:00
|
|
|
signal.Send(signal.Envelope{
|
|
|
|
Type: EventTransactionQueued,
|
|
|
|
Event: SendTransactionEvent{
|
2018-04-04 17:39:38 +00:00
|
|
|
ID: queuedTx.ID,
|
2018-01-26 05:59:21 +00:00
|
|
|
Args: queuedTx.Args,
|
2018-04-04 17:39:38 +00:00
|
|
|
MessageID: messageIDFromContext(queuedTx.Context),
|
2018-01-26 05:59:21 +00:00
|
|
|
},
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
// ReturnSendTransactionEvent is a JSON returned whenever transaction send is returned
|
|
|
|
type ReturnSendTransactionEvent struct {
|
2018-04-04 17:39:38 +00:00
|
|
|
ID string `json:"id"`
|
|
|
|
Args SendTxArgs `json:"args"`
|
|
|
|
MessageID string `json:"message_id"`
|
|
|
|
ErrorMessage string `json:"error_message"`
|
|
|
|
ErrorCode int `json:"error_code,string"`
|
2018-01-26 05:59:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// NotifyOnReturn returns handler that processes responses from internal tx manager
|
2018-04-04 17:39:38 +00:00
|
|
|
func NotifyOnReturn(queuedTx *QueuedTx, err error) {
|
2018-01-05 20:58:17 +00:00
|
|
|
// we don't want to notify a user if tx was sent successfully
|
|
|
|
if err == nil {
|
2018-01-26 05:59:21 +00:00
|
|
|
return
|
|
|
|
}
|
2018-01-05 20:58:17 +00:00
|
|
|
// discard notifications with empty tx
|
|
|
|
if queuedTx == nil {
|
2018-01-26 05:59:21 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
signal.Send(signal.Envelope{
|
|
|
|
Type: EventTransactionFailed,
|
|
|
|
Event: ReturnSendTransactionEvent{
|
2018-04-04 17:39:38 +00:00
|
|
|
ID: queuedTx.ID,
|
2018-01-26 05:59:21 +00:00
|
|
|
Args: queuedTx.Args,
|
2018-04-04 17:39:38 +00:00
|
|
|
MessageID: messageIDFromContext(queuedTx.Context),
|
2018-01-05 20:58:17 +00:00
|
|
|
ErrorMessage: err.Error(),
|
|
|
|
ErrorCode: sendTransactionErrorCode(err),
|
2018-01-26 05:59:21 +00:00
|
|
|
},
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func sendTransactionErrorCode(err error) int {
|
|
|
|
if code, ok := txReturnCodes[err]; ok {
|
|
|
|
return code
|
|
|
|
}
|
|
|
|
return SendTxDefaultErrorCode
|
|
|
|
}
|