-
Notifications
You must be signed in to change notification settings - Fork 5
/
transaction.go
104 lines (88 loc) · 3.05 KB
/
transaction.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
package payd
import (
"context"
"time"
"gopkg.in/guregu/null.v3"
)
// defines states a transaction can have.
const (
StateTxBroadcast TxState = "broadcast"
StateTxFailed TxState = "failed"
StateTxPending TxState = "pending"
)
// TxState defines states a transaction can have.
type TxState string
// Transaction defines a single transaction.
type Transaction struct {
PaymentID string `db:"paymentid"`
TxID string `db:"tx_id"`
TxHex string `db:"tx_hex"`
CreatedAt time.Time `db:"created_at"`
Outputs []Txo `db:"-"`
State string `enums:"pending,broadcast,failed,deleted"`
}
// Txo defines a single txo and can be returned from the data store.
type Txo struct {
Outpoint string `db:"outpoint"`
TxID string `db:"tx_id"`
Vout int `db:"vout"`
KeyName null.String `db:"key_name"`
DerivationPath null.String `db:"derivation_path"`
LockingScript string `db:"locking_script"`
Satoshis uint64 `db:"satoshis"`
SpentAt null.Time `db:"spent_at"`
SpendingTxID null.String `db:"spending_txid"`
CreatedAt time.Time `db:"created_at"`
ModifiedAt time.Time `db:"updated_at"`
}
// TransactionCreate is used to insert a tx into the data store.
// To save calls, Txos can be included to also add in the same transaction.
type TransactionCreate struct {
InvoiceID string `db:"invoice_id"`
RefundTo null.String `db:"refund_to"`
TxID string `db:"tx_id"`
TxHex string `db:"tx_hex"`
Outputs []*TxoCreate `db:"-"`
}
// SpendTxo can be used to update a transaction out with information
// on when it was spent and by what transaction.
type SpendTxo struct {
SpentAt *time.Time
SpendingTxID string
}
// SpendTxoArgs are used to identify the transaction output to mark as spent.
type SpendTxoArgs struct {
Outpoint string
}
// TxoArgs is used to get a single txo.
type TxoArgs struct {
Outpoint string
}
// TransactionArgs are used to identify a specific tx.
type TransactionArgs struct {
TxID string `db:"tx_id"`
}
// TransactionStateUpdate contains information to update a tx.
type TransactionStateUpdate struct {
State TxState `db:"state"`
FailReason null.String `db:"fail_reason"`
}
// TransactionWriter will add and update transaction data.
type TransactionWriter interface {
TransactionCreate(ctx context.Context, req TransactionCreate) error
// TransactionUpdateState can be used to change a tx state (failed, broadcast).
TransactionUpdateState(ctx context.Context, args TransactionArgs, req TransactionStateUpdate) error
}
// TransactionSubmitArgs are used to identify a tx.
type TransactionSubmitArgs struct {
InvoiceID string `param:"invoiceid"`
}
// TransactionSubmit contains the request to store the tx.
type TransactionSubmit struct {
TxHex string `param:"txHex"`
}
// TransactionService is used to handle transactions.
type TransactionService interface {
// Submit is for testing only and allows a finalised tx to be stored.
Submit(ctx context.Context, args TransactionSubmitArgs, req TransactionSubmit) error
}