Skip to content

Commit

Permalink
useGambaEvents
Browse files Browse the repository at this point in the history
  • Loading branch information
bone-house committed Sep 15, 2023
1 parent e2b401a commit 2435b1b
Show file tree
Hide file tree
Showing 7 changed files with 58 additions and 63 deletions.
2 changes: 1 addition & 1 deletion apps/demo/src/components/Section.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ export function SlideSection({ stuff, children, ...rest }: SectionProps) {
const ref = React.useRef<HTMLDivElement>(null!)

const scroll = (x: number) => {
ref.current.scrollBy({ left: ref.current.clientWidth * x, behavior: 'smooth' })
ref.current.scrollBy({ left: ref.current.clientWidth / 2 * x, behavior: 'smooth' })
}

return (
Expand Down
26 changes: 3 additions & 23 deletions apps/demo/src/ui/Home.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,37 +12,17 @@ export function Home() {
Gamba Demo
</h2>
<p style={{ wordWrap: 'break-word' }}>
A decentralized, provably-fair casino built on <Button size="small" variant="soft" as="a" href="https://gamba.so" target="_blank">Gamba</Button>.
A decentralized, provably-fair casino built on Gamba.
</p>
<div style={{ display: 'flex', gap: '10px' }}>
<Button
as="a"
color="white"
href="https://github.com/gamba-labs"
href="https://gamba.so"
target="_blank"
icon={<Icon.ExternalLink />}
>
Github
</Button>
<Button
as="a"
color="white"
variant="ghost"
href="https://discord.gg/TkGr9bAZ"
target="_blank"
icon={<Icon.ExternalLink />}
>
Discord
</Button>
<Button
as="a"
color="white"
variant="ghost"
href="https://twitter.com/GambaLabs"
target="_blank"
icon={<Icon.ExternalLink />}
>
Twitter
Learn more
</Button>
</div>
</Section>
Expand Down
44 changes: 8 additions & 36 deletions apps/demo/src/ui/RecentPlays.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { GameResult, lamportsToSol } from 'gamba'
import { useEventFetcher, useGamba } from 'gamba/react'
import { useGamba, useGambaEvents } from 'gamba/react'
import { formatLamports } from 'gamba/react-ui'
import React from 'react'
import { Icon } from '../components/Icon'
Expand Down Expand Up @@ -69,51 +69,23 @@ function RecentPlay({ time, signature, result, isSelf }: RecentPlayProps) {

export function RecentPlays() {
const gamba = useGamba()
const events = useEventFetcher()

React.useEffect(
() => {
events.fetch({ signatureLimit: 40 })
return events.listen()
}
, [events],
)

const results = React.useMemo(() => {
return events.transactions.filter((x) => !!x.event.gameResult)
}, [events.transactions])
const events = useGambaEvents()

return (
<Section
title="Recent Plays"
stuff={
<>
{/* <Button onClick={() => events.fetchNewer()} size="small" variant="soft">
Update
</Button>
<Button onClick={() => events.fetch({ signatureLimit: 10 })} size="small" variant="soft">
Moar
</Button> */}
</>
}
>
<Section title="Recent Plays">
<div className={styles.container}>
{results.map((transaction) => (
{events.map((transaction) => (
<RecentPlay
key={transaction.signature}
time={transaction.time}
signature={transaction.signature}
result={transaction.event.gameResult!}
isSelf={transaction.event.gameResult!.player.equals(gamba.wallet.publicKey)}
result={transaction.event.gameResult}
isSelf={transaction.event.gameResult.player.equals(gamba.wallet.publicKey)}
/>
))}
{!events.latestSig ? Array.from({ length: 5 }).map((_, i) => (
{!events.length && Array.from({ length: 5 }).map((_, i) => (
<div key={i} className={styles.skeleton} />
)) : !results.length && (
<div>
No events
</div>
)}
))}
</div>
</Section>
)
Expand Down
6 changes: 5 additions & 1 deletion packages/gamba-core/src/EventFetcher.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,10 @@ export class EventFetcher {
this.latestTime = latest.time
}

const eventTransactions = transactions.filter((x) => !!x.event.gameResult)
// todo
const eventTransactions = transactions.filter((x) => {
return !!x.event.gameResult && (x.event.gameResult.creator.equals(this.params.address) || this.params.address.equals(PROGRAM_ID))
})

if (this.params.storeTransactions) {
this.transactions = [
Expand Down Expand Up @@ -130,6 +133,7 @@ export class EventFetcher {
}
const events = parseTransactionEvents(logs.logs)
const gameResult = events.gameResult ?? events.gameResultOld
console.log(gameResult)
this.handleEvents([{
signature: logs.signature,
time: Date.now(),
Expand Down
2 changes: 1 addition & 1 deletion packages/gamba-react/src/hooks/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@ export { useBalances } from './useBalances'
export { useBonusToken } from './useBonusToken'
export { useGamba, useGambaError } from './useGamba'
export { useGambaClient } from './useGambaClient'
export { useEventFetcher } from './useEventFetcher'
export { useEventFetcher, useGambaEvents } from './useEventFetcher'
40 changes: 39 additions & 1 deletion packages/gamba-react/src/hooks/useEventFetcher.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { useConnection } from '@solana/wallet-adapter-react'
import { EventFetcher, EventFetcherParams } from 'gamba-core'
import { EventFetcher, EventFetcherParams, GameResult, ParsedGambaTransaction } from 'gamba-core'
import React from 'react'
import { useRerender } from './useRerender'

Expand All @@ -13,3 +13,41 @@ export function useEventFetcher(params?: EventFetcherParams) {

return fetcher
}

interface UseGambaEventsParams {
// creator?: PublicKey
signatureLimit?: number
listen?: boolean
}

type GambaTransactionWithGameResult = Omit<ParsedGambaTransaction, 'event'> & {event: {gameResult: GameResult}}

export function useGambaEvents(props: UseGambaEventsParams = {}) {
const {
listen = true,
signatureLimit = 20,
// creator,
} = props
const events = useEventFetcher()

const results = React.useMemo(() =>
events.transactions.filter((x) => !!x.event.gameResult) as GambaTransactionWithGameResult[]
, [events.transactions])

React.useEffect(
() => {
if (signatureLimit) {
events.fetch({ signatureLimit: signatureLimit })
}
}
, [events, signatureLimit],
)

React.useEffect(() => {
if (listen) {
return events.listen()
}
}, [events, listen])

return results
}
1 change: 1 addition & 0 deletions packages/gamba-react/src/hooks/useGamba.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ export function useGamba() {

return {
connection,
creator,
wallet,
seed,
updateSeed,
Expand Down

1 comment on commit 2435b1b

@vercel
Copy link

@vercel vercel bot commented on 2435b1b Sep 15, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Successfully deployed to the following URLs:

gamba-play-dev – ./apps/demo

gamba-play-dev-git-dev-gamba.vercel.app
gamba-play-dev-gamba.vercel.app
demo-dev.gamba.so
gamba-demo-ydw7.vercel.app

Please sign in to comment.