Skip to content

Commit

Permalink
Quick fix (#923)
Browse files Browse the repository at this point in the history
* handle zero swap amounts

* exitFeeHook rename

* add hook type name
  • Loading branch information
gmbronco authored Sep 11, 2024
1 parent 46704ef commit db7314d
Show file tree
Hide file tree
Showing 13 changed files with 101 additions and 40 deletions.
5 changes: 5 additions & 0 deletions .changeset/angry-apples-stare.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'backend': minor
---

add hook type name, handle swap amount = 0 errors
2 changes: 1 addition & 1 deletion config/sepolia.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ export default <NetworkData>{
},
hooks: {
feeTakingHook: ['0xde97fc6ecdcd9efa53dac2d29c0fc10e9b482a0b'],
removeLiquidityFeeHook: ['0x8756a6527e8dc94cf07a468d2e4df8e9946bce3d'],
exitFeeHook: ['0x8756a6527e8dc94cf07a468d2e4df8e9946bce3d'],
},
multicall: '0x25eef291876194aefad0d60dff89e268b90754bb',
multicall3: '0xca11bde05977b3631167028862be2a173976ca11',
Expand Down
1 change: 1 addition & 0 deletions modules/actions/pool/sync-hook-data.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ export const syncHookData = async (
prisma.hook.update({
where: { address_chain: { address, chain } },
data: {
name: addresses[address],
dynamicData: data[address],
},
}),
Expand Down
2 changes: 1 addition & 1 deletion modules/network/network-config-types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ export interface NetworkData {
};
hooks?: {
feeTakingHook?: string[];
removeLiquidityFeeHook?: string[];
exitFeeHook?: string[];
};
multicall: string;
multicall3: string;
Expand Down
9 changes: 8 additions & 1 deletion modules/sor/sor.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import {
import { sorV1BeetsService } from './sorV1Beets/sorV1Beets.service';
import { sorV2Service } from './sorV2/sorPathService';
import { GetSwapsInput, GetSwapsV2Input as GetSwapPathsInput, SwapResult } from './types';
import * as Sentry from '@sentry/node';
import { Chain } from '@prisma/client';
import { formatUnits, parseUnits } from '@ethersproject/units';
import { tokenService } from '../token/token.service';
Expand All @@ -23,6 +22,10 @@ export class SorService {
const amountToken = args.swapType === 'EXACT_IN' ? tokenIn : tokenOut;
const emptyResponse = swapPathsZeroResponse(args.tokenIn, args.tokenOut);

if (parseFloat(args.swapAmount) <= 0) {
return emptyResponse;
}

const wethIsEth =
tokenIn === AllNetworkConfigsKeyedOnChain[args.chain].data.eth.address ||
tokenOut === AllNetworkConfigsKeyedOnChain[args.chain].data.eth.address;
Expand Down Expand Up @@ -87,6 +90,10 @@ export class SorService {
const amountToken = args.swapType === 'EXACT_IN' ? tokenIn : tokenOut;
const emptyResponse = zeroResponse(args.swapType, args.tokenIn, args.tokenOut, args.swapAmount);

if (parseFloat(args.swapAmount) <= 0) {
return emptyResponse;
}

// check if tokens addresses exist
try {
await getToken(tokenIn, args.chain!);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@ export default [
},
{
inputs: [
{ internalType: 'uint256', name: 'fee', type: 'uint256' },
{ internalType: 'uint256', name: 'feePercentage', type: 'uint256' },
{ internalType: 'uint256', name: 'limit', type: 'uint256' },
],
name: 'ExitHookFeeAboveLimit',
name: 'ExitFeeAboveLimit',
type: 'error',
},
{
Expand All @@ -24,6 +24,34 @@ export default [
},
{ inputs: [], name: 'PoolDoesNotSupportDonation', type: 'error' },
{ inputs: [{ internalType: 'address', name: 'sender', type: 'address' }], name: 'SenderIsNotVault', type: 'error' },
{
anonymous: false,
inputs: [
{ indexed: true, internalType: 'address', name: 'pool', type: 'address' },
{ indexed: true, internalType: 'contract IERC20', name: 'token', type: 'address' },
{ indexed: false, internalType: 'uint256', name: 'feeAmount', type: 'uint256' },
],
name: 'ExitFeeCharged',
type: 'event',
},
{
anonymous: false,
inputs: [
{ indexed: true, internalType: 'address', name: 'hooksContract', type: 'address' },
{ indexed: true, internalType: 'address', name: 'pool', type: 'address' },
],
name: 'ExitFeeHookExampleRegistered',
type: 'event',
},
{
anonymous: false,
inputs: [
{ indexed: true, internalType: 'address', name: 'hookContract', type: 'address' },
{ indexed: false, internalType: 'uint256', name: 'exitFeePercentage', type: 'uint256' },
],
name: 'ExitFeePercentageChanged',
type: 'event',
},
{
anonymous: false,
inputs: [
Expand All @@ -35,7 +63,14 @@ export default [
},
{
inputs: [],
name: 'MAX_EXIT_HOOK_FEE',
name: 'MAX_EXIT_FEE_PERCENTAGE',
outputs: [{ internalType: 'uint64', name: '', type: 'uint64' }],
stateMutability: 'view',
type: 'function',
},
{
inputs: [],
name: 'exitFeePercentage',
outputs: [{ internalType: 'uint64', name: '', type: 'uint64' }],
stateMutability: 'view',
type: 'function',
Expand All @@ -57,7 +92,7 @@ export default [
{ internalType: 'bool', name: 'shouldCallBeforeRemoveLiquidity', type: 'bool' },
{ internalType: 'bool', name: 'shouldCallAfterRemoveLiquidity', type: 'bool' },
],
internalType: 'struct IHooks.HookFlags',
internalType: 'struct HookFlags',
name: '',
type: 'tuple',
},
Expand Down Expand Up @@ -131,7 +166,7 @@ export default [
{ internalType: 'address', name: 'pool', type: 'address' },
{ internalType: 'bytes', name: 'userData', type: 'bytes' },
],
internalType: 'struct IHooks.AfterSwapParams',
internalType: 'struct AfterSwapParams',
name: '',
type: 'tuple',
},
Expand Down Expand Up @@ -196,7 +231,7 @@ export default [
{ internalType: 'address', name: 'router', type: 'address' },
{ internalType: 'bytes', name: 'userData', type: 'bytes' },
],
internalType: 'struct IBasePool.PoolSwapParams',
internalType: 'struct PoolSwapParams',
name: '',
type: 'tuple',
},
Expand All @@ -219,14 +254,14 @@ export default [
{ internalType: 'address', name: 'router', type: 'address' },
{ internalType: 'bytes', name: 'userData', type: 'bytes' },
],
internalType: 'struct IBasePool.PoolSwapParams',
internalType: 'struct PoolSwapParams',
name: '',
type: 'tuple',
},
{ internalType: 'address', name: '', type: 'address' },
{ internalType: 'uint256', name: '', type: 'uint256' },
],
name: 'onComputeDynamicSwapFee',
name: 'onComputeDynamicSwapFeePercentage',
outputs: [
{ internalType: 'bool', name: '', type: 'bool' },
{ internalType: 'uint256', name: '', type: 'uint256' },
Expand All @@ -237,7 +272,7 @@ export default [
{
inputs: [
{ internalType: 'address', name: '', type: 'address' },
{ internalType: 'address', name: '', type: 'address' },
{ internalType: 'address', name: 'pool', type: 'address' },
{
components: [
{ internalType: 'contract IERC20', name: 'token', type: 'address' },
Expand All @@ -263,7 +298,7 @@ export default [
],
name: 'onRegister',
outputs: [{ internalType: 'bool', name: '', type: 'bool' }],
stateMutability: 'view',
stateMutability: 'nonpayable',
type: 'function',
},
{
Expand All @@ -273,17 +308,10 @@ export default [
stateMutability: 'view',
type: 'function',
},
{
inputs: [],
name: 'removeLiquidityHookFeePercentage',
outputs: [{ internalType: 'uint64', name: '', type: 'uint64' }],
stateMutability: 'view',
type: 'function',
},
{ inputs: [], name: 'renounceOwnership', outputs: [], stateMutability: 'nonpayable', type: 'function' },
{
inputs: [{ internalType: 'uint64', name: 'hookFeePercentage', type: 'uint64' }],
name: 'setRemoveLiquidityHookFeePercentage',
inputs: [{ internalType: 'uint64', name: 'newExitFeePercentage', type: 'uint64' }],
name: 'setExitFeePercentage',
outputs: [],
stateMutability: 'nonpayable',
type: 'function',
Expand Down
11 changes: 11 additions & 0 deletions modules/sources/contracts/hooks/exit-fee-hook.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { ViemMulticallCall } from '../../../web3/multicaller-viem';
import exitFeeHookAbi from '../abis/exit-fee-hook';

export const exitFeeHook = (address: string): ViemMulticallCall[] => [
{
path: `${address}.removeLiquidityFeePercentage`,
address: address as `0x${string}`,
abi: exitFeeHookAbi,
functionName: 'exitFeePercentage',
},
];
6 changes: 3 additions & 3 deletions modules/sources/contracts/hooks/fetch-hook-data.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import type { ViemMulticallCall } from '../../../web3/multicaller-viem';
import { multicallViem } from '../../../web3/multicaller-viem';
import { ViemClient } from '../../types';
import { feeTakingHook } from './fee-taking-hook';
import { removeLiquidityFeeHook } from './remove-liquidity-fee-hook';
import { exitFeeHook } from './exit-fee-hook';

export const fetchHookData = async (client: ViemClient, addresses?: Record<string, HookType>) => {
if (!addresses) {
Expand All @@ -19,8 +19,8 @@ export const fetchHookData = async (client: ViemClient, addresses?: Record<strin
case 'feeTakingHook':
calls = [...calls, ...feeTakingHook(address)];
break;
case 'removeLiquidityFeeHook':
calls = [...calls, ...removeLiquidityFeeHook(address)];
case 'exitFeeHook':
calls = [...calls, ...exitFeeHook(address)];
break;
default:
break;
Expand Down
11 changes: 0 additions & 11 deletions modules/sources/contracts/hooks/remove-liquidity-fee-hook.ts

This file was deleted.

24 changes: 20 additions & 4 deletions modules/sources/transformers/hook-transformer.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
import { Chain, PrismaPoolType } from '@prisma/client';
import { PoolType } from '../subgraphs/balancer-v3-pools/generated/types';
import { StableData } from '../../pool/subgraph-mapper';
import { fx, gyro, element, stable } from '../../pool/pool-data';
import { Chain } from '@prisma/client';
import { JoinedSubgraphPool } from '../subgraphs';
import { zeroAddress } from 'viem';
import config from '../../../config';
import { HookType } from '../../network/network-config-types';

export const hookTransformer = (poolData: JoinedSubgraphPool, chain: Chain) => {
// By default v3 pools have a hook config with the address 0x0
Expand All @@ -17,9 +16,26 @@ export const hookTransformer = (poolData: JoinedSubgraphPool, chain: Chain) => {

const { hook, ...hookFlags } = hookConfig;

const hookAddresses = config[chain].hooks || {};
const hookTypes = Object.keys(hookAddresses) as HookType[];
const mappedHooks = hookTypes.reduce((acc, type: HookType) => {
const addresses = hookAddresses[type] || [];
addresses.forEach((address) => {
acc[address] = type;
});

return acc;
}, {} as Record<string, HookType>);

if (!mappedHooks[hook.address.toLowerCase()]) {
console.error(`Unknown hook address ${hook.address} on ${chain}`);
return undefined;
}

return {
address: hook.address.toLowerCase(),
chain: chain,
name: mappedHooks[hook.address.toLowerCase()],
...hookFlags,
};
};
2 changes: 2 additions & 0 deletions prisma/migrations/20240911214400_hook_type_name/migration.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
-- AlterTable
ALTER TABLE "Hook" ADD COLUMN "name" TEXT NOT NULL DEFAULT '';
1 change: 1 addition & 0 deletions prisma/schema.prisma
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ model Hook {
id Int @id @default(autoincrement())
address String
chain Chain
name String @default("")
enableHookAdjustedAmounts Boolean @default(false)
shouldCallAfterSwap Boolean @default(false)
shouldCallBeforeSwap Boolean @default(false)
Expand Down
1 change: 1 addition & 0 deletions prisma/schema/hook.prisma
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ model Hook {
id Int @id @default(autoincrement())
address String
chain Chain
name String @default("")
enableHookAdjustedAmounts Boolean @default(false)
shouldCallAfterSwap Boolean @default(false)
shouldCallBeforeSwap Boolean @default(false)
Expand Down

0 comments on commit db7314d

Please sign in to comment.