From 1927871031b8faba3c18a2e23a110cf05c7016aa Mon Sep 17 00:00:00 2001 From: Eduardo Ohe Date: Wed, 17 Apr 2024 14:37:00 -0700 Subject: [PATCH] Migrate findAccountsByPublicKey endpoint to QueryAPI --- src/middleware/indexer.js | 35 ++++++++++++++++++++++++++--------- 1 file changed, 26 insertions(+), 9 deletions(-) diff --git a/src/middleware/indexer.js b/src/middleware/indexer.js index 5152e5ca..f84d9d70 100644 --- a/src/middleware/indexer.js +++ b/src/middleware/indexer.js @@ -1,9 +1,11 @@ const { Pool } = require('pg'); const Cache = require('node-cache'); const bunyan = require('bunyan'); +const fetch = require('node-fetch'); const { BRIDGE_TOKEN_FACTORY_ACCOUNT_ID = 'factory.bridge.near', + GRAPHQL_URL = 'https://near-queryapi.api.pagoda.co/v1/graphql', NEAR_WALLET_ENV, // INDEXER_DB_CONNECTION, INDEXER_DB_REPLICAS, @@ -119,15 +121,30 @@ const findAccountActivity = async (ctx) => { const findAccountsByPublicKey = async (ctx) => { try { const { publicKey } = ctx.params; - const { rows } = await pool.query(` - SELECT DISTINCT account_id - FROM access_keys - JOIN accounts USING (account_id) - WHERE public_key = $1 - AND accounts.deleted_by_receipt_id IS NULL - AND access_keys.deleted_by_receipt_id IS NULL - `, [publicKey]); - ctx.body = rows.map(({ account_id }) => account_id); + + const response = await fetch(GRAPHQL_URL, { + method: 'POST', + headers: { + 'content-type': 'application/json', + 'x-hasura-role': 'dataplatform_near' + }, + body: JSON.stringify({ + query: ` + query access_keys_v1_by_public_key { + dataplatform_near_access_keys_v1_access_keys_v1( + where: {public_key: {_eq: \"${publicKey}\"}} + ) { + account_id + } + } + `, + operationName: 'access_keys_v1_by_public_key' + }), + }); + + const respJson = await response.json(); + const rows = respJson.data.dataplatform_near_access_keys_v1_access_keys_v1; + ctx.body = rows.map(item => item.account_id); } catch (e) { if (ctx.log && typeof ctx.log.error === 'function') { ctx.log.error(e);