Skip to content

Commit

Permalink
move horizon requests from server to browser for all the remaining ac…
Browse files Browse the repository at this point in the history
…counts tabs
  • Loading branch information
Chris Hatch committed Mar 22, 2024
1 parent bc51463 commit fbbb9c1
Show file tree
Hide file tree
Showing 16 changed files with 561 additions and 519 deletions.
130 changes: 64 additions & 66 deletions app/lib/loader-util.ts
Original file line number Diff line number Diff line change
@@ -1,83 +1,81 @@
import type { LoaderFunctionArgs, TypedResponse } from '@remix-run/node'
import { json } from '@remix-run/node'
import HorizonServer, { HorizonServerDetails, requestToServer } from './stellar/server'
import type { HorizonServerDetails } from './stellar/server'
import HorizonServer, { requestToServer } from './stellar/server'
import * as serverRequestUtils from './stellar/server_request_utils'

export type ServerReqFnName =
| 'effects'
| 'ledgers'
| 'offers'
| 'operations'
| 'payments'
| 'trades'
| 'transactions'
| 'liquidityPools'
| 'claimableBalances'
| 'effects'
| 'ledgers'
| 'offers'
| 'operations'
| 'payments'
| 'trades'
| 'transactions'
| 'liquidityPools'
| 'claimableBalances'

export function getHorizonRecords<RecordsType>(
serverDetails: HorizonServerDetails,
serverReqFnName: ServerReqFnName,
limit: number,
): Promise<
{
records: RecordsType
cursor?: string
horizonURL: string
}> {
const url = new URL(serverDetails.requestURL)
serverDetails: HorizonServerDetails,
serverReqFnName: ServerReqFnName,
limit: number,
): Promise<{
records: RecordsType
cursor?: string
horizonURL: string
}> {
const url = new URL(serverDetails.requestURL)
const cursor: string | undefined = url.searchParams.get('cursor') ?? undefined
const order: string | undefined = url.searchParams.get('order') ?? undefined

const server = new HorizonServer(
serverDetails.serverAddress,
serverDetails.networkType as string,
)

return serverRequestUtils[serverReqFnName](server, {
cursor,
order: order as 'asc' | 'desc',
limit,
}).then((records: readonly any[]) => ({
records: (order === 'asc'
? [...records].reverse()
: records) as RecordsType,
cursor,
horizonURL: server.serverURL.toString(),
}))
}

export function horizonRecordsLoader<RecordsType>(
serverReqFnName: ServerReqFnName,
limit: number,
) {
return async function ({ request }: LoaderFunctionArgs): Promise<
TypedResponse<{
records: RecordsType
cursor?: string
horizonURL: string
}>
> {
const url = new URL(request.url)
const cursor: string | undefined =
url.searchParams.get('cursor') ?? undefined
url.searchParams.get('cursor') ?? undefined
const order: string | undefined = url.searchParams.get('order') ?? undefined

const server = new HorizonServer(
serverDetails.serverAddress,
serverDetails.networkType as string,
)
const server = await requestToServer(request)

return serverRequestUtils[serverReqFnName](server, {
cursor,
order: order as 'asc' | 'desc',
limit,
cursor,
order: order as 'asc' | 'desc',
limit,
}).then((records: readonly any[]) =>
({
json({
records: (order === 'asc'
? [...records].reverse()
: records) as RecordsType,
? [...records].reverse()
: records) as RecordsType,
cursor,
horizonURL: server.serverURL.toString(),
}))
}

export function horizonRecordsLoader<RecordsType>(
serverReqFnName: ServerReqFnName,
limit: number,
) {
return async function ({ request }: LoaderFunctionArgs): Promise<
TypedResponse<{
records: RecordsType
cursor?: string
horizonURL: string
}>
> {
const url = new URL(request.url)
const cursor: string | undefined =
url.searchParams.get('cursor') ?? undefined
const order: string | undefined = url.searchParams.get('order') ?? undefined

const server = await requestToServer(request)

return serverRequestUtils[serverReqFnName](server, {
cursor,
order: order as 'asc' | 'desc',
limit,
}).then((records: readonly any[]) =>
json({
records: (order === 'asc'
? [...records].reverse()
: records) as RecordsType,
cursor,
horizonURL: server.serverURL.toString(),
}),
)
}
}),
)
}
}
92 changes: 46 additions & 46 deletions app/lib/stellar/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,81 +7,81 @@ import { isLocalhost } from './utils'
import { isValidUrl } from '../utils'

