Skip to content

Commit

Permalink
Convert frontend to use swagger client for rewards data
Browse files Browse the repository at this point in the history
  • Loading branch information
DannyDelott committed Dec 10, 2024
1 parent 7f413a1 commit fdab985
Show file tree
Hide file tree
Showing 7 changed files with 36 additions and 64 deletions.
6 changes: 6 additions & 0 deletions apps/hyperdrive-trading/.env.sample
Original file line number Diff line number Diff line change
Expand Up @@ -59,3 +59,9 @@ VITE_ADDRESS_SCREEN_URL=
# Used to screen VPN, proxy, and Tor Exit Node IP addresses.
VITE_VPN_SCREEN_URL=
##################################################

##################################################
# Rewards
VITE_REWARDS_BASE_URL=https://rewards.hyperdrive.money/swagger

##################################################
8 changes: 7 additions & 1 deletion apps/hyperdrive-trading/scripts/generate-rewards-swagger.sh
Original file line number Diff line number Diff line change
Expand Up @@ -21,5 +21,11 @@ else
exit 1
fi

npx swagger-typescript-api -p $SWAGGER_FILE -o $OUTPUT_DIR -n $TYPES_FILE --api-class-name RewardsApi --custom-config $CUSTOM_CONFIG
npx swagger-typescript-api \
-p $SWAGGER_FILE \
-o $OUTPUT_DIR \
-n $TYPES_FILE \
--api-class-name RewardsApi \
--custom-config $CUSTOM_CONFIG \
--unwrap-response-data

6 changes: 5 additions & 1 deletion apps/hyperdrive-trading/src/rewards/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@ module.exports = {
...constructs,
TypeField: ({ key, value, readonly, optional }) => {
let fieldValue = value;
// Convert vanilla string to the appropriate address type
// NOTE: Matt will standardize all address fields in swagger.json to use
// the -Address suffix
if (
key.endsWith("Address") ||
key.endsWith("Contract") ||
Expand All @@ -24,5 +27,6 @@ module.exports = {
},
};
},
// TODO: Convert fields ending with "-Amount" from string to bigint
// TODO: Convert fields ending with "-Amount" from string to bigint. This will
// require post-processing in the api client
};
Original file line number Diff line number Diff line change
Expand Up @@ -226,7 +226,7 @@ export class HttpClient<SecurityDataType = unknown> {
baseUrl,
cancelToken,
...params
}: FullRequestParams): Promise<HttpResponse<T, E>> => {
}: FullRequestParams): Promise<T> => {
const secureParams =
((typeof secure === "boolean" ? secure : this.baseApiParams.secure) &&
this.securityWorker &&
Expand Down Expand Up @@ -282,7 +282,7 @@ export class HttpClient<SecurityDataType = unknown> {
}

if (!response.ok) throw data;
return data;
return data.data;
});
};
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,21 +10,21 @@ import {
} from "@tanstack/react-table";
import classNames from "classnames";
import { ReactElement } from "react";
import { RewardsResponse } from "src/rewards/generated/RewardsClient";
import { Pagination } from "src/ui/base/components/Pagination";
import { formatBalance } from "src/ui/base/formatting/formatBalance";
import { Reward } from "src/ui/portfolio/rewards/useRewardsData";
import { Address } from "viem";

export function RewardsTableDesktop({
account,
rewards,
}: {
account: Address;
rewards: Reward[] | undefined;
rewards: RewardsResponse | undefined;
}): ReactElement {
const tableInstance = useReactTable({
columns: getColumns(),
data: rewards || [],
data: rewards?.rewards || [],
getCoreRowModel: getCoreRowModel(),
getSortedRowModel: getSortedRowModel(),
getPaginationRowModel: getPaginationRowModel(),
Expand Down Expand Up @@ -130,7 +130,7 @@ export function RewardsTableDesktop({
);
}

const columnHelper = createColumnHelper<Reward>();
const columnHelper = createColumnHelper<RewardsResponse["rewards"]>();

function getColumns() {
return [
Expand Down
65 changes: 10 additions & 55 deletions apps/hyperdrive-trading/src/ui/portfolio/rewards/useRewardsData.ts
Original file line number Diff line number Diff line change
@@ -1,73 +1,28 @@
import { parseFixed } from "@delvtech/fixed-point-wasm";
import { appConfig } from "@delvtech/hyperdrive-appconfig";
import { useQuery } from "@tanstack/react-query";
import { makeQueryKey } from "src/base/makeQueryKey";
import { Address, zeroAddress } from "viem";
import { base } from "viem/chains";

interface RewardsResponse {
userAddress: Address;
rewards: Reward[];
}

export interface Reward {
chainId: number;
claimContract: Address;
claimable: bigint;
rewardToken: Address;
merkleProof: string[] | null;
merkleProofLastUpdated: number;
}

function getDummyRewardsResponse(account: Address) {
const dummyRewardsResponse: RewardsResponse = {
userAddress: account,
rewards: [
{
// rewards for this user that they can claim
chainId: base.id,
claimContract: zeroAddress,
claimable: parseFixed("1000000").bigint,
rewardToken: appConfig.tokens.find(
(token) => token.chainId === 8453 && token.symbol === "MORPHO",
)!.address,
merkleProof: ["0xProof", "0xProof", "0xProof"],
merkleProofLastUpdated: 123892327,
},
{
// rewards are accumulating, but the merkle root hasn't been added
// to the claimContract yet
chainId: base.id,
claimContract: zeroAddress,
claimable: parseFixed("0").bigint,
rewardToken: appConfig.tokens.find(
(token) => token.chainId === 8453 && token.symbol === "USDC",
)!.address,
merkleProof: null,
merkleProofLastUpdated: 123892327,
},
],
};

return dummyRewardsResponse;
}
import {
RewardsApi,
RewardsResponse,
} from "src/rewards/generated/RewardsClient";
import { Address } from "viem";

export function usePortfolioRewardsData({
account,
}: {
account: Address | undefined;
}): {
rewards: Reward[] | undefined;
rewards: RewardsResponse | undefined;
rewardsStatus: "error" | "success" | "loading";
} {
const queryEnabled = !!account;
const { data: rewards, status: rewardsStatus } = useQuery({
queryKey: makeQueryKey("rewards", { account }),
queryFn: queryEnabled
? async () => {
// TODO: Fetch rewards from server
const rewardsResponse = getDummyRewardsResponse(account);
return rewardsResponse.rewards;
const rewardsApi = new RewardsApi({
baseUrl: import.meta.env.VITE_REWARDS_BASE_URL,
});
return rewardsApi.get.rewardsDetail(account);
}
: undefined,
enabled: queryEnabled,
Expand Down
3 changes: 2 additions & 1 deletion turbo.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,8 @@
"VITE_ROLLBAR_ENV",
"VITE_SEPOLIA_RPC_URL",
"VITE_VPN_SCREEN_URL",
"VITE_WALLET_CONNECT_PROJECT_ID"
"VITE_WALLET_CONNECT_PROJECT_ID",
"VITE_REWARDS_BASE_URL"
],
"outputs": ["dist/**", ".next/**"]
},
Expand Down

0 comments on commit fdab985

Please sign in to comment.