Skip to content

Commit

Permalink
api: pull backendwallet from request body
Browse files Browse the repository at this point in the history
  • Loading branch information
alecananian committed Sep 17, 2024
1 parent d82e0cd commit 56d054b
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 23 deletions.
21 changes: 3 additions & 18 deletions apps/api/src/middleware/auth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,21 +10,18 @@ declare module "fastify" {
interface FastifyRequest {
userId: string | undefined;
userAddress: AddressString | undefined;
backendWallet: AddressString;
backendWallet: AddressString | undefined;
authError: string | undefined;
}
}

export const withAuth = async (
app: FastifyInstance,
{ auth, thirdwebAuth, env }: TdkApiContext,
{ auth, thirdwebAuth }: TdkApiContext,
) => {
app.decorateRequest("userId", undefined);
app.decorateRequest("userAddress", undefined);
app.decorateRequest(
"backendWallet",
env.DEFAULT_BACKEND_WALLET as AddressString,
);
app.decorateRequest("backendWallet", undefined);
app.decorateRequest("authError", undefined);
app.addHook("onRequest", async (req) => {
// Check for explicit setting of user address and recover backend wallet address from signature
Expand Down Expand Up @@ -53,18 +50,6 @@ export const withAuth = async (
return;
}

// Fall back to backend wallet from params
const backendWallet =
req.params &&
typeof req.params === "object" &&
"backendWallet" in req.params
? req.params.backendWallet
: undefined;
// TODO: Remove default backend wallet when all partners upgrade to their own
req.backendWallet = isHex(backendWallet)
? backendWallet
: (env.DEFAULT_BACKEND_WALLET as AddressString);

// All other auth methods require an Authorization header
if (!req.headers.authorization) {
return;
Expand Down
9 changes: 6 additions & 3 deletions apps/api/src/routes/magicswap.ts
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,7 @@ export const magicswapRoutes =
nftsOut,
isExactOut,
slippage,
backendWallet = env.DEFAULT_BACKEND_WALLET,
simulateTransaction = env.ENGINE_TRANSACTION_SIMULATION_ENABLED,
} = body;

Expand Down Expand Up @@ -244,7 +245,7 @@ export const magicswapRoutes =
engine,
chainId,
contractAddress: swapArguments.address,
backendWallet: req.backendWallet,
backendWallet: req.backendWallet ?? backendWallet,
smartAccountAddress: userAddress,
abi: magicswapV2RouterAbi,
functionName: swapArguments.functionName,
Expand Down Expand Up @@ -302,6 +303,7 @@ export const magicswapRoutes =
amount1Min,
nfts0,
nfts1,
backendWallet = env.DEFAULT_BACKEND_WALLET,
simulateTransaction = env.ENGINE_TRANSACTION_SIMULATION_ENABLED,
} = body;

Expand Down Expand Up @@ -339,7 +341,7 @@ export const magicswapRoutes =
engine,
chainId,
contractAddress: addLiquidityArgs.address,
backendWallet: req.backendWallet,
backendWallet: req.backendWallet ?? backendWallet,
smartAccountAddress: userAddress,
abi: magicswapV2RouterAbi,
functionName: addLiquidityArgs.functionName,
Expand Down Expand Up @@ -397,6 +399,7 @@ export const magicswapRoutes =
nfts0,
nfts1,
swapLeftover = true,
backendWallet = env.DEFAULT_BACKEND_WALLET,
simulateTransaction = env.ENGINE_TRANSACTION_SIMULATION_ENABLED,
} = body;

Expand Down Expand Up @@ -434,7 +437,7 @@ export const magicswapRoutes =
engine,
chainId,
contractAddress: removeLiquidityArgs.address,
backendWallet: req.backendWallet,
backendWallet: req.backendWallet ?? backendWallet,
smartAccountAddress: userAddress,
abi: magicswapV2RouterAbi,
functionName: removeLiquidityArgs.functionName,
Expand Down
6 changes: 4 additions & 2 deletions apps/api/src/routes/transactions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ export const transactionsRoutes =
functionName,
args,
txOverrides,
backendWallet = env.DEFAULT_BACKEND_WALLET,
simulateTransaction = env.ENGINE_TRANSACTION_SIMULATION_ENABLED,
},
} = req;
Expand Down Expand Up @@ -108,7 +109,7 @@ export const transactionsRoutes =
const { result } = await engine.contract.write(
chainId.toString(),
address,
req.backendWallet,
req.backendWallet ?? backendWallet,
{
abi: transactionAbi,
functionName,
Expand Down Expand Up @@ -160,6 +161,7 @@ export const transactionsRoutes =
value = "0x00",
data,
txOverrides,
backendWallet = env.DEFAULT_BACKEND_WALLET,
simulateTransaction = env.ENGINE_TRANSACTION_SIMULATION_ENABLED,
},
} = req;
Expand All @@ -176,7 +178,7 @@ export const transactionsRoutes =
Sentry.setExtra("transaction", { to, value, data });
const { result } = await engine.backendWallet.sendTransaction(
chainId.toString(),
req.backendWallet,
req.backendWallet ?? backendWallet,
{
toAddress: to,
value: value,
Expand Down

0 comments on commit 56d054b

Please sign in to comment.