export const defaultNetworkAddresses: Record<string, string> = {
public: 'https://horizon.stellar.org',
test: 'https://horizon-testnet.stellar.org',
future: 'https://horizon-futurenet.stellar.org',
local: 'http://localhost:8000',
public: 'https://horizon.stellar.org',
test: 'https://horizon-testnet.stellar.org',
future: 'https://horizon-futurenet.stellar.org',
local: 'http://localhost:8000',
}

export interface HorizonServerDetails {
serverAddress: string
networkType: NetworkKey | null
requestURL: string
serverAddress: string
networkType: NetworkKey | null
requestURL: string
}

/**
* Wrap the stellar-sdk Server.
*/
class HorizonServer extends Horizon.Server {
constructor(networkAddress: string, networkType?: string) {
// allowHttp: public/test use HTTPS; local can use HTTP
super(networkAddress, {
allowHttp: networkType === networks.local || isLocalhost(networkAddress),
})
}
constructor(networkAddress: string, networkType?: string) {
// allowHttp: public/test use HTTPS; local can use HTTP
super(networkAddress, {
allowHttp: networkType === networks.local || isLocalhost(networkAddress),
})
}
}

const requestToServer = async (request: Request): Promise<HorizonServer> => {
const { networkType, customHorizonAddress } =
await requestToNetworkDetails(request)
const { networkType, customHorizonAddress } =
await requestToNetworkDetails(request)

let server: HorizonServer
let server: HorizonServer

if (isValidUrl(customHorizonAddress)) {
server = new HorizonServer(customHorizonAddress)
} else {
server = new HorizonServer(
defaultNetworkAddresses[networkType],
networkType,
)
}
if (isValidUrl(customHorizonAddress)) {
server = new HorizonServer(customHorizonAddress)
} else {
server = new HorizonServer(
defaultNetworkAddresses[networkType],
networkType,
)
}

return server
return server
}

const requestToServerDetails = async (
request: Request,
request: Request,
): Promise<HorizonServerDetails> => {
const { networkType, customHorizonAddress } =
await requestToNetworkDetails(request)
const { networkType, customHorizonAddress } =
await requestToNetworkDetails(request)

const serverAddress: string = isValidUrl(customHorizonAddress)
? customHorizonAddress
: defaultNetworkAddresses[networkType]
const serverAddress: string = isValidUrl(customHorizonAddress)
? customHorizonAddress
: defaultNetworkAddresses[networkType]

return { serverAddress, networkType, requestURL: request.url }
return { serverAddress, networkType, requestURL: request.url }
}

const requestToSorobanServer = async (
request: Request,
request: Request,
): Promise<SorobanServer> => {
const { networkType, customSorobanRPCAddress } =
await requestToNetworkDetails(request)
const { networkType, customSorobanRPCAddress } =
await requestToNetworkDetails(request)

let server: SorobanServer
let server: SorobanServer

if (isValidUrl(customSorobanRPCAddress)) {
server = new SorobanServer(customSorobanRPCAddress)
} else {
server = new SorobanServer(sorobanRpcURIs[networkType], networkType)
}
if (isValidUrl(customSorobanRPCAddress)) {
server = new SorobanServer(customSorobanRPCAddress)
} else {
server = new SorobanServer(sorobanRpcURIs[networkType], networkType)
}

return server
return server
}

export {
HorizonServer as default,
requestToServer,
requestToServerDetails,
requestToSorobanServer,
HorizonServer as default,
requestToServer,
requestToServerDetails,
requestToSorobanServer,
}
Loading

0 comments on commit fbbb9c1

Please sign in to comment.