rw2023Loan type COUNT
    Updated 2024-09-17
    SELECT
    DATE_TRUNC('day', block_timestamp) AS day,
    CASE
    -- Unificar TakeLoan (Core y V3)
    WHEN (inner_instruction:instructions[2]:parsed:type = 'createAccount'
    AND inner_instruction:instructions[4]:parsed:type = 'initializeAccount3'
    AND (inner_instruction:instructions[5]:data like '4F7Dof%' OR inner_instruction:instructions[6]:data = 'T'))
    OR (inner_instruction:instructions[0]:programId = 'CoREENxT6tW1HoK8ypY1SxRMZTcVPm7R94rH4PZNhX7d'
    AND (inner_instruction:instructions[1]:parsed:type = 'transfer' OR inner_instruction:instructions[2]:parsed:type = 'transfer')
    AND (inner_instruction:instructions[3]:parsed:type = 'transfer' OR inner_instruction:instructions[4]:parsed:type = 'transfer'))
    THEN 'TakeLoan'

    -- Unificar ExtendLoan (Core y V3)
    WHEN ((inner_instruction:instructions[6]:parsed:type = 'thawAccount' OR inner_instruction:instructions[7]:parsed:type = 'thawAccount')
    AND (inner_instruction:instructions[7]:parsed:type = 'revoke' OR inner_instruction:instructions[8]:parsed:type = 'revoke'))
    OR ((inner_instruction:instructions[5]:parsed:type = 'transfer' OR inner_instruction:instructions[6]:parsed:type = 'transfer')
    AND (inner_instruction:instructions[6]:parsed:type = 'closeAccount' OR inner_instruction:instructions[7]:parsed:type = 'closeAccount'))
    OR (inner_instruction:instructions[0]:data = '31y1' AND inner_instruction:instructions[4]:parsed:type = 'transfer')
    THEN 'ExtendLoan'

    -- Unificar Foreclose (Core y V3)
    WHEN ((inner_instruction:instructions[10]:parsed:type = 'allocate'
    OR inner_instruction:instructions[11]:parsed:type = 'allocate'
    OR inner_instruction:instructions[12]:parsed:type = 'allocate')
    OR (inner_instruction:instructions[6]:parsed:type = 'thawAccount' AND inner_instruction:instructions[7]:parsed:type = 'transfer'))
    OR (inner_instruction:instructions[0]:data = '31y1' AND inner_instruction:instructions[1]:data = '24o')
    THEN 'ForecloseLoan'

    -- Unificar RepayLoan (V3)
    WHEN inner_instruction:instructions[0]:parsed:type = 'transfer'
    AND inner_instruction:instructions[1]:parsed:type = 'transfer'
    AND inner_instruction:instructions[2]:parsed:type = 'transfer'
    THEN 'RepayLoan'


    END AS tx_type,
    QueryRunArchived: QueryRun has been archived