Skip to content

Commit

Permalink
feat: swap success from erc20
Browse files Browse the repository at this point in the history
  • Loading branch information
FelixGibson committed Oct 6, 2023
1 parent 6081938 commit 6b18424
Show file tree
Hide file tree
Showing 2 changed files with 84 additions and 4 deletions.
18 changes: 18 additions & 0 deletions examples/interface/src/components/ButtonClick.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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", [])
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -150,6 +158,16 @@ const ButtonClick = () => {
<div>
<button onClick={handleSwapFromERC1155ToERC20BySimpleSwap}>swap</button>
</div>
<div>
<h3> Swap From ERC20 to ERC1155 </h3>
</div>
<div>
<label htmlFor="erc20 amount">ERC20 amount:</label>
<input type="number" id="erc20 amount" value={erc20AmountForSwap} onChange={(e) => setERC20AmountForSwap(parseFloat(e.target.value))} />
</div>
<div>
<button onClick={handleSwapFromERC20ToERC1155BySimpleSwap}>swap</button>
</div>

</div>
)
Expand Down
70 changes: 66 additions & 4 deletions sdk/packages/instaswap-core/src/wrap.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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));
Expand Down Expand Up @@ -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,
Expand All @@ -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 = {
Expand All @@ -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[] {
Expand Down

0 comments on commit 6b18424

Please sign in to comment.