|
2 | 2 | """ |
3 | 3 | Example script that shows how to use PyOTA to send a transfer to an address. |
4 | 4 | """ |
5 | | - |
6 | | -from __future__ import absolute_import, division, print_function, \ |
7 | | - unicode_literals |
8 | | - |
9 | | -from argparse import ArgumentParser |
10 | | -from getpass import getpass as secure_input |
11 | | -from sys import argv |
12 | | - |
13 | 5 | from iota import * |
14 | | -from iota import __version__ |
15 | | -from six import binary_type, moves as compat, text_type |
16 | | - |
17 | | - |
18 | | -def main(uri): |
19 | | - # type: (Text, int, Optional[int], bool) -> None |
20 | | - |
21 | | - seed = get_seed() |
22 | | - |
23 | | - if not seed: |
24 | | - print("No seed entered, transfer cancelled.") |
25 | | - return |
26 | | - |
27 | | - addressString = get_address() |
28 | | - |
29 | | - if not addressString: |
30 | | - print("No address entered, transfer cancelled.") |
31 | | - return |
32 | | - |
33 | | - value = get_value() |
34 | | - |
35 | | - print("Sending " + str(value) + "iota to address: " + addressString) |
36 | | - |
37 | | - # Create the API instance. |
38 | | - api = Iota(uri, seed) |
39 | | - |
40 | | - bundle = api.send_transfer( |
41 | | - depth = 100, |
42 | | - transfers = [ |
43 | | - ProposedTransaction( |
44 | | - address = Address(bytearray(addressString)), |
45 | | - value = value, |
46 | | - tag = Tag(b'EXAMPLE'), |
47 | | - message = TryteString.from_string('Hello!'), |
48 | | - ), |
49 | | - ], |
50 | | - ) |
51 | 6 |
|
52 | | - print('Transfer complete') |
53 | 7 |
|
| 8 | +# Create the API instance. |
| 9 | +api =\ |
| 10 | + Iota( |
| 11 | + # URI of a locally running node. |
| 12 | + 'http://localhost:14265/', |
54 | 13 |
|
55 | | -def get_seed(): |
56 | | - # type: () -> binary_type |
57 | | - """ |
58 | | - Prompts the user securely for their seed. |
59 | | - """ |
60 | | - print( |
61 | | - 'Enter seed to send from and press return (typing will not be shown). ' |
62 | | - ) |
63 | | - seed = secure_input('') |
64 | | - |
65 | | - if seed == '': |
66 | | - return None |
67 | | - |
68 | | - return seed.encode('ascii') |
69 | | - |
70 | | - |
71 | | -def get_address(): |
72 | | - # type: () -> binary_type |
73 | | - """ |
74 | | - Prompts the user for the address to send to. |
75 | | - """ |
76 | | - print( |
77 | | - 'Enter address to send to.' |
78 | | - ) |
79 | | - address = secure_input('') |
80 | | - |
81 | | - if address == '': |
82 | | - print("No address") |
83 | | - return None |
84 | | - |
85 | | - return address |
86 | | - |
87 | | - |
88 | | -def get_value(): |
89 | | - """ |
90 | | - Prompts the user for the value to send. |
91 | | - """ |
92 | | - |
93 | | - try: |
94 | | - valueStr = secure_input("Enter a value to send: ") |
95 | | - value = int(valueStr) |
96 | | - return value |
97 | | - except ValueError: |
98 | | - print("Invalid amoumt, defaulting to 0.") |
99 | | - return 0 |
100 | | - |
101 | | -if __name__ == '__main__': |
102 | | - |
103 | | - parser = ArgumentParser( |
104 | | - description = __doc__, |
105 | | - epilog = 'PyOTA v{version}'.format(version=__version__), |
106 | | - ) |
107 | | - |
108 | | - parser.add_argument( |
109 | | - '--uri', |
110 | | - type = text_type, |
111 | | - default = 'http://localhost:14265/', |
112 | | - help = |
113 | | - 'URI of the node to connect to ' |
114 | | - '(defaults to http://localhost:14265/).', |
| 14 | + # Seed used for cryptographic functions. |
| 15 | + seed = b'SEED9GOES9HERE' |
115 | 16 | ) |
116 | 17 |
|
117 | | - main(**vars(parser.parse_args(argv[1:]))) |
| 18 | +# For more information, see :py:meth:`Iota.send_transfer`. |
| 19 | +api.send_transfer( |
| 20 | + depth = 100, |
| 21 | + |
| 22 | + # One or more :py:class:`ProposedTransaction` objects to add to the |
| 23 | + # bundle. |
| 24 | + transfers = [ |
| 25 | + ProposedTransaction( |
| 26 | + # Recipient of the transfer. |
| 27 | + address = |
| 28 | + Address( |
| 29 | + b'TESTVALUE9DONTUSEINPRODUCTION99999FBFFTG' |
| 30 | + b'QFWEHEL9KCAFXBJBXGE9HID9XCOHFIDABHDG9AHDR' |
| 31 | + ), |
| 32 | + |
| 33 | + # Amount of IOTA to transfer. |
| 34 | + # This value may be zero. |
| 35 | + value = 1, |
| 36 | + |
| 37 | + # Optional tag to attach to the transfer. |
| 38 | + tag = Tag(b'EXAMPLE'), |
| 39 | + |
| 40 | + # Optional message to include with the transfer. |
| 41 | + message = TryteString.from_string('Hello!'), |
| 42 | + ), |
| 43 | + ], |
| 44 | +) |
0 commit comments