Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 3eec782

Browse files
Last example example modified.
1 parent df482bf commit 3eec782

File tree

3 files changed

+107
-47
lines changed

3 files changed

+107
-47
lines changed

README.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -344,12 +344,13 @@ if __name__ == "__main__":
344344
Generated output:
345345

346346
```python
347+
#payment_id: 4926869b6b5d50b24cb59f08fd76826cacdf76201b2d4648578fe610af7f786e
347348
{
348-
"jsonrpc": "2.0",
349349
"id": "0",
350+
"jsonrpc": "2.0",
350351
"result": {
351352
"tx_key": "",
352-
"tx_hash": "<c27d2ba8414f6336a6a90d64186bc00c6a42494aa1e1403454ba7c471865525d>"
353+
"tx_hash": "<04764ab4855b8a9f9c42d99e19e1c40956a502260123521ca3f6488dd809797a>"
353354
}
354355
}
355356
```

src/simplewallet_basic_rpc_call_04.py

Lines changed: 103 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# Basic example of json-rpc calls to Monero's simplewallet.
22
#
3-
# Transfer 1 xmr to some address
3+
# Transfer given xmr amount to some address
44
#
55
# The simplewallet RPC docs are here:
66
# https://getmonero.org/knowledge-base/developer-guides/wallet-rpc
@@ -10,6 +10,9 @@
1010

1111
import requests
1212
import json
13+
import os
14+
import binascii
15+
1316

1417
def main():
1518
"""DONT RUN IT without changing the destination address!!!"""
@@ -22,31 +25,125 @@ def main():
2225

2326
destination_address = "489MAxaT7xXP3Etjk2suJT1uDYZU6cqFycsau2ynCTBacncWVEwe9eYFrAD6BqTn4Y2KMs7maX75iX1UFwnJNG5G88wxKoj"
2427

25-
# send 1 xmr to the given destination_address
26-
recipents = [
27-
{"address": destination_address, "amount": 1}
28-
]
28+
29+
# amount of xmr to send
30+
amount = 0.54321
31+
32+
# cryptonote amount format is different then
33+
# that normally used by people.
34+
# thus the float amount must be changed to
35+
# something that cryptonote understands
36+
int_amount = int(get_amount(amount))
37+
38+
# just to make sure that amount->coversion->back
39+
# gives the same amount as in the initial number
40+
assert amount == float(get_money(str(int_amount))), "Amount conversion failed"
41+
42+
# send specified xmr amount to the given destination_address
43+
recipents = [{"address": destination_address,
44+
"amount": int_amount}]
2945

3046
# using given mixin
3147
mixin = 4
3248

49+
# get some random payment_id
50+
payment_id = get_payment_id()
51+
3352
# simplewallet' procedure/method to call
3453
rpc_input = {
3554
"method": "transfer",
36-
"params": {"destinations": recipents, "mixin": mixin}
55+
"params": {"destinations": recipents,
56+
"mixin": mixin,
57+
"payment_id" : payment_id}
3758
}
3859

3960
# add standard rpc values
4061
rpc_input.update({"jsonrpc": "2.0", "id": "0"})
4162

63+
print(json.dumps(rpc_input))
64+
4265
# execute the rpc request
4366
response = requests.post(
4467
url,
4568
data=json.dumps(rpc_input),
4669
headers=headers)
4770

71+
# print the payment_id
72+
print("#payment_id: ", payment_id)
73+
4874
# pretty print json output
4975
print(json.dumps(response.json(), indent=4))
5076

