Skip to content

Commit

Permalink
chore: user last bytes to decode hook edit data
Browse files Browse the repository at this point in the history
  • Loading branch information
yvesfracari committed Oct 21, 2024
1 parent c855fd0 commit 1b2536f
Showing 1 changed file with 4 additions and 32 deletions.
36 changes: 4 additions & 32 deletions apps/withdraw-pool/src/utils/decodeExitPoolHookCalldata.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
import { type Address, type PublicClient, decodeFunctionData } from "viem";
import { cowAmmAbi } from "./abis/cowAmmAbi";
import { decodeFunctionData } from "viem";
import { cowShedAbi } from "./abis/cowShedAbi";
import { getPctFromValue } from "./math";
import type { withdrawSchema } from "./schema";

interface ICalldata {
Expand All @@ -13,13 +11,11 @@ interface ICalldata {
}

export async function decodeExitPoolHookCalldata(
string: `0x${string}`,
publicClient: PublicClient,
user: Address,
callData: `0x${string}`,
): Promise<typeof withdrawSchema._type> {
const decodedFunctionData = decodeFunctionData({
abi: cowShedAbi,
data: string,
data: callData,
});

if (decodedFunctionData.functionName !== "executeHooks") {
Expand All @@ -35,31 +31,7 @@ export async function decodeExitPoolHookCalldata(
const lastCallIndex = calls.length - 1;
const call = calls[lastCallIndex];

const decodedCoWAmmCallData = decodeFunctionData({
abi: cowAmmAbi,
data: call.callData as `0x${string}`,
});

const poolAddress = call.target;

if (decodedCoWAmmCallData.functionName !== "exitPool") {
throw new Error("Invalid decoded function name");
}

if (!decodedCoWAmmCallData.args?.length) {
throw new Error("Invalid decoded args name");
}

const bptAmountToExit = decodedCoWAmmCallData.args[0] as bigint;

const bptUserPoolBalance = (await publicClient.readContract({
address: poolAddress as Address,
abi: cowAmmAbi,
functionName: "balanceOf",
args: [user],
})) as bigint;

const withdrawPct = getPctFromValue(bptAmountToExit, bptUserPoolBalance);
const withdrawPct = Number(`0x${callData.slice(-2)}`);

return {
poolId: call.target,
Expand Down

0 comments on commit 1b2536f

Please sign in to comment.