Skip to content

Remove stateful environment variables #1413

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

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
14 changes: 8 additions & 6 deletions web/packages/api/src/history_v2.ts
Original file line number Diff line number Diff line change
Expand Up @@ -245,8 +245,8 @@ const buildToEthereumTransferResult = (transfer: any): ToEthereumTransferResult
return result
}

export const toPolkadotHistory = async (): Promise<ToPolkadotTransferResult[]> => {
const allTransfers = await fetchToPolkadotTransfers()
export const toPolkadotHistory = async (graphqlApiUrl: string, graphqlQuerySize: number): Promise<ToPolkadotTransferResult[]> => {
const allTransfers = await fetchToPolkadotTransfers(graphqlApiUrl, graphqlQuerySize)
const results: ToPolkadotTransferResult[] = []
for (const transfer of allTransfers) {
let result = buildToPolkadotTransferResult(transfer)
Expand All @@ -255,8 +255,8 @@ export const toPolkadotHistory = async (): Promise<ToPolkadotTransferResult[]> =
return results
}

export const toEthereumHistory = async (): Promise<ToEthereumTransferResult[]> => {
const allTransfers = await fetchToEthereumTransfers()
export const toEthereumHistory = async (graphqlApiUrl: string, graphqlQuerySize: number): Promise<ToEthereumTransferResult[]> => {
const allTransfers = await fetchToEthereumTransfers(graphqlApiUrl, graphqlQuerySize)
const results: ToEthereumTransferResult[] = []
for (const transfer of allTransfers) {
let result = buildToEthereumTransferResult(transfer)
Expand All @@ -266,9 +266,10 @@ export const toEthereumHistory = async (): Promise<ToEthereumTransferResult[]> =
}

export const toPolkadotTransferById = async (
graphqlApiUrl: string,
id: string
): Promise<ToPolkadotTransferResult | undefined> => {
const transfers = await fetchToPolkadotTransferById(id)
const transfers = await fetchToPolkadotTransferById(graphqlApiUrl, id)
if (transfers?.length > 0) {
let result = buildToPolkadotTransferResult(transfers[0])
return result
Expand All @@ -277,9 +278,10 @@ export const toPolkadotTransferById = async (
}

export const toEthereumTransferById = async (
graphqlApiUrl: string,
id: string
): Promise<ToEthereumTransferResult | undefined> => {
const transfers = await fetchToEthereumTransferById(id)
const transfers = await fetchToEthereumTransferById(graphqlApiUrl, id)
if (transfers?.length > 0) {
let result = buildToEthereumTransferResult(transfers[0])
return result
Expand Down
2 changes: 1 addition & 1 deletion web/packages/api/src/status.ts
Original file line number Diff line number Diff line change
Expand Up @@ -260,7 +260,7 @@ export const channelStatusInfo = async (
"0xc173fac324158e77fb5840738a1a541f633cbec8884c6a601c567d2b376a0539"
) {
try {
estimatedDeliveryTime = await fetchEstimatedDeliveryTime(channelId)
estimatedDeliveryTime = await fetchEstimatedDeliveryTime(context.config.graphqlApiUrl, channelId)
} catch (e: any) {
console.error("estimate api error:" + e.message)
}
Expand Down
48 changes: 21 additions & 27 deletions web/packages/api/src/subsquid.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,3 @@
const graphqlApiUrl =
process.env["GRAPHQL_API_URL"] ||
process.env["NEXT_PUBLIC_GRAPHQL_API_URL"] ||
"https://snowbridge.squids.live/snowbridge-subsquid-polkadot@v1/api/graphql"
const graphqlQuerySize = process.env["GRAPHQL_QUERY_SIZE"] || "100"

/**
* Query the recent transfers from Ethereum to Polkadot

Expand Down Expand Up @@ -41,7 +35,7 @@ $graphqlApiUrl --no-progress-meter | jq "."
...
]
**/
export const fetchToPolkadotTransfers = async () => {
export const fetchToPolkadotTransfers = async (graphqlApiUrl: string, graphqlQuerySize: number) => {
let query = `query { transferStatusToPolkadots(limit: ${graphqlQuerySize}, orderBy: timestamp_DESC) {
id
status
Expand Down Expand Up @@ -80,7 +74,7 @@ export const fetchToPolkadotTransfers = async () => {
}
}
}`
let result = await queryByGraphQL(query)
let result = await queryByGraphQL(graphqlApiUrl, query)
return result?.transferStatusToPolkadots
}

Expand Down Expand Up @@ -122,7 +116,7 @@ $graphqlApiUrl --no-progress-meter | jq "."
...
]
**/
export const fetchToEthereumTransfers = async () => {
export const fetchToEthereumTransfers = async (graphqlApiUrl: string, graphqlQuerySize: number) => {
let query = `query { transferStatusToEthereums(limit: ${graphqlQuerySize}, orderBy: timestamp_DESC) {
id
status
Expand Down Expand Up @@ -163,23 +157,23 @@ export const fetchToEthereumTransfers = async () => {
}
}
}`
let result = await queryByGraphQL(query)
let result = await queryByGraphQL(graphqlApiUrl, query)
return result?.transferStatusToEthereums
}

const fetchBridgeHubOutboundMessageAccepted = async (messageID: string) => {
const fetchBridgeHubOutboundMessageAccepted = async (graphqlApiUrl: string, messageID: string) => {
let query = `query { outboundMessageAcceptedOnBridgeHubs(where: {messageId_eq:"${messageID}"}) {
id
nonce
blockNumber
timestamp
}
}`
let result = await queryByGraphQL(query)
let result = await queryByGraphQL(graphqlApiUrl, query)
return result?.outboundMessageAcceptedOnBridgeHubs[0]
}

const fetchEthereumInboundMessageDispatched = async (messageID: string) => {
const fetchEthereumInboundMessageDispatched = async (graphqlApiUrl: string, messageID: string) => {
let query = `query {inboundMessageDispatchedOnEthereums(where: {messageId_eq: "${messageID}"}) {
id
channelId
Expand All @@ -191,11 +185,11 @@ const fetchEthereumInboundMessageDispatched = async (messageID: string) => {
txHash
}
}`
let result = await queryByGraphQL(query)
let result = await queryByGraphQL(graphqlApiUrl, query)
return result?.inboundMessageDispatchedOnEthereums[0]
}

const fetchBridgeHubInboundMessageReceived = async (messageID: string) => {
const fetchBridgeHubInboundMessageReceived = async (graphqlApiUrl: string, messageID: string) => {
let query = `query { inboundMessageReceivedOnBridgeHubs(where: {messageId_eq:"${messageID}"}) {
id
channelId
Expand All @@ -205,11 +199,11 @@ const fetchBridgeHubInboundMessageReceived = async (messageID: string) => {
timestamp
}
}`
let result = await queryByGraphQL(query)
let result = await queryByGraphQL(graphqlApiUrl, query)
return result?.inboundMessageReceivedOnBridgeHubs[0]
}

const fetchMessageProcessedOnPolkadot = async (messageID: string) => {
const fetchMessageProcessedOnPolkadot = async (graphqlApiUrl: string, messageID: string) => {
let query = `query { messageProcessedOnPolkadots(where: {messageId_eq:"${messageID}"}) {
id
blockNumber
Expand All @@ -219,7 +213,7 @@ const fetchMessageProcessedOnPolkadot = async (messageID: string) => {
success
}
}`
let result = await queryByGraphQL(query)
let result = await queryByGraphQL(graphqlApiUrl, query)
return result?.messageProcessedOnPolkadots[0]
}

Expand All @@ -244,16 +238,16 @@ $graphqlApiUrl --no-progress-meter | jq "."
}
}
**/
export const fetchEstimatedDeliveryTime = async (channelId: string) => {
export const fetchEstimatedDeliveryTime = async (graphqlApiUrl: string, channelId: string) => {
let query = `query { toEthereumElapse(channelId:"${channelId}") { elapse } toPolkadotElapse(channelId:"${channelId}") { elapse } }`
let result = await queryByGraphQL(query)
let result = await queryByGraphQL(graphqlApiUrl, query)
return result
}

/**
* Query with a raw graphql
**/
export const queryByGraphQL = async (query: string) => {
export const queryByGraphQL = async (graphqlApiUrl: string, query: string) => {
let response = await fetch(graphqlApiUrl, {
method: "POST",
headers: {
Expand Down Expand Up @@ -303,7 +297,7 @@ $graphqlApiUrl --no-progress-meter | jq "."
}
]
**/
export const fetchToPolkadotTransferById = async (id: string) => {
export const fetchToPolkadotTransferById = async (graphqlApiUrl: string, id: string) => {
let query = `query { transferStatusToPolkadots(where: {messageId_eq: "${id}", OR: {txHash_eq: "${id}"}}) {
id
status
Expand Down Expand Up @@ -342,7 +336,7 @@ export const fetchToPolkadotTransferById = async (id: string) => {
}
}
}`
let result = await queryByGraphQL(query)
let result = await queryByGraphQL(graphqlApiUrl, query)
return result?.transferStatusToPolkadots
}

Expand Down Expand Up @@ -383,7 +377,7 @@ $graphqlApiUrl --no-progress-meter | jq "."
}
]
**/
export const fetchToEthereumTransferById = async (id: string) => {
export const fetchToEthereumTransferById = async (graphqlApiUrl: string, id: string) => {
let query = `query { transferStatusToEthereums(where: {messageId_eq: "${id}", OR: {txHash_eq: "${id}"}}) {
id
status
Expand Down Expand Up @@ -424,7 +418,7 @@ export const fetchToEthereumTransferById = async (id: string) => {
}
}
}`
let result = await queryByGraphQL(query)
let result = await queryByGraphQL(graphqlApiUrl, query)
return result?.transferStatusToEthereums
}

Expand Down Expand Up @@ -455,11 +449,11 @@ $graphqlApiUrl --no-progress-meter | jq "."
}
}
**/
export const fetchLatestBlocksSynced = async () => {
export const fetchLatestBlocksSynced = async (graphqlApiUrl: string) => {
let query = `query { latestBlocks {
height
name
}}`
let result = await queryByGraphQL(query)
let result = await queryByGraphQL(graphqlApiUrl, query)
return result
}
11 changes: 9 additions & 2 deletions web/packages/operations/src/global_transfer_history_v2.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,17 @@ import "dotenv/config"
import { historyV2 } from "@snowbridge/api"

const monitor = async () => {
const graphqlApiUrl =
process.env["GRAPHQL_API_URL"] ||
"https://snowbridge.squids.live/snowbridge-subsquid-polkadot@v1/api/graphql"
const graphqlQuerySize = process.env["GRAPHQL_QUERY_SIZE"] || "100"

console.log("To Ethereum transfers:")
const toEthereums = await historyV2.toEthereumHistory()
const toEthereums = await historyV2.toEthereumHistory(graphqlApiUrl, Number(graphqlQuerySize))
console.log(JSON.stringify(toEthereums, null, 2))

console.log("To Polkadot transfers:")
const toPolkadots = await historyV2.toPolkadotHistory()
const toPolkadots = await historyV2.toPolkadotHistory(graphqlApiUrl, Number(graphqlQuerySize))
console.log(JSON.stringify(toPolkadots, null, 2))

console.log("All transfers order by time:")
Expand All @@ -17,12 +22,14 @@ const monitor = async () => {

console.log("To Polkadot transfer by id:")
const toPolkadot = await historyV2.toPolkadotTransferById(
graphqlApiUrl,
"0xb56662848712da9769a2122ca0d24d199ef7af7c8aedee43778dadbe1c42ebc6"
)
console.log(JSON.stringify(toPolkadot, null, 2))

console.log("To Ethereum transfer by id:")
const toEthereum = await historyV2.toEthereumTransferById(
graphqlApiUrl,
"0x04b7a6c7552d2890094dfe43e037cb5f5495fec2419f33b0072439a9ee7629a0"
)
console.log(JSON.stringify(toEthereum, null, 2))
Expand Down
2 changes: 1 addition & 1 deletion web/packages/operations/src/monitor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,7 @@ export const monitor = async (): Promise<status.AllMetrics> => {
const latestBlockOfBH = (await bridgeHub.query.system.number()).toPrimitive() as number
const latestBlockOfEth = await ethereum.getBlockNumber()

const chains = await subsquid.fetchLatestBlocksSynced()
const chains = await subsquid.fetchLatestBlocksSynced(context.config.graphqlApiUrl ?? snowbridgeEnv.config.GRAPHQL_API_URL!)
for (let chain of chains?.latestBlocks) {
let info: status.IndexerServiceStatusInfo = {
chain: chain.name,
Expand Down