77+
78+
def get_amount(amount):
79+
"""encode amount (float number) to the cryptonote format. Hope its correct."""
80+
81+
CRYPTONOTE_DISPLAY_DECIMAL_POINT = 12
82+
83+
str_amount = str(amount)
84+
85+
fraction_size = 0
86+
87+
if '.' in str_amount:
88+
89+
point_index = str_amount.index('.')
90+
91+
fraction_size = len(str_amount) - point_index - 1
92+
93+
while fraction_size < CRYPTONOTE_DISPLAY_DECIMAL_POINT and '0' == str_amount[-1]:
94+
print(44)
95+
str_amount = str_amount[:-1]
96+
fraction_size = fraction_size - 1
97+
98+
if CRYPTONOTE_DISPLAY_DECIMAL_POINT < fraction_size:
99+
return False
100+
101+
str_amount = str_amount[:point_index] + str_amount[point_index+1:]
102+
103+
if not str_amount:
104+
return False
105+
106+
if fraction_size < CRYPTONOTE_DISPLAY_DECIMAL_POINT:
107+
str_amount = str_amount + '0'*(CRYPTONOTE_DISPLAY_DECIMAL_POINT - fraction_size)
108+
109+
return str_amount
110+
111+
112+
def get_money(amount):
113+
"""decode cryptonote amount format to user friendly format. Hope its correct."""
114+
115+
CRYPTONOTE_DISPLAY_DECIMAL_POINT = 12
116+
117+
s = amount
118+
119+
if len(s) < CRYPTONOTE_DISPLAY_DECIMAL_POINT + 1:
120+
# add some trailing zeros, if needed, to have constant width
121+
s = '0' * (CRYPTONOTE_DISPLAY_DECIMAL_POINT + 1 - len(s)) + s
122+
123+
idx = len(s) - CRYPTONOTE_DISPLAY_DECIMAL_POINT
124+
125+
s = s[0:idx] + "." + s[idx:]
126+
127+
return s
128+
129+
def get_payment_id():
130+
"""generate random payment_id
131+
132+
generate some random payment_id for the
133+
transactions
134+
135+
payment_id is 32 bytes (64 hexadecimal characters)
136+
thus we first generate 32 random byte array
137+
which is then change to string representation, since
138+
json will not not what to do with the byte array.
139+
"""
140+
141+
random_32_bytes = os.urandom(32)
142+
payment_id = "".join(map(chr, binascii.hexlify(random_32_bytes)))
143+
144+
return payment_id
145+
146+
147+
51148
if __name__ == "__main__":
52149
main()

src/simplewallet_rpc_examples.py

Lines changed: 1 addition & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,7 @@ def get_payments(self):
119119
"""get a list of incoming payments using a given payment id"""
120120

121121
# en example of a payment id
122-
payment_id = "426870cb29c598e191184fa87003ca562d9e25f761ee9e520a888aec95195912"
122+
payment_id = "4926869b6b5d50b24cb59f08fd76826cacdf76201b2d4648578fe610af7f786e"
123123

124124
rpc_input = {
125125
"method": "get_payments",
@@ -142,42 +142,6 @@ def incoming_transfers(self):
142142

143143
return response.json()
144144

145-
def transfer(self):
146-
"""send monero to a number of recipients
147-
148-
DON'T RUN THIS FUNCTION without changing
149-
the recipient address! The reason is that it will
150-
send 1 xmr to my address.
151-
152-
For this reason, this method is not executed
153-
by self.execute()
154-
"""
155-
156-
print("transfer():\n send monero to a number of recipients\n")
157-
158-
destination_address = "489MAxaT7xXP3Etjk2suJT1uDYZU6cqFycsau2ynCTBacncWVEwe9eYFrAD6BqTn4Y2KMs7maX75iX1UFwnJNG5G88wxKoj"
159-
160-
# send 1 xmr to the given destination_address
161-
recipents = [
162-
{"address": destination_address, "amount": 1}
163-
]
164-
165-
mixin = 4
166-
167-
rpc_input = {
168-
"method": "transfer",
169-
"params": {"destinations": recipents, "mixin": mixin}
170-
}
171-
172-
print("Transfer 1 xmr to: ", destination_address)
173-
174-
response = self.__do_rpc(rpc_input)
175-
176-
print(response.json(), "\n")
177-
178-
return response.json()
179-
180-
181145

182146
def execute(self):
183147
"""run read-only examples"""
@@ -212,5 +176,3 @@ def __do_rpc(self, rpc_input):
212176
if __name__ == "__main__":
213177
sw = SimplewalletRpcExamples()
214178
sw.execute()
215-
#sw.transfer() #<-- DONT run without modifying destination_address
216-

0 commit comments

Comments
 (0)