forked from BlockIo/block_io-python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbasic.py
More file actions
75 lines (60 loc) · 2.53 KB
/
basic.py
File metadata and controls
75 lines (60 loc) · 2.53 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# Basic example for using Block.io for generating wallet addresses and withdrawing coins
from block_io import BlockIo
from decimal import *
import six # for python2 back-compatibility in printing messages using print as a function
import random
import sys
version = 2 # API version
# use a testnet api key here, say, dogecoin
block_io = BlockIo('TESTNET API KEY', 'SECRET PIN', version)
getcontext().prec = 8 # coins are 8 decimal places at most
# create a new address with a random label
address_label = 'tlabel'+str(int(random.random()*10000))
new_address = None
try:
new_address = block_io.get_new_address(label=address_label)['data']['address']
except Exception:
exc = sys.exc_info()[1]
six.print_(exc)
if (new_address is None):
# the address label already existed, let's get the associated address
new_address = block_io.get_address_by_label(label=address_label)['data']['address']
six.print_("Address Generated for Label=",address_label+":",new_address)
# get address balance
available_balance = Decimal('0.0')
try:
response = block_io.get_address_balance(label=address_label)
available_balance = Decimal(response['data']['available_balance'])
network = response['data']['network']
six.print_("Available Balance in Label=",address_label+":",format(available_balance,'.8f'),network)
except Exception:
exc = sys.exc_info()[1]
six.print_(exc)
# get total balance on the account
try:
response = block_io.get_balance()
available_balance = Decimal(response['data']['available_balance'])
network = response['data']['network']
six.print_("Total Balance in Account=",format(available_balance,'.8f'),network)
except Exception:
exc = sys.exc_info()[1]
six.print_(exc)
# send 1% of the coins in our account to our new label's address
try:
amount_to_send = Decimal(0.01) * available_balance
six.print_("Sending Coins=",format(amount_to_send,'.8f'),"to Label=",address_label)
response = block_io.withdraw(to_label=address_label,amount=format(amount_to_send,'.8f'))
six.print_("Coins sent. Transaction ID=",response['data']['txid'])
except Exception:
exc = sys.exc_info()[1]
six.print_(exc)
# get the new balance on our new address
try:
response = block_io.get_address_balance(label=address_label)
available_balance = Decimal(response['data']['available_balance'])
network = response['data']['network']
six.print_("New Balance in Label=",address_label+":",format(available_balance,'.8f'),network)
except Exception:
exc = sys.exc_info()[1]
six.print_(exc)
# end :)