Skip to content

Commit

Permalink
add user inventory and staked characters to harvester api response (#17)
Browse files Browse the repository at this point in the history
* add characters inventory

* add staked characters
  • Loading branch information
alecananian authored Apr 4, 2024
1 parent ef32de7 commit 893d972
Show file tree
Hide file tree
Showing 9 changed files with 471 additions and 167 deletions.
1 change: 1 addition & 0 deletions apps/api/.env.example
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,5 @@ THIRDWEB_AUTH_PRIVATE_KEY=
THIRDWEB_ENGINE_URL=
THIRDWEB_ENGINE_ACCESS_TOKEN=
THIRDWEB_SECRET_KEY=
TROVE_API_KEY=
ZEEVERSE_API_URL=https://api.zee-verse.com
2 changes: 1 addition & 1 deletion apps/api/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ const main = async () => {
app.register(projectsRoutes(ctx)),
app.register(contractsRoutes(ctx)),
app.register(transactionsRoutes(ctx)),
app.register(harvestersRoutes),
app.register(harvestersRoutes(ctx)),
]);

app.get("/healthcheck", async (_, reply) => reply.send("OK"));
Expand Down
83 changes: 44 additions & 39 deletions apps/api/src/routes/harvesters.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,49 +14,54 @@ import {
type ReadHarvesterReply,
readHarvesterReplySchema,
} from "../schema";
import type { TdkApiContext } from "../types";

