Skip to content

Commit

Permalink
Migrate from interplanetary to datacapstats api
Browse files Browse the repository at this point in the history
  • Loading branch information
filip-neti authored and kacperzuk-neti committed Aug 26, 2024
1 parent 21abbd3 commit e0614c6
Show file tree
Hide file tree
Showing 7 changed files with 111 additions and 55 deletions.
2 changes: 2 additions & 0 deletions .env.prod.template
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ NEXT_PUBLIC_NODE_ADDRESS=wss://node.glif.io/space06/lotus/rpc/v0

NEXT_PUBLIC_NODE_TOKEN=
NEXT_PUBLIC_DMOB_API_KEY=
NEXT_PUBLIC_DMOB_API_URL=
NEXT_PUBLIC_GLIF_URL=
NEXTAUTH_SECRET=
NEXTAUTH_URL=
GITHUB_ID=
Expand Down
4 changes: 2 additions & 2 deletions src/app/application/[owner]/[repo]/[id]/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import ProjectInfoCard from '@/components/cards/ProjectInfoCard'
import { Spinner } from '@/components/ui/spinner'
import { useAllocator } from '@/lib/AllocatorProvider'
import { getApplicationByParams } from '@/lib/apiClient'
import { getAllowanceForAddress } from '@/lib/dmobApi'
import { getAllowanceForVerifier } from '@/lib/glifApi'
// import { anyToBytes, bytesToiB } from '@/lib/utils'
import { useEffect, useState } from 'react'
import { useQuery } from 'react-query'
Expand Down Expand Up @@ -35,7 +35,7 @@ const ApplicationDetailPage: React.FC<ComponentProps> = ({

const getAllowance = async (address: string): Promise<void> => {
try {
const response = await getAllowanceForAddress(address)
const response = await getAllowanceForVerifier(address)

if (response.success) {
setAllowanceMultisig(parseInt(response.data))
Expand Down
4 changes: 2 additions & 2 deletions src/components/AllocatorBalance.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { useAllocator } from '@/lib/AllocatorProvider'
import { getAllowanceForAddress } from '@/lib/dmobApi'
import { getAllowanceForVerifier } from '@/lib/glifApi'
import { bytesToiB } from '@/lib/utils'
import { useEffect, useMemo, useState } from 'react'

Expand All @@ -9,7 +9,7 @@ interface ComponentProps {
}

const getAllowance = async (address: string): Promise<number | null> => {
const response = await getAllowanceForAddress(address)
const response = await getAllowanceForVerifier(address)

if (response.success) {
return parseInt(response.data)
Expand Down
4 changes: 2 additions & 2 deletions src/components/cards/AppInfoCard.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import calculateAmountToRequest, {
import useApplicationActions from '@/hooks/useApplicationActions'
import { useAllocator } from '@/lib/AllocatorProvider'
import { stateColor, stateMapping } from '@/lib/constants'
import { getAllowanceForAddress } from '@/lib/dmobApi'
import { getAllowanceForClient } from '@/lib/glifApi'
import {
anyToBytes,
bytesToiB,
Expand Down Expand Up @@ -175,7 +175,7 @@ const AppInfoCard: React.FC<ComponentProps> = ({
isDialogOpen: false,
})
const address = application.Lifecycle['On Chain Address']
const response = await getAllowanceForAddress(address)
const response = await getAllowanceForClient(address)
if (response.success) {
const allowance = parseFloat(response.data)
const lastAllocation = getLastDatacapAllocation(application)
Expand Down
9 changes: 7 additions & 2 deletions src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,12 @@ const localConfig = {
numberOfWalletAccounts: 5,
mnemonic: process.env.NEXT_PUBLIC_MNEMONIC,
walletClass: 'LedgerWallet',
dmobApiUrl: 'https://api.filplus.d.interplanetary.one/public/api',
dmobApiUrl:
process.env.NEXT_PUBLIC_DMOB_API_URL ??
'https://api.datacapstats.io/public/api',
dmobApiKey: process.env.NEXT_PUBLIC_DMOB_API_KEY ?? '',
glifNodeUrl:
process.env.NEXT_PUBLIC_GLIF_URL ?? 'https://api.node.glif.io/rpc/v1',
}

const prodConfig = {
Expand All @@ -42,8 +46,9 @@ const prodConfig = {
numberOfWalletAccounts: 5,
mnemonic: process.env.MNEMONIC,
walletClass: 'LedgerWallet',
dmobApiUrl: 'https://api.filplus.d.interplanetary.one/public/api',
dmobApiUrl: process.env.NEXT_PUBLIC_DMOB_API_URL ?? '',
dmobApiKey: process.env.NEXT_PUBLIC_DMOB_API_KEY ?? '',
glifNodeUrl: process.env.NEXT_PUBLIC_GLIF_URL ?? '',
}

export const config =
Expand Down
47 changes: 0 additions & 47 deletions src/lib/dmobApi.ts

This file was deleted.

96 changes: 96 additions & 0 deletions src/lib/glifApi.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
import { type ApiAllowanceResponse } from '@/type'
import { config } from '@/config'

/**
* Get the allowance for a verfier from the API.
*
* @param {string} address - The address to get the allowance for.
* @returns {Promise<ApiAllowanceResponse>} ApiAllowanceResponse - The response from the API.
*/
export const getAllowanceForVerifier = async (
address: string,
): Promise<ApiAllowanceResponse> => {
try {
const requestBody = {
jsonrpc: '2.0',
method: 'Filecoin.StateVerifierStatus',
params: [address, null],
id: 1,
}
const response = await fetch(`${config.glifNodeUrl}`, {
method: 'POST',
body: JSON.stringify(requestBody),
})

const data = await response.json()
if (data?.type === 'error') {
return {
data: '',
error: data.error.message,
success: false,
}
}

return {
data: data.result,
error: '',
success: true,
}
} catch (error: unknown) {
const errMessage = `Error accessing GLIF API Filecoin.StateVerifierStatus: ${
(error as Error).message
}`
return {
data: '',
error: errMessage,
success: false,
}
}
}

/**
* Get the allowance for a client from the API.
*
* @param {string} address - The address to get the allowance for.
* @returns {Promise<ApiAllowanceResponse>} ApiAllowanceResponse - The response from the API.
*/
export const getAllowanceForClient = async (
address: string,
): Promise<ApiAllowanceResponse> => {
try {
const requestBody = {
jsonrpc: '2.0',
method: 'Filecoin.StateVerifiedClientStatus',
params: [address, null],
id: 1,
}
const response = await fetch(`${config.glifNodeUrl}`, {
method: 'POST',
body: JSON.stringify(requestBody),
})

const data = await response.json()
if (data?.error) {
return {
data: '',
error: data.error.message,
success: false,
}
}

return {
data: data.result,
error: '',
success: true,
}
} catch (error: unknown) {
const errMessage = `Error accessing GLIF API Filecoin.StateVerifiedClientStatus: ${
(error as Error).message
}`
return {
data: '',
error: errMessage,
success: false,
}
}
}

0 comments on commit e0614c6

Please sign in to comment.