Exemplo n.º 1
0
def distribute_dividends(asset_info):
    print("Distributing dividends for: ", asset_info['bond_name'])
    params = algod_client.suggested_params()
    purchases = [purchase for purchase in db.transaction.find({})
                 if purchase['asset_id'] == asset_info['asset_id'] and purchase['action'] == 'BUY']
    for purchase in purchases:
        user_id = purchase['user_id']
        user = db.user.find_one({'_id': user_id})
        user_mnemonic = user['mnemonic']
        user_pk = mnemonic.to_public_key(user_mnemonic)
        amount = int(asset_info["coupon_rate"])/100*int(asset_info["face_value"]) *\
                (int(purchase['tokens'])/asset_info['issue_size']) # Fraction of real estate owned
        ##
        txn = AssetTransferTxn(
            sender=accounts[1]['pk'],
            sp=params,
            receiver=user_pk,
            amt=int(amount),
            index=USDT_asset_id)
        stxn = txn.sign(accounts[1]['sk'])
        txid = algod_client.send_transaction(stxn)
        print(txid)
        # Wait for the transaction to be confirmed
        wait_for_confirmation(algod_client, txid)
        # The balance should now be 10.
        print_asset_holding(algod_client, user_pk, USDT_asset_id)

        # Save Dividend Transaction
        db_dividend_tx = db.dividends.insert_one({
            'user_id': user_mnemonic,
            'amount': amount,
            'asset_id': asset_info['asset_id']
        })
Exemplo n.º 2
0
def create_asset(payload):
    asset_details = process_payload(payload)
    # Get network params for transactions before every transaction.
    params = algod_client.suggested_params()
    params.fee = 10
    params.flat_fee = True

    # Account 1 creates an asset called latinum and
    # sets Account 2 as the manager, reserve, freeze, and clawback address.
    # Asset Creation transaction

    txn = AssetConfigTxn(
        sender=accounts[1]['pk'],
        sp=params,
        total=asset_details['issue_size'],
        default_frozen=False,
        unit_name=asset_details['bond_name'].upper(),
        asset_name=asset_details['bond_name'],
        manager=accounts[1]['pk'],
        reserve=accounts[1]['pk'],
        freeze=accounts[1]['pk'],
        clawback=accounts[1]['pk'],
        # url="https://path/to/my/asset/details",
        decimals=0,
        metadata_hash=asset_details['metadata_hash'].encode('ascii'))
    # Sign with secret key of creator
    stxn = txn.sign(accounts[1]['sk'])

    # Send the transaction to the network and retrieve the txid.
    txid = algod_client.send_transaction(stxn)
    print(txid)

    # Retrieve the asset ID of the newly created asset by first
    # ensuring that the creation transaction was confirmed,
    # then grabbing the asset id from the transaction.

    # Wait for the transaction to be confirmed
    wait_for_confirmation(algod_client, txid)

    try:
        # Pull account info for the creator
        # account_info = algod_client.account_info(accounts[1]['pk'])
        # get asset_id from tx
        # Get the new asset's information from the creator account
        ptx = algod_client.pending_transaction_info(txid)
        asset_id = ptx["asset-index"]
        print_created_asset(algod_client, accounts[1]['pk'], asset_id)
        print_asset_holding(algod_client, accounts[1]['pk'], asset_id)
    except Exception as e:
        print(e)

    return asset_id
Exemplo n.º 3
0
def p2p_order(buyer, seller, order_details):
    params = algod_client.suggested_params()
    buyer_pk = mnemonic.to_public_key(buyer)
    buyer_sk = mnemonic.to_private_key(buyer)
    seller_pk = mnemonic.to_public_key(seller)
    seller_sk = mnemonic.to_private_key(seller)

    # activate buyer's account
    print("Activating Buyer's account")
    activate_account(order_details['asset_id'], buyer)

    # Token transaction
    print("\n Executing Token Transaction")
    token_txn = AssetTransferTxn(
        sender=seller_pk,
        sp=params,
        receiver=buyer_pk,
        amt=int(order_details['token_amount']),
        index=order_details['asset_id'])
    token_stxn = token_txn.sign(seller_sk)
    token_txid = algod_client.send_transaction(token_stxn)
    print(token_txid)
    wait_for_confirmation(algod_client, token_txid)
    print_asset_holding(algod_client, buyer_pk, order_details['asset_id'])

    # Activating USDT for seller
    print("Activating USDT for Seller")
    activate_account(USDT_asset_id, seller)

    # USDT Transaction
    print("\n USDT Transaction")
    usdt_txn = AssetTransferTxn(
        sender=buyer_pk,
        sp=params,
        receiver=seller_pk,
        amt=int(order_details['usdt_amount']),
        index=USDT_asset_id)
    usdt_stxn = usdt_txn.sign(buyer_sk)
    usdt_txid = algod_client.send_transaction(usdt_stxn)
    print(usdt_txid)
    wait_for_confirmation(algod_client, usdt_txid)
    print_asset_holding(algod_client, seller_pk, USDT_asset_id)
Exemplo n.º 4
0
def activate_account(asset_id, user_memonic):
    # Check if asset_id is in account 3's asset holdings prior
    # to opt-in
    params = algod_client.suggested_params()
    # comment these two lines if you want to use suggested params
    params.fee = 0
    params.flat_fee = False
    params.min_fee = 0

    account = {
        'pk': mnemonic.to_public_key(user_memonic),
        'sk': mnemonic.to_private_key(user_memonic)
    }

    account_info = algod_client.account_info(account['pk'])
    holding = None
    idx = 0
    for my_account_info in account_info['assets']:
        scrutinized_asset = account_info['assets'][idx]
        idx = idx + 1
        if (scrutinized_asset['asset-id'] == asset_id):
            holding = True
            break

    if not holding:
        # Use the AssetTransferTxn class to transfer assets and opt-in
        txn = AssetTransferTxn(
            sender=account['pk'],
            sp=params,
            receiver=account["pk"],
            amt=0,
            index=asset_id)
        stxn = txn.sign(account['sk'])
        txid = algod_client.send_transaction(stxn)
        print(txid)
        # Wait for the transaction to be confirmed
        wait_for_confirmation(algod_client, txid)
        # Now check the asset holding for that account.
        # This should now show a holding with a balance of 0.
        print_asset_holding(algod_client, account['pk'], asset_id)
Exemplo n.º 5
0
def transfer_asset(asset_id, user_memonic, amount):
    account = {
        'pk': mnemonic.to_public_key(user_memonic),
        'sk': mnemonic.to_private_key(user_memonic)
    }
    # transfer asset of 10 from account 1 to account 3
    params = algod_client.suggested_params()
    # comment these two lines if you want to use suggested params
    params.fee = 1
    params.flat_fee = True
    txn = AssetTransferTxn(
        sender=accounts[1]['pk'],
        sp=params,
        receiver=account["pk"],
        amt=int(amount),
        index=asset_id)
    stxn = txn.sign(accounts[1]['sk'])
    txid = algod_client.send_transaction(stxn)
    print(txid)
    # Wait for the transaction to be confirmed
    wait_for_confirmation(algod_client, txid)
    # The balance should now be 10.
    print_asset_holding(algod_client, account['pk'], asset_id)