Skip to content

Commit

Permalink
feat: add eth based swap support (#73)
Browse files Browse the repository at this point in the history
  • Loading branch information
cdotta authored Aug 5, 2024
1 parent 72d45e9 commit ea0053b
Show file tree
Hide file tree
Showing 3 changed files with 106 additions and 12 deletions.
5 changes: 5 additions & 0 deletions .changeset/rude-forks-knock.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@treasure-dev/tdk-core": minor
---

add eth based swap support
5 changes: 5 additions & 0 deletions apps/api/src/routes/magicswap.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
108 changes: 96 additions & 12 deletions packages/core/src/magicswap/swap.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ export const getSwapArgs = ({
address: AddressString;
functionName: string;
args: (string | string[])[];
value?: bigint;
} => {
const contractAddresses = getContractAddresses(chainId);
const magicSwapV2RouterAddress = contractAddresses.MagicswapV2Router;
Expand All @@ -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(
Expand All @@ -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",
Expand All @@ -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",
Expand All @@ -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",
Expand All @@ -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",
Expand Down

0 comments on commit ea0053b

Please sign in to comment.