Skip to content

Commit

Permalink
Migrate findAccountsByPublicKey endpoint to QueryAPI
Browse files Browse the repository at this point in the history
  • Loading branch information
eduohe committed Apr 17, 2024
1 parent 4d1a113 commit 1927871
Showing 1 changed file with 26 additions and 9 deletions.
35 changes: 26 additions & 9 deletions src/middleware/indexer.js
Original file line number Diff line number Diff line change
@@ -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,
Expand Down Expand Up @@ -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);
Expand Down

0 comments on commit 1927871

Please sign in to comment.