def _check_balances(cursor):
 b = cursor.all("\n        select p.username, expected, balance as actual\n          from (\n            select username, sum(a) as expected\n              from (\n                      select participant as username, sum(amount) as a\n                        from exchanges\n                       where amount > 0\n                         and (status = 'unknown' or status = 'succeeded')\n                    group by participant\n\n                       union all\n\n                      select participant as username, sum(amount-fee) as a\n                        from exchanges\n                       where amount < 0\n                         and (status = 'unknown' or status <> 'failed')\n                    group by participant\n\n                       union all\n\n                      select tipper as username, sum(-amount) as a\n                        from transfers\n                    group by tipper\n\n                       union all\n\n                      select participant as username, sum(amount) as a\n                        from payments\n                       where direction='to-participant'\n                    group by participant\n\n                       union all\n\n                      select participant as username, sum(-amount) as a\n                        from payments\n                       where direction='to-team'\n                    group by participant\n\n                       union all\n\n                      select tippee as username, sum(amount) as a\n                        from transfers\n                    group by tippee\n                    ) as foo\n            group by username\n          ) as foo2\n        join participants p on p.username = foo2.username\n        where expected <> p.balance\n    ")
 assert (len(b) == 0), 'conflicting balances: {}'.format(b)

def _check_balances ( cursor ) : b = cursor . all ( "\n        select p.username, expected, balance as actual\n          from (\n            select username, sum(a) as expected\n              from (\n                      select participant as username, sum(amount) as a\n                        from exchanges\n                       where amount > 0\n                         and (status = 'unknown' or status = 'succeeded')\n                    group by participant\n\n                       union all\n\n                      select participant as username, sum(amount-fee) as a\n                        from exchanges\n                       where amount < 0\n                         and (status = 'unknown' or status <> 'failed')\n                    group by participant\n\n                       union all\n\n                      select tipper as username, sum(-amount) as a\n                        from transfers\n                    group by tipper\n\n                       union all\n\n                      select participant as username, sum(amount) as a\n                        from payments\n                       where direction='to-participant'\n                    group by participant\n\n                       union all\n\n                      select participant as username, sum(-amount) as a\n                        from payments\n                       where direction='to-team'\n                    group by participant\n\n                       union all\n\n                      select tippee as username, sum(amount) as a\n                        from transfers\n                    group by tippee\n                    ) as foo\n            group by username\n          ) as foo2\n        join participants p on p.username = foo2.username\n        where expected <> p.balance\n    " ) assert ( len ( b ) == 0 ) , 'conflicting balances: {}' . format ( b )
recalculates balances for all participants from transfers and exchanges .