Skip to content

Commit

Permalink
[WIP] Accounting Fixes + Transaction Data (#141)
Browse files Browse the repository at this point in the history
* fix issue

* fetch tx data

* proposal data

* wip

* wip2

* fetch all transactions

* use bigint for calculating sum of in/out flows

* transactions

* cache results

* display member & proposal & proposal link

* fix proposals

* fix type

* cleanup

* set balancesWithPricesV3 data for csv export

fix

* add elapsed days and ragequit shares

* add in/out/balance USD

* rename

* fix types

* token symbol value

---------

Co-authored-by: vidvidvid <[email protected]>
  • Loading branch information
0xSero and vidvidvid authored Jun 26, 2024
1 parent 763cab0 commit 6c9954c
Show file tree
Hide file tree
Showing 11 changed files with 653 additions and 87 deletions.
3 changes: 3 additions & 0 deletions apps/frontend/.env.example
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,6 @@ NEXT_PUBLIC_WC_PROJECT_ID=
# https://docs.github.com/en/authentication/keeping-your-account-and-data-secure/managing-your-personal-access-tokens
GITHUB_API_TOKEN=
GITHUB_API_URL='https://api.github.com/graphql'

# create one for prod here: https://thegraph.com/studio/
NEXT_PUBLIC_THE_GRAPH_API_KEY=
11 changes: 8 additions & 3 deletions apps/frontend/components/BalancesTable.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
import { Link, Tooltip } from '@raidguild/design-system';
import { ITokenBalanceLineItem } from '@raidguild/dm-types';
import {
ITokenBalanceLineItem,
ITokenBalanceLineItemV3,
} from '@raidguild/dm-types';
import { minMaxNumberFilter, sortNumeric } from '@raidguild/dm-utils';
// @ts-expect-error - no types from RT
import { createColumnHelper } from '@tanstack/react-table';
Expand All @@ -8,10 +11,12 @@ import DataTable from './DataTable';
import TokenWithUsdValue from './TokenWithUsdValue';

interface BalancesTableProps {
data: ITokenBalanceLineItem[];
data: ITokenBalanceLineItem[] | ITokenBalanceLineItemV3[];
}

const columnHelper = createColumnHelper<ITokenBalanceLineItem>();
const columnHelper = createColumnHelper<
ITokenBalanceLineItem | ITokenBalanceLineItemV3
>();

const columns = [
columnHelper.accessor('tokenExplorerLink', {
Expand Down
6 changes: 3 additions & 3 deletions apps/frontend/components/TransactionsTable.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Link, Tooltip } from '@raidguild/design-system';
import { IVaultTransaction } from '@raidguild/dm-types';
import { IVaultTransaction, IVaultTransactionV2 } from '@raidguild/dm-types';
import {
formatNumber,
minMaxDateFilter,
Expand All @@ -14,14 +14,14 @@ import DataTable from './DataTable';
import TokenWithUsdValue from './TokenWithUsdValue';

interface TransactionsTableProps {
data: IVaultTransaction[];
data: IVaultTransaction[] | IVaultTransactionV2[];
}

const columnHelper = createColumnHelper<any>();

const columns = [
columnHelper.accessor('date', {
cell: (info) => info.getValue().toLocaleString(),
cell: (info) => info.getValue()?.toLocaleString(),
header: 'Date',
meta: {
dataType: 'datetime',
Expand Down
129 changes: 80 additions & 49 deletions apps/frontend/pages/accounting.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,17 +10,17 @@ import {
Tabs,
} from '@raidguild/design-system';
import {
useAccountingV3,
useAccountingV2,
useFormattedData,
useFormattedDataV2,
useMemberList,
useFormattedDataV3,
} from '@raidguild/dm-hooks';
import { exportToCsv } from '@raidguild/dm-utils';
import _ from 'lodash';
import { useSession } from 'next-auth/react';
import { NextSeo } from 'next-seo';
import Papa from 'papaparse';
import { useCallback } from 'react';
import { useCallback, useState } from 'react';

import BalancesTable from '../components/BalancesTable';
import SiteLayout from '../components/SiteLayout';
Expand All @@ -29,6 +29,8 @@ import TransactionsTable from '../components/TransactionsTable';

export const Accounting = () => {
const { data: session } = useSession();
const [isV3, setIsV3] = useState(true);

const token = _.get(session, 'token');
const {
data: dataFromMolochV2,
Expand All @@ -37,7 +39,7 @@ export const Accounting = () => {
} = useAccountingV2({
token,
});
const { data: dataFromMolochV3 } = useAccountingV3();

const { data: memberData } = useMemberList({
token,
limit: 1000,
Expand All @@ -47,65 +49,75 @@ export const Accounting = () => {

const {
members,
balancesWithPrices,
transactionsWithPrices,
transactionsWithPricesAndMembers,
} = useFormattedData(memberData, balances, transactions, tokenPrices);
balancesWithPrices: balancesWithPricesV2,
transactionsWithPrices: transactionsWithPricesV2,
transactionsWithPricesAndMembers: transactionsWithPricesAndMembersV2,
} = useFormattedDataV2(memberData, balances, transactions, tokenPrices);

const {
balancesWithPrices: balancesWithPricesV3,
transactionsWithPrices: transactionsWithPricesV3,
transactionsWithPricesAndMembers: transactionsWithPricesAndMembersV3,
} = useFormattedDataV3(memberData);

const onExportCsv = useCallback(
(type: 'transactions' | 'balances' | 'spoils') => {
let csvString = '';
if (type === 'transactions') {
const formattedTransactions = transactionsWithPrices.map((t) => ({
['Date']: t.date,
['Tx Explorer Link']: t.txExplorerLink,
['Elapsed Days']: t.elapsedDays,
['Type']: t.type,
['Applicant']: t.proposalApplicant,
['Applicant Member']:
const formattedTransactions = (
isV3 ? transactionsWithPricesV3 : transactionsWithPricesV2
).map((t) => ({
Date: t.date,
'Tx Explorer Link': t.txExplorerLink,
'Elapsed Days': t.elapsedDays,
Type: t.type,
Applicant: t.proposalApplicant,
'Applicant Member':
members[t.proposalApplicant.toLowerCase()]?.name || '-',
['Shares']: t.proposalShares,
['Loot']: t.proposalLoot,
['Title']: t.proposalTitle,
['Counterparty']: t.counterparty,
['Counterparty Member']:
Shares: t.proposalShares,
Loot: t.proposalLoot,
Title: t.proposalTitle,
Counterparty: t.counterparty,
'Counterparty Member':
members[t.counterparty.toLowerCase()]?.name || '-',
['Token Symbol']: t.tokenSymbol,
['Token Decimals']: t.tokenDecimals,
['Token Address']: t.tokenAddress,
['Inflow']: t.in,
['Inflow USD']: t.priceConversion
'Token Symbol': t.tokenSymbol,
'Token Decimals': t.tokenDecimals,
'Token Address': t.tokenAddress,
Inflow: t.in,
'Inflow USD': t.priceConversion
? `$${(t.in * t.priceConversion).toLocaleString()}`
: '$-',
['Outflow']: t.out,
['Outflow USD']: t.priceConversion
Outflow: t.out,
'Outflow USD': t.priceConversion
? `$${(t.out * t.priceConversion).toLocaleString()}`
: '$-',
['Balance']: t.balance,
['Balance USD']: t.priceConversion
Balance: t.balance,
'Balance USD': t.priceConversion
? `$${(t.balance * t.priceConversion).toLocaleString()}`
: '$-',
}));
csvString = Papa.unparse(formattedTransactions);
} else if (type === 'balances') {
if (type === 'balances') {
const formattedBalances = balancesWithPrices.map((b) => ({
['Token']: b.tokenSymbol,
['Tx Explorer Link']: b.tokenExplorerLink,
['Inflow']: b.inflow.tokenValue,
['Inflow USD']: b.priceConversion
const formattedBalances = (
isV3 ? balancesWithPricesV3 : balancesWithPricesV2
).map((b) => ({
Token: b.tokenSymbol,
'Tx Explorer Link': b.tokenExplorerLink,
Inflow: b.inflow.tokenValue,
'Inflow USD': b.priceConversion
? `$${(
Number(b.inflow.tokenValue) * b.priceConversion
).toLocaleString()}`
: '$-',
['Outflow']: b.outflow.tokenValue,
['Outflow USD']: b.priceConversion
Outflow: b.outflow.tokenValue,
'Outflow USD': b.priceConversion
? `$${(
Number(b.outflow.tokenValue) * b.priceConversion
).toLocaleString()}`
: '$-',
['Balance']: b.closing.tokenValue,
['Balance USD']: b.priceConversion
Balance: b.closing.tokenValue,
'Balance USD': b.priceConversion
? `$${(
Number(b.closing.tokenValue) * b.priceConversion
).toLocaleString()}`
Expand All @@ -115,18 +127,26 @@ export const Accounting = () => {
}
} else if (type === 'spoils') {
const formattedSpoils = spoils.map((s) => ({
['Date']: s.date,
['Raid']: s.raidName,
Date: s.date,
Raid: s.raidName,
// TODO: Get this dynamically from the subgraph
['Token Symbol']: 'wxDAI',
['To DAO Treasury']: `$${s.parentShare.toLocaleString()}`,
['To Raid Party']: `$${s.childShare.toLocaleString()}`,
'Token Symbol': 'wxDAI',
'To DAO Treasury': `$${s.parentShare.toLocaleString()}`,
'To Raid Party': `$${s.childShare.toLocaleString()}`,
}));
csvString = Papa.unparse(formattedSpoils);
}
exportToCsv(csvString, `raidguild-treasury-${type}`);
},
[balancesWithPrices, members, spoils, transactionsWithPrices]
[
balancesWithPricesV2,
balancesWithPricesV3,
isV3,
members,
spoils,
transactionsWithPricesV2,
transactionsWithPricesV3,
]
);

return (
Expand All @@ -136,7 +156,8 @@ export const Accounting = () => {
<SiteLayout
isLoading={loading}
data={[
...transactionsWithPricesAndMembers,
...transactionsWithPricesAndMembersV2,
...transactionsWithPricesAndMembersV3,
...balances,
...Object.values(tokenPrices),
]}
Expand Down Expand Up @@ -164,6 +185,10 @@ export const Accounting = () => {
colorScheme='whiteAlpha'
variant='unstyled'
defaultIndex={0}
onChange={(index) => {
if (index === 0) setIsV3(true);
else setIsV3(false);
}}
>
<Flex
alignItems='right'
Expand All @@ -189,10 +214,10 @@ export const Accounting = () => {

<TabPanels>
<TabPanel>
<BalancesTable data={balancesWithPrices} />
<BalancesTable data={balancesWithPricesV3} />
</TabPanel>
<TabPanel>
<div>This is the placeholder for v3 balances data.</div>
<BalancesTable data={balancesWithPricesV2} />
</TabPanel>
</TabPanels>
</Tabs>
Expand All @@ -203,6 +228,10 @@ export const Accounting = () => {
colorScheme='whiteAlpha'
variant='unstyled'
defaultIndex={0}
onChange={(index) => {
if (index === 0) setIsV3(true);
else setIsV3(false);
}}
>
<Flex
alignItems='right'
Expand All @@ -229,11 +258,13 @@ export const Accounting = () => {
<TabPanels>
<TabPanel>
<TransactionsTable
data={transactionsWithPricesAndMembers}
data={transactionsWithPricesAndMembersV3}
/>
</TabPanel>
<TabPanel>
<div>This is the placeholder for v3 transactions data.</div>
<TransactionsTable
data={transactionsWithPricesAndMembersV2}
/>
</TabPanel>
</TabPanels>
</Tabs>
Expand Down
3 changes: 2 additions & 1 deletion libs/dm-hooks/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@ export { useContacts } from './useContacts';
export { default as useContactUpdate } from './useContactUpdate';
export { default as useDashboardList } from './useDashboardList';
export { default as useDefaultTitle } from './useDefaultTitle';
export { default as useFormattedData } from './useFormattedData';
export { default as useFormattedDataV2 } from './useFormattedDataV2';
export { default as useFormattedDataV3 } from './useFormattedDataV3';
export * from './useLinks';
export { default as useLinksUpdate } from './useLinksUpdate';
export { default as useMemberCreate } from './useMemberCreate';
Expand Down
Loading

0 comments on commit 6c9954c

Please sign in to comment.