Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update Sort v0 API endpoints to use Sort v1 API endpoints #3

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
83 changes: 43 additions & 40 deletions src/pages/api/stats.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,52 +10,55 @@ if (!api_key) {

export async function getStats(offset: number): Promise<Result[]> {
const query = `
-- Define a CTE called 'mapped_sources' that extracts the first 8 characters of the secret value and the function name from the 'user14.transaction' table
WITH mapped_sources AS (
SELECT
SUBSTR(t.function.params[4].value, ${
offset + 3
}, 8) AS source_substr, -- Extract the first 8 characters of the secret value
t.function.name -- Extract the function name
FROM user14.transaction t
WHERE "to" = '0x283af0b28c62c092c9727f1ee09c02ca627eb7f5' -- Filter transactions to the ENS Registrar Controller
AND t.function.name LIKE 'register%' -- Filter transactions to only those that start with 'register'
AND status = 1 -- Filter transactions to only those with a successful status
)
-- Calculate the counts of unique 'source_substr' values that appear only once in the 'mapped_sources' table
SELECT
'No Source Given' AS source,
COUNT(*) AS count
FROM (
SELECT
source_substr
FROM mapped_sources
GROUP BY source_substr
HAVING COUNT(*) <= 1
) subquery
-- Concatenate the results with the counts of non-unique 'source_substr' values, which are optionally mapped to new names
UNION ALL
SELECT
CASE source_substr
WHEN '00000000' THEN 'Null address' -- If the 'source_substr' is '00000', map it to 'Null'
WHEN '03acfad5' THEN 'ensfairy.eth' -- If the 'source_substr' is '03acfa', map it to 'ensfairy.eth'
-- Add more mappings here as needed
ELSE source_substr -- If the 'source_substr' is not mapped, use the original value
END AS source,
COUNT(*) AS "count"
FROM mapped_sources
GROUP BY source
HAVING COUNT(*) > 1
ORDER BY "count" DESC
WITH mapped_sources as (
SELECT t.transaction_hash
,p.bin_val::varchar as secret
,SUBSTR(p.bin_val::varchar,
${ offset + 3 },
8) as source_substr
FROM ethereum.transaction t,
--ethereum.block b,
ethereum.transaction_param p
WHERE p.transaction_id = t.id
AND t.to_address = '0x283af0b28c62c092c9727f1ee09c02ca627eb7f5'
--AND b.id = t.block_id
AND t.function like 'register%'
AND p.path = 'secret'
)
SELECT 'No Source Given' as source, count(*) as "count"
FROM
(SELECT source_substr
FROM mapped_sources
GROUP BY source_substr
HAVING COUNT(*) <= 1) subquery
UNION ALL
SELECT CASE source_substr
WHEN '03acfad5' THEN 'ensfairy.eth'
WHEN '00000000' THEN 'No Secret used'
WHEN '02224567' THEN 'gpt-emoji.eth'
WHEN '89abcdef' THEN 'gpt-emoji.eth'
ELSE source_substr
END as source, count(*) as "count"
FROM mapped_sources
GROUP BY source
HAVING COUNT(*) > 1
ORDER BY "count" DESC
`

const sortxyz: SortResponse = await got
.post('https://api.sort.xyz/v0/sql', {
json: { query, api_key },
.post('https://api.sort.xyz/v1/queries/run', {
headers: {
'x-api-key': api_key as string,
'Accept': 'application/json',
'Content-Type': 'application/json'
},
json: {
query : query
},
})
.json()

return sortxyz.query_response.results
return sortxyz.data.records
}

export default async function handler(
Expand Down
19 changes: 7 additions & 12 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,13 @@ export type Result = {
}

export type SortResponse = {
id: string
success: boolean
query_response: {
collections: string[]
column_fields: Result[]
results: Result[]
query_id: string
stats: {
elapsed_time_ms: number
throttled_time_micros: number
}
status: string
code: number
data: {
durationMs: number
id: string
query: string
records: Result[]
recordCount: number
}
}

Expand Down