XRPLF / xrpl-py

A Python library to interact with the XRP Ledger (XRPL) blockchain
ISC License
148 stars 84 forks source link

Non Standard Currency Code TrustSet not working. #265

Closed parkerburchett closed 3 years ago

parkerburchett commented 3 years ago

I found this code from @ledhed2222 at this issue https://github.com/XRPLF/xrpl-py/issues/259.

This works as is.

# I had to change some of the import paths
from xrpl.clients import JsonRpcClient
from xrpl.models.amounts import IssuedCurrencyAmount
from xrpl.models.transactions import Payment, TrustSet
from xrpl.transaction import safe_sign_and_submit_transaction
from xrpl.wallet import Wallet, generate_faucet_wallet

CLIENT = JsonRpcClient("https://s.altnet.rippletest.net:51234/")

ISSUING_WALLET = generate_faucet_wallet(CLIENT,debug=True)
RECEIVING_WALLET = generate_faucet_wallet(CLIENT,debug=True)

# create a trust line from receiver to issuer. in this case, 
# the receiver trusts the issuer for up to $1,000,000 USD
safe_sign_and_submit_transaction(
  TrustSet(
    account=RECEIVING_WALLET.classic_address,
    limit_amount=IssuedCurrencyAmount(
      issuer=ISSUING_WALLET.classic_address,
      currency="USD",
      value="1000000",
   ),
  ),
  RECEIVING_WALLET,
  CLIENT,
)

# now, issue some USD from issuer to receiver
safe_sign_and_submit_transaction(
  Payment(
    account=ISSUING_WALLET.classic_address,
    amount=IssuedCurrencyAmount(
      issuer=ISSUING_WALLET.classic_address,
      currency="USD",
      value="100",
    ),
    destination=RECEIVING_WALLET.classic_address,
  ),
  ISSUING_WALLET,
  CLIENT,
)

And gives this output:

Attempting to fund address r3giEShN7EPdkhx4AyJRnDr9v9mW9xt49B
Faucet fund successful.
Attempting to fund address rLv15mLwVRhRFUGSuhCHd7Hu5XnqmsQrEs
Faucet fund successful.
Response(status=<ResponseStatus.SUCCESS: 'success'>, result={'accepted': True, 'account_sequence_available': 18616526, 'account_sequence_next': 18616526, 'applied': True, 'broadcast': True, 'engine_result': 'tesSUCCESS', 'engine_result_code': 0, 'engine_result_message': 'The transaction was applied. Only final in a validated ledger.', 'kept': True, 'open_ledger_cost': '10', 'queued': False, 'tx_blob': '120000220000000024011C10CD201B011C10E661D5038D7EA4C680000000000000000000000000005553440000000000545236BEAEF61FA33A6258F545614BF58B57571368400000000000000A7321ED37EDB3F903B2FB6433B105C19FEB636185B5CEC18C26708D6B04F61B8A29FB637440DF40FAD216C8566F1C696CD9F3C6F02E5300D44E27CFE1FDB99FBDFA7EF0886DC0B4C9DB10A6A827BC09045300209C1E5F6E14C1DF9D73CD8960D752AB9FD6078114545236BEAEF61FA33A6258F545614BF58B5757138314DA9AC65CBCEF2AB812BCA5CEE9E180AF0A31E4C2', 'tx_json': {'Account': 'r3giEShN7EPdkhx4AyJRnDr9v9mW9xt49B', 'Amount': {'currency': 'USD', 'issuer': 'r3giEShN7EPdkhx4AyJRnDr9v9mW9xt49B', 'value': '100'}, 'Destination': 'rLv15mLwVRhRFUGSuhCHd7Hu5XnqmsQrEs', 'Fee': '10', 'Flags': 0, 'LastLedgerSequence': 18616550, 'Sequence': 18616525, 'SigningPubKey': 'ED37EDB3F903B2FB6433B105C19FEB636185B5CEC18C26708D6B04F61B8A29FB63', 'TransactionType': 'Payment', 'TxnSignature': 'DF40FAD216C8566F1C696CD9F3C6F02E5300D44E27CFE1FDB99FBDFA7EF0886DC0B4C9DB10A6A827BC09045300209C1E5F6E14C1DF9D73CD8960D752AB9FD607', 'hash': '0E5E087C9F8A83037C2EB97FE42105C4652C9BA404E30082E50BF2457484FDE5'}, 'validated_ledger_index': 18616530}, id=None, type=<ResponseType.RESPONSE: 'response'>)

However when I replace the currency code with the example nonstandard currency code at https://xrpl.org/currency-formats.html#currency-codes

