-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathpayment.go
77 lines (63 loc) · 2.62 KB
/
payment.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
package async_sequencer
import (
"context"
"errors"
"time"
currency_lib "github.com/code-payments/code-server/pkg/currency"
"github.com/code-payments/code-server/pkg/kin"
code_data "github.com/code-payments/code-server/pkg/code/data"
"github.com/code-payments/code-server/pkg/code/data/fulfillment"
"github.com/code-payments/code-server/pkg/code/data/payment"
"github.com/code-payments/code-server/pkg/code/data/transaction"
)
func savePaymentRecord(ctx context.Context, data code_data.Provider, fulfillmentRecord *fulfillment.Record, txnRecord *transaction.Record) error {
var transactionIndex uint32
switch fulfillmentRecord.FulfillmentType {
case fulfillment.NoPrivacyWithdraw:
transactionIndex = 4
case fulfillment.TemporaryPrivacyTransferWithAuthority,
fulfillment.PermanentPrivacyTransferWithAuthority,
fulfillment.TransferWithCommitment,
fulfillment.NoPrivacyTransferWithAuthority:
transactionIndex = 2
default:
return errors.New("cannot save payment for fulfillment type")
}
actionRecord, err := data.GetActionById(ctx, fulfillmentRecord.Intent, fulfillmentRecord.ActionId)
if err != nil {
return err
}
if txnRecord.HasErrors || txnRecord.ConfirmationState == transaction.ConfirmationFailed {
return errors.New("cannot save a failed payment")
}
if txnRecord.ConfirmationState != transaction.ConfirmationFinalized {
return errors.New("cannot save a payment that hasn't finalized")
}
usdExchangeRecord, err := data.GetExchangeRate(ctx, currency_lib.USD, time.Now())
if err != nil {
return err
}
paymentRecord := &payment.Record{
// Source and destination should come from fulfillment, since intent or action
// don't include the intermediary steps between accounts that aren't user accounts.
Source: fulfillmentRecord.Source,
Destination: *fulfillmentRecord.Destination,
// todo: Assumes we don't split payments across multiple transactions in an action
Quantity: *actionRecord.Quantity,
// todo: Just filling this in with KIN currency and latest USD rate. I don't think
// these make sense in payment records anymore. These details are captured by
// intents.
ExchangeCurrency: string(currency_lib.KIN),
ExchangeRate: 1.0,
UsdMarketValue: usdExchangeRecord.Rate * float64(kin.FromQuarks(*actionRecord.Quantity)),
IsExternal: false,
Rendezvous: fulfillmentRecord.Intent,
TransactionId: txnRecord.Signature,
TransactionIndex: transactionIndex,
BlockId: txnRecord.Slot,
BlockTime: txnRecord.BlockTime,
ConfirmationState: transaction.ConfirmationFinalized,
CreatedAt: time.Now(),
}
return data.UpdateOrCreatePayment(ctx, paymentRecord)
}