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

feat: basic gnosis pay support #3821

Closed
wants to merge 4 commits into from
Closed
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
1 change: 1 addition & 0 deletions public/images/common/gnosis-pay.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
17 changes: 15 additions & 2 deletions src/components/common/CheckWallet/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,13 @@ import useIsOnlySpendingLimitBeneficiary from '@/hooks/useIsOnlySpendingLimitBen
import useIsSafeOwner from '@/hooks/useIsSafeOwner'
import useWallet from '@/hooks/wallets/useWallet'
import useConnectWallet from '../ConnectWallet/useConnectWallet'
import { useIsGnosisPayOwner } from '@/features/gnosispay/hooks/useIsGnosisPayOwner'

type CheckWalletProps = {
children: (ok: boolean) => ReactElement
allowSpendingLimit?: boolean
allowNonOwner?: boolean
allowGnosisPayOwner?: boolean
noTooltip?: boolean
}

Expand All @@ -17,14 +19,25 @@ enum Message {
NotSafeOwner = 'Your connected wallet is not a signer of this Safe Account',
}

const CheckWallet = ({ children, allowSpendingLimit, allowNonOwner, noTooltip }: CheckWalletProps): ReactElement => {
const CheckWallet = ({
children,
allowSpendingLimit,
allowNonOwner,
noTooltip,
allowGnosisPayOwner,
}: CheckWalletProps): ReactElement => {
const wallet = useWallet()
const isSafeOwner = useIsSafeOwner()
const isSpendingLimit = useIsOnlySpendingLimitBeneficiary()
const [isGnosisPayOwner] = useIsGnosisPayOwner()
const connectWallet = useConnectWallet()

const message =
wallet && (isSafeOwner || allowNonOwner || (isSpendingLimit && allowSpendingLimit))
wallet &&
(isSafeOwner ||
allowNonOwner ||
(isSpendingLimit && allowSpendingLimit) ||
(allowGnosisPayOwner && Boolean(isGnosisPayOwner)))
? ''
: !wallet
? Message.WalletNotConnected
Expand Down
11 changes: 6 additions & 5 deletions src/components/common/CooldownButton/index.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Button } from '@mui/material'
import { Button, type ButtonProps } from '@mui/material'
import { useState, useCallback, useEffect, type ReactNode } from 'react'

// TODO: Extract into a hook so it can be reused for links and not just buttons
Expand All @@ -7,8 +7,9 @@ const CooldownButton = ({
cooldown,
startDisabled = false,
children,
}: {
onClick: () => void
...props
}: ButtonProps & {
onClick?: () => void
startDisabled?: boolean
cooldown: number // Cooldown in seconds
children: ReactNode
Expand All @@ -30,13 +31,13 @@ const CooldownButton = ({
const handleClick = () => {
setLastSendTime(Date.now())
setRemainingSeconds(cooldown)
onClick()
onClick?.()
}

const isDisabled = remainingSeconds > 0

return (
<Button onClick={handleClick} variant="contained" size="small" disabled={isDisabled}>
<Button onClick={handleClick} variant="contained" size="small" disabled={isDisabled} {...props}>
<span>
{children}
{remainingSeconds > 0 && ` in ${Math.floor(remainingSeconds)}s`}
Expand Down
2 changes: 1 addition & 1 deletion src/components/sidebar/NewTxButton/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ const NewTxButton = (): ReactElement => {
}

return (
<CheckWallet allowSpendingLimit noTooltip>
<CheckWallet allowSpendingLimit allowGnosisPayOwner noTooltip>
{(isOk) =>
isOk ? (
<Button
Expand Down
2 changes: 1 addition & 1 deletion src/components/tx-flow/SafeTxProvider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import type { EIP712TypedData } from '@safe-global/safe-gateway-typescript-sdk'

export const SafeTxContext = createContext<{
safeTx?: SafeTransaction
setSafeTx: Dispatch<SetStateAction<SafeTransaction | undefined>>
setSafeTx: (tx: SafeTransaction | undefined) => void

safeMessage?: EIP712TypedData
setSafeMessage: Dispatch<SetStateAction<EIP712TypedData | undefined>>
Expand Down
12 changes: 10 additions & 2 deletions src/components/tx/SignOrExecuteForm/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ import { TX_EVENTS } from '@/services/analytics/events/transactions'
import { trackEvent } from '@/services/analytics'
import useChainId from '@/hooks/useChainId'
import PermissionsCheck from './PermissionsCheck'
import { useIsGnosisPayOwner } from '@/features/gnosispay/hooks/useIsGnosisPayOwner'
import GnosisPayExecutionForm from '@/features/gnosispay/GnosisPayExecutionForm'

export type SubmitCallback = (txId: string, isExecuted?: boolean) => void

Expand Down Expand Up @@ -78,6 +80,8 @@ export const SignOrExecuteForm = ({
const { safe } = useSafeInfo()
const isCounterfactualSafe = !safe.deployed

const [isGnosisPayOwner] = useIsGnosisPayOwner()

// If checkbox is checked and the transaction is executable, execute it, otherwise sign it
const canExecute = isCorrectNonce && (props.isExecutable || isNewExecutableTx)
const willExecute = (props.onlyExecute || shouldExecute) && canExecute
Expand Down Expand Up @@ -138,15 +142,19 @@ export const SignOrExecuteForm = ({
</ErrorMessage>
)}

{canExecute && !props.onlyExecute && !isCounterfactualSafe && <ExecuteCheckbox onChange={setShouldExecute} />}
{canExecute && !props.onlyExecute && !isCounterfactualSafe && !isGnosisPayOwner && (
<ExecuteCheckbox onChange={setShouldExecute} />
)}

<WrongChainWarning />

<UnknownContractError />

<RiskConfirmationError />

{isCounterfactualSafe ? (
{isGnosisPayOwner ? (
<GnosisPayExecutionForm {...props} safeTx={safeTx} onSubmit={onFormSubmit} />
) : isCounterfactualSafe ? (
<CounterfactualForm {...props} safeTx={safeTx} isCreation={isCreation} onSubmit={onFormSubmit} onlyExecute />
) : willExecute ? (
<ExecuteForm {...props} safeTx={safeTx} isCreation={isCreation} onSubmit={onFormSubmit} />
Expand Down
51 changes: 51 additions & 0 deletions src/features/gnosispay/ExecuteGnosisPayTx.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
import TxCard from '@/components/tx-flow/common/TxCard'
import TxLayout from '@/components/tx-flow/common/TxLayout'
import GnosisPayExecutionForm from './GnosisPayExecutionForm'
import { type GnosisPayTxItem } from '@/store/gnosisPayTxsSlice'
import useAsync from '@/hooks/useAsync'
import { useContext, useEffect } from 'react'
import { SafeTxContext } from '@/components/tx-flow/SafeTxProvider'
import useDecodeTx from '@/hooks/useDecodeTx'
import DecodedTx from '@/components/tx/DecodedTx'
import { RedefineBalanceChanges } from '@/components/tx/security/redefine/RedefineBalanceChange'
import { createTx } from '@/services/tx/tx-sender'

const ExecuteGnosisPayModal = ({ gnosisPayTx }: { gnosisPayTx: GnosisPayTxItem }) => {
const { safeTx, setSafeTx } = useContext(SafeTxContext)

const [fakeSafeTx] = useAsync(() => {
return createTx(gnosisPayTx.safeTxData)
}, [gnosisPayTx])

useEffect(() => {
if (fakeSafeTx) {
setSafeTx(fakeSafeTx)
}
}, [fakeSafeTx, setSafeTx])

const [decodedData, decodedDataError, decodedDataLoading] = useDecodeTx(safeTx)
return (
<TxCard>
<DecodedTx
tx={safeTx}
decodedData={decodedData}
decodedDataError={decodedDataError}
decodedDataLoading={decodedDataLoading}
/>

<RedefineBalanceChanges />

<GnosisPayExecutionForm queuedGnosisPayTx={gnosisPayTx} isExecutable onlyExecute />
</TxCard>
)
}

const ExecuteGnosisPayTx = ({ gnosisPayTx }: { gnosisPayTx: GnosisPayTxItem }) => {
return (
<TxLayout title="Execute Gnosis Pay transaction" step={0}>
<ExecuteGnosisPayModal gnosisPayTx={gnosisPayTx} />
</TxLayout>
)
}

export default ExecuteGnosisPayTx
Loading
Loading