In general we should have the concept of In & Out where deposits, coinbase referral money, and buy transactions would be "In" while withdrawals and sell transactions are "Out"
It's up to you to design this but I think we can create a new table called CashFlow where transaction inherits from and buy TX is "In" cashflow while sell TX is "out" cashflow and similarly for referrals, deposits and withdrawals. And we calculate using cashflows.
In general we should have the concept of In & Out where deposits, coinbase referral money, and buy transactions would be "In" while withdrawals and sell transactions are "Out" It's up to you to design this but I think we can create a new table called CashFlow where transaction inherits from and buy TX is "In" cashflow while sell TX is "out" cashflow and similarly for referrals, deposits and withdrawals. And we calculate using cashflows.