-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Subgraph integration with dApp (#397)
Closes #347 This PR integrates a subgraph with dApp. Subgraph can be run locally or use a production/development URL. To select the right endpoint, set the URL for the following environmental variable: ``` VITE_ACRE_SUBGRAPH_URL=<SUBGRAPH_URL> ``` By default, the development URL is set to subgraph. - If you want to use a local subgraph you need to follow the steps [here](https://github.com/thesis/acre/blob/main/subgraph/README.md). Then set the subgraph URL to localhost. - To find the production URL you need to connect to the [Subgraph Studio](https://thegraph.com/studio/) with Acre Wallet and open the dedicated page for the selected subgraph. Copy the URL and use the API key from the "API Keys" tab. ### Subgraph API - `fetchActivityDatas` - Returns activities for the Ethereum account
- Loading branch information
Showing
18 changed files
with
161 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
import { selectActivities } from "#/store/wallet" | ||
import { useAppSelector } from "./useAppSelector" | ||
|
||
export function useActivities() { | ||
return useAppSelector(selectActivities) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
export * from "./useInitDataFromSubgraph" | ||
export * from "./useFetchActivities" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
import { useCallback } from "react" | ||
import { subgraphAPI } from "#/utils" | ||
import { setActivities } from "#/store/wallet" | ||
import { useAppDispatch } from "../store/useAppDispatch" | ||
import { useWalletContext } from "../useWalletContext" | ||
|
||
// TODO: Use the correct function from SDK | ||
// eslint-disable-next-line @typescript-eslint/no-unused-vars | ||
const calculateEthAddress = (btcAddress: string) => | ||
"0x4c9e39e5ff458a811708c03aea21b8327118cf13" | ||
|
||
export function useFetchActivities() { | ||
const dispatch = useAppDispatch() | ||
const { btcAccount } = useWalletContext() | ||
|
||
return useCallback(async () => { | ||
if (!btcAccount) return | ||
|
||
const ethAddress = calculateEthAddress(btcAccount.address) | ||
const result = await subgraphAPI.fetchActivityData(ethAddress) | ||
|
||
dispatch(setActivities(result)) | ||
}, [btcAccount, dispatch]) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
import { useEffect } from "react" | ||
import { logPromiseFailure } from "#/utils" | ||
import { useFetchActivities } from "./useFetchActivities" | ||
import { useWalletContext } from "../useWalletContext" | ||
|
||
export function useInitDataFromSubgraph() { | ||
const { btcAccount } = useWalletContext() | ||
const fetchActivities = useFetchActivities() | ||
|
||
useEffect(() => { | ||
if (btcAccount) { | ||
logPromiseFailure(fetchActivities()) | ||
} | ||
}, [btcAccount, fetchActivities]) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,8 @@ | ||
import { combineReducers } from "@reduxjs/toolkit" | ||
import { btcSlice } from "./btc/btcSlice" | ||
import { walletSlice } from "./wallet/walletSlice" | ||
|
||
export const reducer = combineReducers({ | ||
btc: btcSlice.reducer, | ||
wallet: walletSlice.reducer, | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
export * from "./walletSelector" | ||
export * from "./walletSlice" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
import { Activity } from "#/types" | ||
import { RootState } from ".." | ||
|
||
export const selectActivities = (state: RootState): Activity[] => | ||
state.wallet.activities |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
import { Activity } from "#/types" | ||
import { PayloadAction, createSlice } from "@reduxjs/toolkit" | ||
|
||
type WalletState = { | ||
activities: Activity[] | ||
} | ||
|
||
const initialState: WalletState = { | ||
activities: [], | ||
} | ||
|
||
export const walletSlice = createSlice({ | ||
name: "wallet", | ||
initialState, | ||
reducers: { | ||
setActivities(state, action: PayloadAction<Activity[]>) { | ||
state.activities = action.payload | ||
}, | ||
}, | ||
}) | ||
|
||
export const { setActivities } = walletSlice.actions |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
export type ActivityDataResponse = { | ||
id: string | ||
bitcoinTransactionId: string | ||
amountToDeposit: string | ||
events: { type: "Initialized" | "Finalized" }[] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
import axios from "axios" | ||
import { Activity, ActivityDataResponse } from "#/types" | ||
|
||
const ACRE_SUBGRAPH_URL = import.meta.env.VITE_ACRE_SUBGRAPH_URL | ||
|
||
const mapToActivity = (activityData: ActivityDataResponse): Activity => { | ||
const { bitcoinTransactionId: txHash, amountToDeposit, events } = activityData | ||
|
||
const status = events.some(({ type }) => type === "Finalized") | ||
? "completed" | ||
: "pending" | ||
|
||
return { | ||
txHash, | ||
amount: BigInt(amountToDeposit), | ||
type: "deposit", | ||
status, | ||
} | ||
} | ||
|
||
// TODO: Fetch transactions for withdrawals | ||
/** | ||
* Returns the activities for a given account. | ||
* @param account The Ethereum address for which the activities will be fetched. | ||
*/ | ||
async function fetchActivityData(account: string): Promise<Activity[]> { | ||
const response = await axios.post<{ | ||
errors?: unknown | ||
data: { activityDatas: ActivityDataResponse[] } | ||
}>(ACRE_SUBGRAPH_URL, { | ||
query: `query { | ||
activityDatas( | ||
where: {depositOwner_: {id: "${account}"}} | ||
) { | ||
id | ||
bitcoinTransactionId | ||
events { | ||
type | ||
} | ||
... on Deposit { | ||
amountToDeposit | ||
} | ||
} | ||
}`, | ||
}) | ||
|
||
if (response.data && response.data.errors) { | ||
const errorMsg = "Failed to fetch data from Acre subgraph API." | ||
console.error(errorMsg, response.data.errors) | ||
throw new Error(errorMsg) | ||
} | ||
|
||
return response.data.data.activityDatas.map(mapToActivity) | ||
} | ||
|
||
export const subgraphAPI = { | ||
fetchActivityData, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters