-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathutil.go
191 lines (166 loc) · 6.51 KB
/
util.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
182
183
184
185
186
187
188
189
190
191
package chat
import (
"context"
"time"
"github.com/mr-tron/base58/base58"
"github.com/pkg/errors"
"google.golang.org/protobuf/types/known/timestamppb"
chatpb "github.com/code-payments/code-protobuf-api/generated/go/chat/v1"
transactionpb "github.com/code-payments/code-protobuf-api/generated/go/transaction/v2"
code_data "github.com/code-payments/code-server/pkg/code/data"
"github.com/code-payments/code-server/pkg/code/data/account"
"github.com/code-payments/code-server/pkg/code/data/action"
"github.com/code-payments/code-server/pkg/code/data/intent"
currency_lib "github.com/code-payments/code-server/pkg/currency"
"github.com/code-payments/code-server/pkg/kin"
)
func newProtoChatMessage(
messageId string,
content []*chatpb.Content,
ts time.Time,
) (*chatpb.ChatMessage, error) {
decodedMessageId, err := base58.Decode(messageId)
if err != nil {
return nil, errors.Wrap(err, "error decoding message id")
}
if len(decodedMessageId) != 32 && len(decodedMessageId) != 64 {
return nil, errors.Errorf("invalid message id length of %d", len(decodedMessageId))
}
msg := &chatpb.ChatMessage{
MessageId: &chatpb.ChatMessageId{
Value: decodedMessageId,
},
Ts: timestamppb.New(ts),
Content: content,
}
if err := msg.Validate(); err != nil {
return nil, errors.Wrap(err, "chat message failed validation")
}
return msg, nil
}
// todo: promote more broadly?
func getExchangeDataFromIntent(intentRecord *intent.Record) (*transactionpb.ExchangeData, bool) {
switch intentRecord.IntentType {
case intent.SendPrivatePayment:
return &transactionpb.ExchangeData{
Currency: string(intentRecord.SendPrivatePaymentMetadata.ExchangeCurrency),
ExchangeRate: intentRecord.SendPrivatePaymentMetadata.ExchangeRate,
NativeAmount: intentRecord.SendPrivatePaymentMetadata.NativeAmount,
Quarks: intentRecord.SendPrivatePaymentMetadata.Quantity,
}, true
case intent.SendPublicPayment:
return &transactionpb.ExchangeData{
Currency: string(intentRecord.SendPublicPaymentMetadata.ExchangeCurrency),
ExchangeRate: intentRecord.SendPublicPaymentMetadata.ExchangeRate,
NativeAmount: intentRecord.SendPublicPaymentMetadata.NativeAmount,
Quarks: intentRecord.SendPublicPaymentMetadata.Quantity,
}, true
case intent.ReceivePaymentsPrivately:
return &transactionpb.ExchangeData{
Currency: string(currency_lib.KIN),
ExchangeRate: 1.0,
NativeAmount: float64(intentRecord.ReceivePaymentsPrivatelyMetadata.Quantity) / kin.QuarksPerKin,
Quarks: intentRecord.ReceivePaymentsPrivatelyMetadata.Quantity,
}, true
case intent.ReceivePaymentsPublicly:
return &transactionpb.ExchangeData{
Currency: string(intentRecord.ReceivePaymentsPubliclyMetadata.OriginalExchangeCurrency),
ExchangeRate: intentRecord.ReceivePaymentsPubliclyMetadata.OriginalExchangeRate,
NativeAmount: intentRecord.ReceivePaymentsPubliclyMetadata.OriginalNativeAmount,
Quarks: intentRecord.ReceivePaymentsPubliclyMetadata.Quantity,
}, true
case intent.MigrateToPrivacy2022:
return &transactionpb.ExchangeData{
Currency: string(currency_lib.KIN),
ExchangeRate: 1.0,
NativeAmount: float64(intentRecord.MigrateToPrivacy2022Metadata.Quantity) / kin.QuarksPerKin,
Quarks: intentRecord.MigrateToPrivacy2022Metadata.Quantity,
}, true
case intent.ExternalDeposit:
return &transactionpb.ExchangeData{
Currency: string(currency_lib.KIN),
ExchangeRate: 1.0,
NativeAmount: float64(intentRecord.ExternalDepositMetadata.Quantity) / kin.QuarksPerKin,
Quarks: intentRecord.ExternalDepositMetadata.Quantity,
}, true
}
return nil, false
}
func getMicroPaymentReceiveExchangeDataByOwner(
ctx context.Context,
data code_data.Provider,
exchangeData *transactionpb.ExchangeData,
intentRecord *intent.Record,
actionRecords []*action.Record,
) (map[string]*transactionpb.ExchangeData, error) {
if intentRecord.IntentType != intent.SendPrivatePayment || !intentRecord.SendPrivatePaymentMetadata.IsMicroPayment {
return nil, errors.New("intent is not a micro payment")
}
// Find the action record where the final payment is made
var thirdPartyPaymentAction *action.Record
for _, actionRecord := range actionRecords {
if actionRecord.ActionType != action.NoPrivacyWithdraw {
continue
}
if *actionRecord.Destination == intentRecord.SendPrivatePaymentMetadata.DestinationTokenAccount {
thirdPartyPaymentAction = actionRecord
break
}
}
// Should never happen if the intent is a micropayment
if thirdPartyPaymentAction == nil {
return nil, errors.New("payment action is missing")
}
quarksByTokenAccount := make(map[string]uint64)
quarksByTokenAccount[*thirdPartyPaymentAction.Destination] = *thirdPartyPaymentAction.Quantity
// Find and consolidate all fee payments into a quark amount by token account
var foundCodeFee bool
for _, actionRecord := range actionRecords {
if actionRecord.ActionType != action.NoPrivacyTransfer {
continue
}
if actionRecord.Source != thirdPartyPaymentAction.Source {
continue
}
// The first fee is always Code, and can be skipped
if !foundCodeFee {
foundCodeFee = true
continue
}
quarksByTokenAccount[*actionRecord.Destination] += *actionRecord.Quantity
}
// Consolidate quark amount by owner account
quarksByOwnerAccount := make(map[string]uint64)
for tokenAccount, quarks := range quarksByTokenAccount {
if tokenAccount == intentRecord.SendPrivatePaymentMetadata.DestinationTokenAccount {
if len(intentRecord.SendPrivatePaymentMetadata.DestinationOwnerAccount) > 0 {
quarksByOwnerAccount[intentRecord.SendPrivatePaymentMetadata.DestinationOwnerAccount] += quarks
}
continue
}
accountInfoRecord, err := data.GetAccountInfoByTokenAddress(ctx, tokenAccount)
if err == nil {
quarksByOwnerAccount[accountInfoRecord.OwnerAccount] += quarks
} else if err != account.ErrAccountInfoNotFound {
return nil, err
}
}
// Map result to an exchange data
res := make(map[string]*transactionpb.ExchangeData)
for ownerAccount, quarks := range quarksByOwnerAccount {
res[ownerAccount] = getExchangeDataInOtherQuarkAmount(exchangeData, quarks)
}
return res, nil
}
func getExchangeDataInOtherQuarkAmount(original *transactionpb.ExchangeData, quarks uint64) *transactionpb.ExchangeData {
nativeAmount := original.NativeAmount
if original.Quarks != quarks {
nativeAmount = original.ExchangeRate * float64(quarks) / float64(kin.QuarksPerKin)
}
return &transactionpb.ExchangeData{
Currency: original.Currency,
ExchangeRate: original.ExchangeRate,
NativeAmount: nativeAmount,
Quarks: quarks,
}
}