Skip to content

Commit

Permalink
feat(hooks-store): use dappId from hook model to match with dapp (#4938)
Browse files Browse the repository at this point in the history
  • Loading branch information
shoom3301 authored Oct 4, 2024
1 parent 83184d2 commit 46699cb
Show file tree
Hide file tree
Showing 23 changed files with 97 additions and 113 deletions.
14 changes: 7 additions & 7 deletions apps/cowswap-frontend/.env
Original file line number Diff line number Diff line change
Expand Up @@ -56,13 +56,13 @@
# To set your own `AppData`, change `REACT_APP_FULL_APP_DATA_<environment>`

# AppData, build yours at https://explorer.cow.fi/appdata
REACT_APP_FULL_APP_DATA_PRODUCTION='{"version":"1.2.0","appCode":"CoW Swap","environment":"production","metadata":{}}'
REACT_APP_FULL_APP_DATA_ENS='{"version":"1.2.0","appCode":"CoW Swap","environment":"ens","metadata":{}}'
REACT_APP_FULL_APP_DATA_BARN='{"version":"1.2.0","appCode":"CoW Swap","environment":"barn","metadata":{}}'
REACT_APP_FULL_APP_DATA_STAGING='{"version":"1.2.0","appCode":"CoW Swap","environment":"staging","metadata":{}}'
REACT_APP_FULL_APP_DATA_PR='{"version":"1.2.0","appCode":"CoW Swap","environment":"pr","metadata":{}}'
REACT_APP_FULL_APP_DATA_DEVELOPMENT='{"version":"1.2.0","appCode":"CoW Swap","environment":"development","metadata":{}}'
REACT_APP_FULL_APP_DATA_LOCAL='{"version":"1.2.0","appCode":"CoW Swap","environment":"local","metadata":{}}'
REACT_APP_FULL_APP_DATA_PRODUCTION='{"version":"1.3.0","appCode":"CoW Swap","environment":"production","metadata":{}}'
REACT_APP_FULL_APP_DATA_ENS='{"version":"1.3.0","appCode":"CoW Swap","environment":"ens","metadata":{}}'
REACT_APP_FULL_APP_DATA_BARN='{"version":"1.3.0","appCode":"CoW Swap","environment":"barn","metadata":{}}'
REACT_APP_FULL_APP_DATA_STAGING='{"version":"1.3.0","appCode":"CoW Swap","environment":"staging","metadata":{}}'
REACT_APP_FULL_APP_DATA_PR='{"version":"1.3.0","appCode":"CoW Swap","environment":"pr","metadata":{}}'
REACT_APP_FULL_APP_DATA_DEVELOPMENT='{"version":"1.3.0","appCode":"CoW Swap","environment":"development","metadata":{}}'
REACT_APP_FULL_APP_DATA_LOCAL='{"version":"1.3.0","appCode":"CoW Swap","environment":"local","metadata":{}}'



Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,10 +49,10 @@ export function AppDataHooksUpdater(): null {
const [permitHook, setPermitHook] = useState<TypedCowHook | undefined>(undefined)

useEffect(() => {
const preInteractionHooks = (preHooks || []).map<TypedCowHook>(({ hookDetails }) =>
const preInteractionHooks = (preHooks || []).map<TypedCowHook>((hookDetails) =>
cowHookToTypedCowHook(hookDetails.hook, 'hookStore'),
)
const postInteractionHooks = (postHooks || []).map<TypedCowHook>(({ hookDetails }) =>
const postInteractionHooks = (postHooks || []).map<TypedCowHook>((hookDetails) =>
cowHookToTypedCowHook(hookDetails.hook, 'hookStore'),
)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { EnvironmentName, environmentName } from '@cowprotocol/common-utils'
import { AppDataInfo } from '../types'
import { toKeccak256 } from '../utils/buildAppData'

const DEFAULT_FULL_APP_DATA = '{"version":"1.2.0","appCode":"CoW Swap","metadata":{}}'
const DEFAULT_FULL_APP_DATA = '{"version":"1.3.0","appCode":"CoW Swap","metadata":{}}'

let appData: AppDataInfo = (() => {
const fullAppData = getFullAppDataByEnv(environmentName)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ export function HookDappContainer({ dapp, isPreHook, onDismiss, hookToEdit }: Ho
chainId,
account,
orderParams,
hookToEdit: hookToEditDetails?.hookDetails,
hookToEdit: hookToEditDetails,
signer,
isSmartContract,
isPreHook,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,14 @@ const DEFAULT_HOOK_STATE: CowHook = {
target: '',
callData: '',
gasLimit: '',
dappId: '',
}

const DEFAULT_ERRORS_STATE: Record<keyof CowHook, string> = {
target: '',
callData: '',
gasLimit: '',
dappId: '',
}

const FIELDS = [
Expand Down Expand Up @@ -52,6 +54,8 @@ export function BuildHookApp({ context }: HookDappProps) {
const newErrors: Record<keyof CowHook, string> = { ...DEFAULT_ERRORS_STATE }

const hasErrors = Object.entries(hook).some(([key, value]) => {
if (key === 'dappId') return false

if (!value.trim()) {
newErrors[key as keyof CowHook] = `${capitalizeFirstLetter(key)} is required`
return true
Expand Down
20 changes: 9 additions & 11 deletions apps/cowswap-frontend/src/modules/hooksStore/hooks/useAddHook.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
import { useSetAtom } from 'jotai'
import { useCallback } from 'react'

import { CowHookDetails } from '@cowprotocol/hook-dapp-lib'

import { v4 as uuidv4 } from 'uuid'

import { setHooksAtom } from '../state/hookDetailsAtom'
import { AddHook, CowHookDetailsSerialized, HookDapp } from '../types/hooks'
import { appendDappIdToCallData } from '../utils'
import { AddHook, HookDapp } from '../types/hooks'

export function useAddHook(dapp: HookDapp, isPreHook: boolean): AddHook {
const updateHooks = useSetAtom(setHooksAtom)
Expand All @@ -15,16 +16,13 @@ export function useAddHook(dapp: HookDapp, isPreHook: boolean): AddHook {
console.log('[hooks] Add ' + (isPreHook ? 'pre-hook' : 'post-hook'), hookToAdd, isPreHook)

const uuid = uuidv4()
const hookDetails: CowHookDetailsSerialized = {
hookDetails: {
...hookToAdd,
uuid,
hook: {
...hookToAdd.hook,
callData: appendDappIdToCallData(hookToAdd.hook.callData, dapp.id),
},
const hookDetails: CowHookDetails = {
...hookToAdd,
uuid,
hook: {
...hookToAdd.hook,
dappId: dapp.id,
},
dappId: dapp.id,
}

updateHooks((hooks) => {
Expand Down
17 changes: 7 additions & 10 deletions apps/cowswap-frontend/src/modules/hooksStore/hooks/useEditHook.ts
Original file line number Diff line number Diff line change
@@ -1,20 +1,19 @@
import { useSetAtom } from 'jotai'
import { useCallback } from 'react'

import { CowHookDetails } from '@cowprotocol/hook-dapp-lib'
import { CowHookToEdit } from '@cowprotocol/hook-dapp-lib'

import { setHooksAtom } from '../state/hookDetailsAtom'
import { EditHook } from '../types/hooks'
import { appendDappIdToCallData } from '../utils'

export function useEditHook(isPreHook: boolean): EditHook {
const updateHooks = useSetAtom(setHooksAtom)

return useCallback(
(update: CowHookDetails) => {
(update: CowHookToEdit) => {
updateHooks((state) => {
const type = isPreHook ? 'preHooks' : 'postHooks'
const hookIndex = state[type].findIndex((i) => i.hookDetails.uuid === update.uuid)
const hookIndex = state[type].findIndex((i) => i.uuid === update.uuid)

if (hookIndex < 0) return state

Expand All @@ -23,12 +22,10 @@ export function useEditHook(isPreHook: boolean): EditHook {

typeState[hookIndex] = {
...hookDetails,
hookDetails: {
...update,
hook: {
...update.hook,
callData: appendDappIdToCallData(update.hook.callData, hookDetails.dappId),
},
...update,
hook: {
...update.hook,
dappId: hookDetails.hook.dappId,
},
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,5 @@ export function useHookById(uuid: string | undefined, isPreHook: boolean) {

if (!uuid) return undefined

return (isPreHook ? hooks.preHooks : hooks.postHooks).find((i) => i.hookDetails.uuid === uuid)
return (isPreHook ? hooks.preHooks : hooks.postHooks).find((i) => i.uuid === uuid)
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,5 @@ export function usePostHooksRecipientOverride() {
* because in the current implementation we always take the value from the last hook
* but it might give an unexpected behaviour
*/
return useMemo(
() => postHooks.reverse().find((i) => i.hookDetails.recipientOverride)?.hookDetails.recipientOverride,
[postHooks],
)
return useMemo(() => postHooks.reverse().find((i) => i.recipientOverride)?.recipientOverride, [postHooks])
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,13 @@ export function useRemoveHook(isPreHook: boolean): RemoveHook {
updateHooks((hooks) => {
if (isPreHook) {
return {
preHooks: hooks.preHooks.filter((hook) => hook.hookDetails.uuid !== uuid),
preHooks: hooks.preHooks.filter((hook) => hook.uuid !== uuid),
postHooks: hooks.postHooks,
}
} else {
return {
preHooks: hooks.preHooks,
postHooks: hooks.postHooks.filter((hook) => hook.hookDetails.uuid !== uuid),
postHooks: hooks.postHooks.filter((hook) => hook.uuid !== uuid),
}
}
})
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
// src/modules/hooksStore/pure/AppliedHookItem/index.tsx

import ICON_CHECK_ICON from '@cowprotocol/assets/cow-swap/check-singular.svg'
import ICON_GRID from '@cowprotocol/assets/cow-swap/grid.svg'
import TenderlyLogo from '@cowprotocol/assets/cow-swap/tenderly-logo.svg'
import ICON_X from '@cowprotocol/assets/cow-swap/x.svg'
import { CowHookDetails } from '@cowprotocol/hook-dapp-lib'
import { InfoTooltip } from '@cowprotocol/ui'

import { Edit2, Trash2, ExternalLink as ExternalLinkIcon } from 'react-feather'
Expand All @@ -12,11 +11,11 @@ import SVG from 'react-inlinesvg'
import * as styledEl from './styled'

import { TenderlySimulate } from '../../containers/TenderlySimulate'
import { CowHookDetailsSerialized, HookDapp } from '../../types/hooks'
import { HookDapp } from '../../types/hooks'

interface HookItemProp {
account: string | undefined
hookDetails: CowHookDetailsSerialized
hookDetails: CowHookDetails
dapp: HookDapp | undefined
isPreHook: boolean
removeHook: (uuid: string, isPreHook: boolean) => void
Expand All @@ -27,15 +26,7 @@ interface HookItemProp {
// TODO: remove once a tenderly bundle simulation is ready
const isBundleSimulationReady = false

export function AppliedHookItem({
account,
hookDetails: { hookDetails },
dapp,
isPreHook,
editHook,
removeHook,
index,
}: HookItemProp) {
export function AppliedHookItem({ account, hookDetails, dapp, isPreHook, editHook, removeHook, index }: HookItemProp) {
// TODO: Determine the simulation status based on actual simulation results
// For demonstration, using a placeholder. Replace with actual logic.
const simulationPassed = true // TODO: Replace with actual condition
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
import { useRef, useEffect } from 'react'

import { CowHookDetails } from '@cowprotocol/hook-dapp-lib'

import Sortable from 'sortablejs'
import styled from 'styled-components/macro'

import { CowHookDetailsSerialized, HookDapp } from '../../types/hooks'
import { HookDapp } from '../../types/hooks'
import { findHookDappById } from '../../utils'
import { AppliedHookItem } from '../AppliedHookItem'

Expand All @@ -19,7 +21,7 @@ const HookList = styled.ul`
interface AppliedHookListProps {
account: string | undefined
dapps: HookDapp[]
hooks: CowHookDetailsSerialized[]
hooks: CowHookDetails[]
isPreHook: boolean
removeHook: (uuid: string, isPreHook: boolean) => void
editHook: (uuid: string) => void
Expand Down Expand Up @@ -68,7 +70,7 @@ export function AppliedHookList({
{hooks.map((hookDetails, index) => {
return (
<AppliedHookItem
key={hookDetails.hookDetails.uuid}
key={hookDetails.uuid}
dapp={findHookDappById(dapps, hookDetails)}
index={index}
account={account}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ export const removeCustomHookDappAtom = atom(null, (get, set, dapp: HookDappIfra

// Delete applied hooks along with the deleting hook-dapp
set(setHooksAtom, (hooksState) => ({
preHooks: (hooksState.preHooks || []).filter((hook) => hook.dappId !== hookDappId),
postHooks: (hooksState.postHooks || []).filter((hook) => hook.dappId !== hookDappId),
preHooks: (hooksState.preHooks || []).filter((hookDetails) => hookDetails.hook.dappId !== hookDappId),
postHooks: (hooksState.postHooks || []).filter((hookDetails) => hookDetails.hook.dappId !== hookDappId),
}))
})
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,12 @@ import { atomWithStorage } from 'jotai/utils'

import { getJotaiIsolatedStorage } from '@cowprotocol/core'
import { mapSupportedNetworks, SupportedChainId } from '@cowprotocol/cow-sdk'
import { CowHookDetails } from '@cowprotocol/hook-dapp-lib'
import { walletInfoAtom } from '@cowprotocol/wallet'

import { CowHookDetailsSerialized } from '../types/hooks'

export type HooksStoreState = {
preHooks: CowHookDetailsSerialized[]
postHooks: CowHookDetailsSerialized[]
preHooks: CowHookDetails[]
postHooks: CowHookDetails[]
}

type StatePerAccount = Record<string, HooksStoreState>
Expand All @@ -21,7 +20,7 @@ const EMPTY_STATE: HooksStoreState = {
}

const hooksAtomInner = atomWithStorage<StatePerNetwork>(
'hooksStoreAtom:v2',
'hooksStoreAtom:v3',
mapSupportedNetworks({}),
getJotaiIsolatedStorage(),
)
Expand Down
6 changes: 0 additions & 6 deletions apps/cowswap-frontend/src/modules/hooksStore/types/hooks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import {
HookDappOrderParams,
CoWHookDappActions,
HookDappContext as GenericHookDappContext,
CowHookDetails,
HookDappBase,
HookDappType,
} from '@cowprotocol/hook-dapp-lib'
Expand All @@ -26,11 +25,6 @@ export interface HookDappIframe extends HookDappBase {

export type HookDapp = HookDappInternal | HookDappIframe

export interface CowHookDetailsSerialized {
hookDetails: CowHookDetails
dappId: string
}

export type AddHook = CoWHookDappActions['addHook']
export type EditHook = CoWHookDappActions['editHook']
export type RemoveHook = (uuid: string) => void
Expand Down
12 changes: 4 additions & 8 deletions apps/cowswap-frontend/src/modules/hooksStore/utils.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,12 @@
import { HookDappType } from '@cowprotocol/hook-dapp-lib'
import { CowHookDetails, HookDappType } from '@cowprotocol/hook-dapp-lib'

import { CowHookDetailsSerialized, HookDapp, HookDappIframe } from './types/hooks'
import { HookDapp, HookDappIframe } from './types/hooks'

// Do a safe guard assertion that receives a HookDapp and asserts is a HookDappIframe
export function isHookDappIframe(dapp: HookDapp): dapp is HookDappIframe {
return dapp.type === HookDappType.IFRAME
}

export function findHookDappById(dapps: HookDapp[], hookDetails: CowHookDetailsSerialized): HookDapp | undefined {
return dapps.find((i) => i.id === hookDetails.dappId)
}

export function appendDappIdToCallData(callData: string, dappId: string): string {
return callData.endsWith(dappId) ? callData : callData + dappId
export function findHookDappById(dapps: HookDapp[], hookDetails: CowHookDetails): HookDapp | undefined {
return dapps.find((i) => i.id === hookDetails.hook.dappId)
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@

exports[`useTradeQuotePolling() When wallet is NOT connected Then the "useAddress" field in the quote request should be 0x000...0000 1`] = `
{
"appData": "{"version":"1.2.0","appCode":"CoW Swap","metadata":{}}",
"appDataHash": "0xf7a2879636a64a86e7e292deea11c301ee241404f195f38c94798665509209ff",
"appData": "{"version":"1.3.0","appCode":"CoW Swap","metadata":{}}",
"appDataHash": "0x2b75dd3fccea5d141e8782354d789fb147bbf51a454c2cd0384ad2d76216116e",
"buyToken": "0x0625aFB445C3B6B7B929342a04A22599fd5dBB59",
"from": "0x0000000000000000000000000000000000000000",
"kind": "sell",
Expand All @@ -18,8 +18,8 @@ exports[`useTradeQuotePolling() When wallet is NOT connected Then the "useAddres

exports[`useTradeQuotePolling() When wallet is connected Then should put account address into "useAddress" field in the quote request 1`] = `
{
"appData": "{"version":"1.2.0","appCode":"CoW Swap","metadata":{}}",
"appDataHash": "0xf7a2879636a64a86e7e292deea11c301ee241404f195f38c94798665509209ff",
"appData": "{"version":"1.3.0","appCode":"CoW Swap","metadata":{}}",
"appDataHash": "0x2b75dd3fccea5d141e8782354d789fb147bbf51a454c2cd0384ad2d76216116e",
"buyToken": "0x0625aFB445C3B6B7B929342a04A22599fd5dBB59",
"from": "0x333333f332a06ecb5d20d35da44ba07986d6e203",
"kind": "sell",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ exports[`Create TWAP order When sell token is NOT approved AND token needs zero
{
"handler": "0x6cF1e9cA41f7611dEf408122793c358a3d11E5a5",
"salt": "0x00000000000000000000000000000000000000000000000000000015c90b9b2a",
"staticInput": "0x0000000000000000000000000625afb445c3b6b7b929342a04a22599fd5dbb59000000000000000000000000fff9976782d46cc05630d1f6ebab18b2324d6b14000000000000000000000000b4fbf271143f4fbf7b91a5ded31805e42b2208d600000000000000000000000000000000000000000000000000000007c2d24d55000000000000000000000000000000000000000000000000000000000001046a00000000000000000000000000000000000000000000000000000000646b782c000000000000000000000000000000000000000000000000000000000000000300000000000000000000000000000000000000000000000000000000000002580000000000000000000000000000000000000000000000000000000000000000f7a2879636a64a86e7e292deea11c301ee241404f195f38c94798665509209ff",
"staticInput": "0x0000000000000000000000000625afb445c3b6b7b929342a04a22599fd5dbb59000000000000000000000000fff9976782d46cc05630d1f6ebab18b2324d6b14000000000000000000000000b4fbf271143f4fbf7b91a5ded31805e42b2208d600000000000000000000000000000000000000000000000000000007c2d24d55000000000000000000000000000000000000000000000000000000000001046a00000000000000000000000000000000000000000000000000000000646b782c0000000000000000000000000000000000000000000000000000000000000003000000000000000000000000000000000000000000000000000000000000025800000000000000000000000000000000000000000000000000000000000000002b75dd3fccea5d141e8782354d789fb147bbf51a454c2cd0384ad2d76216116e",
},
"0x52eD56Da04309Aca4c3FECC595298d80C2f16BAc",
"0x",
Expand Down Expand Up @@ -66,7 +66,7 @@ exports[`Create TWAP order When sell token is NOT approved, then should generate
{
"handler": "0x6cF1e9cA41f7611dEf408122793c358a3d11E5a5",
"salt": "0x00000000000000000000000000000000000000000000000000000015c90b9b2a",
"staticInput": "0x0000000000000000000000000625afb445c3b6b7b929342a04a22599fd5dbb59000000000000000000000000fff9976782d46cc05630d1f6ebab18b2324d6b14000000000000000000000000b4fbf271143f4fbf7b91a5ded31805e42b2208d600000000000000000000000000000000000000000000000000000007c2d24d55000000000000000000000000000000000000000000000000000000000001046a00000000000000000000000000000000000000000000000000000000646b782c000000000000000000000000000000000000000000000000000000000000000300000000000000000000000000000000000000000000000000000000000002580000000000000000000000000000000000000000000000000000000000000000f7a2879636a64a86e7e292deea11c301ee241404f195f38c94798665509209ff",
"staticInput": "0x0000000000000000000000000625afb445c3b6b7b929342a04a22599fd5dbb59000000000000000000000000fff9976782d46cc05630d1f6ebab18b2324d6b14000000000000000000000000b4fbf271143f4fbf7b91a5ded31805e42b2208d600000000000000000000000000000000000000000000000000000007c2d24d55000000000000000000000000000000000000000000000000000000000001046a00000000000000000000000000000000000000000000000000000000646b782c0000000000000000000000000000000000000000000000000000000000000003000000000000000000000000000000000000000000000000000000000000025800000000000000000000000000000000000000000000000000000000000000002b75dd3fccea5d141e8782354d789fb147bbf51a454c2cd0384ad2d76216116e",
},
"0x52eD56Da04309Aca4c3FECC595298d80C2f16BAc",
"0x",
Expand Down Expand Up @@ -109,7 +109,7 @@ exports[`Create TWAP order When sell token is approved, then should generate onl
{
"handler": "0x6cF1e9cA41f7611dEf408122793c358a3d11E5a5",
"salt": "0x00000000000000000000000000000000000000000000000000000015c90b9b2a",
"staticInput": "0x0000000000000000000000000625afb445c3b6b7b929342a04a22599fd5dbb59000000000000000000000000fff9976782d46cc05630d1f6ebab18b2324d6b14000000000000000000000000b4fbf271143f4fbf7b91a5ded31805e42b2208d600000000000000000000000000000000000000000000000000000007c2d24d55000000000000000000000000000000000000000000000000000000000001046a00000000000000000000000000000000000000000000000000000000646b782c000000000000000000000000000000000000000000000000000000000000000300000000000000000000000000000000000000000000000000000000000002580000000000000000000000000000000000000000000000000000000000000000f7a2879636a64a86e7e292deea11c301ee241404f195f38c94798665509209ff",
"staticInput": "0x0000000000000000000000000625afb445c3b6b7b929342a04a22599fd5dbb59000000000000000000000000fff9976782d46cc05630d1f6ebab18b2324d6b14000000000000000000000000b4fbf271143f4fbf7b91a5ded31805e42b2208d600000000000000000000000000000000000000000000000000000007c2d24d55000000000000000000000000000000000000000000000000000000000001046a00000000000000000000000000000000000000000000000000000000646b782c0000000000000000000000000000000000000000000000000000000000000003000000000000000000000000000000000000000000000000000000000000025800000000000000000000000000000000000000000000000000000000000000002b75dd3fccea5d141e8782354d789fb147bbf51a454c2cd0384ad2d76216116e",
},
"0x52eD56Da04309Aca4c3FECC595298d80C2f16BAc",
"0x",
Expand Down
Loading

0 comments on commit 46699cb

Please sign in to comment.