示例#1
0
def send_coins(wallet: PushWallet, to=None, amount=None, payload='', wait=True, gas_coin=None):
    private_key = MinterWallet.create(mnemonic=wallet.mnemonic)['private_key']
    response = NodeAPI.get_balance(wallet.address)
    nonce = int(response['transaction_count']) + 1
    balances = response['balance']
    balances_bip = effective_balance(balances)
    main_coin, main_balance_bip = max(balances_bip.items(), key=lambda i: i[1])
    main_balance = float(to_bip(balances[main_coin]))

    gas_coin, tx_fee = find_gas_coin(balances, get_fee=True, payload=payload)
    gas_coin_balance = float(to_bip(balances.get(gas_coin, 0)))

    if not gas_coin or not tx_fee or gas_coin_balance < tx_fee:
        return 'Not enough balance to pay commission'
    tx_fee = float(tx_fee)

    # если в обычной пересылке пришлют сумму без учета комиссии - не будем мучать ошибками
    amount = main_balance - tx_fee if amount == truncate(main_balance, 4) \
        and gas_coin == main_coin and not payload else amount
    tx_fee = tx_fee if gas_coin == main_coin else 0
    if amount > main_balance - tx_fee:
        return 'Not enough balance'
    tx = send_coin_tx(private_key, main_coin, amount, to, nonce, payload=payload, gas_coin=gas_coin)
    NodeAPI.send_tx(tx, wait=wait)
    return True
示例#2
0
def get_address_balance(address, virtual=None):
    if virtual:
        balances = {'BIP': virtual}
        balances_bip = {'BIP': Decimal(to_bip(virtual))}
    else:
        balances = NodeAPI.get_balance(address)['balance']
        balances_bip = effective_balance(balances)

    main_coin, main_balance_bip = max(balances_bip.items(), key=lambda i: i[1])
    bip_value_total = truncate(float(main_balance_bip), 4)

    usd_value_total = truncate(bip_to_usdt(bip_value_total), 4)
    usd_rates = fiat_to_usd_rates()
    local_fiat = 'RUB'
    local_fiat_value = truncate(usd_value_total * usd_rates[local_fiat], 4)
    coin_value = to_bip(balances[main_coin])
    coin_value = truncate(float(effective_value(coin_value, main_coin)), 4)
    return {
        'balance': {
            'coin': main_coin,
            'value': coin_value,
            'bip_value': bip_value_total,
            'usd_value': usd_value_total,
            'local_fiat': local_fiat,
            'local_fiat_value': local_fiat_value
        },
        'fiat_rates': {
            symbol: rate
            for symbol, rate in usd_rates.items()
            if symbol in ['UAH', 'USD', 'RUB']
        }
    }
示例#3
0
def mobile_top_up(wallet: PushWallet, phone=None, amount=None, confirm=True):
    if not confirm:
        return get_info()

    phone_reqs = get_tx_requirements(phone)
    if not phone_reqs:
        return f'Phone number {phone} not supported or invalid'

    response = NodeAPI.get_balance(wallet.address)
    balance = response['balance']
    balances_bip = effective_balance(balance)
    main_coin, main_balance_bip = max(balances_bip.items(), key=lambda i: i[1])
    balance_coin = to_bip(balance[main_coin])
    nonce = int(response['transaction_count']) + 1
    to_send = amount or balance_coin

    private_key = MinterWallet.create(mnemonic=wallet.mnemonic)['private_key']

    gas_coin = find_gas_coin(balance)
    if not gas_coin:
        return 'Coin not spendable. Send any coin to pay fee'
    # fee = estimate_custom_fee(gas_coin)
    min_topup = phone_reqs['min_bip_value']
    effective_topup = rub_to_bip(
        to_send) if main_coin == 'ROUBLE' else main_balance_bip

    if balance_coin < to_send:
        return 'Not enough balance'
    if effective_topup < min_topup:
        return f"Minimal top-up: {min_topup} BIP"

    tx = send_coin_tx(private_key,
                      main_coin,
                      to_send,
                      BIP2PHONE_PAYMENT_ADDRESS,
                      nonce,
                      payload=phone_reqs['payload'],
                      gas_coin=gas_coin)
    try:
        NodeAPI.send_tx(tx, wait=True)
    except MinterAPIException as exc:
        return exc.message
    return True