// Create or updates an existing Payment, associated with the given block. // When updating, sets the status to GOOD and sets the block* if not already set. func createOrUpdatePayment(rpcPayment *rpc.RPCPayment, block *Block) *Payment { // SANITY CHECK if block == nil { if rpcPayment.Blockhash != "" || rpcPayment.Blockheight != 0 { panic(NewError("Something is wrong, a rpcPayment with no block should have no hash or height")) } } else { if rpcPayment.Blockhash != block.Hash || rpcPayment.Blockheight != block.Height { panic(NewError("Something is wrong, expected rpcPayment.Blockhash to match block.Hash")) } } // END SANITY CHECK payment := FromRPCPayment(rpcPayment) _, err := SavePayment(db.GetModelDB(), payment) switch db.GetErrorType(err) { case db.ERR_DUPLICATE_ENTRY: UpdatePayment(db.GetModelDB(), payment) default: if err != nil { panic(err) } } return payment }
// This just creates a new row in the accounts_deposits table. // It doesn't actually credit the account, etc. // Must be idempotent. func LoadOrCreateDepositForPayment(payment *bitcoin.Payment) *Deposit { if payment.Id == 0 { panic(NewError("Cannot add deposit for unsaved payment")) } addr := bitcoin.LoadAddress(payment.Address) if addr == nil { panic(NewError("Expected address for payment to deposit")) } deposit := &Deposit{ Type: DEPOSIT_TYPE_CRYPTO, UserId: addr.UserId, Wallet: addr.Wallet, Coin: addr.Coin, Amount: payment.Amount, PaymentId: payment.Id, Status: DEPOSIT_STATUS_PENDING, } _, err := SaveDeposit(db.GetModelDB(), deposit) switch db.GetErrorType(err) { case db.ERR_DUPLICATE_ENTRY: return LoadDepositForPayment(db.GetModelDB(), payment.Id) default: if err != nil { panic(err) } } return deposit }
// If the payment isn't already orphaned, sets it as orphaned. // If need be, also uncredits the user's account. func orphanPayment(payment *Payment) { // Orphan an existing payment. payment.Orphaned = PAYMENT_ORPHANED_STATUS_ORPHANED UpdatePayment(db.GetModelDB(), payment) // Uncredit deposit if need be. balance := UncreditDepositForPayment(payment) if balance.Amount < 0 { OnNegative(balance) } }
// This just creates a new row in the accounts_deposits table. // It doesn't actually credit the account, etc. // Must be idempotent. func CreateDeposit(deposit *Deposit) *Deposit { // SANITY CHECK if deposit.Id != 0 { panic(NewError("Expected a new deposit but got a saved one")) } if deposit.Type != DEPOSIT_TYPE_FIAT { panic(NewError("Expected a fiat (bank) deposit")) } if deposit.PaymentId != 0 { panic(NewError("Fiat (bank) deposit cannot have a payment")) } // TODO move to validator. if deposit.Status != DEPOSIT_STATUS_PENDING { panic(NewError("Expected a pending deposit")) } // END SANITY CHECK _, err := SaveDeposit(db.GetModelDB(), deposit) if err != nil { panic(err) } return deposit }