@cowprotocol/hook-dapp-lib 1.7.1 → 2.0.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -27,10 +27,6 @@ And it expects calling two callbacks:
27
27
  npm install @cowprotocol/hook-dapp-lib --save
28
28
  ```
29
29
 
30
- ```
31
- yarn add @cowprotocol/hook-dapp-lib
32
- ```
33
-
34
30
  Let's create a simple hook-dapp that will check the COW token balance of the order creator.
35
31
 
36
32
  ```html
package/demo.png ADDED
Binary file
package/jest.config.ts ADDED
@@ -0,0 +1,10 @@
1
+ export default {
2
+ displayName: 'hook-dapp-lib',
3
+ preset: '../../jest.preset.js',
4
+ transform: {
5
+ '^.+\\.[tj]s$': ['ts-jest', { tsconfig: '<rootDir>/tsconfig.spec.json' }],
6
+ },
7
+ setupFilesAfterEnv: ['../../jest.setup.ts'],
8
+ moduleFileExtensions: ['ts', 'js', 'html'],
9
+ coverageDirectory: '../../../coverage/libs/hook-dapp-lib',
10
+ }
package/package.json CHANGED
@@ -1,26 +1,15 @@
1
1
  {
2
2
  "name": "@cowprotocol/hook-dapp-lib",
3
- "version": "1.7.1",
4
- "type": "commonjs",
3
+ "version": "2.0.2",
5
4
  "description": "CoW Swap Hook Dapp Library. Allows you to develop pre/post hooks dapps for CoW Protocol.",
6
- "main": "index.js",
7
- "exports": {
8
- "import": "./index.js",
9
- "require": "./index.mjs"
10
- },
5
+ "type": "commonjs",
6
+ "main": "./src/index.ts",
7
+ "types": "./src/index.d.ts",
11
8
  "license": "ISC",
12
- "repository": {
13
- "type": "git",
14
- "url": "https://github.com/cowprotocol/cowswap"
9
+ "publishConfig": {
10
+ "access": "public"
15
11
  },
16
- "keywords": [
17
- "cowprotocol",
18
- "hooks",
19
- "cowswap",
20
- "dex",
21
- "hook-dapp-lib"
22
- ],
23
12
  "dependencies": {
24
- "@cowprotocol/iframe-transport": "^1.0.0"
13
+ "@cowprotocol/iframe-transport": "2.0.2"
25
14
  }
26
- }
15
+ }
package/project.json ADDED
@@ -0,0 +1,51 @@
1
+ {
2
+ "name": "hook-dapp-lib",
3
+ "$schema": "../../node_modules/nx/schemas/project-schema.json",
4
+ "sourceRoot": "libs/hook-dapp-lib/src",
5
+ "projectType": "library",
6
+ "targets": {
7
+ "build": {
8
+ "executor": "@nx/js:tsc",
9
+ "outputs": ["{options.outputPath}"],
10
+ "options": {
11
+ "outputPath": "dist/libs/hook-dapp-lib",
12
+ "main": "libs/hook-dapp-lib/src/index.ts",
13
+ "tsConfig": "libs/hook-dapp-lib/tsconfig.lib.json",
14
+ "assets": ["libs/hook-dapp-lib/*.md"],
15
+ "updateBuildableProjectDepsInPackageJson": true,
16
+ "buildableProjectDepsInPackageJsonType": "dependencies"
17
+ }
18
+ },
19
+ "serve": {
20
+ "executor": "@nx/vite:dev-server",
21
+ "defaultConfiguration": "development",
22
+ "options": {
23
+ "buildTarget": "hook-dapp-lib:build",
24
+ "port": 3012
25
+ },
26
+ "configurations": {}
27
+ },
28
+ "lint": {
29
+ "executor": "@nx/eslint:lint",
30
+ "outputs": ["{options.outputFile}"],
31
+ "options": {
32
+ "lintFilePatterns": ["libs/hook-dapp-lib/**/*.ts"]
33
+ }
34
+ },
35
+ "test": {
36
+ "executor": "@nx/jest:jest",
37
+ "outputs": ["{workspaceRoot}/coverage/{projectRoot}"],
38
+ "options": {
39
+ "jestConfig": "libs/hook-dapp-lib/jest.config.ts",
40
+ "passWithNoTests": true
41
+ },
42
+ "configurations": {
43
+ "ci": {
44
+ "ci": true,
45
+ "codeCoverage": true
46
+ }
47
+ }
48
+ }
49
+ },
50
+ "tags": []
51
+ }
package/src/consts.ts ADDED
@@ -0,0 +1,13 @@
1
+ export enum HookDappType {
2
+ INTERNAL = 'INTERNAL',
3
+ IFRAME = 'IFRAME',
4
+ }
5
+
6
+ export enum HookDappWalletCompatibility {
7
+ EOA = 'EOA',
8
+ SMART_CONTRACT = 'SMART_CONTRACT',
9
+ }
10
+
11
+ export const HOOK_DAPP_ID_LENGTH = 64
12
+
13
+ export const PERMIT_HOOK_DAPP_ID = 'cow-swap://libs/hook-dapp-lib/permit'
@@ -0,0 +1,23 @@
1
+ import { IframeTransport } from '@cowprotocol/iframe-transport'
2
+
3
+ import { CowHookCreation, CowHookDetails, HookDappContext, TokenData } from './types'
4
+
5
+ export enum CoWHookDappEvents {
6
+ ACTIVATE = 'ACTIVATE',
7
+ CONTEXT_UPDATE = 'CONTEXT_UPDATE',
8
+ ADD_HOOK = 'ADD_HOOK',
9
+ EDIT_HOOK = 'EDIT_HOOK',
10
+ SET_SELL_TOKEN = 'SET_SELL_TOKEN',
11
+ SET_BUY_TOKEN = 'SET_BUY_TOKEN',
12
+ }
13
+
14
+ export interface HookDappEventsPayloadMap {
15
+ [CoWHookDappEvents.ACTIVATE]: void
16
+ [CoWHookDappEvents.CONTEXT_UPDATE]: HookDappContext
17
+ [CoWHookDappEvents.ADD_HOOK]: CowHookCreation
18
+ [CoWHookDappEvents.EDIT_HOOK]: CowHookDetails
19
+ [CoWHookDappEvents.SET_SELL_TOKEN]: TokenData
20
+ [CoWHookDappEvents.SET_BUY_TOKEN]: TokenData
21
+ }
22
+
23
+ export const hookDappIframeTransport = new IframeTransport<HookDappEventsPayloadMap>('cowSwapHookDapp')
@@ -0,0 +1,209 @@
1
+ import { PERMIT_HOOK_DAPP_ID } from './consts'
2
+
3
+ export const hookDappsRegistry = {
4
+ BUILD_CUSTOM_HOOK: {
5
+ type: 'INTERNAL',
6
+ name: `Build your own hook`,
7
+ descriptionShort: `Call any smart contract with your own parameters`,
8
+ description: `Didn't find a suitable hook? You can always create your own! To do this, you need to specify which smart contract you want to call, the parameters for the call and the gas limit.`,
9
+ image:
10
+ 'https://raw.githubusercontent.com/cowprotocol/cowswap/refs/heads/develop/apps/cowswap-frontend/src/modules/hooksStore/dapps/BuildHookApp/build.png',
11
+ version: 'v0.1.0',
12
+ website: 'https://docs.cow.fi/cow-protocol/tutorials/hook-dapp',
13
+ },
14
+ CLAIM_GNO_FROM_VALIDATORS: {
15
+ type: 'INTERNAL',
16
+ name: `Claim GNO from validators`,
17
+ descriptionShort: `Withdraw rewards from your Gnosis validators.`,
18
+ description: `This hook allows you to withdraw rewards from your Gnosis Chain validators through CoW Swap. It automates the process of interacting with the Gnosis Deposit Contract, enabling you to claim any available rewards directly to your specified withdrawal address. The hook monitors your validator's accrued rewards and triggers the claimWithdrawals function when rewards are ready for withdrawal. This simplifies the management of Gnosis validator earnings without requiring ready for withdrawal. This simplifies the management of Gnosis validator earnings without requiring manual contract interaction, providing a smoother and more efficient experience for users.`,
19
+ image:
20
+ 'https://raw.githubusercontent.com/cowprotocol/cowswap/897ce91ca60a6b2d3823e6a002c3bf64c5384afe/libs/assets/src/cow-swap/network-gnosis-chain-logo.svg',
21
+ version: 'v0.1.1',
22
+ website: 'https://www.gnosis.io',
23
+ conditions: {
24
+ supportedNetworks: [100],
25
+ position: 'pre',
26
+ },
27
+ },
28
+ [PERMIT_HOOK_DAPP_ID]: {
29
+ type: 'INTERNAL',
30
+ name: `Permit a token`,
31
+ descriptionShort: `Permit an address to spend one token on your behalf.`,
32
+ description: `This hook allows you to permit an address to spend your tokens on your behalf. This is useful for allowing a smart contract to spend your tokens without needing to approve each transaction.`,
33
+ image:
34
+ 'https://raw.githubusercontent.com/cowprotocol/cowswap/refs/heads/develop/apps/cowswap-frontend/src/modules/hooksStore/dapps/PermitHookApp/icon.png',
35
+ version: 'v0.1.0',
36
+ website: 'https://docs.cow.fi/cow-protocol/tutorials/hook-dapp',
37
+ conditions: {
38
+ walletCompatibility: ['EOA'],
39
+ supportedNetworks: [11155111],
40
+ },
41
+ },
42
+ CLAIM_COW_AIRDROP: {
43
+ type: 'INTERNAL',
44
+ name: `Claim COW Airdrop`,
45
+ descriptionShort: `Retrieve COW tokens before or after a swap.`,
46
+ description: `Effortless Airdrop Claims! The Claim COW Airdrop feature simplifies the process of collecting free COW tokens before or after your swap, seamlessly integrating into the CoW Swap platform. Whether you're claiming new airdrops or exploring CoW on a new network, this tool ensures you get your rewards quickly and easily.`,
47
+ image:
48
+ 'https://raw.githubusercontent.com/cowprotocol/cowswap/897ce91ca60a6b2d3823e6a002c3bf64c5384afe/apps/cowswap-frontend/src/modules/hooksStore/dapps/AirdropHookApp/airdrop.svg',
49
+ version: 'v0.1.0',
50
+ website: 'https://github.com/bleu/cow-airdrop-contract-deployer',
51
+ conditions: {
52
+ supportedNetworks: [11155111],
53
+ },
54
+ },
55
+ COW_AMM_WITHDRAW: {
56
+ name: `CoW AMM Withdraw Liquidity`,
57
+ type: 'IFRAME',
58
+ descriptionShort: `Remove liquidity from a CoW AMM pool before the swap`,
59
+ description: `Reduce or withdraw liquidity from a pool before a token swap integrating the process directly into the transaction flow. By adjusting your liquidity ahead of time, you gain more control over your assets without any extra steps. Optimize your position in a pool, all in one seamless action — no need for multiple transactions or added complexity.`,
60
+ version: '0.0.1',
61
+ website: 'https://balancer.fi/pools/cow',
62
+ image: 'https://cow-hooks-dapps-withdraw-pool.vercel.app/icon.png',
63
+ url: 'https://cow-hooks-dapps-withdraw-pool.vercel.app/',
64
+ conditions: {
65
+ position: 'pre',
66
+ walletCompatibility: ['EOA'],
67
+ supportedNetworks: [1, 100, 8453, 42161, 11155111],
68
+ },
69
+ },
70
+ CLAIM_LLAMAPAY_VESTING: {
71
+ type: 'IFRAME',
72
+ name: `Claim LlamaPay Vesting Hook`,
73
+ 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.`,
74
+ descriptionShort: `Claim your LlamaPay vesting contract funds`,
75
+ image: 'https://cow-hooks-dapps-claim-vesting.vercel.app/llama-pay-icon.png',
76
+ version: '0.1.0',
77
+ website: 'https://github.com/bleu/cow-hooks-dapps',
78
+ url: 'https://cow-hooks-dapps-claim-vesting.vercel.app',
79
+ conditions: {
80
+ supportedNetworks: [1, 100, 42161],
81
+ walletCompatibility: ['EOA'],
82
+ },
83
+ },
84
+ CREATE_LLAMAPAY_VESTING: {
85
+ type: 'IFRAME',
86
+ name: `Create LlamaPay Vesting`,
87
+ descriptionShort: `Create a LlamaPay vesting contract`,
88
+ description: `This hook allows you to easily set up vesting contracts with LlamaPay. Enter the recipient’s address or ENS name, then choose how much to transfer: the token buy will be automatically detected by the hook and the contracts will be linked to your LlamaPay dashboard for seamless tracking.`,
89
+ version: '0.1.0',
90
+ website: 'https://llamapay.io/vesting',
91
+ image: 'https://cow-hooks-dapps-create-vesting.vercel.app/llama-pay-icon.png',
92
+ url: 'https://cow-hooks-dapps-create-vesting.vercel.app',
93
+ conditions: {
94
+ position: 'post',
95
+ walletCompatibility: ['EOA'],
96
+ supportedNetworks: [1, 100, 42161],
97
+ },
98
+ },
99
+ UNI_V2_WITHDRAW: {
100
+ type: 'IFRAME',
101
+ name: `Withdraw Uniswap v2 Liquidity`,
102
+ descriptionShort: `Remove liquidity from a Uniswap v2 Weighted pool before the swap`,
103
+ description: `Reduce or withdraw Uniswap v2 from a pool before a token swap integrating the process directly into the transaction flow. By adjusting your liquidity ahead of time, you gain more control over your assets without any extra steps. Optimize your position in a pool, all in one seamless action — no need for multiple transactions or added complexity.`,
104
+ version: '0.0.1',
105
+ website: 'https://app.uniswap.org/pools/v2',
106
+ image: 'https://cow-hooks-dapps-withdraw-uniswap-v2.vercel.app/icon.png',
107
+ url: 'https://cow-hooks-dapps-withdraw-uniswap-v2.vercel.app/',
108
+ conditions: {
109
+ position: 'pre',
110
+ walletCompatibility: ['EOA'],
111
+ supportedNetworks: [1, 42161, 11155111, 8453],
112
+ },
113
+ },
114
+ COW_AMM_DEPOSIT: {
115
+ name: `CoW AMM Deposit`,
116
+ type: 'IFRAME',
117
+ descriptionShort: `Add liquidity to any of the CoW AMM pools`,
118
+ description: `Easily increase your position in a liquidity pool, adding both tokens in the right ratio.\n\n Whether you're seeking to increase your exposure to specific tokens or maximize the returns from your assets, this tool offers a quick and efficient way to enhance your liquidity position after a token swap, all in a smooth, efficient process.`,
119
+ version: '0.0.1',
120
+ website: 'https://balancer.fi/pools/cow',
121
+ image: 'https://cow-hooks-dapps-cow-amm-deposit.vercel.app/icon.png',
122
+ url: 'https://cow-hooks-dapps-cow-amm-deposit.vercel.app/',
123
+ conditions: {
124
+ position: 'post',
125
+ walletCompatibility: ['EOA'],
126
+ supportedNetworks: [1, 100, 8453, 42161, 11155111],
127
+ },
128
+ },
129
+ UNI_V2_DEPOSIT: {
130
+ name: `Uniswap v2 Deposit`,
131
+ type: 'IFRAME',
132
+ descriptionShort: `Add liquidity to a Uniswap v2 pool after the swap`,
133
+ description: `Easily increase your position in a liquidity pool, adding both tokens in the right ratio.\n\n Whether you're seeking to increase your exposure to specific tokens or maximize the returns from your assets, this tool offers a quick and efficient way to enhance your liquidity position after a token swap, all in a smooth, efficient process.`,
134
+ version: '0.0.1',
135
+ website: 'https://app.uniswap.org/pools/v2',
136
+ image: 'https://cow-hooks-dapps-9zio.vercel.app/icon.png',
137
+ url: 'https://cow-hooks-dapps-9zio.vercel.app/',
138
+ conditions: {
139
+ position: 'post',
140
+ walletCompatibility: ['EOA'],
141
+ supportedNetworks: [1, 42161, 11155111, 8453],
142
+ },
143
+ },
144
+ MORPHO_BORROW: {
145
+ name: `Morpho Borrow`,
146
+ type: 'IFRAME',
147
+ descriptionShort: `Supply collateral, borrow, repay and withdraw collateral on Morpho Protocol.`,
148
+ description: `The Morpho Borrow hook enables users to seamlessly combine swaps with borrow-related actions. Users can enter new positions, leave existing ones, or move between different markets through a unified, streamlined process.`,
149
+ version: '0.0.1',
150
+ website: 'https://app.morpho.org/',
151
+ image: 'https://cow-hooks-dapps-morpho-lending.vercel.app/icon.png',
152
+ url: 'https://cow-hooks-dapps-morpho-lending.vercel.app/',
153
+ conditions: {
154
+ walletCompatibility: ['EOA'],
155
+ supportedNetworks: [1, 137, 8453],
156
+ },
157
+ },
158
+ 'cow-sdk://bridging/providers/bungee': {
159
+ name: `Bungee`,
160
+ type: 'INTERNAL',
161
+ descriptionShort: `Bungee Protocol - Swap tokens across chains`,
162
+ description: `Bungee is a liquidity marketplace that lets you swap into any token on any chain in a fully abstracted manner. Trade any token with the best quotes and a gasless UX!`,
163
+ version: '0.0.1',
164
+ website: 'https://www.bungee.exchange',
165
+ image: 'https://files.cow.fi/cow-sdk/bridging/providers/bungee/bungee-logo.png',
166
+ conditions: {
167
+ walletCompatibility: ['EOA'],
168
+ },
169
+ },
170
+ 'cow-sdk://bridging/providers/across': {
171
+ name: `Across`,
172
+ type: 'INTERNAL',
173
+ descriptionShort: `Across Protocol - Transfer Assets Between Layer 2s and Mainnet`,
174
+ description: `Across is the fastest, cheapest and most secure cross-chain bridge for Ethereum, Arbitrum, Optimism, Polygon and other Layer 1 and Layer 2 networks. Transfer tokens with Across.`,
175
+ version: '0.0.1',
176
+ website: 'https://across.to',
177
+ image: 'https://files.cow.fi/cow-sdk/bridging/providers/across/across-logo.png',
178
+ conditions: {
179
+ walletCompatibility: ['EOA'],
180
+ },
181
+ },
182
+ 'cow-sdk://flashloans/aave/v3/collateral-swap': {
183
+ name: `Aave Collateral Swap`,
184
+ type: 'INTERNAL',
185
+ descriptionShort: `Aave Collateral Swap Flashloan`,
186
+ description: `The swap adapter contracts integrate Aave Flash Loans and CoW Swap to facilitate advanced actions like swapping collateral assets when there are borrow positions. Learn more at https://aave.com/docs/developers/smart-contracts/swap-features.`,
187
+ version: '0.0.1',
188
+ website: 'https://aave.com',
189
+ image: 'https://files.cow.fi/token-lists/images/1/0x7fc66500c84a76ad7e9c93437bfc5ac33e2ddae9/logo.png',
190
+ },
191
+ 'cow-sdk://flashloans/aave/v3/debt-swap': {
192
+ name: `Aave Debt Swap`,
193
+ type: 'INTERNAL',
194
+ descriptionShort: `Aave Debt Swap Flashloan`,
195
+ description: `The swap adapter contracts integrate Aave Flash Loans and CoW Swap to facilitate advanced actions like swapping debt assets. Learn more at https://aave.com/docs/developers/smart-contracts/swap-features.`,
196
+ version: '0.0.1',
197
+ website: 'https://aave.com',
198
+ image: 'https://files.cow.fi/token-lists/images/1/0x7fc66500c84a76ad7e9c93437bfc5ac33e2ddae9/logo.png',
199
+ },
200
+ 'cow-sdk://flashloans/aave/v3/repay-with-collateral': {
201
+ name: `Aave Repay`,
202
+ type: 'INTERNAL',
203
+ descriptionShort: `Aave Repay with Collateral Flashloan`,
204
+ description: `The swap adapter contracts integrate Aave Flash Loans and CoW Swap to facilitate advanced actions like repaying borrow positions using collateral. Learn more at https://aave.com/docs/developers/smart-contracts/swap-features.`,
205
+ version: '0.0.1',
206
+ website: 'https://aave.com',
207
+ image: 'https://files.cow.fi/token-lists/images/1/0x7fc66500c84a76ad7e9c93437bfc5ac33e2ddae9/logo.png',
208
+ },
209
+ }
package/src/index.ts ADDED
@@ -0,0 +1,6 @@
1
+ export { initCoWHookDapp } from './initCoWHookDapp'
2
+ export * from './hookDappIframeTransport'
3
+ export * from './types'
4
+ export * from './consts'
5
+ export * from './utils'
6
+ export { hookDappsRegistry } from './hookDappsRegistry'
@@ -0,0 +1,42 @@
1
+ import { WidgetEthereumProvider } from '@cowprotocol/iframe-transport'
2
+
3
+ import { CoWHookDappEvents, hookDappIframeTransport } from './hookDappIframeTransport'
4
+ import { CowHookCreation, CoWHookDappActions, CowHookDetails, HookDappContext, TokenData } from './types'
5
+
6
+ interface CoWHookDappInit {
7
+ onContext(context: HookDappContext): void
8
+ }
9
+
10
+ export interface CoWHookDappHandler {
11
+ actions: CoWHookDappActions
12
+ provider: WidgetEthereumProvider
13
+ }
14
+
15
+ export function initCoWHookDapp({ onContext }: CoWHookDappInit): CoWHookDappHandler {
16
+ const parent = window.parent
17
+ const provider = new WidgetEthereumProvider()
18
+ const actions = getCoWHookDappActions()
19
+
20
+ hookDappIframeTransport.listenToMessageFromWindow(window, CoWHookDappEvents.CONTEXT_UPDATE, onContext)
21
+
22
+ hookDappIframeTransport.postMessageToWindow(parent, CoWHookDappEvents.ACTIVATE, void 0)
23
+
24
+ return { actions, provider }
25
+ }
26
+
27
+ function getCoWHookDappActions(): CoWHookDappActions {
28
+ return {
29
+ addHook(payload: CowHookCreation) {
30
+ hookDappIframeTransport.postMessageToWindow(parent, CoWHookDappEvents.ADD_HOOK, payload)
31
+ },
32
+ editHook(payload: CowHookDetails) {
33
+ hookDappIframeTransport.postMessageToWindow(parent, CoWHookDappEvents.EDIT_HOOK, payload)
34
+ },
35
+ setSellToken(token: TokenData) {
36
+ hookDappIframeTransport.postMessageToWindow(parent, CoWHookDappEvents.SET_SELL_TOKEN, token)
37
+ },
38
+ setBuyToken(token: TokenData) {
39
+ hookDappIframeTransport.postMessageToWindow(parent, CoWHookDappEvents.SET_BUY_TOKEN, token)
40
+ },
41
+ }
42
+ }
package/src/types.ts ADDED
@@ -0,0 +1,141 @@
1
+ import { HookDappType, HookDappWalletCompatibility } from './consts'
2
+
3
+ export interface CowHook {
4
+ target: string
5
+ callData: string
6
+ gasLimit: string
7
+ dappId: string
8
+ }
9
+
10
+ export interface HookDappConditions {
11
+ position?: 'post' | 'pre'
12
+ walletCompatibility?: HookDappWalletCompatibility[]
13
+ supportedNetworks?: number[]
14
+ }
15
+
16
+ export interface CowHookCreation {
17
+ hook: Omit<CowHook, 'dappId'>
18
+ recipientOverride?: string
19
+ }
20
+
21
+ export interface TokenData {
22
+ address: string
23
+ }
24
+
25
+ export interface CowHookDetails extends CowHookCreation {
26
+ hook: CowHook
27
+ uuid: string
28
+ }
29
+
30
+ export interface CowHookToEdit extends CowHookCreation {
31
+ uuid: string
32
+ }
33
+
34
+ export interface CoWHookDappActions {
35
+ addHook(payload: CowHookCreation): void
36
+
37
+ editHook(payload: CowHookToEdit): void
38
+
39
+ setSellToken(token: TokenData): void
40
+
41
+ setBuyToken(token: TokenData): void
42
+ }
43
+
44
+ export interface HookDappOrderParams {
45
+ kind: 'buy' | 'sell'
46
+ validTo: number
47
+ sellTokenAddress: string
48
+ buyTokenAddress: string
49
+ receiver: string
50
+ sellAmount: string
51
+ buyAmount: string
52
+ }
53
+
54
+ enum SimpleTypeType {
55
+ Address = 'address',
56
+ Bool = 'bool',
57
+ Bytes = 'bytes',
58
+ Slice = 'slice',
59
+ String = 'string',
60
+ Uint = 'uint',
61
+ }
62
+
63
+ interface Type {
64
+ type: SimpleTypeType
65
+ }
66
+
67
+ enum SoltypeType {
68
+ Address = 'address',
69
+ Bool = 'bool',
70
+ Bytes32 = 'bytes32',
71
+ MappingAddressUint256 = 'mapping (address => uint256)',
72
+ MappingUint256Uint256 = 'mapping (uint256 => uint256)',
73
+ String = 'string',
74
+ Tuple = 'tuple',
75
+ TypeAddress = 'address[]',
76
+ TypeTuple = 'tuple[]',
77
+ Uint16 = 'uint16',
78
+ Uint256 = 'uint256',
79
+ Uint48 = 'uint48',
80
+ Uint56 = 'uint56',
81
+ Uint8 = 'uint8',
82
+ }
83
+
84
+ enum StorageLocation {
85
+ Calldata = 'calldata',
86
+ Default = 'default',
87
+ Memory = 'memory',
88
+ Storage = 'storage',
89
+ }
90
+
91
+ interface SoltypeElement {
92
+ name: string
93
+ type: SoltypeType
94
+ storage_location: StorageLocation
95
+ components: SoltypeElement[] | null
96
+ offset: number
97
+ index: string
98
+ indexed: boolean
99
+ simple_type?: Type
100
+ }
101
+
102
+ interface RawElement {
103
+ address: string
104
+ key: string
105
+ original: string
106
+ dirty: string
107
+ }
108
+
109
+ export interface StateDiff {
110
+ address: string
111
+ soltype: SoltypeElement | null
112
+ original: string | Record<string, unknown>
113
+ dirty: string | Record<string, unknown>
114
+ raw: RawElement[]
115
+ }
116
+
117
+ export interface HookDappContext {
118
+ chainId: number
119
+ account?: string
120
+ orderParams: HookDappOrderParams | null
121
+ hookToEdit?: CowHookDetails
122
+ isSmartContract: boolean | undefined
123
+ isPreHook: boolean
124
+ isDarkMode: boolean
125
+ // { [address: string]: { [token: string]: balanceDiff: string } }
126
+ // example: { '0x123': { '0x456': '100', '0xabc': '-100' } }
127
+ balancesDiff: Record<string, Record<string, string>>
128
+ stateDiff: StateDiff[]
129
+ }
130
+
131
+ export interface HookDappBase {
132
+ id: string
133
+ name: string
134
+ descriptionShort?: string
135
+ description?: string
136
+ type: HookDappType
137
+ version: string
138
+ website: string
139
+ image: string
140
+ conditions?: HookDappConditions
141
+ }
package/src/utils.ts ADDED
@@ -0,0 +1,68 @@
1
+ import { PERMIT_HOOK_DAPP_ID } from './consts'
2
+ import { hookDappsRegistry } from './hookDappsRegistry'
3
+ import { CowHook, HookDappBase } from './types'
4
+
5
+ const hookDapps = Object.keys(hookDappsRegistry).reduce((acc, id) => {
6
+ const dapp = (hookDappsRegistry as Record<string, Omit<HookDappBase, 'id'>>)[id]
7
+
8
+ acc.push({ id, ...dapp })
9
+ return acc
10
+ }, [] as HookDappBase[])
11
+
12
+ // permit() function selector
13
+ const EIP_2612_PERMIT_SELECTOR = '0xd505accf'
14
+ const DAI_PERMIT_SELECTOR = '0x8fcbaf0c'
15
+
16
+ // Before the hooks store the dappId wasn't included in the hook object
17
+ type StrictCowHook = Omit<CowHook, 'dappId'> & { dappId?: string }
18
+
19
+ export interface HookToDappMatch {
20
+ dapp: HookDappBase | null
21
+ hook: CowHook
22
+ }
23
+
24
+ export function matchHooksToDapps(hooks: StrictCowHook[], dapps: HookDappBase[]): HookToDappMatch[] {
25
+ const dappsMap = dapps.reduce(
26
+ (acc, dapp) => {
27
+ acc[dapp.id] = dapp
28
+ return acc
29
+ },
30
+ {} as Record<string, HookDappBase | undefined>,
31
+ )
32
+
33
+ return (
34
+ hooks
35
+ // Skip hooks before the hooks store was introduced
36
+ .filter((hook) => !!hook.dappId)
37
+ .map((_hook) => {
38
+ const hook = _hook as CowHook
39
+ const dapp = dappsMap[hook.dappId]
40
+
41
+ /**
42
+ * Permit token is a special case, as it's not a dapp, but a hook
43
+ */
44
+ if (doesHookHavePermit(hook)) {
45
+ return {
46
+ hook,
47
+ dapp: hookDappsRegistry[PERMIT_HOOK_DAPP_ID] as HookDappBase,
48
+ }
49
+ }
50
+
51
+ return {
52
+ hook,
53
+ dapp: dapp || null,
54
+ }
55
+ })
56
+ )
57
+ }
58
+
59
+ export function doesHookHavePermit(hook: { callData: string }): boolean {
60
+ return hook.callData.startsWith(EIP_2612_PERMIT_SELECTOR) || hook.callData.startsWith(DAI_PERMIT_SELECTOR)
61
+ }
62
+
63
+ export function matchHooksToDappsRegistry(
64
+ hooks: StrictCowHook[],
65
+ additionalHookDapps: HookDappBase[] = [],
66
+ ): HookToDappMatch[] {
67
+ return matchHooksToDapps(hooks, hookDapps.concat(additionalHookDapps))
68
+ }
package/tsconfig.json ADDED
@@ -0,0 +1,22 @@
1
+ {
2
+ "extends": "../../tsconfig.base.json",
3
+ "compilerOptions": {
4
+ "module": "commonjs",
5
+ "forceConsistentCasingInFileNames": true,
6
+ "strict": true,
7
+ "noImplicitOverride": true,
8
+ "noPropertyAccessFromIndexSignature": true,
9
+ "noImplicitReturns": true,
10
+ "noFallthroughCasesInSwitch": true
11
+ },
12
+ "files": [],
13
+ "include": [],
14
+ "references": [
15
+ {
16
+ "path": "./tsconfig.lib.json"
17
+ },
18
+ {
19
+ "path": "./tsconfig.spec.json"
20
+ }
21
+ ]
22
+ }
@@ -0,0 +1,10 @@
1
+ {
2
+ "extends": "./tsconfig.json",
3
+ "compilerOptions": {
4
+ "outDir": "../../dist/out-tsc",
5
+ "declaration": true,
6
+ "types": ["node"]
7
+ },
8
+ "include": ["src/**/*.ts"],
9
+ "exclude": ["src/**/*.spec.ts", "src/**/*.test.ts"]
10
+ }
@@ -0,0 +1,9 @@
1
+ {
2
+ "extends": "./tsconfig.json",
3
+ "compilerOptions": {
4
+ "outDir": "../../../dist/out-tsc",
5
+ "module": "commonjs",
6
+ "types": ["jest", "node"]
7
+ },
8
+ "include": ["jest.config.ts", "src/**/*.test.ts", "src/**/*.spec.ts", "src/**/*.d.ts", "src/lib/widget.ts"]
9
+ }