export const harvestersRoutes: FastifyPluginAsync = async (app) => {
app.get<{
Params: ReadHarvesterParams;
Reply: ReadHarvesterReply | ErrorReply;
}>(
"/harvesters/:id",
{
schema: {
response: {
200: readHarvesterReplySchema,
export const harvestersRoutes =
({ env }: TdkApiContext): FastifyPluginAsync =>
async (app) => {
app.get<{
Params: ReadHarvesterParams;
Reply: ReadHarvesterReply | ErrorReply;
}>(
"/harvesters/:id",
{
schema: {
response: {
200: readHarvesterReplySchema,
},
},
},
},
async (req, reply) => {
const {
chainId,
params: { id },
} = req;
async (req, reply) => {
const {
chainId,
params: { id },
} = req;

const harvesterAddress = id as AddressString;
const harvesterInfo = await getHarvesterInfo({
chainId,
harvesterAddress,
});
const harvesterAddress = id as AddressString;
const harvesterInfo = await getHarvesterInfo({
chainId,
harvesterAddress,
tokenApiKey: env.TROVE_API_KEY,
});

if (harvesterInfo.nftHandlerAddress === zeroAddress) {
return reply.code(404).send({ error: "Not found" });
}
if (harvesterInfo.nftHandlerAddress === zeroAddress) {
return reply.code(404).send({ error: "Not found" });
}

const user = await getUser(req);
const harvesterUserInfo = user?.address
? await getHarvesterUserInfo({
chainId,
harvesterInfo,
userAddress: user.address as AddressString,
})
: undefined;
const user = await getUser(req);
const harvesterUserInfo = user?.address
? await getHarvesterUserInfo({
chainId,
harvesterInfo,
userAddress: user.address as AddressString,
inventoryApiKey: env.TROVE_API_KEY,
})
: undefined;

reply.send({
...harvesterInfo,
...harvesterUserInfo,
});
},
);
};
reply.send({
...harvesterInfo,
...harvesterUserInfo,
});
},
);
};
28 changes: 28 additions & 0 deletions apps/api/src/schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,27 @@ export type AuthVerifyBody = Static<typeof authVerifyBodySchema>;
export type AuthVerifyReply = Static<typeof authVerifyReplySchema>;

// Harvesters
const tokenSchema = Type.Object({
address: Type.String(),
tokenId: Type.Number(),
name: Type.String(),
image: Type.String(),
attributes: Type.Array(
Type.Object({
type: Type.String(),
value: Type.Union([Type.String(), Type.Number()]),
}),
),
});

const inventoryTokenSchema = Type.Intersect([
tokenSchema,
Type.Object({
user: Type.String(),
balance: Type.Number(),
}),
]);

const harvesterInfoSchema = Type.Object({
id: Type.String(),
// NFT Handler
Expand All @@ -75,6 +96,8 @@ const harvesterInfoSchema = Type.Object({
legionsStakingRulesAddress: Type.Optional(Type.String()),
treasuresStakingRulesAddress: Type.Optional(Type.String()),
charactersStakingRulesAddress: Type.Optional(Type.String()),
// NFTs settings
charactersAddress: Type.Optional(Type.String()),
// Permits settings
permitsAddress: Type.String(),
permitsTokenId: Type.Number(),
Expand Down Expand Up @@ -126,6 +149,9 @@ const harvesterUserInfoSchema = Type.Object({
userTotalBoost: Type.Number(),
userPermitsMaxStakeable: Type.Number(),
userPermitsStaked: Type.Number(),
userInventoryCharacters: Type.Array(inventoryTokenSchema),
userStakedCharacters: Type.Array(tokenSchema),
userCharactersApproved: Type.Boolean(),
userCharactersMaxStakeable: Type.Number(),
userCharactersStaked: Type.Number(),
userCharactersMaxBoost: Type.Number(),
Expand All @@ -144,6 +170,8 @@ export const readHarvesterReplySchema = Type.Composite([
Type.Partial(harvesterUserInfoSchema),
]);

export type Token = Static<typeof tokenSchema>;
export type InventoryToken = Static<typeof inventoryTokenSchema>;
export type HarvesterInfo = Static<typeof harvesterInfoSchema>;
export type HarvesterUserInfo = Static<typeof harvesterUserInfoSchema>;
export type ReadHarvesterParams = Static<typeof readHarvesterParamsSchema>;
Expand Down
1 change: 1 addition & 0 deletions apps/api/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ export type TdkApiEnv = {
THIRDWEB_ENGINE_URL: string;
THIRDWEB_ENGINE_ACCESS_TOKEN: string;
THIRDWEB_SECRET_KEY: string;
TROVE_API_KEY: string;
ZEEVERSE_API_URL: string;
};

Expand Down
1 change: 1 addition & 0 deletions apps/api/src/utils/env.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ export const getEnv = async (): Promise<TdkApiEnv> => {
"",
THIRDWEB_SECRET_KEY:
process.env.THIRDWEB_SECRET_KEY ?? remoteEnv?.THIRDWEB_SECRET_KEY ?? "",
TROVE_API_KEY: process.env.TROVE_API_KEY ?? remoteEnv?.TROVE_API_KEY ?? "",
ZEEVERSE_API_URL:
process.env.ZEEVERSE_API_URL ?? "https://api.zee-verse.com",
};
Expand Down
12 changes: 12 additions & 0 deletions packages/core/src/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,18 @@ export const DEFAULT_TDK_APP = "app";
export const DEFAULT_TDK_CHAIN_ID = arbitrum.id;
export const DEFAULT_TDK_LOGIN_DOMAIN = "https://login.treasure.lol";

export const BRIDGEWORLD_API_URL = {
[arbitrum.id]:
"https://api.goldsky.com/api/public/project_clrm53zqegpoi01x18coz2fb5/subgraphs/bridgeworld/live/gn",
[arbitrumSepolia.id]:
"https://api.goldsky.com/api/public/project_clrm53zqegpoi01x18coz2fb5/subgraphs/bridgeworld-dev/live/gn",
} as const;

export const TROVE_API_URL = {
[arbitrum.id]: "https://trove-api.treasure.lol",
[arbitrumSepolia.id]: "https://trove-api-dev.treasure.lol",
} as const;

export const SUPPORTED_CHAINS = [arbitrum, arbitrumSepolia] as const;
export const SUPPORTED_CHAIN_IDS = SUPPORTED_CHAINS.map(({ id }) => id);

Expand Down
Loading

0 comments on commit 893d972

Please sign in to comment.