Skip to content

Commit

Permalink
feat: extract SwapApi type, add default implementation WIP🚧
Browse files Browse the repository at this point in the history
  • Loading branch information
toniocodo committed Sep 1, 2023
1 parent 9cc9d16 commit 7ae33b2
Show file tree
Hide file tree
Showing 5 changed files with 62 additions and 62 deletions.
33 changes: 33 additions & 0 deletions libs/oeth/swap/src/actions/defaultApi.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import { isNilOrEmpty } from '@origin/shared/utils';

import { getAvailableRoutes } from '../utils';

import type { SwapApi, SwapState } from '../types';

const estimateAmount = async ({ tokenIn, tokenOut, amountIn }: SwapState) => {
if (amountIn === 0n) {
return 0n;
}

return amountIn;
};

const estimateRoutes = async ({ tokenIn, tokenOut, amountIn }: SwapState) => {
if (amountIn === 0n) {
return [];
}

return getAvailableRoutes(tokenIn, tokenOut);
};

const swap = async ({ tokenIn, tokenOut, amountIn, swapRoute }: SwapState) => {
if (amountIn === 0n || isNilOrEmpty(swapRoute)) {
return;
}
};

export default {
estimateAmount,
estimateRoutes,
swap,
} as SwapApi;
17 changes: 10 additions & 7 deletions libs/oeth/swap/src/actions/index.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
import defaultApi from './defaultApi';
import mintVault from './mintVault';
import redeemMix from './redeemMix';

export const swapActions = {
'swap-curve': mintVault,
'swap-zapper': redeemMix,
'mint-vault': redeemMix,
'redeem-mix': redeemMix,
'wrap-oeth': redeemMix,
'unwrap-woeth': redeemMix,
import type { SwapAction, SwapApi } from '../types';

export const swapActions: Record<SwapAction, SwapApi> = {
'swap-curve': { ...defaultApi, ...mintVault },
'swap-zapper': { ...defaultApi, ...redeemMix },
'mint-vault': { ...defaultApi },
'redeem-mix': { ...defaultApi },
'wrap-oeth': { ...defaultApi },
'unwrap-woeth': { ...defaultApi },
};
28 changes: 9 additions & 19 deletions libs/oeth/swap/src/actions/mintVault.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,9 @@ import { isNilOrEmpty } from '@origin/shared/utils';
import { readContract } from '@wagmi/core';
import { readContracts } from 'wagmi';

import type { SwapState } from '../types';
import type { SwapApi, SwapState } from '../types';

const estimateAmount = async ({
tokenIn,
tokenOut,
amountIn,
}: Pick<SwapState, 'tokenIn' | 'tokenOut' | 'amountIn'>) => {
const estimateAmount = async ({ tokenIn, tokenOut, amountIn }: SwapState) => {
if (amountIn === 0n) {
return 0n;
}
Expand All @@ -26,13 +22,9 @@ const estimateAmount = async ({
return amountIn;
};

const estimateRoutes = async ({
tokenIn,
tokenOut,
amountIn,
}: Pick<SwapState, 'tokenIn' | 'tokenOut' | 'amountIn'>) => {
const estimateRoutes = async ({ tokenIn, tokenOut, amountIn }: SwapState) => {
if (amountIn === 0n) {
return;
return [];
}

const data = await readContracts({
Expand All @@ -45,15 +37,13 @@ const estimateRoutes = async ({
},
],
});

console.log(data);

return [];
};

const swap = async ({
tokenIn,
tokenOut,
amountIn,
swapRoute,
}: Pick<SwapState, 'tokenIn' | 'tokenOut' | 'amountIn' | 'swapRoute'>) => {
const swap = async ({ tokenIn, tokenOut, amountIn, swapRoute }: SwapState) => {
if (amountIn === 0n || isNilOrEmpty(swapRoute)) {
return;
}
Expand All @@ -63,4 +53,4 @@ export default {
estimateAmount,
estimateRoutes,
swap,
};
} as Partial<SwapApi>;
40 changes: 4 additions & 36 deletions libs/oeth/swap/src/actions/redeemMix.ts
Original file line number Diff line number Diff line change
@@ -1,45 +1,13 @@
import { isNilOrEmpty } from '@origin/shared/utils';
import type { SwapApi, SwapState } from '../types';

import { getAvailableRoutes } from '../utils';

import type { SwapState } from '../types';

const estimateAmount = async ({
tokenIn,
tokenOut,
amountIn,
}: Pick<SwapState, 'tokenIn' | 'tokenOut' | 'amountIn'>) => {
const estimateAmount = async ({ tokenIn, tokenOut, amountIn }: SwapState) => {
if (amountIn === 0n) {
return 0n;
}

return amountIn;
};

const estimateRoutes = async ({
tokenIn,
tokenOut,
amountIn,
}: Pick<SwapState, 'tokenIn' | 'tokenOut' | 'amountIn'>) => {
if (amountIn === 0n) {
return;
}
return getAvailableRoutes(tokenIn, tokenOut);
};

const swap = async ({
tokenIn,
tokenOut,
amountIn,
swapRoute,
}: Pick<SwapState, 'tokenIn' | 'tokenOut' | 'amountIn' | 'swapRoute'>) => {
if (amountIn === 0n || isNilOrEmpty(swapRoute)) {
return;
}
return amountIn * 2n;
};

export default {
estimateAmount,
estimateRoutes,
swap,
};
} as Partial<SwapApi>;
6 changes: 6 additions & 0 deletions libs/oeth/swap/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,12 @@ export type SwapAction =
| 'wrap-oeth'
| 'unwrap-woeth';

export type SwapApi = {
estimateAmount: (state: SwapState) => Promise<bigint>;
estimateRoutes: (state: SwapState) => Promise<SwapRoute[]>;
swap: (state: SwapState) => Promise<void>;
};

export type SwapRoute = {
tokenIn: Token;
tokenOut: Token;
Expand Down

0 comments on commit 7ae33b2

Please sign in to comment.