XRPLF / xrpl-py

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

Utility function to derive flags from integer #390

Open LimpidCrypto opened 2 years ago

LimpidCrypto commented 2 years ago

When receiving a transactions data, the Flags field contains all flags set expressed as integer. Example:

{
    "Account": "rMapXdAPsZBScjMKUki7hJBz7aZH51BRqi",
    "Fee": "15",
    "Flags": 2148139008,
    "LastLedgerSequence": 71663679,
    "Memos": [
        {
            "Memo": {
                "MemoData": "4F666665722076696120546F6B656E205472617368657220784170702E",
                "MemoType": "7872706C2E7365727669636573"
            }
        }
    ],
    "Sequence": 63126083,
    "SigningPubKey": "02846E0549D225326F22EB8845B0AB97B4BF95EF5BB698694E3FE97CCCF88E7EAF",
    "TakerGets": {
        "currency": "585250616E646100000000000000000000000000",
        "issuer": "r9uQt7Y34SwSyKqdb5sMmAqk37rh3Y4V7",
        "value": "0.05"
    },
    "TakerPays": "1",
    "TransactionType": "OfferCreate",
    "TxnSignature": "3044022062AE9E76F5E5E76CBBA4397DFE07384A2111B1FED0FD3E1E41EC0DAADAD5D35502203A4C39C431C0E4C67DBC0E231DCB2D73FA8826D553349B708910597AE07F50C0",
    "date": 705918052,
    "hash": "684A057DE7FB687DD8883628D69222A86F72A3DB8F350CDF5681DCB42AED2C1E",
    "inLedger": 71663671,
    "ledger_index": 71663671,
    "meta": ...
}

In this example the Flags set are 2148139008. Since it's not immediately obvious which flags are set for that transaction, there should be a utility function that derives the flag names from this integer. The function should take the transaction type and the flags expressed as integer (in this example 2148139008). For the example it should return something like ['tf_immediate_or_cancel', 'tf_sell', 'tf_fully_canonical_sig'].

JST5000 commented 1 year ago

This is an example of this done in xrpl.js specifically for AccountRoot flags which may be a helpful reference: https://github.com/XRPLF/xrpl.js/pull/1699/files

(xrpl.org is the best source for what the flags do)

LimpidCrypto commented 1 year ago

This is an example of this done in xrpl.js specifically for AccountRoot flags which may be a helpful reference: https://github.com/XRPLF/xrpl.js/pull/1699/files

(xrpl.org is the best source for what the flags do)

Yeah, I also already posted an example for python on Discord once:

ACCOUNT_ROOT_LEDGER_FLAGS: Dict[str, int] = {
    "lsfPasswordSpent": 0x00010000,
    "lsfRequireDestTag": 0x00020000,
    "lsfRequireAuth": 0x00040000,
    "lsfRequireAuth": 0x00040000,
    "lsfDisableMaster": 0x00100000,
    "lsfNoFreeze": 0x00200000,
    "lsfGlobalFreeze": 0x00400000,
    "lsfDefaultRipple": 0x00800000,
    "lsfDepositAuth": 0x01000000,
}

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

print(parse_account_root_flags(4194304))