Beispiel #1
0
    async def test_multiple_currencies(self):
        await helpers.call_change_balance(account_id=666, currency=1, amount=1000)

        await self.apply_operations([bank_pb2.Operation(account_id=666, currency=1, amount=1000, type='x.1', description='y.1'),
                                     bank_pb2.Operation(account_id=666, currency=1, amount=-300, type='x.2', description='y.2')])

        await self.apply_operations([bank_pb2.Operation(account_id=667, currency=1, amount=50, type='x.3', description='y.3'),
                                     bank_pb2.Operation(account_id=666, currency=2, amount=1, type='x.4', description='y.4')])

        request = await self.client.post('/accounts/history', data=bank_pb2.AccountHistoryRequest(account_id=666).SerializeToString())
        answer = await self.check_success(request, bank_pb2.AccountHistoryResponse)

        self.assertEqual([(record.currency, record.amount, record.description) for record in answer.history],
                         [(1, 1000, 'y.1'),
                          (1, -300, 'y.2'),
                          (2, 1, 'y.4')])
Beispiel #2
0
async def load_operations():
    results = await db.sql('SELECT * FROM operations ORDER BY created_at ASC')

    return [bank_pb2.Operation(account_id=row['account'],
                               currency=row['currency'],
                               amount=row['amount'],
                               type=row['type'],
                               description=row['description']) for row in results]
Beispiel #3
0
def energy_balance(account_id):
    answer = tt_api.sync_request(url=conf.game_settings.TT_ENERGY_BALANCE,
                                 data=bank_pb2.AccountBalanceRequest(account_id=account_id),
                                 AnswerType=bank_pb2.AccountBalanceResponse)

    return answer.balance.get(ENERGY_CURRENCY_ID, 0)


def change_energy_balance(account_id, type, energy, async=False, autocommit=False):

    if async and not autocommit:
        raise exceptions.AutocommitRequiredForAsyncTransaction()

    operations = [bank_pb2.Operation(account_id=account_id,
                                     currency=ENERGY_CURRENCY_ID,
                                     amount=energy,
                                     type=type)]

    if not async:
        try:
            answer = tt_api.sync_request(url=conf.game_settings.TT_ENERGY_START_TRANSACTION,
                                         data=bank_pb2.StartTransactionRequest(lifetime=conf.game_settings.ENERGY_TRANSACTION_LIFETIME,
                                                                               operations=operations,
                                                                               autocommit=autocommit),
                                         AnswerType=bank_pb2.StartTransactionResponse)
        except utils_exceptions.TTAPIUnexpectedAPIStatus:
            return False, None

        return True, answer.transaction_id

    tt_api.async_request(url=conf.game_settings.TT_ENERGY_START_TRANSACTION,
Beispiel #4
0
from aiohttp import test_utils

from tt_protocol.protocol import bank_pb2

from tt_web import s11n
from tt_web import postgresql as db

from .. import relations

from . import helpers

TEST_OPERATIONS = [
    bank_pb2.Operation(account_id=666,
                       currency=1,
                       amount=-1000,
                       type='x.1',
                       description='y.1'),
    bank_pb2.Operation(account_id=667,
                       currency=1,
                       amount=300,
                       type='x.2',
                       description='y.2'),
    bank_pb2.Operation(account_id=666,
                       currency=1,
                       amount=500,
                       type='x.3',
                       description='y.3'),
    bank_pb2.Operation(account_id=667,
                       currency=1,
                       amount=-100,