diff --git a/examples/interface/src/components/ButtonClick.tsx b/examples/interface/src/components/ButtonClick.tsx index 0673a55..5adf71c 100644 --- a/examples/interface/src/components/ButtonClick.tsx +++ b/examples/interface/src/components/ButtonClick.tsx @@ -16,6 +16,7 @@ const ButtonClick = () => { const [erc1155Amount, setAddLiquidityERC1155Amount] = useState(0); const [ethAmount, setAddLiquidityEthAmount] = useState(0); const [erc1155AmountForSwap, setERC1155AmountForSwap] = useState(0); + const [erc20AmountForSwap, setERC20AmountForSwap] = useState(0); const erc1155_address = useMemo(() => "0x03467674358c444d5868e40b4de2c8b08f0146cbdb4f77242bd7619efcf3c0a6", []) const werc20_address = useMemo(() => "0x06b09e4c92a08076222b392c77e7eab4af5d127188082713aeecbe9013003bf4", []) @@ -73,6 +74,13 @@ const ButtonClick = () => { account?.execute(wrap.swapFromERC1155ToERC20BySimpleSwapper(realERC1155Amount, 1313331313, simple_swapper, account.address, FeeAmount.LOWEST, 0.99, currentPrice)) }, [account, erc1155AmountForSwap, currentPrice, avnu_address]) + const handleSwapFromERC20ToERC1155BySimpleSwap = useCallback(() => { + if (!account) return; + debugger; + const realERC1155Amount = erc20AmountForSwap * (10 **18); + account?.execute(wrap.swapFromERC20ToERC1155BySimpleSwapper(realERC1155Amount, 1313331313, simple_swapper, account.address, FeeAmount.LOWEST, 0.99, currentPrice)) + }, [account, erc20AmountForSwap, currentPrice, avnu_address]) + const mayInitializePool = useCallback(() => { let initialize_tick = { mag: 0n, @@ -150,6 +158,16 @@ const ButtonClick = () => {
+
+

Swap From ERC20 to ERC1155

+
+
+ + setERC20AmountForSwap(parseFloat(e.target.value))} /> +
+
+ +
) diff --git a/sdk/packages/instaswap-core/src/wrap.ts b/sdk/packages/instaswap-core/src/wrap.ts index b97934c..f9fd5fe 100644 --- a/sdk/packages/instaswap-core/src/wrap.ts +++ b/sdk/packages/instaswap-core/src/wrap.ts @@ -238,7 +238,6 @@ export class Wrap { } public swapFromERC1155ToERC20BySimpleSwapper(erc1155AmountIn: BigNumberish, minERC20AmountOut: BigNumberish, simpleSwapperAddress: string, userAddress: string, fee: FeeAmount, slippage: number, currentPrice: number) { - debugger; // sort tokens // TODO check length const sortedTokens: Contract[] = [Wrap.ERC20Contract, Wrap.WERC20Contract].sort((a, b) => a.address.localeCompare(b.address)); @@ -274,6 +273,8 @@ export class Wrap { amount: cairo.uint256(BigInt(erc1155AmountIn) * (BigInt(10 ** 18))) // wrap token has 18 decimals }) } + let isToken1 = (Wrap.ERC20Contract.address > Wrap.WERC20Contract.address) ? true : false; + let sqrt_ratio_limit = (Wrap.ERC20Contract.address > Wrap.WERC20Contract.address) ? MAX_SQRT_RATIO : MIN_SQRT_RATIO; let tmp = { pool_key: { token0: sortedTokens[0].address, @@ -287,12 +288,12 @@ export class Wrap { mag: werc20AmountIn, sign: false }, - is_token1: true, - sqrt_ratio_limit: cairo.uint256(MAX_SQRT_RATIO), + is_token1: isToken1, + sqrt_ratio_limit: cairo.uint256(sqrt_ratio_limit), skip_ahead: 4294967295, }, recipient: userAddress, - calculated_amount_threshold: 12000000, + calculated_amount_threshold: 0, }; // swap const simpleSwap: Call = { @@ -315,7 +316,68 @@ export class Wrap { }) } return [approveForAll, depositToWERC20, transferWERC20, simpleSwap, clearToken0, clearToken1]; + } + public swapFromERC20ToERC1155BySimpleSwapper(erc20AmountIn: BigNumberish, minERC1155AmountOut: BigNumberish, simpleSwapperAddress: string, userAddress: string, fee: FeeAmount, slippage: number, currentPrice: number) { + // sort tokens + // TODO check length + const sortedTokens: Contract[] = [Wrap.ERC20Contract, Wrap.WERC20Contract].sort((a, b) => a.address.localeCompare(b.address)); + if (slippage < 0 || slippage > 1) { + throw new Error("slippage should be between 0 and 1"); + } + + // transfer werc20 + const transferERC20: Call = { + contractAddress: Wrap.ERC20Contract.address, + entrypoint: "transfer", + calldata: CallData.compile({ + recipient: simpleSwapperAddress, + amount: cairo.uint256(erc20AmountIn) + }) + } + let isToken1 = (Wrap.ERC20Contract.address > Wrap.WERC20Contract.address) ? true : false; + let sqrt_ratio_limit = (Wrap.ERC20Contract.address > Wrap.WERC20Contract.address) ? MAX_SQRT_RATIO : MIN_SQRT_RATIO; + let tmp = { + pool_key: { + token0: sortedTokens[0].address, + token1: sortedTokens[1].address, + fee: Wrap.getFeeX128(fee), + tick_spacing: 200, + extension: 0, + }, + swap_params: { + amount: { + mag: erc20AmountIn, + sign: false + }, + is_token1: isToken1, + sqrt_ratio_limit: cairo.uint256(sqrt_ratio_limit), + skip_ahead: 4294967295, + }, + recipient: userAddress, + calculated_amount_threshold: 0, + }; + // swap + const simpleSwap: Call = { + contractAddress: simpleSwapperAddress, + entrypoint: "swap", + calldata: CallData.compile(tmp) + } + const clearToken0: Call = { + contractAddress: simpleSwapperAddress, + entrypoint: "clear", + calldata: CallData.compile({ + token: sortedTokens[0].address + }) + } + const clearToken1: Call = { + contractAddress: simpleSwapperAddress, + entrypoint: "clear", + calldata: CallData.compile({ + token: sortedTokens[1].address + }) + } + return [transferERC20, simpleSwap, clearToken0, clearToken1]; } public mayInitializePool(fee: FeeAmount, initial_tick: { mag: BigNumberish, sign: boolean }): Call[] {