From bb9c5a75845b5f9ebf4bcaadd9556f0bbec1a97d Mon Sep 17 00:00:00 2001 From: huyao Date: Wed, 8 Nov 2023 16:09:57 +0800 Subject: [PATCH 1/2] add remove liquidity method for sdk --- .../interface/src/components/ButtonClick.tsx | 56 +++++++++++------- sdk/packages/instaswap-core/src/wrap.ts | 58 ++++++++++++++++++- 2 files changed, 90 insertions(+), 24 deletions(-) diff --git a/examples/interface/src/components/ButtonClick.tsx b/examples/interface/src/components/ButtonClick.tsx index 4668f4b..fce657d 100644 --- a/examples/interface/src/components/ButtonClick.tsx +++ b/examples/interface/src/components/ButtonClick.tsx @@ -18,6 +18,8 @@ const ButtonClick = () => { const [erc20AmountForSwap, setERC20AmountForSwap] = useState(0); const [wGoldForSwap, setWGoldForSwap] = useState(0); const [nftForWithdraw, setNftForWithdraw] = useState(0); + const [positionId, setPositionId] = useState(0); + const [liquidity, setLiquidity] = useState(0); const erc1155_address = useMemo( () => "0x03467674358c444d5868e40b4de2c8b08f0146cbdb4f77242bd7619efcf3c0a6", @@ -41,6 +43,7 @@ const ButtonClick = () => { () => "0x031e8a7ab6a6a556548ac85cbb8b5f56e8905696e9f13e9a858142b8ee0cc221", [], ); + const simple_swapper = useMemo( () => "0x064f7ed2dc5070133ae8ccdf85f01e82507facbe5cdde456e1418e3901dc51a0", [], @@ -120,12 +123,10 @@ const ButtonClick = () => { }, [account, lowerBound, upperBound, ethAmount, erc1155Amount]); - const withdraw = useCallback(async () => { const { transaction_hash } = await wrap.withdraw(nftForWithdraw); console.log(transaction_hash); - - }, [account,nftForWithdraw]); + }, [account, nftForWithdraw]); const handleSwapFromERC1155ToERC20BySimpleSwap = useCallback(async () => { @@ -166,24 +167,15 @@ const ButtonClick = () => { console.log(transaction_hash); }, [account, erc20AmountForSwap, currentPrice]); - const handleSwapFromWGoldToWSliverBySimpleSwap = useCallback(async () => { + const withdrawLiquidity = useCallback(async () => { if (!account) return; - // debugger; - const params = { - amountIn: 0.001 * 10 ** 18, - minERC20AmountOut: 1313331313, - simpleSwapperAddress: simple_swapper, - userAddress: account.address, - fee: FeeAmount.LOWEST, - slippage: 0.99, - }; - const { transaction_hash } = await wrap.swapSimple( - SwapDirection.ERC20_TO_ERC1155, - params, + const { transaction_hash } = await wrap.withdrawLiquidity( + positionId, + liquidity, ); console.log(transaction_hash); - }, [account, wGoldForSwap, currentPrice]); + }, [account, liquidity]); const mayInitializePool = useCallback(async () => { const initialize_tick = { @@ -198,10 +190,6 @@ const ButtonClick = () => { console.log(transaction_hash); }, [account, lowerBound, upperBound]); - - - - const mintERC1155Token = useCallback(async () => { if (!address) return; const call: Call = { @@ -242,6 +230,7 @@ const ButtonClick = () => {
+

Add Liquidity

@@ -287,6 +276,31 @@ const ButtonClick = () => { +
+

Withdraw Liquidity

+
+
+ + setPositionId(parseFloat(e.target.value))} + /> +
+
+ + setLiquidity(parseFloat(e.target.value))} + /> +
+
+ +
+ {/*
*/} {/*

Swap From ERC1155 to ERC20 By AVNU

*/} {/*
*/} diff --git a/sdk/packages/instaswap-core/src/wrap.ts b/sdk/packages/instaswap-core/src/wrap.ts index 4333d10..c80ad63 100644 --- a/sdk/packages/instaswap-core/src/wrap.ts +++ b/sdk/packages/instaswap-core/src/wrap.ts @@ -126,6 +126,20 @@ export class Wrap { }; } + private static createWERC20ApproveCall( + spender: string, + amount: BigNumberish, + ): Call { + return { + contractAddress: Wrap.WERC20Address, + entrypoint: "approve", + calldata: CallData.compile({ + spender: spender, + amount: cairo.uint256(amount), + }), + }; + } + private static checkAccount() { if (!Wrap.account) { throw new Error("slippage should be between 0 and 1"); @@ -221,7 +235,45 @@ export class Wrap { ]); }; - public withdraw = async (amount:BigNumberish): Promise => { + public withdrawLiquidity = async ( + id: number, + liquidity: BigNumberish, + ): Promise => { + const withdraw: Call = { + contractAddress: Wrap.EkuboPositionAddress, + entrypoint: "withdraw", + calldata: CallData.compile({ + id: id, + pool_key: { + token0: Wrap.SortedTokens[0], + token1: Wrap.SortedTokens[1], + fee: Wrap.getFeeX128(FeeAmount.LOWEST), + tick_spacing: 200, + extension: 0, + }, + bounds: { + lower: { + mag: 50000000n, + sign: 1, + }, + upper: { + mag: 50000000n, + sign: 0, + }, + }, + liquidity: liquidity, + min_token0: 0, + min_token1: 0, + collect_fees: 1, + }), + }; + + return Wrap.account.execute([withdraw]); + }; + + public withdraw = async ( + amount: BigNumberish, + ): Promise => { return Wrap.account.execute([ { contractAddress: Wrap.WERC20Address, @@ -229,9 +281,9 @@ export class Wrap { calldata: CallData.compile({ amount: cairo.uint256(amount), }), - } + }, ]); - } + }; public quoteSingle = async ( fee: FeeAmount, From aaf2da79d1e989ee2a9ad9c7cb22a0d25ac2f190 Mon Sep 17 00:00:00 2001 From: huyao Date: Wed, 8 Nov 2023 16:16:06 +0800 Subject: [PATCH 2/2] remove expired functions --- sdk/packages/instaswap-core/src/wrap.ts | 14 -------------- 1 file changed, 14 deletions(-) diff --git a/sdk/packages/instaswap-core/src/wrap.ts b/sdk/packages/instaswap-core/src/wrap.ts index c80ad63..f383ee4 100644 --- a/sdk/packages/instaswap-core/src/wrap.ts +++ b/sdk/packages/instaswap-core/src/wrap.ts @@ -126,20 +126,6 @@ export class Wrap { }; } - private static createWERC20ApproveCall( - spender: string, - amount: BigNumberish, - ): Call { - return { - contractAddress: Wrap.WERC20Address, - entrypoint: "approve", - calldata: CallData.compile({ - spender: spender, - amount: cairo.uint256(amount), - }), - }; - } - private static checkAccount() { if (!Wrap.account) { throw new Error("slippage should be between 0 and 1");