Skip to content

Commit

Permalink
(WIP) bridge transfer snippet
Browse files Browse the repository at this point in the history
  • Loading branch information
mvadari committed Jun 8, 2023
1 parent 8a06661 commit 4800772
Show file tree
Hide file tree
Showing 2 changed files with 108 additions and 0 deletions.
99 changes: 99 additions & 0 deletions snippets/bridge_transfer.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
"""CLI command for setting up a bridge."""

from time import sleep

from xrpl.account import does_account_exist, get_balance
from xrpl.clients import JsonRpcClient
from xrpl.models import (
AccountObjects,
XChainAccountCreateCommit,
XChainBridge,
XChainCommit,
XChainCreateClaimID,
)
from xrpl.transaction import submit_and_wait
from xrpl.utils import xrp_to_drops
from xrpl.wallet import Wallet, generate_faucet_wallet

locking_client = JsonRpcClient("https://sidechain-net1.devnet.rippletest.net:51234")
issuing_client = JsonRpcClient("https://sidechain-net2.devnet.rippletest.net:51234")

locking_chain_door = "rMAXACCrp3Y8PpswXcg3bKggHX76V3F8M4"
bridge_data = locking_client.request(AccountObjects(account=locking_chain_door)).result[
"account_objects"
][0]
bridge = XChainBridge.from_xrpl(bridge_data["XChainBridge"])
print(bridge)

wallet1 = generate_faucet_wallet(locking_client, debug=True)
wallet2 = Wallet.create()

print(f"Creating {wallet2.classic_address} on the issuing chain via the bridge...")

fund_tx = XChainAccountCreateCommit(
account=wallet1.classic_address,
xchain_bridge=bridge,
signature_reward=bridge_data["SignatureReward"],
destination=wallet2.classic_address,
amount=bridge_data["MinAccountCreateAmount"] * 2,
)
fund_response = submit_and_wait(fund_tx, locking_client, wallet1)


# wait for the attestation to go through
ledgers_waited = 0
MAX_LEDGERS_WAITED = 5
while ledgers_waited < MAX_LEDGERS_WAITED:
sleep(4)
if does_account_exist(wallet2.classic_address, issuing_client):
print(
f"Destination account {wallet2.classic_address} has been created via the "
"bridge"
)
initial_balance = get_balance(wallet2.classic_address, issuing_client)
break

ledgers_waited += 1
if ledgers_waited == MAX_LEDGERS_WAITED:
raise Exception("Destination account creation via the bridge failed.")

seq_num_tx = XChainCreateClaimID(
account=wallet2.classic_address,
xchain_bridge=bridge,
signature_reward=bridge_data["SignatureReward"],
other_chain_source=wallet1.classic_address,
)
seq_num_result = submit_and_wait(seq_num_tx, issuing_client, wallet2)

# # extract new sequence number from metadata
nodes = seq_num_result.result["meta"]["AffectedNodes"]
created_nodes = [node["CreatedNode"] for node in nodes if "CreatedNode" in node.keys()]
claim_ids_ledger_entries = [
node for node in created_nodes if node["LedgerEntryType"] == "XChainOwnedClaimID"
]
assert len(claim_ids_ledger_entries) == 1, len(claim_ids_ledger_entries)
xchain_claim_id = claim_ids_ledger_entries[0]["NewFields"]["XChainClaimID"]

# # XChainCommit

commit_tx = XChainCommit(
account=wallet1.classic_address,
amount=xrp_to_drops(1),
xchain_bridge=bridge,
xchain_claim_id=xchain_claim_id,
other_chain_destination=wallet2.classic_address,
)
submit_and_wait(commit_tx, locking_client, wallet1)

# wait for the attestations to go through
ledgers_waited = 0
while ledgers_waited < MAX_LEDGERS_WAITED:
sleep(4)
current_balance = get_balance(wallet2.classic_address, issuing_client)
if current_balance != initial_balance:
print("Transfer is complete")
break

ledgers_waited += 1
if ledgers_waited == MAX_LEDGERS_WAITED:
raise Exception("Bridge transfer failed.")
9 changes: 9 additions & 0 deletions xrpl/models/currencies/xrp.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,3 +72,12 @@ def to_amount(self: XRP, value: Union[str, int]) -> str:
A string representation of XRP amount.
"""
return str(value)

def __repr__(self: XRP) -> str:
"""
Generate string representation of XRP.
Returns:
A string representation of XRP currency.
"""
return "XRP()"

0 comments on commit 4800772

Please sign in to comment.