Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

replace hooks with smart-invoice #165

Open
wants to merge 7 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 2 additions & 3 deletions apps/frontend/.env.example
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,10 @@ NEXTAUTH_URL=http://localhost:4200
# Secret hash used for encoding JWTs
NEXTAUTH_SECRET=doremifasolatidodoremifasolatidodoremifasolatidodoremifasolatido

NEXT_PUBLIC_ALCHEMY_KEY=
NEXT_PUBLIC_ALCHEMY_ID=
NEXT_PUBLIC_INFURA_ID=
NEXT_PUBLIC_NETWORK_ID=
NEXT_PUBLIC_WC_PROJECT_ID=

NEXT_PUBLIC_WALLETCONNECT_ID=
# Used for GitHub's GraphQL API. Read-only permissions until DM needs mutations on our GitHub repo
# https://docs.github.com/en/authentication/keeping-your-account-and-data-secure/managing-your-personal-access-tokens
GITHUB_API_TOKEN=
Expand Down
23 changes: 17 additions & 6 deletions apps/frontend/components/Escrow/DepositFunds.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,10 @@ import {
Stack,
Text,
Tooltip,
useToast,
VStack,
} from '@raidguild/design-system';
import { commify, getTxLink } from '@raidguild/dm-utils';
import { useDeposit } from '@raidguild/escrow-hooks';
import {
checkedAtIndex,
depositedMilestones,
Expand All @@ -27,6 +27,7 @@ import {
parseTokenAddress,
PAYMENT_TYPES,
} from '@raidguild/escrow-utils';
import { useDeposit } from '@smartinvoicexyz/hooks';
import _ from 'lodash';
import { useEffect, useMemo, useState } from 'react';
import { useForm } from 'react-hook-form';
Expand All @@ -47,6 +48,7 @@ const DepositFunds = ({
const { token, amounts, currentMilestone } = invoice;
const chainId = useChainId();
const { address } = useAccount();
const toast = useToast();

const TOKEN_DATA = useMemo(
() => ({
Expand Down Expand Up @@ -83,12 +85,21 @@ const DepositFunds = ({
paymentType?.value === PAYMENT_TYPES.NATIVE ? 18 : tokenBalance?.decimals;
const hasAmount = balance >= parseUnits(amount, decimals);

const { handleDeposit, isLoading, isReady } = useDeposit({
invoice,
amount,
decimals,
const { handleDeposit, isLoading } = useDeposit({
invoice: {
tokenMetadata: {
address: invoice.token, // only address is needed
name: '',
symbol: '',
decimals,
totalSupply: BigInt(0),
},
address: invoice.address,
},
amount: amount && parseUnits(amount, decimals),
hasAmount, // (+ gas)
paymentType: paymentType?.value,
toast,
});

const depositHandler = async () => {
Expand Down Expand Up @@ -280,7 +291,7 @@ const DepositFunds = ({

<Button
onClick={depositHandler}
isDisabled={amount <= 0 || !isReady || !hasAmount}
isDisabled={amount <= 0 || isLoading || !hasAmount}
isLoading={isLoading}
textTransform='uppercase'
variant='solid'
Expand Down
18 changes: 14 additions & 4 deletions apps/frontend/components/Escrow/EscrowConfirmation.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,11 @@ import {
} from '@raidguild/design-system';
import { IRaid } from '@raidguild/dm-types';
import { chainsMap, commify } from '@raidguild/dm-utils';
import { useEscrowZap, useRegister } from '@raidguild/escrow-hooks';
import { useRegister } from '@raidguild/escrow-hooks';
import { GANGGANG_MULTISIG, NETWORK_CONFIG } from '@raidguild/escrow-utils';
import { useEscrowZap } from '@smartinvoicexyz/hooks';
import { WriteContractReturnType } from '@wagmi/core';
import useDetailsPin from 'libs/escrow-hooks/src/useDetailsPin';
import _ from 'lodash';
import { Dispatch, SetStateAction, useMemo } from 'react';
import { UseFormReturn } from 'react-hook-form';
Expand Down Expand Up @@ -88,6 +90,10 @@ const EscrowConfirmation = ({
enabled: canRegisterDirectly,
});

const { data: details, isLoading: detailsLoading } = useDetailsPin({
...detailsData,
});

const { writeAsync: writeEscrowZap, isLoading: zapLoading } = useEscrowZap({
ownersAndAllocations,
threshold,
Expand All @@ -96,9 +102,10 @@ const EscrowConfirmation = ({
provider: provider || zeroAddress,
client,
safetyValveDate,
detailsData,
details,
projectTeamSplit: raidPartySplit,
daoSplit,
networkConfig: NETWORK_CONFIG,
enabled: !canRegisterDirectly,
onSuccess: (tx: WriteContractReturnType) => setTxHash(tx),
});
Expand Down Expand Up @@ -232,7 +239,7 @@ const EscrowConfirmation = ({
variant='outline'
minW='25%'
mr='.5rem'
isDisabled={zapLoading || registerLoading}
isDisabled={zapLoading || registerLoading || detailsLoading}
onClick={backStep}
>
Back
Expand All @@ -241,7 +248,10 @@ const EscrowConfirmation = ({
variant='solid'
w='100%'
isDisabled={
registerLoading || zapLoading || !(writeAsync || writeEscrowZap)
registerLoading ||
zapLoading ||
detailsLoading ||
!(writeAsync || writeEscrowZap)
}
isLoading={registerLoading || zapLoading}
onClick={createInvoice}
Expand Down
10 changes: 6 additions & 4 deletions apps/frontend/components/Escrow/ReleaseFunds.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,10 @@ import {
VStack,
} from '@raidguild/design-system';
import { getInvoice } from '@raidguild/escrow-gql';
// import { getTxLink } from '@raidguild/dm-utils';
import { usePollSubgraph, useRelease } from '@raidguild/escrow-hooks';
import { usePollSubgraph } from '@raidguild/escrow-hooks';
import { Invoice, parseTokenAddress } from '@raidguild/escrow-utils';
// import { getTxLink } from '@raidguild/dm-utils';
import { useRelease } from '@smartinvoicexyz/hooks';
import { formatUnits } from 'viem';
import { useChainId } from 'wagmi';

Expand Down Expand Up @@ -48,8 +49,9 @@ const ReleaseFunds = ({ invoice, balance }: ReleaseFundsProp) => {
};

const { writeAsync: releaseFunds, isLoading } = useRelease({
invoice,
onSuccess,
invoice: { address: invoice.address },
onTxSuccess: onSuccess,
toast,
});

// const pollSubgraph = async () => {
Expand Down
21 changes: 14 additions & 7 deletions apps/frontend/components/Escrow/WithdrawFunds.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,12 @@ import {
// Link,
Spinner,
Text,
useToast,
VStack,
} from '@raidguild/design-system';
// import { getTxLink } from '@raidguild/dm-utils';
import { useWithdraw } from '@raidguild/escrow-hooks';
import { Invoice, parseTokenAddress } from '@raidguild/escrow-utils';
import { useWithdraw } from '@smartinvoicexyz/hooks';
import { formatUnits } from 'viem';
import { useChainId } from 'wagmi';

Expand All @@ -20,13 +21,19 @@ const WithdrawFunds = ({
balance: bigint;
}) => {
const chainId = useChainId();
const toast = useToast();

// const onSuccess = () => {
// // toast
// // close modal
// };

const { writeAsync: withdrawFunds, isLoading } = useWithdraw({ invoice });
const { writeAsync: withdrawFunds, isLoading } = useWithdraw({
invoice: { address: invoice.address },
onTxSuccess: () => {
toast({
title: 'Withdrawal Successful',
description: 'Funds have been successfully withdrawn from escrow',
status: 'success',
});
},
toast,
});

return (
<VStack w='100%' spacing='1rem'>
Expand Down
4 changes: 2 additions & 2 deletions apps/frontend/components/Escrow/shared/AccountLink.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { NETWORK_CONFIG, safeUrl, splitsLink } from '@raidguild/escrow-utils';
import blockies from 'blockies-ts';
import _ from 'lodash';
import { Hex } from 'viem';
import { useChainId, useContractRead, useEnsName } from 'wagmi';
import { useChainId, useReadContract, useEnsName } from 'wagmi';

type AccountLinkProps = {
name?: string;
Expand Down Expand Up @@ -32,7 +32,7 @@ const AccountLink = ({

const imageUrl = blockies.create({ seed: address }).toDataURL();

const { data: isSafe } = useContractRead({
const { data: isSafe } = useReadContract({
address,
abi: [
{
Expand Down
1 change: 1 addition & 0 deletions apps/frontend/next.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ const nextConfig = {
'@raidguild/dm-utils',
'@raidguild/escrow-utils',
'@raidguild/escrow-gql',
'@smartinvoicexyz/hooks',
],
};

Expand Down
37 changes: 21 additions & 16 deletions libs/dm-utils/src/chains.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,17 +10,17 @@ import {
sepolia,
} from 'viem/chains';

const chains: Chain[] = [
const chains: readonly Chain[] = [
mainnet,
gnosis,
polygon,
arbitrum,
optimism,
sepolia,
goerli,
];
] as const;

export const chainsList: { [key: number]: Chain } = {
export const chainsList: Record<number, Chain> = {
100: gnosis,
137: polygon,
42151: arbitrum,
Expand All @@ -35,28 +35,32 @@ export type SupportedChainId = (typeof SUPPORTED_CHAIN_IDS)[number];
export type SupportedChain = (typeof chains)[number];
export const SUPPORTED_CHAINS = chains as [SupportedChain, ...SupportedChain[]];

export const infuraNetworkName: Partial<Record<SupportedChainId, string>> = {
export const infuraNetworkName: Record<SupportedChainId, string | undefined> = {
[mainnet.id]: 'mainnet',
[polygon.id]: 'polygon-mainnet',
[arbitrum.id]: 'arbitrum-mainnet',
[optimism.id]: 'optimism-mainnet',
[sepolia.id]: 'sepolia',
};

export const alchemyNetworkName: Partial<Record<SupportedChainId, string>> = {
[mainnet.id]: 'eth-mainnet',
[polygon.id]: 'polygon-mainnet',
[arbitrum.id]: 'arb-mainnet',
[optimism.id]: 'opt-mainnet',
[sepolia.id]: 'eth-sepolia',
[gnosis.id]: 'gnosis-mainnet',
};
export const alchemyNetworkName: Record<SupportedChainId, string | undefined> =
{
[mainnet.id]: 'eth-mainnet',
[polygon.id]: 'polygon-mainnet',
[arbitrum.id]: 'arb-mainnet',
[optimism.id]: 'opt-mainnet',
[sepolia.id]: 'eth-sepolia',
[gnosis.id]: 'gnosis-mainnet',
};

if (process.env.NODE_ENV === 'development') {
chains.push(hardhat);
(chains as Chain[]).push(hardhat);
}
export const chainsMap = (chainId: number) => chainsList[chainId];
export const chainIdToIconMap = (chainId: number) => {

export const chainsMap = (chainId: number): Chain | undefined =>
chainsList[chainId];

export const chainIdToIconMap = (chainId: number): string => {
switch (chainId) {
case optimism.id:
return '/icons/optimism.png';
Expand All @@ -66,7 +70,8 @@ export const chainIdToIconMap = (chainId: number) => {
return '';
}
};
export const networkToIdMap = (network: string) => {

export const networkToIdMap = (network: string): number => {
switch (network) {
case 'optimism':
return optimism.id;
Expand Down
4 changes: 2 additions & 2 deletions libs/dm-utils/src/web3.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import {
SupportedChainId,
} from './chains';

const projectId = process.env.NEXT_PUBLIC_WC_PROJECT_ID || '';
const projectId = process.env.NEXT_PUBLIC_WALLETCONNECT_ID || '';

type _transports = Record<SupportedChainId, Transport>;

Expand All @@ -43,7 +43,7 @@ const transports: _transports = SUPPORTED_CHAINS.reduce(
const alchemyUrl =
alchemyNetwork && process.env.NEXT_PUBLIC_RPC_KEY
? `https://${alchemyNetwork}.g.alchemy.com/v2/${
process.env.NEXT_PUBLIC_ALCHEMY_KEY as string
process.env.NEXT_PUBLIC_ALCHEMY_ID as string
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🛠️ Refactor suggestion

Update documentation for environment variable changes.

The Alchemy API key environment variable has been renamed. Please ensure that all documentation, including README files and deployment guides, are updated to reflect these changes:

  • NEXT_PUBLIC_WC_PROJECT_IDNEXT_PUBLIC_WALLETCONNECT_ID
  • NEXT_PUBLIC_ALCHEMY_KEYNEXT_PUBLIC_ALCHEMY_ID

}`
: undefined;
if (alchemyUrl) list.push(http(alchemyUrl));
Expand Down
6 changes: 1 addition & 5 deletions libs/escrow-hooks/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,10 @@
export { default as useDebounce } from './useDebounce';
export { default as useDeposit } from './useDeposit';
export { default as useEscrowZap } from './useEscrowZap';
export { default as useEscrowEvents } from './useEscrowEvents';
export { default as useInvoiceDetails } from './useInvoiceDetails';
export { default as useLock } from './useLock';
export { default as usePollSubgraph } from './usePollSubgraph';
export { default as useRegister } from './useRegister';
export { default as useRelease } from './useRelease';
export { default as useResolve } from './useResolve';
export { default as useSplitEarnings } from './useSplitEarnings';
export { default as useSplitsMetadata } from './useSplitsMetadata';
export { default as useTokenBalance } from './useTokenBalance';
export { default as useWithdraw } from './useWithdraw';
export { default as useEscrowEvents } from './useEscrowEvents';
Loading