dkforest

A forum and chat platform (onion)
git clone https://git.dasho.dev/n0tr1v/dkforest.git
Log | Files | Refs | LICENSE

tablePokerXmrTransactions.go (3338B)


      1 package database
      2 
      3 import (
      4 	"dkforest/pkg/utils"
      5 	"github.com/monero-ecosystem/go-monero-rpc-client/wallet"
      6 	"github.com/sirupsen/logrus"
      7 	"time"
      8 )
      9 
     10 const (
     11 	PokerXmrTransactionStatusPending = 1
     12 	PokerXmrTransactionStatusSuccess = 2
     13 	PokerXmrTransactionStatusFailed  = 3
     14 )
     15 
     16 type PokerXmrTransaction struct {
     17 	ID            int64
     18 	Status        int64
     19 	TxID          string
     20 	UserID        UserID
     21 	Address       string
     22 	Amount        Piconero
     23 	Fee           Piconero
     24 	Confirmations uint64
     25 	Height        uint64
     26 	IsIn          bool
     27 	Processed     bool
     28 	CreatedAt     time.Time
     29 	UpdatedAt     time.Time
     30 	User          User
     31 }
     32 
     33 func (t *PokerXmrTransaction) SetStatus(db *DkfDB, newStatus int64) error {
     34 	return db.db.Model(t).Select("Status").Updates(PokerXmrTransaction{Status: newStatus}).Error
     35 }
     36 
     37 func (t *PokerXmrTransaction) HasEnoughConfirmations() bool {
     38 	return t.Confirmations >= t.ConfirmationsNeeded()
     39 }
     40 
     41 func (t *PokerXmrTransaction) ConfirmationsNeeded() uint64 {
     42 	return utils.Ternary(t.Amount.ToPokerChip() <= 20000, uint64(2), 10)
     43 }
     44 
     45 func (d *DkfDB) GetPokerXmrTransactionsSumIn() (out Piconero, err error) {
     46 	var tmp struct{ Amount Piconero }
     47 	err = d.db.Raw(`SELECT SUM(amount) AS amount FROM poker_xmr_transactions WHERE is_in = 1 AND confirmations >= 10`).Scan(&tmp).Error
     48 	return tmp.Amount, err
     49 }
     50 
     51 func (d *DkfDB) GetPokerXmrTransactionsSumOut() (out Piconero, err error) {
     52 	var tmp struct {
     53 		Amount Piconero
     54 		Fee    Piconero
     55 	}
     56 	err = d.db.Raw(`SELECT SUM(amount) AS amount, SUM(fee) AS fee FROM poker_xmr_transactions WHERE is_in = 0 AND status = 2`).Scan(&tmp).Error
     57 	return tmp.Amount + tmp.Fee, err
     58 }
     59 
     60 func (d *DkfDB) GetPokerXmrPendingTransactions() (out []PokerXmrTransaction, err error) {
     61 	err = d.db.Find(&out, "is_in = 0 AND status = 1").Error
     62 	return
     63 }
     64 
     65 func (d *DkfDB) GetLastUserWithdrawPokerXmrTransaction(userID UserID) (out PokerXmrTransaction, err error) {
     66 	err = d.db.Order("id DESC").First(&out, "user_id = ? AND is_in = 0", userID).Error
     67 	return
     68 }
     69 
     70 func (d *DkfDB) GetUserPokerXmrTransactions(userID UserID) (out []PokerXmrTransaction, err error) {
     71 	err = d.db.Order("id DESC").Find(&out, "user_id = ?", userID).Error
     72 	return
     73 }
     74 
     75 func (d *DkfDB) GetPokerXmrTransaction(txID string) (out PokerXmrTransaction, err error) {
     76 	err = d.db.First(&out, "tx_id = ?", txID).Error
     77 	return
     78 }
     79 
     80 func (d *DkfDB) CreatePokerXmrInTransaction(userID UserID, transfer *wallet.Transfer) (out PokerXmrTransaction, err error) {
     81 	out = PokerXmrTransaction{
     82 		TxID:          transfer.TxID,
     83 		UserID:        userID,
     84 		Address:       transfer.Address,
     85 		Height:        transfer.Height,
     86 		Amount:        Piconero(transfer.Amount),
     87 		Fee:           Piconero(transfer.Fee),
     88 		Confirmations: transfer.Confirmations,
     89 		IsIn:          true,
     90 	}
     91 	err = d.db.Create(&out).Error
     92 	return
     93 }
     94 
     95 func (d *DkfDB) CreatePokerXmrTransaction(userID UserID, transfer *wallet.ResponseTransfer) (out PokerXmrTransaction, err error) {
     96 	out = PokerXmrTransaction{
     97 		Status: PokerXmrTransactionStatusPending,
     98 		UserID: userID,
     99 		Amount: Piconero(transfer.Amount),
    100 		Fee:    Piconero(transfer.Fee),
    101 		IsIn:   false}
    102 	err = d.db.Create(&out).Error
    103 	return
    104 }
    105 
    106 func (t *PokerXmrTransaction) Save(db *DkfDB) error {
    107 	return db.db.Save(t).Error
    108 }
    109 
    110 func (t *PokerXmrTransaction) DoSave(db *DkfDB) {
    111 	if err := t.Save(db); err != nil {
    112 		logrus.Error(err)
    113 	}
    114 }