Example #1
0
def gratz_buy(wallet, product, confirm=True, contact=None, price_fiat=None):
    logging.info(f'Buy gratz product id {product}')
    response = gratz_order_create(product)
    if isinstance(response, str):
        return response
    logging.info(f'  order create response {response}')

    price_bip = response['price_bip']
    if not confirm:
        return {'price_bip': price_bip}
    result = send_coins(wallet,
                        to=response['address'],
                        amount=price_bip,
                        wait=True)
    if isinstance(result, str):
        return result

    order = OrderHistory.create(provider='gratz',
                                product_id=product,
                                price_pip=str(to_pip(price_bip)),
                                address_from=wallet.address,
                                address_to=response['address'],
                                contact=contact)
    result = gratz_order_confirm(response['order_id'])
    logging.info(f'  order confirmation response {result}')

    cat, shop, value = get_full_product_name(product)
    order_name = f'Product ID: {product} (Category: {cat}, Shop: {shop}, value: {value})'
    schedule_gratz_notification(order.id, result, order_name)

    if not result.get('success'):
        return f"Gratz Provider Error: {result['error']}"
    return result
Example #2
0
def create_campaign(recipients,
                    sender,
                    cost,
                    campaign_pass=None,
                    wallet_pass=None,
                    target=None,
                    customization_id=None):
    campaign_pass_hash = pbkdf2_sha256.hash(
        campaign_pass) if campaign_pass is not None else None
    campaign_wallet = generate_and_save_wallet()
    campaign = PushCampaign.create(wallet_link_id=campaign_wallet.link_id,
                                   status='open',
                                   cost_pip=str(to_pip(cost)),
                                   company=sender,
                                   password_hash=campaign_pass_hash,
                                   customization_setting_id=customization_id)

    for info in recipients.values():
        balance = str(to_pip(info['amount'] + 0.01))
        wallet = generate_and_save_wallet(
            sender=sender,
            recipient=info['name'],
            password=wallet_pass,
            campaign_id=campaign.id,
            virtual_balance=balance,
            customization_setting_id=customization_id)
        info['token'] = wallet.link_id

    Recipient.bulk_create([
        Recipient(email=email,
                  campaign_id=campaign.id,
                  name=info['name'],
                  amount_pip=str(to_pip(info['amount'])),
                  wallet_link_id=info['token'],
                  target_shop=target) for email, info in recipients.items()
    ],
                          batch_size=100)
    return campaign, campaign_wallet
Example #3
0
def estimate_custom_fee(coin):
    if coin == BASE_COIN:
        return Decimal('0.01')
    coin_info = NodeAPI.get_coin_info(coin)
    if coin_info['reserve_balance'] < to_pip(MIN_RESERVE_BIP + 0.01):
        return
    w = MinterWallet.create()
    tx = send_coin_tx(w['private_key'],
                      coin,
                      0,
                      w['address'],
                      1,
                      gas_coin=coin)
    return to_bip(NodeAPI.estimate_tx_commission(tx.signed_tx)['commission'])
Example #4
0
def effective_balance(balances):
    balances_bip = {}
    for coin, balance in balances.items():
        if coin == BASE_COIN:
            balances_bip[coin] = max(Decimal(0),
                                     to_bip(balance) - Decimal('0.01'))
            continue

        # ROUBLE WORKAROUND
        coin_info = NodeAPI.get_coin_info(coin)
        if coin_info['reserve_balance'] < to_pip(
                Decimal(MIN_RESERVE_BIP) + Decimal('0.01')):
            return {coin: Decimal(0)}

        est_sell_response = NodeAPI.estimate_coin_sell(coin, balance,
                                                       BASE_COIN)
        will_get_pip, comm_pip = est_sell_response[
            'will_get'], est_sell_response['commission']
        if int(balance) < int(comm_pip):
            continue
        will_get_pip = int(will_get_pip) - to_pip(0.01)
        if will_get_pip > 0:
            balances_bip[coin] = to_bip(will_get_pip)
    return balances_bip or {'BIP': Decimal(0)}
Example #5
0
    def post(self):
        args = parser_campaign_create.parse_args()
        count = args['count']
        coin = args['action_coin']
        name = args['name']
        action_type = args['action_type']
        action_reward = args['action_reward']
        action_link = args['action_link']
        action_duration = args['action_duration']

        action = {
            'type': action_type,
            'reward': action_reward,
            'link': action_link,
            'duration': action_duration
        }
        campaign_id = uuid()
        wallet = MinterWallet.create()
        action_reward = float(action['reward'])
        one_tx_fee = float(estimate_custom_fee(coin) or 0)
        campaign_cost = (action_reward + one_tx_fee) * count
        deeplink = TxDeeplink.create('send',
                                     to=wallet['address'],
                                     value=campaign_cost,
                                     coin=coin)

        icon_storage = args['icon']
        filename = images.save(
            icon_storage,
            name=f'{campaign_id}.{extension(icon_storage.filename)}')
        icon = RewardIcon.create(filename=filename, url=images.url(filename))
        RewardCampaign.create(link_id=campaign_id,
                              address=wallet['address'],
                              mnemonic=wallet['mnemonic'],
                              name=name,
                              count=count,
                              coin=coin,
                              action_type=action['type'],
                              action_reward=to_pip(action_reward),
                              action_params=action,
                              icon=icon)
        return {
            'id': campaign_id,
            'address': wallet['address'],
            'deeplink': deeplink.web
        }
Example #6
0
def push_resend(wallet,
                new_password=None,
                sender=None,
                recipient=None,
                amount=None,
                virtual=None):
    if not amount:
        return 'Amount should be >0'
    virtual_balance = str(to_pip(amount)) if virtual else None
    new_wallet = generate_and_save_wallet(sender=sender,
                                          recipient=recipient,
                                          new_password=new_password,
                                          virtual_balance=virtual_balance,
                                          sent_from=wallet.link_id)
    if not virtual:
        result = send_coins(wallet, new_wallet.address, amount, wait=True)
        if isinstance(result, str):
            return result
    return {'new_link_id': new_wallet.link_id}
Example #7
0
def gift_buy(wallet, product, confirm=True, price_fiat=None):
    response = gift_order_create(product)
    if isinstance(response, str):
        return response
    price_bip = response['price_bip']

    if not confirm:
        return {'price_bip': price_bip}

    OrderHistory.create(provider='gift',
                        product_id=product,
                        price_pip=str(to_pip(price_bip)),
                        address_from=wallet.address,
                        address_to=response['address'])

    result = send_coins(wallet,
                        to=response['address'],
                        amount=price_bip,
                        wait=True)
    if isinstance(result, str):
        return result

    return gift_order_confirm(response['order_id'])