Last active
September 30, 2021 13:47
-
-
Save mvadari/83a0c5323ed8b842fdf312b715f4209c to your computer and use it in GitHub Desktop.
The code from the Apex Dev Summit 2021 xrpl-py demo
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"cells": [ | |
{ | |
"cell_type": "code", | |
"execution_count": 1, | |
"id": "sized-bubble", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"Requirement already satisfied: xrpl-py in /Users/mvadari/.pyenv/versions/3.9.1/lib/python3.9/site-packages (1.1.0)\n", | |
"Requirement already satisfied: ECPy<2.0.0,>=1.2.5 in /Users/mvadari/.pyenv/versions/3.9.1/lib/python3.9/site-packages (from xrpl-py) (1.2.5)\n", | |
"Requirement already satisfied: websockets<10.0.0,>=9.0.1 in /Users/mvadari/.pyenv/versions/3.9.1/lib/python3.9/site-packages (from xrpl-py) (9.1)\n", | |
"Requirement already satisfied: typing-extensions<4.0.0,>=3.7.4 in /Users/mvadari/.pyenv/versions/3.9.1/lib/python3.9/site-packages (from xrpl-py) (3.10.0.0)\n", | |
"Requirement already satisfied: httpx<0.19.0,>=0.18.1 in /Users/mvadari/.pyenv/versions/3.9.1/lib/python3.9/site-packages (from xrpl-py) (0.18.1)\n", | |
"Requirement already satisfied: base58<3.0.0,>=2.1.0 in /Users/mvadari/.pyenv/versions/3.9.1/lib/python3.9/site-packages (from xrpl-py) (2.1.0)\n", | |
"Requirement already satisfied: sniffio in /Users/mvadari/.pyenv/versions/3.9.1/lib/python3.9/site-packages (from httpx<0.19.0,>=0.18.1->xrpl-py) (1.2.0)\n", | |
"Requirement already satisfied: httpcore<0.14.0,>=0.13.0 in /Users/mvadari/.pyenv/versions/3.9.1/lib/python3.9/site-packages (from httpx<0.19.0,>=0.18.1->xrpl-py) (0.13.6)\n", | |
"Requirement already satisfied: certifi in /Users/mvadari/.pyenv/versions/3.9.1/lib/python3.9/site-packages (from httpx<0.19.0,>=0.18.1->xrpl-py) (2021.5.30)\n", | |
"Requirement already satisfied: rfc3986[idna2008]<2,>=1.3 in /Users/mvadari/.pyenv/versions/3.9.1/lib/python3.9/site-packages (from httpx<0.19.0,>=0.18.1->xrpl-py) (1.5.0)\n", | |
"Requirement already satisfied: h11<0.13,>=0.11 in /Users/mvadari/.pyenv/versions/3.9.1/lib/python3.9/site-packages (from httpcore<0.14.0,>=0.13.0->httpx<0.19.0,>=0.18.1->xrpl-py) (0.12.0)\n", | |
"Requirement already satisfied: anyio==3.* in /Users/mvadari/.pyenv/versions/3.9.1/lib/python3.9/site-packages (from httpcore<0.14.0,>=0.13.0->httpx<0.19.0,>=0.18.1->xrpl-py) (3.1.0)\n", | |
"Requirement already satisfied: idna>=2.8 in /Users/mvadari/.pyenv/versions/3.9.1/lib/python3.9/site-packages (from anyio==3.*->httpcore<0.14.0,>=0.13.0->httpx<0.19.0,>=0.18.1->xrpl-py) (3.2)\n", | |
"\u001b[33mWARNING: You are using pip version 21.1.2; however, version 21.2.4 is available.\n", | |
"You should consider upgrading via the '/Users/mvadari/.pyenv/versions/3.9.1/bin/python3.9 -m pip install --upgrade pip' command.\u001b[0m\n", | |
"Note: you may need to restart the kernel to use updated packages.\n" | |
] | |
} | |
], | |
"source": [ | |
"pip install xrpl-py" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 2, | |
"id": "annual-coaching", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"Response(status=<ResponseStatus.SUCCESS: 'success'>, result={}, id=None, type=<ResponseType.RESPONSE: 'response'>)" | |
] | |
}, | |
"execution_count": 2, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"from xrpl.asyncio.clients import AsyncJsonRpcClient\n", | |
"client = AsyncJsonRpcClient('https://s.altnet.rippletest.net:51234')\n", | |
"\n", | |
"from xrpl.models import Ping\n", | |
"await client.request(Ping())" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 3, | |
"id": "automotive-large", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"public_key: EDD2ACCA25231A08581EF68C058232AEB31F77DB163D19CC4702D5379F973795D5\n", | |
"private_key: -HIDDEN-\n", | |
"classic_address: rLH398DgLHSyiZLAMZ6DPdKwC5kFA6Aqdq\n" | |
] | |
} | |
], | |
"source": [ | |
"from xrpl.asyncio.wallet import generate_faucet_wallet\n", | |
"\n", | |
"wallet = await generate_faucet_wallet(client)\n", | |
"print(wallet)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 11, | |
"id": "objective-wisdom", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"Decimal('969.999990')" | |
] | |
}, | |
"execution_count": 11, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"from xrpl.asyncio.account import get_balance\n", | |
"from xrpl.utils import drops_to_xrp\n", | |
"drops_to_xrp(str(await get_balance(wallet.classic_address, client)))" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 5, | |
"id": "novel-thomas", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"Payment(account='rLH398DgLHSyiZLAMZ6DPdKwC5kFA6Aqdq', transaction_type=<TransactionType.PAYMENT: 'Payment'>, fee=None, sequence=None, account_txn_id=None, flags=0, last_ledger_sequence=None, memos=None, signers=None, source_tag=None, signing_pub_key='', txn_signature=None, amount='30000000', destination='rPT1Sjq2YGrBMTttX4GZHjKu9dyfzbpAYe', destination_tag=None, invoice_id=None, paths=None, send_max=None, deliver_min=None)\n" | |
] | |
} | |
], | |
"source": [ | |
"from xrpl.models import Payment\n", | |
"from xrpl.utils import xrp_to_drops\n", | |
"payment = Payment(account=wallet.classic_address, amount=xrp_to_drops(30), destination='rPT1Sjq2YGrBMTttX4GZHjKu9dyfzbpAYe')\n", | |
"print(payment)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 6, | |
"id": "explicit-casting", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"Payment(account='rLH398DgLHSyiZLAMZ6DPdKwC5kFA6Aqdq', transaction_type=<TransactionType.PAYMENT: 'Payment'>, fee='10', sequence=21432430, account_txn_id=None, flags=0, last_ledger_sequence=21432512, memos=None, signers=None, source_tag=None, signing_pub_key='EDD2ACCA25231A08581EF68C058232AEB31F77DB163D19CC4702D5379F973795D5', txn_signature='B37DA1ACCD857631C2286049F1E3F3E19654F3022089988A1EE51A93B7A03B5C0520A77E8E9ED840FCE661AC70BCAA64D71D93463C32AF61C33DA895EEAD2906', amount='30000000', destination='rPT1Sjq2YGrBMTttX4GZHjKu9dyfzbpAYe', destination_tag=None, invoice_id=None, paths=None, send_max=None, deliver_min=None)" | |
] | |
}, | |
"execution_count": 6, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"from xrpl.asyncio.transaction import safe_sign_and_autofill_transaction\n", | |
"signed_payment = await safe_sign_and_autofill_transaction(payment, wallet, client)\n", | |
"signed_payment" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 7, | |
"id": "generous-living", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"Response(status=<ResponseStatus.SUCCESS: 'success'>, result={'Account': 'rLH398DgLHSyiZLAMZ6DPdKwC5kFA6Aqdq', 'Amount': '30000000', 'Destination': 'rPT1Sjq2YGrBMTttX4GZHjKu9dyfzbpAYe', 'Fee': '10', 'Flags': 0, 'LastLedgerSequence': 21432512, 'Sequence': 21432430, 'SigningPubKey': 'EDD2ACCA25231A08581EF68C058232AEB31F77DB163D19CC4702D5379F973795D5', 'TransactionType': 'Payment', 'TxnSignature': 'B37DA1ACCD857631C2286049F1E3F3E19654F3022089988A1EE51A93B7A03B5C0520A77E8E9ED840FCE661AC70BCAA64D71D93463C32AF61C33DA895EEAD2906', 'date': 686264893, 'hash': '1F43B3FF60F8664E165CB8DDFA67F06F4AEFA23AAEA46B219C3549143A66C03C', 'inLedger': 21432511, 'ledger_index': 21432511, 'meta': {'AffectedNodes': [{'ModifiedNode': {'FinalFields': {'Account': 'rPT1Sjq2YGrBMTttX4GZHjKu9dyfzbpAYe', 'Balance': '96126576774644296', 'Flags': 0, 'OwnerCount': 0, 'Sequence': 2868413}, 'LedgerEntryType': 'AccountRoot', 'LedgerIndex': '31CCE9D28412FF973E9AB6D0FA219BACF19687D9A2456A0C2ABC3280E9D47E37', 'PreviousFields': {'Balance': '96126576744644296'}, 'PreviousTxnID': 'F59A81B3F73FC9452BF7057518313ACCCFF4DE0F73EA695ACA6864309D9B839A', 'PreviousTxnLgrSeq': 21432503}}, {'ModifiedNode': {'FinalFields': {'Account': 'rLH398DgLHSyiZLAMZ6DPdKwC5kFA6Aqdq', 'Balance': '969999990', 'Flags': 0, 'OwnerCount': 0, 'Sequence': 21432431}, 'LedgerEntryType': 'AccountRoot', 'LedgerIndex': '665E3CD10B42D619711D96850944AB027342DFE2A83B985C321A9EBB1926824C', 'PreviousFields': {'Balance': '1000000000', 'Sequence': 21432430}, 'PreviousTxnID': 'CB1CFBF79E2D14EC33B7EFB2C8CDFF6AB71AB87CA00397736D6154A18C586484', 'PreviousTxnLgrSeq': 21432430}}], 'TransactionIndex': 1, 'TransactionResult': 'tesSUCCESS', 'delivered_amount': '30000000'}, 'validated': True}, id=None, type=<ResponseType.RESPONSE: 'response'>)\n" | |
] | |
} | |
], | |
"source": [ | |
"from xrpl.asyncio.transaction import send_reliable_submission\n", | |
"response = await send_reliable_submission(signed_payment, client)\n", | |
"print(response)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 8, | |
"id": "traditional-burns", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"{'Account': 'rLH398DgLHSyiZLAMZ6DPdKwC5kFA6Aqdq',\n", | |
" 'Amount': '30000000',\n", | |
" 'Destination': 'rPT1Sjq2YGrBMTttX4GZHjKu9dyfzbpAYe',\n", | |
" 'Fee': '10',\n", | |
" 'Flags': 0,\n", | |
" 'LastLedgerSequence': 21432512,\n", | |
" 'Sequence': 21432430,\n", | |
" 'SigningPubKey': 'EDD2ACCA25231A08581EF68C058232AEB31F77DB163D19CC4702D5379F973795D5',\n", | |
" 'TransactionType': 'Payment',\n", | |
" 'TxnSignature': 'B37DA1ACCD857631C2286049F1E3F3E19654F3022089988A1EE51A93B7A03B5C0520A77E8E9ED840FCE661AC70BCAA64D71D93463C32AF61C33DA895EEAD2906',\n", | |
" 'date': 686264893,\n", | |
" 'hash': '1F43B3FF60F8664E165CB8DDFA67F06F4AEFA23AAEA46B219C3549143A66C03C',\n", | |
" 'inLedger': 21432511,\n", | |
" 'ledger_index': 21432511,\n", | |
" 'meta': {'AffectedNodes': [{'ModifiedNode': {'FinalFields': {'Account': 'rPT1Sjq2YGrBMTttX4GZHjKu9dyfzbpAYe',\n", | |
" 'Balance': '96126576774644296',\n", | |
" 'Flags': 0,\n", | |
" 'OwnerCount': 0,\n", | |
" 'Sequence': 2868413},\n", | |
" 'LedgerEntryType': 'AccountRoot',\n", | |
" 'LedgerIndex': '31CCE9D28412FF973E9AB6D0FA219BACF19687D9A2456A0C2ABC3280E9D47E37',\n", | |
" 'PreviousFields': {'Balance': '96126576744644296'},\n", | |
" 'PreviousTxnID': 'F59A81B3F73FC9452BF7057518313ACCCFF4DE0F73EA695ACA6864309D9B839A',\n", | |
" 'PreviousTxnLgrSeq': 21432503}},\n", | |
" {'ModifiedNode': {'FinalFields': {'Account': 'rLH398DgLHSyiZLAMZ6DPdKwC5kFA6Aqdq',\n", | |
" 'Balance': '969999990',\n", | |
" 'Flags': 0,\n", | |
" 'OwnerCount': 0,\n", | |
" 'Sequence': 21432431},\n", | |
" 'LedgerEntryType': 'AccountRoot',\n", | |
" 'LedgerIndex': '665E3CD10B42D619711D96850944AB027342DFE2A83B985C321A9EBB1926824C',\n", | |
" 'PreviousFields': {'Balance': '1000000000',\n", | |
" 'Sequence': 21432430},\n", | |
" 'PreviousTxnID': 'CB1CFBF79E2D14EC33B7EFB2C8CDFF6AB71AB87CA00397736D6154A18C586484',\n", | |
" 'PreviousTxnLgrSeq': 21432430}}],\n", | |
" 'TransactionIndex': 1,\n", | |
" 'TransactionResult': 'tesSUCCESS',\n", | |
" 'delivered_amount': '30000000'},\n", | |
" 'validated': True}\n" | |
] | |
} | |
], | |
"source": [ | |
"from pprint import pprint\n", | |
"pprint(response.result)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"id": "canadian-attitude", | |
"metadata": {}, | |
"outputs": [], | |
"source": [] | |
} | |
], | |
"metadata": { | |
"kernelspec": { | |
"display_name": "Python 3", | |
"language": "python", | |
"name": "python3" | |
}, | |
"language_info": { | |
"codemirror_mode": { | |
"name": "ipython", | |
"version": 3 | |
}, | |
"file_extension": ".py", | |
"mimetype": "text/x-python", | |
"name": "python", | |
"nbconvert_exporter": "python", | |
"pygments_lexer": "ipython3", | |
"version": "3.9.1" | |
} | |
}, | |
"nbformat": 4, | |
"nbformat_minor": 5 | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
# To install the library: | |
# pip install xrpl-py | |
from xrpl.clients import JsonRpcClient | |
client = JsonRpcClient('https://s.altnet.rippletest.net:51234') | |
from xrpl.models import Ping | |
client.request(Ping()) | |
from xrpl.wallet import generate_faucet_wallet | |
wallet = generate_faucet_wallet(client) | |
print(wallet) | |
from xrpl.account import get_balance | |
from xrpl.utils import drops_to_xrp | |
drops_to_xrp(str(get_balance(wallet.classic_address, client))) | |
from xrpl.models import Payment | |
from xrpl.utils import xrp_to_drops | |
payment = Payment(account=wallet.classic_address, amount=xrp_to_drops(30), destination='rPT1Sjq2YGrBMTttX4GZHjKu9dyfzbpAYe') | |
print(payment) | |
from xrpl.transaction import safe_sign_and_autofill_transaction | |
signed_payment = safe_sign_and_autofill_transaction(payment, wallet, client) | |
signed_payment | |
from xrpl.transaction import send_reliable_submission | |
response = send_reliable_submission(signed_payment, client) | |
print(response) | |
from pprint import pprint | |
pprint(response.result) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment