Skip to content

Commit

Permalink
feat(#1368): add entry point (app) and API
Browse files Browse the repository at this point in the history
  • Loading branch information
adamazad committed Aug 31, 2022
1 parent 206ccbf commit 338d0b8
Show file tree
Hide file tree
Showing 2 changed files with 112 additions and 0 deletions.
50 changes: 50 additions & 0 deletions src/modules/expeditions/api/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
import { EXPEDITIONS_API_BASE_URL } from '../constants'

export interface WeeklyFragmentRewards {
claimableFragments: number
claimedFragments: number
totalAmountUSD: number
liquidityDeposits: any[]
}

export interface GetWeeklyFragmentRewardsResponse {
data: {
liquidityProvision: WeeklyFragmentRewards
liquidityStaking: WeeklyFragmentRewards
}
}

/**
* Get weekly fragment rewards for given user
* @param address user address
* @returns
*/
export function getUserExpeditionsRewards(address: string): Promise<GetWeeklyFragmentRewardsResponse> {
const params = new URLSearchParams({
address,
})

const url = `${EXPEDITIONS_API_BASE_URL}/expeditions/weekly-fragments?${params.toString()}`

console.log({ url })

return fetch(url).then(res => res.json())
}

/**
*
* @param address user address
* @param signature EIP712 signature
* @returns
*/
export function claimUserWeeklyFragments(address: string, signature: string): Promise<any> {
const url = new URL('/expeditions/weekly-fragments/claim', EXPEDITIONS_API_BASE_URL)

return fetch(url, {
method: 'POST',
body: JSON.stringify({
address,
signature,
}),
}).then(res => res.json())
}
62 changes: 62 additions & 0 deletions src/modules/expeditions/app/index.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
import type { Web3Provider } from '@ethersproject/providers'

import { useEffect, useState } from 'react'

import { HeaderButton } from '../../../components/Header/HeaderButton'
import { useToggleShowExpeditionsPopup } from '../../../state/application/hooks'
import { getUserExpeditionsRewards } from '../api'
import { ExpeditionsModal } from '../components/ExpeditionsModal'
import { ExpeditionsContext } from '../contexts/ExpeditionsContext'

export interface SwaprExpeditionsAppProps {
provider: Web3Provider
account: string
}

export function App({ provider, account }: SwaprExpeditionsAppProps) {
const toggleExpeditionsPopup = useToggleShowExpeditionsPopup()

if (!account) {
throw new Error('SwaprExpeditionsApp: No account')
}

if (!provider) {
throw new Error('SwaprExpeditionsApp: No provider')
}
// Controls fetching of data from the backend
const [isLoading, setIsLoading] = useState(true)
const [rewards, setRewards] = useState<ExpeditionsContext['rewards']>()

useEffect(() => {
getUserExpeditionsRewards(account as string)
.then(userRewards => {
console.log({
userRewards,
})
setRewards(userRewards.data)
})
.catch(error => {
console.error(error)
})
.finally(() => {
setIsLoading(false)
})
}, [account])

return (
<ExpeditionsContext.Provider
value={
{
userAddress: account,
isLoading,
rewards,
} as ExpeditionsContext
}
>
<HeaderButton onClick={toggleExpeditionsPopup} style={{ marginRight: '7px' }}>
&#10024;&nbsp;Expeditions
</HeaderButton>
<ExpeditionsModal onDismiss={toggleExpeditionsPopup} />
</ExpeditionsContext.Provider>
)
}

0 comments on commit 338d0b8

Please sign in to comment.