Skip to content

Commit

Permalink
Merge pull request #28 from paytaca/feature/tx-broadcast-api
Browse files Browse the repository at this point in the history
Endpoint for broadcasting signed transactions
  • Loading branch information
joemarct authored May 30, 2021
2 parents 3bd1b98 + 6405e5e commit 8462860
Show file tree
Hide file tree
Showing 9 changed files with 69 additions and 19 deletions.
3 changes: 2 additions & 1 deletion main/serializers/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,5 @@
from main.serializers.serializer_token import *
from main.serializers.serializer_transaction import *
from main.serializers.serializer_user_auth import *
from main.serializers.serializer_subscriber import *
from main.serializers.serializer_subscriber import *
from main.serializers.serializer_broadcast import *
5 changes: 5 additions & 0 deletions main/serializers/serializer_broadcast.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
from rest_framework import serializers


class BroadcastSerializer(serializers.Serializer):
transaction = serializers.CharField()
10 changes: 10 additions & 0 deletions main/tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -625,3 +625,13 @@ def get_token_meta_data(self, token_id):
pass
else:
self.retry(countdown=5)


@shared_task(bind=True, queue='broadcast', max_retries=5)
def broadcast_transaction(self, transaction):
try:
obj = BCHDQuery()
txid = obj.broadcast_transaction(transaction)
return txid
except AttributeError:
self.retry(countdown=1)
1 change: 1 addition & 0 deletions main/templates/main/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ <h4>Targeted Use Case</h4>
<li>Webhook notification system</li>
<li>Two-way communication through websockets</li>
<li>HD wallet addresseses tracking</li>
<li>Broadcast of signed transactions</li>
</ul>
</b-col>
</b-row>
Expand Down
9 changes: 3 additions & 6 deletions main/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,21 +16,18 @@

main_urls = router.urls




main_urls += [
re_path(r"^subscription/$", views.SubscribeViewSet.as_view(), name='subscribe'),
re_path(r"^balance/bch/(?P<bchaddress>[\w+:]+)/$", views.Balance.as_view(),name='bch-balance'),
re_path(r"^balance/slp/(?P<slpaddress>[\w+:]+)/$", views.Balance.as_view(),name='slp-balance'),
re_path(r"^balance/slp/(?P<slpaddress>[\w+:]+)/(?P<tokenid>[\w+]+)", views.Balance.as_view(),name='slp-token-balance'),
re_path(r"^utxo/bch/(?P<bchaddress>[\w+:]+)/$", views.UTXO.as_view(),name='bch-utxo'),
re_path(r"^utxo/slp/(?P<slpaddress>[\w+:]+)/$", views.UTXO.as_view(),name='slp-utxo'),
re_path(r"^utxo/slp/(?P<slpaddress>[\w+:]+)/(?P<tokenid>[\w+]+)", views.UTXO.as_view(),name='slp-token-utxo')
re_path(r"^utxo/slp/(?P<slpaddress>[\w+:]+)/(?P<tokenid>[\w+]+)", views.UTXO.as_view(),name='slp-token-utxo'),
path('broadcast', views.BroadcastViewSet.as_view(), name="broadcast-transaction")
]


test_urls = [
re_path(r"^(?P<address>[\w+:]+)/$", views.TestSocket.as_view(),name='testbch'),
re_path(r"^(?P<address>[\w+:]+)/(?P<tokenid>[\w+]+)", views.TestSocket.as_view(),name='testbch'),
]
]
30 changes: 21 additions & 9 deletions main/utils/queries/bchd.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#!/usr/bin/env python3
import grpc, random
import grpc
import random
import logging
from main.utils.bchd import bchrpc_pb2 as pb
from main.utils.bchd import bchrpc_pb2_grpc as bchrpc
Expand All @@ -10,13 +11,11 @@ class BCHDQuery(object):

def __init__(self):
nodes = [
'bchd.imaginary.cash:8335',
'bchd.ny1.simpleledger.io:8335',
'bchd.greyh.at:8335'
'bchd.imaginary.cash:8335',
'bchd.ny1.simpleledger.io:8335',
'bchd.greyh.at:8335'
]
self.base_url = random.choice(nodes)



def get_latest_block(self):
creds = grpc.ssl_channel_credentials()
Expand Down Expand Up @@ -47,7 +46,6 @@ def get_raw_transaction(self, transaction_hash):
resp = stub.GetTransaction(req)
return resp.transaction


def get_utxos(self, address):
creds = grpc.ssl_channel_credentials()

Expand All @@ -71,5 +69,19 @@ def get_transactions_count(self, blockheight):
req.full_transactions = False
resp = stub.GetBlock(req)

trs = resp.block.transaction_data
return len(trs)
trs = resp.block.transaction_data
return len(trs)

def broadcast_transaction(self, transaction):
txn_bytes = bytes.fromhex(transaction)
creds = grpc.ssl_channel_credentials()

with grpc.secure_channel(self.base_url, creds) as channel:
stub = bchrpc.bchrpcStub(channel)

req = pb.SubmitTransactionRequest()
req.transaction = txn_bytes
resp = stub.SubmitTransaction(req)

tx_hash = bytearray(resp.hash[::-1]).hex()
return tx_hash
3 changes: 2 additions & 1 deletion main/views/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,5 @@
from main.views.view_subscribe import *
from main.views.view_test import *
from main.views.view_balance import *
from main.views.view_utxo import *
from main.views.view_utxo import *
from main.views.view_broadcast import *
23 changes: 23 additions & 0 deletions main/views/view_broadcast.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
from rest_framework.permissions import AllowAny
from rest_framework.response import Response
from rest_framework import generics
from rest_framework import status
from main import serializers
from main.tasks import broadcast_transaction

class BroadcastViewSet(generics.GenericAPIView):
serializer_class = serializers.BroadcastSerializer
permission_classes = [AllowAny,]

def post(self, request, format=None):
serializer = self.get_serializer(data=request.data)
response = {'success': False}
if serializer.is_valid():
job = broadcast_transaction.delay(serializer.data['transaction'])
response['txid'] = job.get()
response['success'] = True
if response['success']:
return Response(response, status=status.HTTP_200_OK)
else:
return Response(response, status=status.HTTP_409_CONFLICT)
return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
4 changes: 2 additions & 2 deletions supervisord.conf
Original file line number Diff line number Diff line change
Expand Up @@ -206,8 +206,8 @@ stderr_logfile_maxbytes=0
stopasgroup = true


[program:celery_get_token_metadata]
command = celery -A watchtower worker -l INFO -Ofair -Q token_metadata --max-tasks-per-child=100
[program:celery_token_metadata_and_txn_broadcast]
command = celery -A watchtower worker -l INFO -Ofair -Q token_metadata,broadcast --max-tasks-per-child=100
stdout_logfile=/dev/stdout
stdout_logfile_maxbytes=0
stderr_logfile=/dev/stderr
Expand Down

0 comments on commit 8462860

Please sign in to comment.