Skip to content

Commit

Permalink
feat: add swap endpoint to magicswap (#63)
Browse files Browse the repository at this point in the history
* feat: add magicswap to core api

* chore: add changeset
  • Loading branch information
cdotta authored Jul 24, 2024
1 parent 3d4fc51 commit eeb81b0
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 4 deletions.
5 changes: 5 additions & 0 deletions .changeset/grumpy-clouds-melt.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@treasure-dev/tdk-core": minor
---

Added magicswap helpers to tdk api client
10 changes: 6 additions & 4 deletions apps/api/src/routes/magicswap.ts
Original file line number Diff line number Diff line change
Expand Up @@ -186,10 +186,12 @@ export const magicswapRoutes =
wagmiConfig,
});

const poolTokens = pools.reduce((acc, poolToken) => {
acc[poolToken.id] = poolToken;
return acc;
}, {});
const poolTokens = pools
.flatMap(({ token0, token1 }) => [token0, token1])
.reduce((acc, poolToken) => {
acc[poolToken.id] = poolToken;
return acc;
}, {});

const tokenIn = poolTokens[tokenInId];
const tokenOut = poolTokens[tokenOutId];
Expand Down
21 changes: 21 additions & 0 deletions packages/core/src/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,12 @@ import type {
CreateTransactionBody,
LoginBody,
LoginReply,
PoolReply,
PoolsReply,
ReadCurrentUserSessionsQuerystring,
ReadCurrentUserSessionsReply,
ReadLoginPayloadReply,
RouteReply,
} from "../../../apps/api/src/schema";
import type {
CreateTransactionReply,
Expand All @@ -24,6 +27,10 @@ import type {
ReadProjectReply,
ReadTransactionReply,
} from "../../../apps/api/src/schema";
import type {
RouteBody,
SwapBody,
} from "../../../apps/api/src/schema/magicswap";
import { DEFAULT_TDK_API_BASE_URI, DEFAULT_TDK_CHAIN_ID } from "./constants";

// @ts-expect-error: Patch BigInt for JSON serialization
Expand Down Expand Up @@ -251,4 +258,18 @@ export class TDKAPI {
harvester = {
get: (id: string) => this.get<ReadHarvesterReply>(`/harvesters/${id}`),
};

magicswap = {
getPools: () => this.get<PoolsReply>("/magicswap/pools"),
getPool: (id: string) => this.get<PoolReply>(`/magicswap/pools/${id}`),
getRoute: (body: RouteBody) =>
this.post<RouteBody, RouteReply>("/magicswap/route", body),
swap: async (body: SwapBody, waitForCompletion = true) => {
const result = await this.post<SwapBody, CreateTransactionReply>(
"/magicswap/swap",
body,
);
return waitForCompletion ? this.transaction.wait(result.queueId) : result;
},
};
}

0 comments on commit eeb81b0

Please sign in to comment.