Skip to content

Commit

Permalink
Finalize implementation for send assets action template
Browse files Browse the repository at this point in the history
  • Loading branch information
mudrila committed Dec 20, 2024
1 parent 8f94f8e commit 789164f
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 66 deletions.
63 changes: 63 additions & 0 deletions src/hooks/DAO/useSendAssetsActionModal.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
import { useTranslation } from "react-i18next";
import { useNavigate } from "react-router-dom";
import { ModalType } from "../../components/ui/modals/ModalProvider";
import { SendAssetsData } from "../../components/ui/modals/SendAssetsModal";
import { useDecentModal } from "../../components/ui/modals/useDecentModal";
import { DAO_ROUTES } from "../../constants/routes";
import { useFractal } from "../../providers/App/AppProvider";
import { useNetworkConfigStore } from "../../providers/NetworkConfig/useNetworkConfigStore";
import { useProposalActionsStore } from "../../store/actions/useProposalActionsStore";
import { useDaoInfoStore } from "../../store/daoInfo/useDaoInfoStore";
import { ProposalActionType } from "../../types/proposalBuilder";
import { isNativeAsset, prepareSendAssetsActionData } from "../../utils/dao/prepareSendAssetsActionData";

export default function useSendAssetsActionModal() {
const { safe } = useDaoInfoStore();
const { addressPrefix } = useNetworkConfigStore();
const { t } = useTranslation(['modals']);
const { addAction } = useProposalActionsStore();
const navigate = useNavigate();
const { governance: { isAzorius } } = useFractal();
const sendAssetsAction = async (sendAssetsData: SendAssetsData) => {
if (!safe?.address) {
return;
}
const isNative = isNativeAsset(sendAssetsData.asset);
const transactionData = prepareSendAssetsActionData({
transferAmount: sendAssetsData.transferAmount,
asset: sendAssetsData.asset,
destinationAddress: sendAssetsData.destinationAddress,
});
addAction({
actionType: ProposalActionType.TRANSFER,
content: <></>,
transactions: [
{
targetAddress: transactionData.calldata,
ethValue: {
bigintValue: transactionData.value,
value: transactionData.value.toString(),
},
functionName: isNative ? '' : 'transfer',
parameters: isNative
? []
: [
{ signature: 'address', value: sendAssetsData.destinationAddress },
{ signature: 'uint256', value: sendAssetsData.transferAmount.toString() },
],
},
],
});
navigate(DAO_ROUTES.proposalWithActionsNew.relative(addressPrefix, safe.address));
};

const openSendAssetsModal = useDecentModal(ModalType.SEND_ASSETS, {
onSubmit: sendAssetsAction,
submitButtonText: t('submitProposal', { ns: 'modals' }),
showNonceInput: !isAzorius,
});

return {
openSendAssetsModal,
};
}
14 changes: 3 additions & 11 deletions src/pages/dao/proposal-templates/SafeProposalTemplatesPage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,10 @@ import ExampleTemplateCard from '../../../components/ProposalTemplates/ExampleTe
import ProposalTemplateCard from '../../../components/ProposalTemplates/ProposalTemplateCard';
import NoDataCard from '../../../components/ui/containers/NoDataCard';
import { InfoBoxLoader } from '../../../components/ui/loaders/InfoBoxLoader';
import { ModalType } from '../../../components/ui/modals/ModalProvider';
import { SendAssetsData } from '../../../components/ui/modals/SendAssetsModal';
import { useDecentModal } from '../../../components/ui/modals/useDecentModal';
import PageHeader from '../../../components/ui/page/Header/PageHeader';
import Divider from '../../../components/ui/utils/Divider';
import { DAO_ROUTES } from '../../../constants/routes';
import useSendAssetsActionModal from '../../../hooks/DAO/useSendAssetsActionModal';
import { useCanUserCreateProposal } from '../../../hooks/utils/useCanUserSubmitProposal';
import { analyticsEvents } from '../../../insights/analyticsEvents';
import { useFractal } from '../../../providers/App/AppProvider';
Expand All @@ -27,21 +25,15 @@ export function SafeProposalTemplatesPage() {

const { t } = useTranslation();
const {
governance: { proposalTemplates, isAzorius },
governance: { proposalTemplates },
} = useFractal();
const { safe } = useDaoInfoStore();
const { canUserCreateProposal } = useCanUserCreateProposal();
const { addressPrefix } = useNetworkConfigStore();
const navigate = useNavigate();

const safeAddress = safe?.address;
const openSendAssetsModal = useDecentModal(ModalType.SEND_ASSETS, {
onSubmit: (sendAssetData: SendAssetsData) => {
console.log(sendAssetData);
},
submitButtonText: t('submitProposal', { ns: 'modals' }),
showNonceInput: !isAzorius,
});
const { openSendAssetsModal } = useSendAssetsActionModal();

const EXAMPLE_TEMPLATES = useMemo(() => {
if (!safeAddress) return [];
Expand Down
58 changes: 3 additions & 55 deletions src/pages/dao/treasury/SafeTreasuryPage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,92 +2,40 @@ import * as amplitude from '@amplitude/analytics-browser';
import { Box, Divider, Flex, Grid, GridItem, Show } from '@chakra-ui/react';
import { useEffect, useState } from 'react';
import { useTranslation } from 'react-i18next';
import { useNavigate } from 'react-router-dom';
import { Assets } from '../../../components/DAOTreasury/components/Assets';
import {
PaginationButton,
PaginationCount,
Transactions,
} from '../../../components/DAOTreasury/components/Transactions';
import { TitledInfoBox } from '../../../components/ui/containers/TitledInfoBox';
import { ModalType } from '../../../components/ui/modals/ModalProvider';
import { SendAssetsData } from '../../../components/ui/modals/SendAssetsModal';
import { useDecentModal } from '../../../components/ui/modals/useDecentModal';
import PageHeader from '../../../components/ui/page/Header/PageHeader';
import { DAO_ROUTES } from '../../../constants/routes';
import useSendAssetsActionModal from '../../../hooks/DAO/useSendAssetsActionModal';
import { useCanUserCreateProposal } from '../../../hooks/utils/useCanUserSubmitProposal';
import { analyticsEvents } from '../../../insights/analyticsEvents';
import { useFractal } from '../../../providers/App/AppProvider';
import { useNetworkConfigStore } from '../../../providers/NetworkConfig/useNetworkConfigStore';
import { useProposalActionsStore } from '../../../store/actions/useProposalActionsStore';
import { useDaoInfoStore } from '../../../store/daoInfo/useDaoInfoStore';
import { ProposalActionType } from '../../../types';
import {
isNativeAsset,
prepareSendAssetsActionData,
} from '../../../utils/dao/prepareSendAssetsActionData';

export function SafeTreasuryPage() {
useEffect(() => {
amplitude.track(analyticsEvents.TreasuryPageOpened);
}, []);
const { safe } = useDaoInfoStore();
const {
governance: { isAzorius },
treasury: { assetsFungible, transfers },
} = useFractal();
const { subgraphInfo } = useDaoInfoStore();
const [shownTransactions, setShownTransactions] = useState(20);
const { t } = useTranslation(['treasury', 'modals']);
const { canUserCreateProposal } = useCanUserCreateProposal();
const { addAction } = useProposalActionsStore();
const navigate = useNavigate();
const { addressPrefix } = useNetworkConfigStore();

const hasAnyBalanceOfAnyFungibleTokens =
assetsFungible.reduce((p, c) => p + BigInt(c.balance), 0n) > 0n;

const showSendButton = canUserCreateProposal && hasAnyBalanceOfAnyFungibleTokens;

const totalTransfers = transfers?.length || 0;
const showLoadMoreTransactions = totalTransfers > shownTransactions && shownTransactions < 100;
const sendAssetsAction = async (sendAssetsData: SendAssetsData) => {
if (!safe?.address) {
return;
}
const isNative = isNativeAsset(sendAssetsData.asset);
const transactionData = prepareSendAssetsActionData({
transferAmount: sendAssetsData.transferAmount,
asset: sendAssetsData.asset,
destinationAddress: sendAssetsData.destinationAddress,
});
addAction({
actionType: ProposalActionType.TRANSFER,
content: <></>,
transactions: [
{
targetAddress: transactionData.calldata,
ethValue: {
bigintValue: transactionData.value,
value: transactionData.value.toString(),
},
functionName: isNative ? '' : 'transfer',
parameters: isNative
? []
: [
{ signature: 'address', value: sendAssetsData.destinationAddress },
{ signature: 'uint256', value: sendAssetsData.transferAmount.toString() },
],
},
],
});
navigate(DAO_ROUTES.proposalWithActionsNew.relative(addressPrefix, safe.address));
};

const openSendAssetsModal = useDecentModal(ModalType.SEND_ASSETS, {
onSubmit: sendAssetsAction,
submitButtonText: t('submitProposal', { ns: 'modals' }),
showNonceInput: !isAzorius,
});
const { openSendAssetsModal } = useSendAssetsActionModal();

return (
<Box>
Expand Down

0 comments on commit 789164f

Please sign in to comment.