Skip to content

Commit

Permalink
Wire up Miles claim
Browse files Browse the repository at this point in the history
  • Loading branch information
DannyDelott committed Feb 11, 2025
1 parent f013ece commit 412b1ee
Show file tree
Hide file tree
Showing 3 changed files with 91 additions and 55 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import {
import classNames from "classnames";
import { ReactElement } from "react";
import Skeleton from "react-loading-skeleton";
import { ClaimableReward } from "src/rewards/ClaimableReward";
import { Reward } from "src/rewards/generated/HyperdriveRewardsApi";
import { useAppConfigForConnectedChain } from "src/ui/appconfig/useAppConfigForConnectedChain";
import { Pagination } from "src/ui/base/components/Pagination";
Expand All @@ -28,7 +29,7 @@ export function RewardsTableDesktop({
rewards,
}: {
account: Address;
rewards: Reward[];
rewards: ClaimableReward[];
}): ReactElement {
const appConfig = useAppConfigForConnectedChain({ strict: false });
const tableInstance = useReactTable({
Expand Down Expand Up @@ -139,7 +140,7 @@ export function RewardsTableDesktop({
);
}

const columnHelper = createColumnHelper<Reward>();
const columnHelper = createColumnHelper<ClaimableReward>();

function getColumns({
account,
Expand Down Expand Up @@ -240,7 +241,7 @@ function ClaimRewardsButton({
reward,
}: {
account: Address | undefined;
reward: Reward;
reward: ClaimableReward;
}): ReactElement {
const connectedChainId = useChainId();
const { claimed } = useClaimedRewards({
Expand Down
137 changes: 86 additions & 51 deletions apps/hyperdrive-trading/src/ui/rewards/hooks/useClaimReward.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,20 +7,23 @@ import { usePublicClient, useWriteContract } from "wagmi";
import { getToken } from "@delvtech/hyperdrive-appconfig";
import { hyperdriveRewardsAbi } from "@delvtech/hyperdrive-js";
import { useCallback, useState } from "react";
import { assertNever } from "src/base/assertNever";
import { QueryStatusWithIdle } from "src/base/queryStatus";
import { Reward } from "src/rewards/generated/HyperdriveRewardsApi";
import { ClaimableReward } from "src/rewards/ClaimableReward";
import { useAppConfigForConnectedChain } from "src/ui/appconfig/useAppConfigForConnectedChain";
import { SUCCESS_TOAST_DURATION } from "src/ui/base/toasts";
import { merklDistributorAbi } from "src/ui/rewards/merklDistributorAbi";
import TransactionToast from "src/ui/transactions/TransactionToast";
import { Address } from "viem";
import { WriteContractVariables } from "wagmi/query";

export function useClaimReward({
account,
reward,
enabled = true,
}: {
account: Address | undefined;
reward: Reward;
reward: ClaimableReward;
enabled?: boolean;
}): {
claim: (() => void) | undefined;
Expand All @@ -44,63 +47,46 @@ export function useClaimReward({
return;
}

return writeContract(
{
abi: hyperdriveRewardsAbi,
address: reward.claimContractAddress,
chainId: reward.chainId,
functionName: "claim",
args: [
account,
reward.rewardTokenAddress,
BigInt(reward.claimableAmount), // must be the claimable amount, since it's baked into the merkle proof
reward.merkleProof,
],
},
{
onSuccess: async (hash) => {
addRecentTransaction({
hash,
description: "Claim Reward",
});
setIsTransactionMined(false);
toast.loading(
<TransactionToast
chainId={reward.chainId}
message={`Claiming ${token?.symbol} reward...`}
txHash={hash}
/>,
{ id: hash },
);
const claimArgs = getClaimArgs(account, reward);
writeContract(claimArgs as any, {
onSuccess: async (hash) => {
addRecentTransaction({
hash,
description: "Claim Reward",
});
setIsTransactionMined(false);
toast.loading(
<TransactionToast
chainId={reward.chainId}
message={`Claiming ${token?.symbol} reward...`}
txHash={hash}
/>,
{ id: hash },
);

await waitForTransactionAndInvalidateCache({
publicClient,
hash,
queryClient,
});
setIsTransactionMined(true);
await waitForTransactionAndInvalidateCache({
publicClient,
hash,
queryClient,
});
setIsTransactionMined(true);

toast.success(
<TransactionToast
chainId={reward.chainId}
message={`Claimed ${token?.symbol} reward`}
txHash={hash}
/>,
{ id: hash, duration: SUCCESS_TOAST_DURATION },
);
},
toast.success(
<TransactionToast
chainId={reward.chainId}
message={`Claimed ${token?.symbol} reward`}
txHash={hash}
/>,
{ id: hash, duration: SUCCESS_TOAST_DURATION },
);
},
);
});
}, [
account,
addRecentTransaction,
publicClient,
queryEnabled,
reward.chainId,
reward.claimContractAddress,
reward.claimableAmount,
reward.merkleProof,
reward.rewardTokenAddress,
reward,
token?.symbol,
writeContract,
]);
Expand All @@ -111,3 +97,52 @@ export function useClaimReward({
isTransactionMined,
};
}

function getClaimArgs(account: Address, reward: ClaimableReward) {
switch (reward.merkleType) {
case "HyperdriveMerkle": {
const claimArgs: WriteContractVariables<
typeof hyperdriveRewardsAbi,
"claim",
any,
any,
any
> = {
address: reward.claimContractAddress,
abi: hyperdriveRewardsAbi,
functionName: "claim",
args: [
account,
reward.rewardTokenAddress,
BigInt(reward.claimableAmount),
reward.merkleProof,
],
};

return claimArgs;
}
case "MerklXyz": {
const claimArgs: WriteContractVariables<
typeof merklDistributorAbi,
"claim",
any,
any,
any
> = {
address: reward.claimContractAddress,
abi: merklDistributorAbi,
functionName: "claim",
args: [
[account],
[reward.rewardTokenAddress],
[BigInt(reward.claimableAmount)],
[reward.merkleProof],
],
};

return claimArgs;
}
default:
assertNever(reward.merkleType);
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// Abi for the Merkl Distributor contract: https://app.merkl.xyz/status

export const MerklDistributorAbi = [
export const merklDistributorAbi = [
{ inputs: [], stateMutability: "nonpayable", type: "constructor" },
{ inputs: [], name: "InvalidDispute", type: "error" },
{ inputs: [], name: "InvalidLengths", type: "error" },
Expand Down

0 comments on commit 412b1ee

Please sign in to comment.