Django Web框架的比特币应用集成
django-bitcoin的Python项目详细描述
Introduction
django-bitcoin是一个Django web framework 用于构建比特币网络应用程序的应用程序。
Features
- 简单的比特币钱包管理
- 比特币支付处理
- 比特币市场信息
Installation
要安装,只需将应用程序添加到您的设置中。py installed_apps,如:
INSTALLED_APPS = [ ... 'django_bitcoin', ... ] ```
此外,您还必须运行本地比特币实例,并在设置中指定连接字符串:
BITCOIND_CONNECTION_STRING = "http://bitcoinuser:password@localhost:8332"
Usage
Wallet websites, escrow services using the “Wallet”-model
您可以使用wallet类来执行不同的比特币移动应用程序。典型的例子是一个市场风格的网站,有多个卖家和买家。或自由职业者网站,需要代管。或者甚至可以用这个抽象来完成交换(但是需要一些额外的类)。
请注意,当您在钱包对象之间移动比特币时,只有传入和传出交易才需要比特币交易。 系统“钱包”对象之间的交易不会生成“真实”比特币交易。每个事务(传入事务除外)都会记录到wallettransaction对象以简化记帐。
这也意味着比特币交易是“混合的”:
from django_bitcoin import Wallet, currency class Profile(models.Model): wallet = ForeignKey(Wallet) outgoing_bitcoin_address = CharField() class Escrow(models.Model): wallet = ForeignKey(Wallet) buyer_happy = BooleanField(default=False) buyer=Profile.objects.create() seller=Profile.objects.create() purchase=Escrow.objects.create() AMOUNT_USD="9.99" m=currency.Money(AMOUNT_USD, "USD") btc_amount=currency.exchange(m, "BTC") print "Send "+str(btc_amount)+" BTC to address "+buyer.wallet.receiving_address() sleep(5000) # wait for transaction if p1.wallet.total_balance()>=btc_amount: p1.send_to_wallet(purchase, btc_amount) sleep(1000) # wait for product/service delivery if purchase.buyer_happy: purchase.wallet.send_to_wallet(seller.wallet) seller.wallet.send_to_address(seller.outgoing_bitcoin_address, seller.wallet.total_balance()) else: print "WHY U NO HAPPY" #return bitcoins to buyer, 50/50 split or something
Transaction notifications
要启用比特币交易通知,请在settings.py中设置以下标志:
BITCOIN_TRANSACTION_SIGNALING = True
之后,您需要设置一个cron作业来每分钟运行一次,如下所示:
* * * * * (cd $APP_PATH && python manage.py python manage.py CheckTransactions >> $APP_PATH/logs/email_sends.log 2>&1)
之后,您可以定义您的余额更改和余额更改确认信号:
from django_bitcoin.models import balance_changed, balance_changed_confirmed from django.dispatch import receiver @receiver(balance_changed) def balance_changed_handler(sender, **kwargs): pass # try: # print "balance changed", sender.id, kwargs["changed"], sender.total_balance() @receiver(balance_changed_confirmed) def balance_changed_confirmed_handler(sender, **kwargs): pass