Skip to content

Commit

Permalink
more refactor
Browse files Browse the repository at this point in the history
1. moved types to standalone file
2. passed account to wrap function as parameters
3. removed some useless code
  • Loading branch information
huyao committed Oct 11, 2023
1 parent 71071b2 commit 02e8447
Show file tree
Hide file tree
Showing 6 changed files with 165 additions and 148 deletions.
2 changes: 1 addition & 1 deletion examples/interface/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
"@starknet-react/core": "^1.0.1",
"react": "^18.2.0",
"react-dom": "^18.2.0",
"@bibliothecadao/instaswap-core": "link:@bibliothecadao/instaswap-core"
"@bibliothecadao/instaswap-core": "link:/instaswap/packages/instaswap-core"
},
"devDependencies": {
"@types/react": "^18.0.27",
Expand Down
44 changes: 21 additions & 23 deletions examples/interface/src/components/ButtonClick.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,12 @@ const ButtonClick = () => {
erc20Address:eth_address,
ekuboPositionAddress:ekubo_position_address,
ekuboCoreAddress:ekubo_core_address,
provider:provider
provider:provider,
account:account
}


const wrap = new Wrap(config);
const wrap = new Wrap(config);

const getERC1155Balance = useCallback(async () => {
if (!address) return;
Expand All @@ -60,7 +61,7 @@ const ButtonClick = () => {
return () => clearInterval(interval);
}, [getERC1155Balance]);

const handleAddLiquidity = useCallback(() => {
const handleAddLiquidity = useCallback(async () => {

if (!account) return;

Expand All @@ -73,14 +74,12 @@ const ButtonClick = () => {
};

//add liquidity
account?.execute(wrap.addLiquidity(params));
const { transaction_hash } = await wrap.addLiquidity(params);
console.log(transaction_hash);

// const realERC1155Amount = erc1155Amount;
// const realERC20Amount = ethAmount * (10 **18);
// account?.execute(wrap.addLiquidity(realERC1155Amount, realERC20Amount, FeeAmount.LOWEST, lowerBound, upperBound))
}, [account, lowerBound, upperBound, ethAmount, erc1155Amount])

const handleSwapFromERC1155ToERC20ByAVNU = useCallback(() => {
const handleSwapFromERC1155ToERC20ByAVNU = useCallback(async () => {
if (!account) return;

const params = {
Expand All @@ -92,14 +91,11 @@ const ButtonClick = () => {
slippage: 0.99,
currentPrice: currentPrice,
}

account?.execute(wrap.swapFromERC1155ToERC20ByAVNU(params));
//
// const realERC1155Amount = erc1155AmountForSwap;
// account?.execute(wrap.swapFromERC1155ToERC20ByAVNU(realERC1155Amount, 1313331313, avnu_address, account.address, FeeAmount.LOWEST, 0.99, currentPrice))
const { transaction_hash } = await wrap.swapFromERC1155ToERC20ByAVNU(params);
console.log(transaction_hash);
}, [account, erc1155AmountForSwap, currentPrice, avnu_address])

const handleSwapFromERC1155ToERC20BySimpleSwap = useCallback(() => {
const handleSwapFromERC1155ToERC20BySimpleSwap = useCallback(async () => {
if (!account) return;

const params = {
Expand All @@ -110,12 +106,12 @@ const ButtonClick = () => {
fee: FeeAmount.LOWEST,
slippage: 0.99,
}
account?.execute(wrap.swapBySimple(SwapDirection.ERC1155_TO_ERC20,params))
// const realERC1155Amount = erc1155AmountForSwap;
// account?.execute(wrap.swapFromERC1155ToERC20BySimpleSwapper(realERC1155Amount, 1313331313, simple_swapper, account.address, FeeAmount.LOWEST, 0.99, currentPrice))

const { transaction_hash } = await wrap.swapSimple(SwapDirection.ERC1155_TO_ERC20,params);
console.log(transaction_hash);
}, [account, erc1155AmountForSwap, currentPrice, avnu_address])

const handleSwapFromERC20ToERC1155BySimpleSwap = useCallback(() => {
const handleSwapFromERC20ToERC1155BySimpleSwap = useCallback(async () => {
if (!account) return;
// debugger;
const params = {
Expand All @@ -126,17 +122,19 @@ const ButtonClick = () => {
fee: FeeAmount.LOWEST,
slippage: 0.99,
}
account?.execute(wrap.swapBySimple(SwapDirection.ERC20_TO_ERC1155,params));
// const realERC1155Amount = erc20AmountForSwap * (10 **18);
// account?.execute(wrap.swapFromERC20ToERC1155BySimpleSwapper(realERC1155Amount, 1313331313, simple_swapper, account.address, FeeAmount.LOWEST, 0.99, currentPrice))

const { transaction_hash } = await wrap.swapSimple(SwapDirection.ERC20_TO_ERC1155,params);
console.log(transaction_hash);
}, [account, erc20AmountForSwap, currentPrice, avnu_address])

const mayInitializePool = useCallback(() => {
const mayInitializePool = useCallback(async () => {
const initialize_tick = {
mag: 0n,
sign: false
}
account?.execute(wrap.mayInitializePool(FeeAmount.LOWEST, initialize_tick))

const { transaction_hash } = await wrap.mayInitializePool(FeeAmount.LOWEST, initialize_tick);
console.log(transaction_hash);
}, [account, lowerBound, upperBound])

const mintERC1155Token = useCallback(async () => {
Expand Down
5 changes: 3 additions & 2 deletions sdk/packages/instaswap-core/src/constants.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
export const MAX_SQRT_RATIO = 6277100250585753475930931601400621808602321654880405518632n;
export const MIN_SQRT_RATIO = 18446748437148339061n;
export const MAX_SQRT_RATIO = 6277100250585753475930931601400621808602321654880405518632n;
export const MIN_SQRT_RATIO = 18446748437148339061n;
export const AVNU_SQRT_RATIO = 363034526046013994104916607590000000000000000000001n;

/**
* The default factory enabled fee amounts, denominated in hundredths of bips.
Expand Down
1 change: 0 additions & 1 deletion sdk/packages/instaswap-core/src/tickMath.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { Decimal } from 'decimal.js-light';
// import JSBI from 'jsbi';


export abstract class TickMath {
Expand Down
40 changes: 40 additions & 0 deletions sdk/packages/instaswap-core/src/types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import {AccountInterface, BigNumberish, Provider} from "starknet";
import {FeeAmount} from "./constants";

export type Config = {
erc1155Address: string;
werc20Address:string;
erc20Address:string;
ekuboPositionAddress:string;
ekuboCoreAddress:string;
account:AccountInterface | undefined;
provider?:Provider;
};


export type LiquidityParams = {
erc1155Amount: BigNumberish;
erc20Amount: BigNumberish;
fee: FeeAmount;
lowerPrice: number;
upperPrice: number;
}


export type AVNUSwapParams = SwapParams & {
erc1155AmountIn: BigNumberish;
aggregatorAddress: string;
}

export type SimpleSwapParams = SwapParams & {
simpleSwapperAddress: string;
amountIn: BigNumberish;
}


type SwapParams = {
minERC20AmountOut: BigNumberish;
userAddress: string;
fee: FeeAmount;
slippage: number;
}
Loading

0 comments on commit 02e8447

Please sign in to comment.