Exemplo n.º 1
0
    def test_calc_lmsr_marginal_price(self):
        outcomeTokenIndex = 0
        outcomeTokenCount = 1e18
        # create markets
        outcome_token = OutcomeTokenFactory()
        event = outcome_token.event
        market = MarketFactory(event=event)

        net_outcome_tokens_sold = [0, 1]  # market.net_outcome_tokens_sold
        result = calc_lmsr_marginal_price(1, net_outcome_tokens_sold,
                                          market.funding)
        self.assertIsNotNone(result)
        self.assertTrue(result > 0)
Exemplo n.º 2
0
    def rollback(self):
        token_index = self.validated_data.get('outcomeTokenIndex')
        token_count = self.validated_data.get('outcomeTokenCount')
        market = models.Market.objects.get(
            address=self.validated_data.get('address'))
        market.net_outcome_tokens_sold[token_index] -= token_count
        market.collected_fees -= self.validated_data.get('marketFees')
        market.trading_volume -= self.instance.cost
        market.marginal_prices = map(
            lambda (index, _): Decimal(
                calc_lmsr_marginal_price(int(index), [
                    int(x) for x in market.net_outcome_tokens_sold
                ], int(market.funding))),
            enumerate(market.net_outcome_tokens_sold))

        # Remove order
        self.instance.delete()
        market.save()
Exemplo n.º 3
0
    def create(self, validated_data):
        try:
            market = models.Market.objects.get(
                address=validated_data.get('address'))
            token_index = validated_data.get('outcomeTokenIndex')
            token_count = validated_data.get('outcomeTokenCount')
            market.net_outcome_tokens_sold[token_index] += token_count
            market.collected_fees += validated_data.get('marketFees')

            outcome_token = market.event.outcome_tokens.get(index=token_index)

            # Create Order
            order = models.BuyOrder()
            order.creation_date_time = validated_data.get('creation_date_time')
            order.creation_block = validated_data.get('creation_block')
            order.market = market
            order.sender = validated_data.get('buyer')
            order.outcome_token = outcome_token
            order.outcome_token_count = token_count
            order.cost = validated_data.get(
                'outcomeTokenCost') + validated_data.get('marketFees')
            order.outcome_token_cost = validated_data.get('outcomeTokenCost')
            order.fees = validated_data.get('marketFees')
            order.net_outcome_tokens_sold = market.net_outcome_tokens_sold
            # calculate current marginal price
            order.marginal_prices = map(
                lambda (index, _): Decimal(
                    calc_lmsr_marginal_price(int(index), [
                        int(x) for x in market.net_outcome_tokens_sold
                    ], int(market.funding))),
                enumerate(market.net_outcome_tokens_sold))
            # Save order successfully, save market changes, then save the share entry
            order.save()
            market.trading_volume += order.cost
            market.marginal_prices = order.marginal_prices
            market.save()
            return order
        except models.Market.DoesNotExist:
            raise serializers.ValidationError(
                'Market with address {} does not exist.'.format(
                    validated_data.get('address')))