-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #140 from raid-guild/133-gnosissafe-sdk-accountingv3
needs clean up
- Loading branch information
Showing
2 changed files
with
43 additions
and
3 deletions.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,12 +1,49 @@ | ||
import { useState } from 'react'; | ||
import { useEffect, useState } from 'react'; | ||
import { getAddress } from 'viem'; | ||
|
||
const API_URL = 'https://safe-transaction-gnosis-chain.safe.global/api/v1'; | ||
|
||
const transformTokenBalances = (tokenBalanceRes, safeAddress: string) => { | ||
const fiatTotal = tokenBalanceRes.reduce( | ||
(sum: number, balance) => sum + Number(balance.fiatBalance), | ||
0 | ||
); | ||
return { safeAddress, tokenBalances: tokenBalanceRes, fiatTotal }; | ||
}; | ||
|
||
const listTokenBalances = async (safeAddress: string) => { | ||
try { | ||
const res = await fetch(`${API_URL}/safes/${safeAddress}/balances/usd/`); | ||
const data = await res.json(); | ||
return { data: transformTokenBalances(data, safeAddress) }; | ||
} catch (err) { | ||
return { error: 'Error fetching token balances. Please try again.' }; | ||
} | ||
}; | ||
|
||
// this is a dummy hook | ||
const useAccountingV3 = () => { | ||
const [data, setData] = useState(null); | ||
const [loading, setLoading] = useState(true); | ||
const [error, setError] = useState(null); | ||
|
||
return { data: null, loading: false, error: null }; | ||
useEffect(() => { | ||
const fetchData = async () => { | ||
const checksum = getAddress('0x181ebdb03cb4b54f4020622f1b0eacd67a8c63ac'); | ||
const response = await listTokenBalances(checksum); | ||
|
||
if (response.error) { | ||
setError(response.error); | ||
} else { | ||
setData(response.data); | ||
} | ||
|
||
setLoading(false); | ||
}; | ||
|
||
fetchData(); | ||
}, []); | ||
|
||
return { data, loading, error }; | ||
}; | ||
|
||
export default useAccountingV3; |