/
models_transaction_cc_bitcoin_cash.go
331 lines (295 loc) · 7.83 KB
/
models_transaction_cc_bitcoin_cash.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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
package marketplace
import (
"github.com/bobanderson-malibu/dotfree/dot-free-market/modules/apis"
)
type BitcoinCashTransaction struct {
Uuid string `json:"uuid" gorm:"primary_key"`
Amount float64 `json:"amount"`
IsMultisig bool `json:"is_multisig"`
BuyerPublicKey string `json:"buyer_public_key"`
SellerPublicKey string `json:"seller_public_key"`
MarketPublicKey string `json:"market_public_key"`
MarketPrivateKey string `json:"market_private_key"`
}
/*
Database Methods
*/
func (t BitcoinCashTransaction) Save() error {
if existing, _ := FindBitcoinCashTransactionByUuid(t.Uuid); existing == nil {
return database.Create(&t).Error
}
return database.Save(&t).Error
}
func FindBitcoinCashTransactionByUuid(uuid string) (*BitcoinCashTransaction, error) {
var item BitcoinCashTransaction
err := database.
Where(&BitcoinCashTransaction{Uuid: uuid}).
First(&item).
Error
if err != nil {
return nil, err
}
return &item, err
}
/*
Financial Methods
*/
func (bt BitcoinCashTransaction) UpdateTransactionStatus(t Transaction) error {
newAmount, err := apis.GetAmountOnBCHAddress(t.Uuid)
if err != nil {
return err
}
if t.CurrentAmountPaid() == newAmount.Balance {
return nil
}
if t.IsPending() {
if (bt.Amount - newAmount.Balance) <= bt.Amount*0.01 {
return t.SetTransactionStatus(
"COMPLETED",
newAmount.Balance,
"Transaction funded",
"",
nil,
)
}
return t.SetTransactionStatus(
"PENDING",
newAmount.Balance,
"Transaction amount updated",
"",
nil,
)
}
return t.SetTransactionStatus(
t.CurrentPaymentStatus(),
newAmount.Balance,
"Transaction amount updated",
"",
nil,
)
}
func (bt BitcoinCashTransaction) Release(t Transaction, comment, userUuid string) error {
var (
err error
addressFrom = t.Uuid
commission = t.CommissionPercent()
payments = []apis.BCHPayment{}
)
buyer, err := FindUserByUuid(t.BuyerUuid)
if err != nil {
return err
}
store, err := FindStoreByUuid(t.StoreUuid)
if err != nil {
return err
}
if bt.IsMultisig {
t.SetTransactionStatus(
"RELEASED",
t.CurrentAmountPaid(),
comment,
userUuid,
nil,
)
return nil
}
// Vendor address
addressTo := store.BitcoinCashAddress()
payments = []apis.BCHPayment{
{
Address: addressTo,
Percent: 1. - commission,
},
}
var buyerReferralPayment *ReferralPayment
var sellerReferralPayment *ReferralPayment
usdRate := GetCurrencyRate("BCH", "USD")
// Buyer Inviter commission address
buyerInviter := buyer.Iniviter()
if buyerInviter != nil {
inviterPercent := MARKETPLACE_SETTINGS.FreeAccountReferralPercent
if buyerInviter.IsGoldAccount {
inviterPercent = MARKETPLACE_SETTINGS.GoldAccountCommission
}
if buyerInviter.IsSilverAccount {
inviterPercent = MARKETPLACE_SETTINGS.SilverAccountCommission
}
if buyerInviter.IsBronzeAccount {
inviterPercent = MARKETPLACE_SETTINGS.BronzeAccountCommission
}
inviterWallet := buyerInviter.BitcoinCash
if inviterWallet == "" {
inviterWallet = buyerInviter.FindRecentBitcoinCashWallet().PublicKey
}
payments = append(payments, apis.BCHPayment{
Address: inviterWallet,
Percent: commission * inviterPercent,
})
buyerReferralPayment = &ReferralPayment{
TransactionUuid: t.Uuid,
ReferralPercent: inviterPercent,
ReferralPaymentBCH: commission * inviterPercent * bt.Amount,
ReferralPaymentUSD: commission * inviterPercent * bt.Amount * usdRate,
UserUuid: buyer.InviterUuid,
IsBuyerReferral: true,
}
}
// Buyer Inviter commission address
vendorInviter := store.InviterUser()
if vendorInviter != nil {
inviterPercent := MARKETPLACE_SETTINGS.FreeAccountReferralPercent
if vendorInviter.IsGoldAccount {
inviterPercent = MARKETPLACE_SETTINGS.GoldAccountCommission
}
if vendorInviter.IsSilverAccount {
inviterPercent = MARKETPLACE_SETTINGS.SilverAccountCommission
}
if vendorInviter.IsBronzeAccount {
inviterPercent = MARKETPLACE_SETTINGS.BronzeAccountCommission
}
inviterWallet := vendorInviter.BitcoinCash
if inviterWallet == "" {
inviterWallet = vendorInviter.FindRecentBitcoinCashWallet().PublicKey
}
payments = append(payments, apis.BCHPayment{
Address: inviterWallet,
Percent: commission * inviterPercent,
})
sellerReferralPayment = &ReferralPayment{
TransactionUuid: t.Uuid,
ReferralPercent: inviterPercent,
ReferralPaymentBCH: commission * inviterPercent * bt.Amount,
ReferralPaymentUSD: commission * inviterPercent * bt.Amount * usdRate,
UserUuid: vendorInviter.Uuid,
IsBuyerReferral: false,
}
}
result, err := apis.SendBCHFromSingleWalletWithPercentSplit(addressFrom, payments)
if err != nil {
return err
}
receipt, err := CreateBCHPaymentReceipt(result)
if err != nil {
return err
}
if len(t.Status) > 0 && t.Status[(len(t.Status)-1)].Status != "RELEASED" {
t.SetTransactionStatus(
"RELEASED",
t.CurrentAmountPaid(),
comment,
userUuid,
&receipt,
)
if buyerReferralPayment != nil {
buyerReferralPayment.Save()
}
if sellerReferralPayment != nil {
sellerReferralPayment.Save()
}
}
return nil
}
func (bt BitcoinCashTransaction) Cancel(t Transaction, comment, userUuid string) error {
if bt.IsMultisig {
t.SetTransactionStatus(
"CANCELLED",
t.CurrentAmountPaid(),
comment,
userUuid,
nil,
)
return nil
}
buyer, err := FindUserByUuid(t.BuyerUuid)
if err != nil {
return err
}
var (
addressFrom = t.Uuid
buyerWallet = buyer.FindRecentBitcoinCashWallet()
addressTo = buyerWallet.PublicKey
)
payments := []apis.BCHPayment{
{Address: addressTo, Percent: 1.},
}
result, err := apis.SendBCHFromSingleWalletWithPercentSplit(addressFrom, payments)
if err != nil {
return err
}
receipt, err := CreateBCHPaymentReceipt(result)
if err != nil {
return err
}
if len(t.Status) > 0 && t.Status[(len(t.Status)-1)].Status != "CANCELLED" {
t.SetTransactionStatus(
"CANCELLED",
t.CurrentAmountPaid(),
comment,
userUuid,
&receipt,
)
}
return nil
}
func CreateBitcoinCashTransaction(
itemPackage Package,
buyer User,
tp string,
quantity int,
shippingPrice float64,
) (BitcoinCashTransaction, error) {
var bitcoinTransaction BitcoinCashTransaction
wallet, err := apis.GenerateBCHAddress("escrow")
if err != nil {
return BitcoinCashTransaction{}, err
}
switch tp {
case "bitcoin_cash_multisig":
var (
buyerPk = buyer.BitcoinMultisigPublicKey
sellerPk = itemPackage.Store.BitcoinCashMultisigPublicKey()
)
address, marketPublicKey, marketPrivateKey, err := apis.GenerateBCHMultisigAddress(buyerPk, sellerPk)
if err != nil {
return BitcoinCashTransaction{}, err
}
bitcoinTransaction = BitcoinCashTransaction{
Uuid: address,
Amount: itemPackage.GetPrice("BCH")*float64(quantity) + shippingPrice,
IsMultisig: true,
SellerPublicKey: sellerPk,
BuyerPublicKey: buyerPk,
MarketPublicKey: marketPublicKey,
MarketPrivateKey: marketPrivateKey,
}
default:
bitcoinTransaction = BitcoinCashTransaction{
Uuid: wallet,
Amount: itemPackage.GetPrice("BCH")*float64(quantity) + shippingPrice,
}
}
return bitcoinTransaction, bitcoinTransaction.Save()
}
/*
Tx Stats
*/
func GetBitcoinCashTxStatsForVendor(uuid string) TxStats {
var stats TxStats
database.
Table("v_current_bitcoin_cash_transaction_statuses").
Select("count(*) as tx_number, sum(amount) as tx_volume").
Where("store_uuid = ?", uuid).
Where("current_status NOT IN ('CANCELLED', 'FAILED', 'PENDING')").
First(&stats)
return stats
}
func GetBitcoinCashTxStatsForBuyer(uuid string) TxStats {
var stats TxStats
database.
Table("v_current_bitcoin_cash_transaction_statuses").
Select("count(*) as tx_count, sum(amount) as tx_volume").
Where("buyer_uuid = ?", uuid).
Where("current_status NOT IN ('CANCELLED', 'FAILED', 'PENDING')").
First(&stats)
return stats
}