from xrpl.clients import JsonRpcClient
from xrpl.models.amounts import IssuedCurrencyAmount
from xrpl.models.transactions import Payment, TrustSet
from xrpl.transaction import safe_sign_and_submit_transaction
from xrpl.wallet import Wallet, generate_faucet_wallet

non_standard_currency_code = "0158415500000000C1F76FF6ECB0BAC600000000"

CLIENT = JsonRpcClient("https://s.altnet.rippletest.net:51234/")

ISSUING_WALLET = generate_faucet_wallet(CLIENT,debug=True)
RECEIVING_WALLET = generate_faucet_wallet(CLIENT,debug=True)

# create a trust line from receiver to issuer. in this case, 
# the receiver trusts the issuer for up to $1,000,000 USD
safe_sign_and_submit_transaction(
  TrustSet(
    account=RECEIVING_WALLET.classic_address,
    limit_amount=IssuedCurrencyAmount(
      issuer=ISSUING_WALLET.classic_address,
      currency=non_standard_currency_code,
      value="1000000",
   ),
  ),
  RECEIVING_WALLET,
  CLIENT,
)

# now, issue some USD from issuer to receiver
safe_sign_and_submit_transaction(
  Payment(
    account=ISSUING_WALLET.classic_address,
    amount=IssuedCurrencyAmount(
      issuer=ISSUING_WALLET.classic_address,
      currency=non_standard_currency_code,
      value="100",
    ),
    destination=RECEIVING_WALLET.classic_address,
  ),
  ISSUING_WALLET,
  CLIENT,
)

I get this error:

Attempting to fund address rMUNabsbHeHVDkLhXvbYKEHMcuLpAoazfc
Faucet fund successful.
Attempting to fund address rGDUq8RcWMgYku4Ri8LHDSddTu818ifWo6
Faucet fund successful.
UnicodeDecodeError: 'ascii' codec can't decode byte 0xec in position 0: ordinal not in range(128)

I tried non_standard_currency_code = u"0158415500000000C1F76FF6ECB0BAC600000000"

and non_standard_currency_code = "0158415500000000C1F76FF6ECB0BAC600000000".encode('utf-8')

Neither fixed it.

Does anyone know how to create a TrustSet and Payment with a nonstandard currency code?

ledhed2222 commented 3 years ago

What version of the lib are you using? since you had to change the imports, I'm guessing it's 1.0.0. We fixed a lot of bugs in 1.1.0. Can you confirm that this issue is present on 1.1.0?

parkerburchett commented 3 years ago

I'm now using version 1.1 with this code

from xrpl.clients import JsonRpcClient
from xrpl.models import IssuedCurrencyAmount, Payment, TrustSet
from xrpl.transaction import safe_sign_and_submit_transaction
from xrpl.wallet import Wallet, generate_faucet_wallet

non_standard_currency_code = "0158415500000000C1F76FF6ECB0BAC600000000"
CLIENT = JsonRpcClient("https://s.altnet.rippletest.net:51234/")

# create wallets
ISSUING_WALLET = generate_faucet_wallet(CLIENT,debug=True)
RECEIVING_WALLET = generate_faucet_wallet(CLIENT,debug=True)

# create a trust line from receiver to issuer.
safe_sign_and_submit_transaction(
  TrustSet(
    account=RECEIVING_WALLET.classic_address,
    limit_amount=IssuedCurrencyAmount(
      issuer=ISSUING_WALLET.classic_address,
      currency=non_standard_currency_code,
      value="1000000",
   ),
  ),
  RECEIVING_WALLET,
  CLIENT,
)

# now, issue some non standard currency code from issuer to receiver
response = safe_sign_and_submit_transaction(
  Payment(
    account=ISSUING_WALLET.classic_address,
    amount=IssuedCurrencyAmount(
      issuer=ISSUING_WALLET.classic_address,
      currency=non_standard_currency_code,
      value="100",
    ),
    destination=RECEIVING_WALLET.classic_address,
  ),
  ISSUING_WALLET,
  CLIENT,
)

print(response.result)

I get the same error.

my_venv\lib\site-packages\xrpl\core\binarycodec\types\currency.py", line 80, in __init__
    iso = code_bytes.decode("ascii")
UnicodeDecodeError: 'ascii' codec can't decode byte 0xec in position 0: ordinal not in range(128)

I stepped through the code and the error is in safe_sign_and_submit_transaction

ledhed2222 commented 3 years ago

indeed. the linked PR fixes this and we'll likely release a patch 1.1.1 version with that change in a few days

ledhed2222 commented 3 years ago

v1.1.1 is now published. have fun @parkerburchett !