mirror of
https://github.com/XRPLF/xrpl-dev-portal.git
synced 2025-11-28 07:35:50 +00:00
Merge pull request #1564 from ObiajuluM/patch-1
Added Python code samples to Freeze and unfreeze issued tokens, check freeze status and give up the ability to freeze tokens 2
This commit is contained in:
45
content/_code-samples/freeze/py/check_freeze_status.py
Normal file
45
content/_code-samples/freeze/py/check_freeze_status.py
Normal file
@@ -0,0 +1,45 @@
|
||||
from xrpl.clients import JsonRpcClient
|
||||
from xrpl.models import AccountLines
|
||||
|
||||
client = JsonRpcClient("https://xrplcluster.com")
|
||||
|
||||
print("connected to mainnet")
|
||||
|
||||
# Real accounts that were frozen on mainnet as an example
|
||||
|
||||
# Issuer address
|
||||
issuer_addr = "rf1BiGeXwwQoi8Z2ueFYTEXSwuJYfV2Jpn"
|
||||
|
||||
# Target address to query for freeze status
|
||||
target_addr = "rUpy3eEg8rqjqfUoLeBnZkscbKbFsKXC3v"
|
||||
|
||||
token_name = "USD"
|
||||
print(f"searching for a trustline for {token_name} issued by {target_addr} for the address {issuer_addr}")
|
||||
|
||||
# Build account line query
|
||||
acc_info = AccountLines(account=issuer_addr, ledger_index="validated")
|
||||
|
||||
# Submit query
|
||||
response = client.request(acc_info)
|
||||
|
||||
# Parse response for result
|
||||
result = response.result
|
||||
|
||||
# Parse result for account lines
|
||||
found_target_line = False
|
||||
if "lines" in result:
|
||||
lines = result["lines"]
|
||||
for line in lines:
|
||||
# Query result with trustline params
|
||||
if target_addr == line["account"] and token_name == line["currency"]:
|
||||
found_target_line = True
|
||||
|
||||
if 'freeze' in line:
|
||||
print(f'freeze status of trustline: {line["freeze"]}')
|
||||
else:
|
||||
print(f'freeze status of trustline: False')
|
||||
|
||||
if(not(found_target_line)):
|
||||
print(f"no such trustline exists for {token_name} issued by {target_addr} for the address {issuer_addr}")
|
||||
else:
|
||||
print("this account has no trustlines")
|
||||
39
content/_code-samples/freeze/py/check_global_freeze.py
Normal file
39
content/_code-samples/freeze/py/check_global_freeze.py
Normal file
@@ -0,0 +1,39 @@
|
||||
from xrpl.clients import JsonRpcClient
|
||||
from xrpl.models import AccountInfo
|
||||
|
||||
client = JsonRpcClient("https://s.altnet.rippletest.net:51234") # Connect to testnetwork
|
||||
|
||||
|
||||
ACCOUNT_ROOT_LEDGER_FLAGS: dict[str, int] = {
|
||||
"lsfNoFreeze": 0x00200000,
|
||||
"lsfGlobalFreeze": 0x00400000,
|
||||
}
|
||||
|
||||
def parse_account_root_flags(flags: int) -> list[str]:
|
||||
flags_enabled = []
|
||||
for flag in ACCOUNT_ROOT_LEDGER_FLAGS:
|
||||
check_flag = ACCOUNT_ROOT_LEDGER_FLAGS[flag]
|
||||
if check_flag & flags == check_flag:
|
||||
flags_enabled.append(flag)
|
||||
return flags_enabled
|
||||
|
||||
# Issuer address to query for global freeze status
|
||||
issuer_addr = "rfDJ98Z8k7ubr6atbZoCqAPdg9MetyBwcg"
|
||||
|
||||
# Build account line query
|
||||
print(f"Checking if global freeze is enabled for the address {issuer_addr}")
|
||||
acc_info = AccountInfo(account=issuer_addr, ledger_index="validated")
|
||||
|
||||
# Submit query
|
||||
response = client.request(acc_info)
|
||||
|
||||
# Parse response for result
|
||||
result = response.result
|
||||
|
||||
# Query result for global freeze status
|
||||
if "account_data" in result:
|
||||
if "Flags" in result["account_data"]:
|
||||
if "lsfGlobalFreeze" in parse_account_root_flags(result["account_data"]["Flags"]):
|
||||
print("Global Freeze is enabled")
|
||||
else:
|
||||
print("Global Freeze is disabled")
|
||||
39
content/_code-samples/freeze/py/check_no_freeze.py
Normal file
39
content/_code-samples/freeze/py/check_no_freeze.py
Normal file
@@ -0,0 +1,39 @@
|
||||
from xrpl.clients import JsonRpcClient
|
||||
from xrpl.models import AccountInfo
|
||||
|
||||
client = JsonRpcClient("https://xrplcluster.com") # Connect to a network
|
||||
|
||||
|
||||
ACCOUNT_ROOT_LEDGER_FLAGS: dict[str, int] = {
|
||||
"lsfNoFreeze": 0x00200000,
|
||||
"lsfGlobalFreeze": 0x00400000,
|
||||
}
|
||||
|
||||
def parse_account_root_flags(flags: int) -> list[str]:
|
||||
flags_enabled = []
|
||||
for flag in ACCOUNT_ROOT_LEDGER_FLAGS:
|
||||
check_flag = ACCOUNT_ROOT_LEDGER_FLAGS[flag]
|
||||
if check_flag & flags == check_flag:
|
||||
flags_enabled.append(flag)
|
||||
return flags_enabled
|
||||
|
||||
# Issuer address to query for no freeze status
|
||||
issuer_addr = "rUpy3eEg8rqjqfUoLeBnZkscbKbFsKXC3v"
|
||||
|
||||
# Build account line query
|
||||
print(f"Checking if the 'No Freeze' flag is enabled for the address {issuer_addr}")
|
||||
acc_info = AccountInfo(account=issuer_addr, ledger_index="validated")
|
||||
|
||||
# Submit query
|
||||
response = client.request(acc_info)
|
||||
|
||||
# Parse response for result
|
||||
result = response.result
|
||||
|
||||
# Query result for no freeze status
|
||||
if "account_data" in result:
|
||||
if "Flags" in result["account_data"]:
|
||||
if "lsfNoFreeze" in parse_account_root_flags(result["account_data"]["Flags"]):
|
||||
print("No Freeze is enabled")
|
||||
else:
|
||||
print("No Freeze is disabled")
|
||||
34
content/_code-samples/freeze/py/enable_no_freeze.py
Normal file
34
content/_code-samples/freeze/py/enable_no_freeze.py
Normal file
@@ -0,0 +1,34 @@
|
||||
from xrpl.clients import JsonRpcClient
|
||||
from xrpl.models import AccountSet, AccountSetFlag
|
||||
from xrpl.transaction import (safe_sign_and_autofill_transaction,
|
||||
send_reliable_submission)
|
||||
from xrpl.wallet import Wallet, generate_faucet_wallet
|
||||
|
||||
client = JsonRpcClient("https://s.altnet.rippletest.net:51234") # connect to testnet
|
||||
|
||||
|
||||
# generate wallet
|
||||
sender_wallet = generate_faucet_wallet(client=client)
|
||||
|
||||
print("Successfully generated test wallet")
|
||||
|
||||
# build accountset transaction to disable freezing
|
||||
accountset = AccountSet(account=sender_wallet.classic_address, set_flag=AccountSetFlag.ASF_NO_FREEZE)
|
||||
|
||||
# sign transaction
|
||||
stxn = safe_sign_and_autofill_transaction(accountset, sender_wallet, client)
|
||||
|
||||
print("Now sending an AccountSet transaction to set the ASF_NO_FREEZE flag...")
|
||||
|
||||
# submit transaction and wait for result
|
||||
stxn_response = send_reliable_submission(stxn, client)
|
||||
|
||||
|
||||
# parse response for result
|
||||
stxn_result = stxn_response.result
|
||||
|
||||
# print result and transaction hash
|
||||
if stxn_result["meta"]["TransactionResult"] == "tesSUCCESS":
|
||||
print(f'Successfully enabled no freeze for {sender_wallet.classic_address}')
|
||||
print(stxn_result["hash"])
|
||||
|
||||
42
content/_code-samples/freeze/py/freeze_token.py
Normal file
42
content/_code-samples/freeze/py/freeze_token.py
Normal file
@@ -0,0 +1,42 @@
|
||||
from xrpl.clients import JsonRpcClient
|
||||
from xrpl.models import IssuedCurrencyAmount, TrustSet
|
||||
from xrpl.models.transactions import TrustSetFlag
|
||||
from xrpl.transaction import (safe_sign_and_autofill_transaction,
|
||||
send_reliable_submission)
|
||||
from xrpl.wallet import generate_faucet_wallet
|
||||
|
||||
client = JsonRpcClient("https://s.altnet.rippletest.net:51234") # Connect to testnet
|
||||
|
||||
token_name = "FOO"
|
||||
|
||||
# Amount a trustline can handle, for this transaction it is set to 0
|
||||
value = "100"
|
||||
|
||||
target_addr = generate_faucet_wallet(client=client).classic_address
|
||||
|
||||
sender_wallet = generate_faucet_wallet(client=client)
|
||||
|
||||
print("Successfully generated test wallets")
|
||||
|
||||
# Build trustline freeze transaction
|
||||
trustset = TrustSet(account=sender_wallet.classic_address, limit_amount=IssuedCurrencyAmount(
|
||||
currency= token_name,
|
||||
issuer=target_addr,
|
||||
value = value),
|
||||
flags=TrustSetFlag.TF_SET_FREEZE)
|
||||
|
||||
# Sign transaction
|
||||
stxn = safe_sign_and_autofill_transaction(trustset, sender_wallet, client)
|
||||
|
||||
print("Now setting a trustline with the TF_SET_FREEZE flag enabled...")
|
||||
|
||||
# Submit transaction and wait for result
|
||||
stxn_response = send_reliable_submission(stxn, client)
|
||||
|
||||
# Parse response for result
|
||||
stxn_result = stxn_response.result
|
||||
|
||||
if(stxn_result["meta"]["TransactionResult"] == 'tesSUCCESS'):
|
||||
print(f"Froze {token_name} issued by {target_addr} for address {sender_wallet.classic_address}")
|
||||
if(stxn_result["meta"]["TransactionResult"] == 'tesSUCCESS'):
|
||||
print(f"Froze {token_name} issued by {target_addr} for address {sender_wallet.classic_address}")
|
||||
1
content/_code-samples/freeze/py/requirements.txt
Normal file
1
content/_code-samples/freeze/py/requirements.txt
Normal file
@@ -0,0 +1 @@
|
||||
xrpl-py
|
||||
30
content/_code-samples/freeze/py/set_global_freeze.py
Normal file
30
content/_code-samples/freeze/py/set_global_freeze.py
Normal file
@@ -0,0 +1,30 @@
|
||||
from xrpl.clients import JsonRpcClient
|
||||
from xrpl.models import AccountSet, AccountSetFlag
|
||||
from xrpl.transaction import (safe_sign_and_autofill_transaction,
|
||||
send_reliable_submission)
|
||||
from xrpl.wallet import generate_faucet_wallet
|
||||
|
||||
client = JsonRpcClient("https://s.altnet.rippletest.net:51234") # Connect to testnet
|
||||
|
||||
# Sender wallet object
|
||||
sender_wallet = generate_faucet_wallet(client)
|
||||
|
||||
print("Successfully generated test wallet")
|
||||
|
||||
# Build accountset transaction to enable global freeze
|
||||
accountset = AccountSet(account=sender_wallet.classic_address,
|
||||
set_flag=AccountSetFlag.ASF_GLOBAL_FREEZE)
|
||||
|
||||
print("Preparing and submitting Account set transaction with ASF_GLOBAL_FREEZE ...")
|
||||
|
||||
# Sign and submit transaction
|
||||
stxn = safe_sign_and_autofill_transaction(accountset, sender_wallet, client)
|
||||
stxn_response = send_reliable_submission(stxn, client)
|
||||
|
||||
# Parse response for result
|
||||
stxn_result = stxn_response.result
|
||||
|
||||
# Print result and transaction hash
|
||||
if stxn_result["meta"]["TransactionResult"] == "tesSUCCESS":
|
||||
print(f'Successfully enabled global freeze for {sender_wallet.classic_address}')
|
||||
print(stxn_result["hash"])
|
||||
50
content/_code-samples/freeze/py/unfreeze_token.py
Normal file
50
content/_code-samples/freeze/py/unfreeze_token.py
Normal file
@@ -0,0 +1,50 @@
|
||||
from xrpl.clients import JsonRpcClient
|
||||
from xrpl.models import IssuedCurrencyAmount, TrustSet, TrustSetFlag
|
||||
from xrpl.transaction import (safe_sign_and_autofill_transaction,
|
||||
send_reliable_submission)
|
||||
from xrpl.wallet import generate_faucet_wallet
|
||||
|
||||
client = JsonRpcClient("https://s.altnet.rippletest.net:51234") # connect to testnet
|
||||
|
||||
token_name = "USD"
|
||||
|
||||
# Amount a trustline can handle
|
||||
value = "0"
|
||||
print("Generating two test wallets...")
|
||||
|
||||
# Address to unfreeze trustline
|
||||
target_addr = generate_faucet_wallet(client=client).classic_address
|
||||
print("Successfully generated the target account")
|
||||
|
||||
# Sender wallet
|
||||
sender_wallet = generate_faucet_wallet(client=client)
|
||||
print("Successfully generated the sender account")
|
||||
|
||||
print("Successfully generated test wallets")
|
||||
|
||||
# Build trustline freeze transaction
|
||||
trustset = TrustSet(account=sender_wallet.classic_address, limit_amount=IssuedCurrencyAmount(
|
||||
currency=token_name,
|
||||
issuer=target_addr,
|
||||
value = value
|
||||
),
|
||||
flags=TrustSetFlag.TF_CLEAR_FREEZE)
|
||||
|
||||
print("prepared and submitting TF_CLEAR_FREEZE transaction")
|
||||
|
||||
# Sign and Submit transaction
|
||||
stxn = safe_sign_and_autofill_transaction(trustset, sender_wallet, client)
|
||||
stxn_response = send_reliable_submission(stxn, client)
|
||||
|
||||
# Parse response for result
|
||||
stxn_result = stxn_response.result
|
||||
|
||||
# Print result and transaction hash
|
||||
if stxn_result["meta"]["TransactionResult"] == "tesSUCCESS":
|
||||
print(f'Successfully enabled no freeze for {sender_wallet.classic_address}')
|
||||
if stxn_result["meta"]["TransactionResult"] == "tecNO_LINE_REDUNDANT":
|
||||
print("This was used on an account which didn't have a trustline yet. To try this out, modify `target_addr` to point to an account with a frozen trustline, and make sure the currency code matches.")
|
||||
else:
|
||||
print(f"Transaction failed with {stxn_result['meta']['TransactionResult']}")
|
||||
|
||||
print(stxn_result["hash"])
|
||||
Reference in New Issue
Block a user