Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add swagger codegen for rewards endpoint #1675

Merged
merged 3 commits into from
Dec 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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

##################################################
6 changes: 4 additions & 2 deletions apps/hyperdrive-trading/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,8 @@
"test:watch": "vitest --reporter=verbose",
"typecheck": "tsc --project tsconfig.json --noEmit",
"gen:version": "bash ./scripts/generate-version.sh",
"gen:walletconnect": "bash ./scripts/generate-walletconnect.sh"
"gen:walletconnect": "bash ./scripts/generate-walletconnect.sh",
"gen:rewards-swagger": "bash ./scripts/generate-rewards-swagger.sh"
},
"dependencies": {
"@delvtech/drift": "^0.0.1-beta.11",
Expand All @@ -41,7 +42,7 @@
"@tanstack/react-query": "^4.29.12",
"@tanstack/react-router": "^1.87.7",
"@tanstack/router-devtools": "^1.87.7",
"@tanstack/react-table": "^8.10.3",
"@tanstack/react-table": "^8.20.5",
"@tanstack/query-core": "^4.36.1",
"@types/d3-format": "^3.0.4",
"@types/lodash.sortby": "^4.7.9",
Expand Down Expand Up @@ -77,6 +78,7 @@
},
"devDependencies": {
"@hyperdrive/eslint-config": "*",
"swagger-typescript-api": "^13.0.23",
"@hyperdrive/prettier-config": "*",
"@hyperdrive/tsconfig": "*",
"@tanstack/eslint-plugin-query": "^5.28.11",
Expand Down
31 changes: 31 additions & 0 deletions apps/hyperdrive-trading/scripts/generate-rewards-swagger.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
#!/bin/bash

# Define the URL and output file path
URL="https://rewards.hyperdrive.money/swagger.json"
OUTPUT_DIR="src/rewards/generated"
SWAGGER_FILE="$OUTPUT_DIR/rewards-swagger.json"
TYPES_FILE="RewardsClient.ts"
CUSTOM_CONFIG="src/rewards/config.js"

# Create the directory if it doesn't exist
mkdir -p "$(dirname "$SWAGGER_FILE")"

# Download the file
curl -o "$SWAGGER_FILE" "$URL"

# Check if the download was successful
if [ $? -eq 0 ]; then
echo "File downloaded successfully and saved to $SWAGGER_FILE"
else
echo "Failed to download the file from $URL"
exit 1
fi

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

26 changes: 26 additions & 0 deletions apps/hyperdrive-trading/src/rewards/config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
// swagger-typescript-api config file

module.exports = {
codeGenConstructs: (constructs) => {
return {
...constructs,
TypeField: ({ key, value, readonly, optional }) => {
let fieldValue = value;
// Convert vanilla string to the appropriate address type
if (key.endsWith("Address")) {
fieldValue = "`0x${string}`";
}

if (key.endsWith("Proof")) {
fieldValue = "`0x${string}`[]";
}

return [readonly && "readonly ", key, optional && "?", ": ", fieldValue]
.filter(Boolean)
.join("");
},
};
},
// TODO: Convert fields ending with "-Amount" from string to bigint. This will
// require post-processing in the api client
};
Loading
Loading