diff --git a/.changeset/rude-forks-knock.md b/.changeset/rude-forks-knock.md new file mode 100644 index 00000000..48bf335f --- /dev/null +++ b/.changeset/rude-forks-knock.md @@ -0,0 +1,5 @@ +--- +"@treasure-dev/tdk-core": minor +--- + +add eth based swap support diff --git a/apps/api/src/routes/magicswap.ts b/apps/api/src/routes/magicswap.ts index 1fb68ced..e29c5ccf 100644 --- a/apps/api/src/routes/magicswap.ts +++ b/apps/api/src/routes/magicswap.ts @@ -241,6 +241,11 @@ export const magicswapRoutes = abi: magicSwapV2RouterABI, functionName: swapArguments.functionName, args: swapArguments.args as string[], + txOverrides: swapArguments.value + ? { + value: swapArguments.value.toString(), + } + : undefined, }, false, undefined, diff --git a/packages/core/src/magicswap/swap.ts b/packages/core/src/magicswap/swap.ts index be0a553b..9f0bdad5 100644 --- a/packages/core/src/magicswap/swap.ts +++ b/packages/core/src/magicswap/swap.ts @@ -37,6 +37,7 @@ export const getSwapArgs = ({ address: AddressString; functionName: string; args: (string | string[])[]; + value?: bigint; } => { const contractAddresses = getContractAddresses(chainId); const magicSwapV2RouterAddress = contractAddresses.MagicswapV2Router; @@ -49,7 +50,8 @@ export const getSwapArgs = ({ const collectionsIn = nftsIn.map(() => collectionId as AddressString); const tokenIdsIn = nftsIn.map(({ id }) => id); const quantitiesIn = nftsIn.map(({ quantity }) => quantity.toString()); - // To NFT + + // NFT-NFT if (tokenOut.isNFT) { const collectionIdOut = tokenOut.collectionId; const collectionsOut = nftsOut.map( @@ -75,11 +77,28 @@ export const getSwapArgs = ({ }; } - // To Token const amountOutMin = isExactOut ? amountOut : getAmountMin(amountOut, slippage); + // NFT-ETH + if (tokenOut.isETH) { + return { + address: magicSwapV2RouterAddress, + functionName: "swapNftForETH", + args: [ + collectionsIn, + tokenIdsIn, + quantitiesIn, + amountOutMin.toString(), + path, + toAddress, + deadline, + ], + }; + } + + // NFT-ERC20 return { address: magicSwapV2RouterAddress, functionName: "swapNftForTokens", @@ -106,6 +125,24 @@ export const getSwapArgs = ({ const tokenIdsOut = nftsOut.map(({ id }) => id); const quantitiesOut = nftsOut.map(({ quantity }) => quantity.toString()); + // ETH-NFT + if (tokenOut.isETH) { + return { + address: magicSwapV2RouterAddress, + functionName: "swapETHForNft", + args: [ + collectionsOut, + tokenIdsOut, + quantitiesOut, + path, + toAddress, + deadline, + ], + value: amountInMax, + }; + } + + // ERC20-NFT return { address: magicSwapV2RouterAddress, functionName: "swapTokensForNft", @@ -121,12 +158,63 @@ export const getSwapArgs = ({ }; } - // From Token to Token Exact Out - if (isExactOut) { - const amountInMax = isExactOut - ? getAmountMax(amountIn, slippage) - : amountIn; + const amountInMax = isExactOut ? getAmountMax(amountIn, slippage) : amountIn; + const amountOutMin = isExactOut + ? amountOut + : getAmountMin(amountOut, slippage); + + if (tokenIn.isETH) { + // ETH-ERC20 exact out + if (isExactOut) { + return { + address: magicSwapV2RouterAddress, + functionName: "swapETHForExactTokens", + args: [amountOut.toString(), path, toAddress, deadline], + value: amountInMax, + }; + } + // ETH-ERC20 exact in + return { + address: magicSwapV2RouterAddress, + functionName: "swapExactETHForTokens", + args: [amountOutMin.toString(), path, toAddress, deadline], + value: amountIn, + }; + } + + if (tokenOut.isETH) { + // ERC20-ETH exact out + if (isExactOut) { + return { + address: magicSwapV2RouterAddress, + functionName: "swapTokensForExactETH", + args: [ + amountOut.toString(), + amountInMax.toString(), + path, + toAddress, + deadline, + ], + }; + } + + // ERC20-ETH exact in + return { + address: magicSwapV2RouterAddress, + functionName: "swapExactTokensForETH", + args: [ + amountIn.toString(), + amountOutMin.toString(), + path, + toAddress, + deadline, + ], + }; + } + + // ERC20-ERC20 exact out + if (isExactOut) { return { address: magicSwapV2RouterAddress, functionName: "swapTokensForExactTokens", @@ -140,11 +228,7 @@ export const getSwapArgs = ({ }; } - // From Token to Token Exact In - const amountOutMin = isExactOut - ? amountOut - : getAmountMin(amountOut, slippage); - + // ERC20-ERC20 exact in return { address: magicSwapV2RouterAddress, functionName: "swapExactTokensForTokens",