Skip to content

Commit

Permalink
add toAddress field to all write functions
Browse files Browse the repository at this point in the history
  • Loading branch information
alecananian committed Nov 13, 2024
1 parent 24566a5 commit c3f1bdc
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 33 deletions.
39 changes: 33 additions & 6 deletions apps/api/src/routes/magicswap.ts
Original file line number Diff line number Diff line change
Expand Up @@ -190,11 +190,19 @@ export const magicswapRoutes =
nftsOut,
isExactOut,
slippage,
toAddress,
toAddress = req.userAddress,
},
chain,
} = req;

if (!toAddress) {
throw new TdkError({
name: TDK_ERROR_NAMES.MagicswapError,
code: TDK_ERROR_CODES.MAGICSWAP_SWAP_FAILED,
message: "No toAddress provided",
});
}

const pools = await fetchPoolsForSwap({ chainId: chain.id });
const poolTokens = pools
.flatMap(({ token0, token1 }) => [token0, token1])
Expand Down Expand Up @@ -260,6 +268,7 @@ export const magicswapRoutes =
nftsOut,
isExactOut,
slippage,
toAddress,
backendWallet = req.backendWallet,
simulateTransaction = env.ENGINE_TRANSACTION_SIMULATION_ENABLED,
},
Expand Down Expand Up @@ -306,7 +315,7 @@ export const magicswapRoutes =
value,
} = createSwapArgs({
chainId: chain.id,
toAddress: userAddress,
toAddress: (toAddress as Address | undefined) ?? userAddress,
tokenIn,
tokenOut,
nftsIn,
Expand Down Expand Up @@ -372,12 +381,20 @@ export const magicswapRoutes =
amount1Min,
nfts0,
nfts1,
toAddress,
toAddress = req.userAddress,
},
chain,
params,
} = req;

if (!toAddress) {
throw new TdkError({
name: TDK_ERROR_NAMES.MagicswapError,
code: TDK_ERROR_CODES.MAGICSWAP_ADD_LIQUIDITY_FAILED,
message: "No toAddress provided",
});
}

try {
const pool = await fetchPoolForLiquidity({
chainId: chain.id,
Expand Down Expand Up @@ -435,6 +452,7 @@ export const magicswapRoutes =
amount1Min,
nfts0,
nfts1,
toAddress,
backendWallet = req.backendWallet,
simulateTransaction = env.ENGINE_TRANSACTION_SIMULATION_ENABLED,
},
Expand Down Expand Up @@ -474,7 +492,7 @@ export const magicswapRoutes =
value,
} = createAddLiquidityArgs({
chainId: chain.id,
toAddress: userAddress,
toAddress: (toAddress as Address | undefined) ?? userAddress,
amount0: amount0 ? BigInt(amount0) : undefined,
amount1: amount1 ? BigInt(amount1) : undefined,
amount0Min: amount0Min ? BigInt(amount0Min) : undefined,
Expand Down Expand Up @@ -538,12 +556,20 @@ export const magicswapRoutes =
nfts0,
nfts1,
swapLeftover = true,
toAddress,
toAddress = req.userAddress,
},
chain,
params,
} = req;

if (!toAddress) {
throw new TdkError({
name: TDK_ERROR_NAMES.MagicswapError,
code: TDK_ERROR_CODES.MAGICSWAP_REMOVE_LIQUIDITY_FAILED,
message: "No toAddress provided",
});
}

try {
const pool = await fetchPoolForLiquidity({
chainId: chain.id,
Expand Down Expand Up @@ -601,6 +627,7 @@ export const magicswapRoutes =
nfts0,
nfts1,
swapLeftover = true,
toAddress,
backendWallet = req.backendWallet,
simulateTransaction = env.ENGINE_TRANSACTION_SIMULATION_ENABLED,
},
Expand Down Expand Up @@ -640,7 +667,7 @@ export const magicswapRoutes =
value,
} = createRemoveLiquidityArgs({
chainId: chain.id,
toAddress: userAddress,
toAddress: (toAddress as Address | undefined) ?? userAddress,
amountLP: BigInt(amountLP),
amount0Min: BigInt(amount0Min),
amount1Min: BigInt(amount1Min),
Expand Down
42 changes: 15 additions & 27 deletions apps/api/src/schema/magicswap.ts
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,7 @@ const nftInputSchema = Type.Object({
quantity: Type.Number({ description: "The quantity of the NFT" }),
});

const swapBaseBodySchema = Type.Object({
export const swapArgsBodySchema = Type.Object({
tokenInId: Type.String({
description: "The unique identifier of the `in` token",
}),
Expand All @@ -212,24 +212,20 @@ const swapBaseBodySchema = Type.Object({
Type.Array(nftInputSchema, { description: "Array of NFTs to swap out" }),
),
slippage: Type.Optional(Type.Number({ description: "Slippage tolerance" })),
toAddress: Type.Optional(
Type.String({ description: "Address to send tokens" }),
),
});

export const swapArgsBodySchema = Type.Intersect([
swapBaseBodySchema,
Type.Object({
toAddress: Type.String({ description: "Address to send tokens" }),
}),
]);

export const swapBodySchema = Type.Intersect([
swapBaseBodySchema,
swapArgsBodySchema,
Type.Object({
backendWallet: Type.Optional(Type.String()),
simulateTransaction: Type.Optional(Type.Boolean()),
}),
]);

const addLiquidityBaseBodySchema = Type.Object({
export const addLiquidityArgsBodySchema = Type.Object({
nfts0: Type.Optional(
Type.Array(nftInputSchema, { description: "Array of NFTs for token0" }),
),
Expand All @@ -244,24 +240,20 @@ const addLiquidityBaseBodySchema = Type.Object({
amount1Min: Type.Optional(
Type.String({ description: "Minimum amount for token1" }),
),
toAddress: Type.Optional(
Type.String({ description: "Address to send LP tokens" }),
),
});

export const addLiquidityArgsBodySchema = Type.Intersect([
addLiquidityBaseBodySchema,
Type.Object({
toAddress: Type.String({ description: "Address to send LP tokens" }),
}),
]);

export const addLiquidityBodySchema = Type.Intersect([
addLiquidityBaseBodySchema,
addLiquidityArgsBodySchema,
Type.Object({
backendWallet: Type.Optional(Type.String()),
simulateTransaction: Type.Optional(Type.Boolean()),
}),
]);

const removeLiquidityBaseBodySchema = Type.Object({
export const removeLiquidityArgsBodySchema = Type.Object({
nfts0: Type.Optional(
Type.Array(nftInputSchema, { description: "Array of NFTs for token0" }),
),
Expand All @@ -277,17 +269,13 @@ const removeLiquidityBaseBodySchema = Type.Object({
description: "Boolean indicating if swap leftover",
}),
),
toAddress: Type.Optional(
Type.String({ description: "Address to send tokens" }),
),
});

export const removeLiquidityArgsBodySchema = Type.Intersect([
removeLiquidityBaseBodySchema,
Type.Object({
toAddress: Type.String({ description: "Address to send tokens" }),
}),
]);

export const removeLiquidityBodySchema = Type.Intersect([
removeLiquidityBaseBodySchema,
removeLiquidityArgsBodySchema,
Type.Object({
backendWallet: Type.Optional(Type.String()),
simulateTransaction: Type.Optional(Type.Boolean()),
Expand Down

0 comments on commit c3f1bdc

Please sign in to comment.