Skip to content

Commit

Permalink
add option to use custom wagmi config; configure thirdweb rpc (#27)
Browse files Browse the repository at this point in the history
* add option to use custom wagmi config; configure thirdweb rpc

* simplify default wagmi config
  • Loading branch information
alecananian authored May 8, 2024
1 parent 65c036d commit 14b01be
Show file tree
Hide file tree
Showing 11 changed files with 2,988 additions and 2,616 deletions.
5 changes: 5 additions & 0 deletions .changeset/tidy-ligers-thank.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@treasure-dev/tdk-core": patch
---

added option to pass custom wagmi configs for contract calls
1 change: 1 addition & 0 deletions apps/api/.env.example
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ DATABASE_URL=postgresql://postgres:@localhost:5432/tdk_api?schema=public
DEFAULT_BACKEND_WALLET=
THIRDWEB_AUTH_DOMAIN=localhost:5173
THIRDWEB_AUTH_PRIVATE_KEY=
THIRDWEB_CLIENT_ID=
THIRDWEB_ENGINE_URL=
THIRDWEB_ENGINE_ACCESS_TOKEN=
THIRDWEB_SECRET_KEY=
Expand Down
1 change: 1 addition & 0 deletions apps/api/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
"@thirdweb-dev/auth": "^4.1.4",
"@thirdweb-dev/engine": "^0.0.5",
"@treasure-dev/tdk-core": "*",
"@wagmi/core": "^2.9.1",
"abitype": "^1.0.0",
"dotenv": "^16.3.1",
"fastify": "^4.24.3",
Expand Down
21 changes: 21 additions & 0 deletions apps/api/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
import type { TypeBoxTypeProvider } from "@fastify/type-provider-typebox";
import { PrismaClient } from "@prisma/client";
import { Engine } from "@thirdweb-dev/engine";
import type { SupportedChainId } from "@treasure-dev/tdk-core";
import { SUPPORTED_CHAINS } from "@treasure-dev/tdk-core";
import type { Transport } from "@wagmi/core";
import { createConfig, fallback, http } from "@wagmi/core";
import Fastify from "fastify";
import { createThirdwebClient } from "thirdweb";
import { createAuth } from "thirdweb/auth";
Expand Down Expand Up @@ -51,6 +55,23 @@ const main = async () => {
url: env.THIRDWEB_ENGINE_URL,
accessToken: env.THIRDWEB_ENGINE_ACCESS_TOKEN,
}),
wagmiConfig: env.THIRDWEB_CLIENT_ID
? createConfig({
chains: SUPPORTED_CHAINS,
transports: SUPPORTED_CHAINS.reduce(
(acc, chain) => ({
...acc,
[chain.id]: fallback([
http(
`https://${chain.id}.rpc.thirdweb.com/${env.THIRDWEB_CLIENT_ID}`,
),
http(),
]),
}),
{} as Record<SupportedChainId, Transport>,
),
})
: undefined,
};

// Middleware
Expand Down
5 changes: 4 additions & 1 deletion apps/api/src/routes/harvesters.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ import type { TdkApiContext } from "../types";
import { verifyAuth } from "../utils/auth";

export const harvestersRoutes =
({ env, auth }: TdkApiContext): FastifyPluginAsync =>
({ env, auth, wagmiConfig }: TdkApiContext): FastifyPluginAsync =>
async (app) => {
app.get<{
Params: ReadHarvesterParams;
Expand All @@ -47,6 +47,7 @@ export const harvestersRoutes =
const harvesterInfo = await getHarvesterInfo({
chainId,
harvesterAddress,
wagmiConfig,
});

if (harvesterInfo.nftHandlerAddress === zeroAddress) {
Expand All @@ -66,6 +67,7 @@ export const harvestersRoutes =
userAddress,
inventoryApiUrl: env.TROVE_API_URL,
inventoryApiKey: env.TROVE_API_KEY,
wagmiConfig,
})
: undefined;

Expand Down Expand Up @@ -107,6 +109,7 @@ export const harvestersRoutes =
userAddress,
inventoryApiUrl: env.TROVE_API_URL,
inventoryApiKey: env.TROVE_API_KEY,
wagmiConfig,
});
reply.send(harvesterCorruptionRemovalInfo);
},
Expand Down
3 changes: 3 additions & 0 deletions apps/api/src/types.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import type { PrismaClient } from "@prisma/client";
import type { Engine } from "@thirdweb-dev/engine";
import type { Config as WagmiConfig } from "@wagmi/core";
import type { ThirdwebClient } from "thirdweb";
import type { createAuth } from "thirdweb/auth";

Expand All @@ -20,6 +21,7 @@ export type TdkApiEnv = {
DEFAULT_BACKEND_WALLET: string;
THIRDWEB_AUTH_DOMAIN: string;
THIRDWEB_AUTH_PRIVATE_KEY: string;
THIRDWEB_CLIENT_ID: string;
THIRDWEB_ENGINE_URL: string;
THIRDWEB_ENGINE_ACCESS_TOKEN: string;
THIRDWEB_SECRET_KEY: string;
Expand All @@ -34,4 +36,5 @@ export type TdkApiContext = {
client: ThirdwebClient;
auth: ThirdwebAuth;
engine: Engine;
wagmiConfig: WagmiConfig | undefined;
};
2 changes: 2 additions & 0 deletions apps/api/src/utils/env.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,8 @@ export const getEnv = async (): Promise<TdkApiEnv> => {
process.env.THIRDWEB_AUTH_PRIVATE_KEY ??
envSecret?.THIRDWEB_AUTH_PRIVATE_KEY ??
"",
THIRDWEB_CLIENT_ID:
process.env.THIRDWEB_CLIENT_ID ?? envSecret?.THIRDWEB_CLIENT_ID ?? "",
THIRDWEB_ENGINE_URL:
process.env.THIRDWEB_ENGINE_URL ?? envSecret?.THIRDWEB_ENGINE_URL ?? "",
THIRDWEB_ENGINE_ACCESS_TOKEN:
Expand Down
Loading

0 comments on commit 14b01be

Please sign in to comment.