Skip to content

Commit c0ee322

Browse files
shoom3301anxolin
andauthored
refactor(hooks-store): use readable string as hook-dapp id (#5093)
Co-authored-by: Anxo Rodriguez <[email protected]>
1 parent 041c53c commit c0ee322

File tree

3 files changed

+41
-31
lines changed

3 files changed

+41
-31
lines changed
Lines changed: 18 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,24 @@
1-
import { hookDappsRegistry } from '@cowprotocol/hook-dapp-lib'
1+
import { HookDappBase, hookDappsRegistry } from '@cowprotocol/hook-dapp-lib'
22

33
import { AirdropHookApp } from './dapps/AirdropHookApp'
44
import { BuildHookApp } from './dapps/BuildHookApp'
55
import { ClaimGnoHookApp } from './dapps/ClaimGnoHookApp'
66
import { PermitHookApp } from './dapps/PermitHookApp'
7-
import { HookDapp } from './types/hooks'
7+
import { HookDapp, HookDappInternal } from './types/hooks'
88

9-
export const ALL_HOOK_DAPPS = [
10-
{
11-
...hookDappsRegistry.BUILD_CUSTOM_HOOK,
12-
component: (props) => <BuildHookApp {...props} />,
13-
},
14-
{
15-
...hookDappsRegistry.CLAIM_GNO_FROM_VALIDATORS,
16-
component: (props) => <ClaimGnoHookApp {...props} />,
17-
},
18-
{
19-
...hookDappsRegistry.PERMIT_TOKEN,
20-
component: (props) => <PermitHookApp {...props} />,
21-
},
22-
{
23-
...hookDappsRegistry.CLAIM_COW_AIRDROP,
24-
component: (props) => <AirdropHookApp {...props} />,
25-
},
26-
hookDappsRegistry.COW_AMM_WITHDRAW,
27-
hookDappsRegistry.CLAIM_LLAMAPAY_VESTING,
28-
hookDappsRegistry.CREATE_LLAMAPAY_VESTING
29-
] as HookDapp[]
9+
const HOOK_DAPPS_OVERRIDES: Record<string, Partial<HookDappInternal>> = {
10+
BUILD_CUSTOM_HOOK: { component: (props) => <BuildHookApp {...props} /> },
11+
CLAIM_GNO_FROM_VALIDATORS: { component: (props) => <ClaimGnoHookApp {...props} /> },
12+
PERMIT_TOKEN: { component: (props) => <PermitHookApp {...props} /> },
13+
CLAIM_COW_AIRDROP: { component: (props) => <AirdropHookApp {...props} /> },
14+
}
15+
16+
export const ALL_HOOK_DAPPS = Object.keys(hookDappsRegistry).map((id) => {
17+
const item = (hookDappsRegistry as Record<string, Omit<HookDappBase, 'id'>>)[id]
18+
19+
return {
20+
...item,
21+
...HOOK_DAPPS_OVERRIDES[id],
22+
id,
23+
}
24+
}) as HookDapp[]

libs/hook-dapp-lib/src/hookDappsRegistry.json

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
{
22
"BUILD_CUSTOM_HOOK": {
3-
"id": "c768665aa144bcf18c14eea0249b6322050e5daeba046d7e94df743a2e504586",
43
"type": "INTERNAL",
54
"name": "Build your own hook",
65
"descriptionShort": "Call any smart contract with your own parameters",
@@ -10,7 +9,6 @@
109
"website": "https://docs.cow.fi/cow-protocol/tutorials/hook-dapp"
1110
},
1211
"CLAIM_GNO_FROM_VALIDATORS": {
13-
"id": "ee4a6b1065cda592972b9ff7448ec111f29a566f137fef101ead7fbf8b01dd0b",
1412
"type": "INTERNAL",
1513
"name": "Claim GNO from validators",
1614
"descriptionShort": "Withdraw rewards from your Gnosis validators.",
@@ -24,7 +22,6 @@
2422
}
2523
},
2624
"PERMIT_TOKEN": {
27-
"id": "1db4bacb661a90fb6b475fd5b585acba9745bc373573c65ecc3e8f5bfd5dee1f",
2825
"type": "INTERNAL",
2926
"name": "Permit a token",
3027
"descriptionShort": "Infinite permit an address to spend one token on your behalf.",
@@ -38,7 +35,6 @@
3835
}
3936
},
4037
"CLAIM_COW_AIRDROP": {
41-
"id": "40ed08569519f3b58c410ba35a8e684612663a7c9b58025e0a9c3a54551fb0ff",
4238
"type": "INTERNAL",
4339
"name": "Claim COW Airdrop",
4440
"descriptionShort": "Retrieve COW tokens before or after a swap.",
@@ -51,7 +47,6 @@
5147
}
5248
},
5349
"COW_AMM_WITHDRAW": {
54-
"id": "5dc71fa5829d976c462bdf37b38b6fd9bbc289252a5a18e61525f8c8a3c775df",
5550
"name": "CoW AMM Withdraw Liquidity",
5651
"type": "IFRAME",
5752
"descriptionShort": "Remove liquidity from a CoW AMM pool before the swap",
@@ -67,7 +62,6 @@
6762
}
6863
},
6964
"CLAIM_LLAMAPAY_VESTING": {
70-
"id": "5d2c081d11a01ca0b76e2fafbc0d3c62a4c9945ce404706fb1e49e826c0f99eb",
7165
"type": "IFRAME",
7266
"name": "Claim LlamaPay Vesting Hook",
7367
"description": "The Claim LlamaPay Vesting Hook is a powerful and user-friendly feature designed to streamline the process of claiming funds from LlamaPay vesting contracts. This tool empowers users to easily access and manage their vested tokens, ensuring a smooth and efficient experience in handling time-locked assets.",
@@ -83,7 +77,6 @@
8377
}
8478
},
8579
"CREATE_LLAMAPAY_VESTING": {
86-
"id": "a316488cecc23fde8c39d3e748e0cb12bd4d18305826d36576d4bba74bd97baf",
8780
"type": "IFRAME",
8881
"name": "Create LlamaPay Vesting",
8982
"descriptionShort": "Create a LlamaPay vesting contract",

libs/hook-dapp-lib/src/utils.ts

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,18 @@
11
import * as hookDappsRegistry from './hookDappsRegistry.json'
22
import { CowHook, HookDappBase } from './types'
33

4+
const hookDapps = Object.keys(hookDappsRegistry).reduce((acc, id) => {
5+
const dapp = (hookDappsRegistry as Record<string, Omit<HookDappBase, 'id'>>)[id]
6+
7+
acc.push({ id, ...dapp })
8+
return acc
9+
}, [] as HookDappBase[])
10+
11+
// permit() function selector
12+
const PERMIT_SELECTOR = '0xd505accf'
13+
// TODO: remove it after 01.01.2025
14+
const PERMIT_DAPP_ID = '1db4bacb661a90fb6b475fd5b585acba9745bc373573c65ecc3e8f5bfd5dee1f'
15+
416
// Before the hooks store the dappId wasn't included in the hook object
517
type StrictCowHook = Omit<CowHook, 'dappId'> & { dappId?: string }
618

@@ -26,6 +38,16 @@ export function matchHooksToDapps(hooks: StrictCowHook[], dapps: HookDappBase[])
2638
const hook = _hook as CowHook
2739
const dapp = dappsMap[hook.dappId]
2840

41+
/**
42+
* Permit token is a special case, as it's not a dapp, but a hook
43+
*/
44+
if ((!dapp || hook.dappId === PERMIT_DAPP_ID) && hook.callData.startsWith(PERMIT_SELECTOR)) {
45+
return {
46+
hook,
47+
dapp: hookDappsRegistry.PERMIT_TOKEN as HookDappBase,
48+
}
49+
}
50+
2951
return {
3052
hook,
3153
dapp: dapp || null,
@@ -38,5 +60,5 @@ export function matchHooksToDappsRegistry(
3860
hooks: StrictCowHook[],
3961
additionalHookDapps: HookDappBase[] = [],
4062
): HookToDappMatch[] {
41-
return matchHooksToDapps(hooks, (Object.values(hookDappsRegistry) as HookDappBase[]).concat(additionalHookDapps))
63+
return matchHooksToDapps(hooks, hookDapps.concat(additionalHookDapps))
4264
}

0 commit comments

Comments
 (0)