-
Notifications
You must be signed in to change notification settings - Fork 2
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
Add tx history endpoints #67
Merged
Merged
Changes from 8 commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
4d36cd5
Add tx history endpoints
samchuk-vlad c8d2fb1
Refactor code
samchuk-vlad 9a87679
Add new envs to feature based deployment file
samchuk-vlad 15da00c
Change tx aggregator client endpoint
samchuk-vlad 23b8325
Change aggregator endpoint
samchuk-vlad 0d483f0
Remove console.log
samchuk-vlad 9dbbd9c
Change tx aggregator endpoint
samchuk-vlad d5cc3bc
Change envs for aggregator
samchuk-vlad 9f4faba
Refactor after review
samchuk-vlad d0fcd83
Change txAggregatorGraphQlClient url
samchuk-vlad 6395c39
Revert prod configs
samchuk-vlad File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
import { Router } from 'express' | ||
import { getAccountTxHistory, getAccountTxHistoryWithQueue } from '../../services/txHistory' | ||
|
||
const createTxHistoryRouter = () => { | ||
const router = Router() | ||
|
||
router.get('/history/queue', async function (req, res) { | ||
const { address, pageSize, offset } = req.query | ||
const txs = await getAccountTxHistoryWithQueue({ | ||
address: address as string, | ||
pageSize: parseInt(pageSize as string), | ||
offset: parseInt(offset as string), | ||
}) | ||
|
||
res.send(txs) | ||
}) | ||
|
||
router.get('/history', async function (req, res) { | ||
const { address, pageSize, offset, networks, events } = req.query | ||
const txs = await getAccountTxHistory({ | ||
address: address as string, | ||
pageSize: parseInt(pageSize as string), | ||
offset: parseInt(offset as string), | ||
networks: networks as string[], | ||
events: events as string[], | ||
}) | ||
|
||
res.send(txs) | ||
}) | ||
|
||
return router | ||
} | ||
|
||
export default createTxHistoryRouter |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,7 @@ | ||
import express from 'express' | ||
import cors from 'cors' | ||
import timeout from 'connect-timeout' | ||
import { reqTimeoutSecs, allowedOrigins, port } from './constant/env' | ||
import { reqTimeoutSecs, port } from './constant/env' | ||
import { newLogger } from '@subsocial/utils' | ||
|
||
import { createRoutes } from './routes' | ||
|
@@ -22,28 +22,28 @@ export const startHttpServer = (apis: Connections) => { | |
|
||
app.use(express.static('public')) | ||
|
||
app.use( | ||
cors((req, callback) => { | ||
const corsOptions = { origin: true } | ||
const origin = req.header('Origin') | ||
const isAllowedOrigin = allowedOrigins.some((allowedOrigin) => | ||
origin?.includes(allowedOrigin) | ||
) | ||
if (!isAllowedOrigin) { | ||
corsOptions.origin = false | ||
} | ||
callback(null, corsOptions) | ||
}) | ||
) | ||
|
||
// For localhost testing | ||
// app.use( | ||
// cors((req, callback) => { | ||
// const origin = req.method === 'GET' ? '*' : '*' | ||
// callback(null, { origin }) | ||
// const corsOptions = { origin: true } | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. change this back too, or use |
||
// const origin = req.header('Origin') | ||
// const isAllowedOrigin = allowedOrigins.some((allowedOrigin) => | ||
// origin?.includes(allowedOrigin) | ||
// ) | ||
// if (!isAllowedOrigin) { | ||
// corsOptions.origin = false | ||
// } | ||
// callback(null, corsOptions) | ||
// }) | ||
// ) | ||
|
||
// For localhost testing | ||
app.use( | ||
cors((req, callback) => { | ||
const origin = req.method === 'GET' ? '*' : '*' | ||
callback(null, { origin }) | ||
}) | ||
) | ||
|
||
function haltOnTimedout(req: express.Request, _res: express.Response, next) { | ||
if (!req.timedout) next() | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,110 @@ | ||
import { gql } from 'graphql-request' | ||
import { u8aToHex } from '@polkadot/util' | ||
import { decodeAddress } from '@polkadot/util-crypto' | ||
import { getOrCreateQueue } from './queue' | ||
import { txAggregatorGraphQlClient } from '../../constant/graphQlClients' | ||
|
||
const ADD_QUEUE_JOB_NAME = 'REFRESH_TX_HISTORY_FOR_ACCOUNT_ON_DEMAND' | ||
|
||
const buildGetAccountTxHistoryQuery = (networks?: string[], events?: string[]) => { | ||
const networkFilterValues = networks ? ', blockchainTag: $networks' : '' | ||
const networkFilterParams = networks ? ', $networks: [BlockchainTag!]' : '' | ||
|
||
const eventFilterValues = events ? ', txKind: $txKind' : '' | ||
const eventFilterParams = events ? ', $txKind: [TransactionKind!]' : '' | ||
|
||
return gql` | ||
query getAccountTxHistory($address: String!, $pageSize: Int!, $offset: Int! ${networkFilterParams}${eventFilterParams}) { | ||
accountTxHistory( | ||
args: { where: { publicKey: $address ${networkFilterValues}${eventFilterValues} }, pageSize: $pageSize, offset: $offset } | ||
) { | ||
data { | ||
id | ||
txKind | ||
blockchainTag | ||
amount | ||
senderOrTargetPublicKey | ||
timestamp | ||
success | ||
transaction { | ||
transferNative { | ||
extrinsicHash | ||
} | ||
} | ||
} | ||
} | ||
} | ||
` | ||
} | ||
|
||
type GetAccountTransactionsWithQueue = { | ||
address: string | ||
pageSize: number | ||
offset: number | ||
} | ||
|
||
type GetAccountTransactions = GetAccountTransactionsWithQueue & { | ||
networks?: string[] | ||
events?: string[] | ||
} | ||
|
||
export const getAccountTxHistory = async ({ | ||
address, | ||
pageSize, | ||
offset, | ||
networks, | ||
events | ||
}: GetAccountTransactions) => { | ||
const networkFilterValues = networks | ||
? { networks: networks.map((network) => network.toUpperCase()) } | ||
: {} | ||
const eventsFilterValues = events ? { txKind: events.map((event) => event.toUpperCase()) } : {} | ||
|
||
const query = buildGetAccountTxHistoryQuery(networks, events) | ||
|
||
const txs = await txAggregatorGraphQlClient.request(query, { | ||
address, | ||
pageSize, | ||
offset, | ||
...networkFilterValues, | ||
...eventsFilterValues | ||
}) | ||
|
||
return txs?.accountTxHistory?.data | ||
} | ||
|
||
export const getAccountTxHistoryWithQueue = async (props: GetAccountTransactionsWithQueue) => { | ||
samchuk-vlad marked this conversation as resolved.
Show resolved
Hide resolved
|
||
const txs = await getAccountTxHistory(props) | ||
|
||
const address = props.address | ||
|
||
const jobId = `${address}-${ADD_QUEUE_JOB_NAME}` | ||
const queue = getOrCreateQueue() | ||
const jobByAddress = await queue.getJob(jobId) | ||
|
||
let actualData = false | ||
|
||
if (jobByAddress) { | ||
const jobState = await jobByAddress.getState() | ||
|
||
if (jobState === 'completed') { | ||
await jobByAddress.remove() | ||
|
||
actualData = true | ||
} | ||
} else { | ||
const taskPayload = { | ||
publicKey: u8aToHex(decodeAddress(address)) | ||
} | ||
|
||
await queue.add(ADD_QUEUE_JOB_NAME, taskPayload, { | ||
attempts: 5, | ||
jobId, | ||
removeOnComplete: false, | ||
removeOnFail: true, | ||
priority: 1 | ||
}) | ||
} | ||
|
||
return { txs, actualData } | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
import Queue from 'bull' | ||
|
||
let queue = null | ||
|
||
export const getOrCreateQueue = () => { | ||
if (!queue) { | ||
const aggregatorRedisConfig = { | ||
host: process.env.AGGREGATOR_REDIS_HOST || '', | ||
password: process.env.AGGREGATOR_REDIS_PASSWORD || '', | ||
port: (process.env.AGGREGATOR_REDIS_PORT as unknown as number) || 0 | ||
} | ||
|
||
queue = new Queue('ACCOUNT_AGGREGATION_FLOW', { | ||
redis: aggregatorRedisConfig, | ||
samchuk-vlad marked this conversation as resolved.
Show resolved
Hide resolved
|
||
prefix: process.env.AGGREGATOR_REDIS_PREFIX, | ||
settings: { | ||
lockDuration: 20000, // Check for stalled jobs each 2 min | ||
lockRenewTime: 10000, | ||
stalledInterval: 20 * 60 * 1000, | ||
maxStalledCount: 1 | ||
} | ||
}) | ||
} | ||
|
||
return queue | ||
} |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
change this back, or if you got time, maybe put this in env instead?