Django的CryptoAssets.core库集成
cryptoassets.django的Python项目详细描述
Django的CryptoAssets库集成
这个包为Django web framework提供了cryptoassets.core比特币和cryptoasset框架的集成。
功能
- 使用djangosettings.pyconfigure机制设置cryptoassets.core
- 用于cryptoassets helper service 的django管理命令映射
- Django本地日志记录
- Django事件调度集成
- 在django中设置sqlalchemy会话和数据库冲突解决
用法
在settings.py:
中将cryptoassets.django添加到django应用程序列表中INSTALLED_APPS = ( 'cryptoassets.django', )
将cryptoassets.core configuration作为python字典添加到CRYPTOASSETS变量下的django settings.py模块中。
例如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 } }
注意
如果复制粘贴这些设置,请在block.io注册您自己的API密钥。
初始化数据库
运行:
python manage.py cryptoassets_initialize_database
这将为配置的加密货币生成数据库表。
这是用于cryptoassets-initialize-database的django管理命令warpper。
启动CryptoAssets帮助程序服务
启动助手服务。这个独立的进程运行,连接到api和网络,监听传入事务,广播传出事务。
注意
CryptoAssets帮助程序服务不在Web服务器进程中运行。它在服务器上作为独立进程运行。
运行:
python manage.py cryptoassets_helper_service
有关详细信息,请参见helper service command。
处理传入事务
确保在CRYPTOASSETS设置中配置了walletnotify,如上所述。它将把传入的进程间通信转换为django事件。
在txupdatesignal:
中获取应用程序代码中的传入事务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 helper service进程内执行。
访问加密资产数据
访问数据库模型
要访问数据库模型:
from cryptoassets.django.app import get_cryptoassets cryptoassets = get_cryptoassets() BitcoinWallet = cryptoassets.coins.get("btc").coin_description.Wallet
进行数据库查询
所有数据库访问都要经过一个单独的sqlalcemy会话,该会话用database transaction conflict resolver包装。
为了方便起见,提供了cryptoassets.django.assetsdb.managed_transaction()decorator:
示例代码:
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()
剩下的是model API和SQLAlchemy。
其他
示例django应用程序
CryptoAssets.core教程
将助手服务作为系统服务运行
要为cryptoassets助手服务提供自动启动/停止和其他功能,请使用类似于systemd或supervisord的东西来管理python manage.py cryptoassets_helper_service。