跳转到主要内容

为Django集成的cryptoassets.core库

项目描述

Cryptoassets库集成于Django

此包提供了cryptoassets.core比特币和加密资产框架与Django网络框架的集成。

功能

  • 使用Django settings.py 配置机制设置 cryptoassets.core

  • Django管理命令映射cryptoassets辅助服务

  • Django原生日志记录

  • Django事件调度集成

  • 在Django内部设置SQLAlchemy会话和数据库冲突解决

使用方法

cryptoassets.django 添加到 settings.py 中的Django应用程序列表

INSTALLED_APPS = (
    'cryptoassets.django',
)

cryptoassets.core配置 作为Python字典添加到Django settings.py 模块中的 CRYPTOASSETS 变量。

示例 settings.py

# TESTNET settings
CRYPTOASSETS = {

    # It is recommended to use separate database for cryptoassets,
    # but you can share the database with Django as well.
    # In any case, cryptoassets
    # will use a separate db connection.
    # cryptoassets.django does not read the existing DATABASES setting.
    # Configure the connection using SQLAlchemy syntax:
    # http://cryptoassetscore.readthedocs.org/en/latest/config.html#database
    "database": {
        "url": "postgresql://localhost/cryptoassets",
        "echo": False,
    },

    # Configure block.io API service with Bitcoin testnet
    # (let's not play around with real Bitcoins yet)
    "coins": {
        "btc": {
            "backend": {
                "class": "cryptoassets.core.backend.blockio.BlockIo",
                "api_key": "923f-xxxx-yyyy-zzzz",
                "network": "btctest",
                "pin": "foobar123",
                # Cryptoassets helper process will use this UNIX named pipe to communicate
                # with bitcoind
                "walletnotify": {
                    "class": "cryptoassets.core.backend.sochainwalletnotify.SochainWalletNotifyHandler",
                    "pusher_app_key": "e9f5cc20074501ca7395"
                },
            }
        },
    },

    # Bind cryptoassets.core event handler to Django dispacth wrapper
    "events": {
        "django": {
            "class": "cryptoassets.core.event.python.InProcessEventHandler",
            "callback": "cryptoassets.django.incoming.handle_tx_update"
        }
    },

    # Start simple status at port 9001 for diagnostics
    "status_server": {
        "ip": "127.0.0.1",
        "port": 9001
    }
}

初始化数据库

运行

python manage.py cryptoassets_initialize_database

这将构建配置加密货币的数据库表。

这是Django管理命令包装器cryptoassets-initialize-database

启动cryptoassets辅助服务

启动辅助服务。此独立进程运行,连接到API和网络,监听传入的交易,广播发出的交易。

运行

python manage.py cryptoassets_helper_service

更多信息请参阅 辅助服务命令

处理传入的交易

确保按照上述说明在 CRYPTOASSETS 设置中配置了 walletnotify。它将传入的进程间通信转换为Django事件。

在应用程序代码中通过 txupdate 信号抓取传入的交易

from cryptoassets.django.signals import txupdate
from django.dispatch import receiver

@receiver(txupdate)
def txupdate_received(event_name, data, **kwargs):
    """ Received transaction update from cryptoassets.core.

    """

    if data.get("transaction_type") != "deposit":
        # We are only interest updates on incoming transctions
        return

    transaction_hash = data["txid"]
    value = data['amount']
    address = data['address']
    confirmations = int(data.get('confirmations', -1))

    logger.info("Transaction update received: %s BTC:%s address:%s confirmations:%d", transaction_hash, value, address, confirmations)

处理程序在 加密资产辅助服务 进程内执行。

关于cryptoassets.core事件的更多信息.

访问加密资产数据

访问数据库模型

要获取对数据库模型的访问权限

from cryptoassets.django.app import get_cryptoassets

cryptoassets = get_cryptoassets()
BitcoinWallet = cryptoassets.coins.get("btc").coin_description.Wallet

执行数据库查询

所有数据库访问都通过一个单独的SQLAlcemy会话进行,该会话由 数据库事务冲突解析器 包装。

为了方便,提供了 cryptoassets.django.assetsdb.managed_transaction() 装饰器

示例代码

from cryptoassets.django.app import get_cryptoassets
from cryptoassets.django import assetdb

def get_wallet(session):
    """Return the master shared wallet used to receive payments. """
    cryptoassets = get_cryptoassets()
    BitcoinWallet = cryptoassets.coins.get("btc").coin_description.Wallet
    wallet = BitcoinWallet.get_or_create_by_name("default", session)
    return wallet

def create_new_receiving_address(label):

    @assetdb.managed_transaction
    def tx(session):

        wallet = get_wallet(session=session)

        account = wallet.get_or_create_account_by_name("my account")
        session.flush()  # account id gets written inside commit
        addr = wallet.create_receiving_address(account, label)
        logging.info("Created receiving address %s", addr.address)
        address = addr.address
        return address

    return tx()

其余部分请参阅 模型APISQLAlchemy

其他

示例Django应用程序

查看Liberty Music Store (源代码)。

cryptoassets.core教程

参阅入门指南.

将辅助服务作为系统服务运行

为了自动启动/停止和其他功能,使用类似 systemdsupervisord 来管理 python manage.py cryptoassets_helper_service

作者

Mikko Ohtamaa (博客, Facebook, Twitter, Google+)

项目详情


下载文件

下载适合您平台的应用程序。如果您不确定选择哪个,请了解有关 安装包 的更多信息。

源代码分发

cryptoassets.django-0.1.zip (15.9 kB 查看散列值)

上传时间 源代码

支持者: