Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add eth based swap support #73

Merged
merged 1 commit into from
Aug 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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