Skip to content

Commit

Permalink
feat(app): environment-specific graphql endpoint (#3117)
Browse files Browse the repository at this point in the history
  • Loading branch information
cor authored Oct 17, 2024
2 parents 948cb4f + 5ec1396 commit 56f2fee
Show file tree
Hide file tree
Showing 13 changed files with 58 additions and 48 deletions.
4 changes: 2 additions & 2 deletions app/src/lib/components/transfer-details.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ let transfers = createQuery({
refetchInterval: query => (query.state.data?.length === 0 ? 1_000 : false), // fetch every second until we have the transaction
placeholderData: (previousData, _) => previousData,
queryFn: async () => {
const response = await request(URLS.GRAPHQL, transfersBySourceHashBaseQueryDocument, {
const response = await request(URLS().GRAPHQL, transfersBySourceHashBaseQueryDocument, {
source_transaction_hash: source
})
Expand Down Expand Up @@ -120,7 +120,7 @@ let tracesAndHops = createQuery({
placeholderData: (previousData, _) => previousData,
queryFn: async () =>
(
await request(URLS.GRAPHQL, transfersBySourceHashTracesAndHopsQueryDocument, {
await request(URLS().GRAPHQL, transfersBySourceHashTracesAndHopsQueryDocument, {
source_transaction_hash: source
})
).v1_transfers
Expand Down
56 changes: 33 additions & 23 deletions app/src/lib/constants/index.ts
Original file line number Diff line number Diff line change
@@ -1,29 +1,39 @@
import { sepolia } from "viem/chains"

export const UNO = {
COIN_TYPE: 118,
NATIVE_DENOM: "muno",
ADDRESS_PREFIX: "union",
SYMBOL: "UNO"
} as const
type Environment = "PRODUCTION" | "STAGING" | "DEVELOPMENT"

export const URLS = {
GRAPHQL: "https://blue.graphql.union.build/v1/graphql",
GRAPHQL_WSS: "wss//hubble-blue.hasura.app/v1/graphql",
GRAPHQL_REST: "https://blue.graphql.union.build/api/rest",
UNION: {
/**
* TODO: add array of RPCs and pass to `viem`'s `fallback` array
*/
RPC: "https://rpc.testnet-8.union.build",
// REST: "https://api.testnet.bonlulu.uno"
REST: "https://rest.testnet-8.union.build/"
},
SEPOLIA: {
RPC: "https://rpc.ankr.com/eth_sepolia/6c72c8d164912bed4694cb882fc4ca55574126511a4f5f66828a53fa2448a20a",
REST: null
export const ENV = (): Environment =>
window.location.hostname === "app.union.build"
? "PRODUCTION"
: window.location.hostname === "staging.app.union.build"
? "STAGING"
: "DEVELOPMENT"

export const URLS = () => {
const GRAPHQL_BASE =
ENV() === "PRODUCTION"
? "graphql.union.build"
: ENV() === "STAGING"
? "staging.graphql.union.build"
: "development.graphql.union.build"

return {
GRAPHQL: `https://${GRAPHQL_BASE}/v1/graphql`,
GRAPHQL_REST: `https://${GRAPHQL_BASE}/api/rest`,
UNION: {
/**
* TODO: add array of RPCs and pass to `viem`'s `fallback` array
*/
RPC: "https://rpc.testnet-8.union.build",
// REST: "https://api.testnet.bonlulu.uno"
REST: "https://rest.testnet-8.union.build/"
},
SEPOLIA: {
RPC: "https://rpc.ankr.com/eth_sepolia/6c72c8d164912bed4694cb882fc4ca55574126511a4f5f66828a53fa2448a20a",
REST: null
}
}
} as const
}

export const CHAINS = ["SEPOLIA", "UNION"] as const
export type Chain = (typeof CHAINS)[number]
Expand All @@ -40,5 +50,5 @@ export const CHAIN = {
} satisfies Record<Chain, { ID: string; NAME: string }>

export const CHAIN_URLS = {
[CHAIN.UNION.ID]: URLS.UNION
[CHAIN.UNION.ID]: URLS().UNION
}
2 changes: 1 addition & 1 deletion app/src/lib/queries/chains.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ export const chainsQuery = () =>
createQuery({
queryKey: ["chains"],
placeholderData: (previousData, _) => previousData,
queryFn: async () => (await request(URLS.GRAPHQL, chainsQueryDocument, {})).v1_chains,
queryFn: async () => (await request(URLS().GRAPHQL, chainsQueryDocument, {})).v1_chains,
enabled: true,
refetchInterval: 6_000,
refetchOnWindowFocus: false
Expand Down
18 changes: 9 additions & 9 deletions app/src/lib/queries/packets.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ const packetTransform = (p: FragmentOf<typeof packetListDataFragment>) => {
type PacketsReturnType = Promise<Array<ReturnType<typeof packetTransform>>>

export async function packetsLatest({ limit = 12 }: { limit?: number } = {}): PacketsReturnType {
const { v1_packets } = await request(URLS.GRAPHQL, packetsLatestQuery, {
const { v1_packets } = await request(URLS().GRAPHQL, packetsLatestQuery, {
limit
})
return v1_packets.map(packetTransform)
Expand All @@ -60,7 +60,7 @@ export async function packetsTimestamp({
limit,
timestamp
}: { limit: number; timestamp: string }): PacketsReturnType {
const { newer, older } = await request(URLS.GRAPHQL, packetsTimestampQuery, {
const { newer, older } = await request(URLS().GRAPHQL, packetsTimestampQuery, {
timestamp,
limit: limit / 2
})
Expand All @@ -72,7 +72,7 @@ export async function packetsByChainIdLatest({
limit,
chain_id
}: { limit: number; chain_id: string }): PacketsReturnType {
const { v1_packets } = await request(URLS.GRAPHQL, packetsByChainLatestQuery, {
const { v1_packets } = await request(URLS().GRAPHQL, packetsByChainLatestQuery, {
limit,
chain_id
})
Expand All @@ -84,7 +84,7 @@ export async function packetsByChainIdTimestamp({
chain_id,
timestamp
}: { limit: number; chain_id: string; timestamp: string }): PacketsReturnType {
const { newer, older } = await request(URLS.GRAPHQL, packetsByChainTimestampQuery, {
const { newer, older } = await request(URLS().GRAPHQL, packetsByChainTimestampQuery, {
limit,
chain_id,
timestamp
Expand All @@ -97,7 +97,7 @@ export async function packetsByConnectionIdLatest({
chain_id,
connection_id
}: { limit: number; chain_id: string; connection_id: string }): PacketsReturnType {
const { v1_packets } = await request(URLS.GRAPHQL, packetsByConnectionIdLatestQuery, {
const { v1_packets } = await request(URLS().GRAPHQL, packetsByConnectionIdLatestQuery, {
limit,
chain_id,
connection_id
Expand All @@ -116,7 +116,7 @@ export async function packetsByConnectionIdTimestamp({
connection_id: string
timestamp: string
}): PacketsReturnType {
const { newer, older } = await request(URLS.GRAPHQL, packetsByConnectionIdTimestampQuery, {
const { newer, older } = await request(URLS().GRAPHQL, packetsByConnectionIdTimestampQuery, {
limit,
chain_id,
connection_id,
Expand All @@ -136,7 +136,7 @@ export async function packetsByChannelIdLatest({
connection_id: string
channel_id: string
}): PacketsReturnType {
const { v1_packets } = await request(URLS.GRAPHQL, packetsByChannelIdLatestQuery, {
const { v1_packets } = await request(URLS().GRAPHQL, packetsByChannelIdLatestQuery, {
limit,
chain_id,
connection_id,
Expand All @@ -158,7 +158,7 @@ export async function packetsByChannelIdTimestamp({
channel_id: string
timestamp: string
}): PacketsReturnType {
const { newer, older } = await request(URLS.GRAPHQL, packetsByChannelIdTimestampQuery, {
const { newer, older } = await request(URLS().GRAPHQL, packetsByChannelIdTimestampQuery, {
limit,
chain_id,
connection_id,
Expand Down Expand Up @@ -294,7 +294,7 @@ export const packetDetailsQuery = (
refetchInterval: 5_000,
placeholderData: keepPreviousData,
queryFn: async () =>
await request(URLS.GRAPHQL, packetDetailsQueryDocument, {
await request(URLS().GRAPHQL, packetDetailsQueryDocument, {
chain_id,
connection_id,
channel_id,
Expand Down
4 changes: 2 additions & 2 deletions app/src/lib/queries/stats.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { URLS } from "$lib/constants"
export const statsQuery = () =>
createQuery({
queryKey: ["stats"],
queryFn: async () => (await request(URLS.GRAPHQL, statsQueryDocument, {})).v1_statistics,
queryFn: async () => (await request(URLS().GRAPHQL, statsQueryDocument, {})).v1_statistics,
enabled: true,
refetchInterval: 5_000,
refetchOnWindowFocus: false
Expand All @@ -17,7 +17,7 @@ export const transfersPerDayQuery = (limit: number) =>
createQuery({
queryKey: ["transfer-per-day"],
queryFn: async () =>
(await request(URLS.GRAPHQL, transfersPerDayQueryDocument, { limit })).v1_daily_transfers,
(await request(URLS().GRAPHQL, transfersPerDayQueryDocument, { limit })).v1_daily_transfers,
enabled: true,
refetchInterval: 6_000,
refetchOnWindowFocus: false
Expand Down
8 changes: 4 additions & 4 deletions app/src/lib/queries/transfers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ type TransfersReturnType = Promise<Array<ReturnType<typeof transferTransform>>>
export async function transfersLatest({
limit = 12
}: { limit?: number } = {}): TransfersReturnType {
const { data } = await request(URLS.GRAPHQL, transfersLatestQuery, {
const { data } = await request(URLS().GRAPHQL, transfersLatestQuery, {
limit
})
return data.map(transferTransform)
Expand All @@ -53,7 +53,7 @@ export async function transfersTimestamp({
limit: number
timestamp: string
}): TransfersReturnType {
const { older, newer } = await request(URLS.GRAPHQL, transfersTimestampQuery, {
const { older, newer } = await request(URLS().GRAPHQL, transfersTimestampQuery, {
limit: limit / 2,
timestamp
})
Expand All @@ -68,7 +68,7 @@ export async function transfersByAddressesLatest({
limit: number
addresses: Array<string>
}): TransfersReturnType {
const { data } = await request(URLS.GRAPHQL, transfersByAddressesLatestQuery, {
const { data } = await request(URLS().GRAPHQL, transfersByAddressesLatestQuery, {
limit,
addresses
})
Expand All @@ -84,7 +84,7 @@ export async function transfersByAddressesTimestamp({
timestamp: string
addresses: Array<string>
}): TransfersReturnType {
const { older, newer } = await request(URLS.GRAPHQL, transfersByAddressesTimestampQuery, {
const { older, newer } = await request(URLS().GRAPHQL, transfersByAddressesTimestampQuery, {
limit: limit / 2,
timestamp,
addresses
Expand Down
2 changes: 1 addition & 1 deletion app/src/routes/explorer/(components)/table-channels.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ let channels = createQuery({
queryKey: ["channels"],
refetchInterval: 5_000,
retryDelay: attempt => Math.min(attempt > 1 ? 2 ** attempt * 1000 : 1000, 30 * 1000),
queryFn: async () => request(URLS.GRAPHQL, channelsQuery, {}),
queryFn: async () => request(URLS().GRAPHQL, channelsQuery, {}),
select: data =>
data.v1_channels.map(channel => ({
source_chain: {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ let connections = createQuery({
queryKey: ["connections"],
refetchInterval: 5_000,
retryDelay: attempt => Math.min(attempt > 1 ? 2 ** attempt * 1000 : 1000, 30 * 1000), // expo backoff
queryFn: async () => request(URLS.GRAPHQL, connectionsQuery, {}),
queryFn: async () => request(URLS().GRAPHQL, connectionsQuery, {}),
select: data => {
if (!data.v1_connections) raise("error fetching transfers")
Expand Down
2 changes: 1 addition & 1 deletion app/src/routes/explorer/blocks/+page.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import { truncate } from "$lib/utilities/format"
let cosmosBlocks = createQuery({
queryKey: ["cosmos-blocks"],
refetchInterval: 6_000,
queryFn: async () => request(URLS.GRAPHQL, cosmosBlocksQuery, { limit: 100 }),
queryFn: async () => request(URLS().GRAPHQL, cosmosBlocksQuery, { limit: 100 }),
select: ({ data }) => {
if (!data) raise("No data found in cosmos blocks")
return data.map(block => ({
Expand Down
2 changes: 1 addition & 1 deletion app/src/routes/explorer/index-status/+page.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import { indexStatusQuery } from "$lib/graphql/queries/index-status.ts"
let indexStatus = createQuery({
queryKey: ["index-status"],
refetchInterval: 500,
queryFn: async () => request(URLS.GRAPHQL, indexStatusQuery, {}),
queryFn: async () => request(URLS().GRAPHQL, indexStatusQuery, {}),
select: data => {
const enabledChains = data.chains.flatMap(chain => chain.chain_id)
return data.statuses
Expand Down
2 changes: 1 addition & 1 deletion app/src/routes/faucet/(components)/dydx-faucet.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ const requestDydxFromFaucet = async () => {
if ($dydxFaucetState.kind === "SUBMITTING") {
try {
const result = await request(URLS.GRAPHQL, dydxFaucetMutation, {
const result = await request(URLS().GRAPHQL, dydxFaucetMutation, {
address: $dydxAddress,
captchaToken: $dydxFaucetState.captchaToken
})
Expand Down
2 changes: 1 addition & 1 deletion app/src/routes/faucet/(components)/stride-faucet.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ const requestStrdFromFaucet = async () => {
if ($strideFaucetState.kind === "SUBMITTING") {
try {
const result = await request(URLS.GRAPHQL, strideFaucetMutation, {
const result = await request(URLS().GRAPHQL, strideFaucetMutation, {
address: $strideAddress,
captchaToken: $strideFaucetState.captchaToken
})
Expand Down
2 changes: 1 addition & 1 deletion app/src/routes/faucet/+page.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ const requestUnoFromFaucet = async () => {
if ($unoFaucetState.kind === "SUBMITTING") {
try {
const result = await request(URLS.GRAPHQL, faucetUnoMutation2, {
const result = await request(URLS().GRAPHQL, faucetUnoMutation2, {
address,
captchaToken: $unoFaucetState.captchaToken
})
Expand Down

0 comments on commit 56f2fee

Please sign in to comment.