Skip to content

Commit

Permalink
sdk-refactor
Browse files Browse the repository at this point in the history
refactor wrap.ts code.  Move duplicate code to standalone function.
  • Loading branch information
huyao committed Oct 10, 2023
1 parent c30f410 commit 71071b2
Show file tree
Hide file tree
Showing 8 changed files with 411 additions and 356 deletions.
5 changes: 4 additions & 1 deletion examples/interface/.eslintrc.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,8 @@
},
"plugins": ["react", "@typescript-eslint"],
"root": true,
"rules": {}
"rules": {
"no-unused-vars": "off",
"@typescript-eslint/no-unused-vars": ["off"]
}
}
1 change: 1 addition & 0 deletions examples/interface/.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ lerna-debug.log*
node_modules
dist
dist-ssr
.vite
*.local

# Editor directories and files
Expand Down
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:instaswap/packages/instaswap-core"
"@bibliothecadao/instaswap-core": "link:@bibliothecadao/instaswap-core"
},
"devDependencies": {
"@types/react": "^18.0.27",
Expand Down
110 changes: 86 additions & 24 deletions examples/interface/src/components/ButtonClick.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { useAccount, useConnectors } from '@starknet-react/core'
import { useCallback, useMemo, useState, useEffect } from 'react'
import { Contract, uint256, CallData, RawArgs, Call, num } from 'starknet'
import { Wrap } from '@bibliothecadao/instaswap-core'
import { FeeAmount } from '@bibliothecadao/instaswap-core'
import { FeeAmount,SwapDirection } from '@bibliothecadao/instaswap-core'
import { Provider, constants, cairo } from "starknet"


Expand All @@ -27,17 +27,22 @@ const ButtonClick = () => {
const simple_swapper = useMemo(() => "0x064f7ed2dc5070133ae8ccdf85f01e82507facbe5cdde456e1418e3901dc51a0", [])
const provider = new Provider({ sequencer: { network: constants.NetworkName.SN_GOERLI } });

let wrap = new Wrap(
erc1155_address,
werc20_address,
eth_address,
ekubo_position_address,
ekubo_core_address,
provider
)

const config = {
erc1155Address: erc1155_address,
werc20Address:werc20_address,
erc20Address:eth_address,
ekuboPositionAddress:ekubo_position_address,
ekuboCoreAddress:ekubo_core_address,
provider:provider
}


const wrap = new Wrap(config);

const getERC1155Balance = useCallback(async () => {
if (!address) return;
let b = await Wrap.getERC1155Balance(address, 1);
const b = await Wrap.getERC1155Balance(address, 1);
setBalance(b.toString());
}, [address, erc1155_address]);

Expand All @@ -56,33 +61,78 @@ const ButtonClick = () => {
}, [getERC1155Balance]);

const handleAddLiquidity = useCallback(() => {
if (!account) return;
const realERC1155Amount = erc1155Amount;
const realERC20Amount = ethAmount * (10 **18);
account?.execute(wrap.addLiquidity(realERC1155Amount, realERC20Amount, FeeAmount.LOWEST, lowerBound, upperBound))

if (!account) return;

const params = {
erc1155Amount: erc1155Amount,
erc20Amount: ethAmount * (10 **18),
fee: FeeAmount.LOWEST,
lowerPrice: lowerBound,
upperPrice:upperBound,
};

//add liquidity
account?.execute(wrap.addLiquidity(params));

// 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(() => {
if (!account) return;
const realERC1155Amount = erc1155AmountForSwap;
account?.execute(wrap.swapFromERC1155ToERC20ByAVNU(realERC1155Amount, 1313331313, avnu_address, account.address, FeeAmount.LOWEST, 0.99, currentPrice))

const params = {
erc1155AmountIn: erc1155AmountForSwap,
minERC20AmountOut: 1313331313,
aggregatorAddress: avnu_address,
userAddress: account.address,
fee: FeeAmount.LOWEST,
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))
}, [account, erc1155AmountForSwap, currentPrice, avnu_address])

const handleSwapFromERC1155ToERC20BySimpleSwap = useCallback(() => {
if (!account) return;
const realERC1155Amount = erc1155AmountForSwap;
account?.execute(wrap.swapFromERC1155ToERC20BySimpleSwapper(realERC1155Amount, 1313331313, simple_swapper, account.address, FeeAmount.LOWEST, 0.99, currentPrice))

const params = {
amountIn: erc1155AmountForSwap,
minERC20AmountOut: 1313331313,
simpleSwapperAddress: simple_swapper,
userAddress: account.address,
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))
}, [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))
// debugger;
const params = {
amountIn: erc20AmountForSwap * (10 **18),
minERC20AmountOut: 1313331313,
simpleSwapperAddress: simple_swapper,
userAddress: account.address,
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))
}, [account, erc20AmountForSwap, currentPrice, avnu_address])

const mayInitializePool = useCallback(() => {
let initialize_tick = {
const initialize_tick = {
mag: 0n,
sign: false
}
Expand Down Expand Up @@ -148,8 +198,20 @@ const ButtonClick = () => {
<div>
<button onClick={handleAddLiquidity}>add liquidity</button>
</div>

{/*<div>*/}
{/* <h3> Swap From ERC1155 to ERC20 By AVNU </h3>*/}
{/*</div>*/}
{/*<div>*/}
{/* <label htmlFor="erc1155 amount">ERC1155 amount:</label>*/}
{/* <input type="number" id="erc1155 amount" value={erc1155AmountForSwap} onChange={(e) => setERC1155AmountForSwap(parseFloat(e.target.value))} />*/}
{/*</div>*/}
{/*<div>*/}
{/* <button onClick={handleSwapFromERC1155ToERC20ByAVNU}>swap</button>*/}
{/*</div>*/}

<div>
<h3> Swap From ERC1155 to ERC20 </h3>
<h3> Swap From ERC1155 to ERC20 By SimpleSwapper </h3>
</div>
<div>
<label htmlFor="erc1155 amount">ERC1155 amount:</label>
Expand All @@ -159,7 +221,7 @@ const ButtonClick = () => {
<button onClick={handleSwapFromERC1155ToERC20BySimpleSwap}>swap</button>
</div>
<div>
<h3> Swap From ERC20 to ERC1155 </h3>
<h3> Swap From ERC20 to ERC1155 By SimpleSwapper</h3>
</div>
<div>
<label htmlFor="erc20 amount">ERC20 amount:</label>
Expand Down
Binary file modified sdk/bun.lockb
Binary file not shown.
28 changes: 21 additions & 7 deletions sdk/packages/instaswap-core/src/constants.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
export const MAX_SQRT_RATIO = 6277100250585753475930931601400621808602321654880405518632n;
export const MIN_SQRT_RATIO = 18446748437148339061n;

/**
* The default factory enabled fee amounts, denominated in hundredths of bips.
Expand All @@ -7,14 +9,26 @@ export enum FeeAmount {
LOW = 500,
MEDIUM = 3000,
HIGH = 10000
}
/**
* The default factory tick spacings by fee amount.
*/
export const TICK_SPACINGS: { [amount in FeeAmount]: number } = {
}

/**
* The default factory tick spacings by fee amount.
*/
export const TICK_SPACINGS: { [amount in FeeAmount]: number } = {
[FeeAmount.LOWEST]: 200,
[FeeAmount.LOW]: 1000,
[FeeAmount.MEDIUM]: 5096,
[FeeAmount.HIGH]: 10000
}
}

export enum SwapType {
AVNU = 'AVNU',
SIMPLE_SWAPPER = 'SIMPLE_SWAPPER'
}

export enum SwapDirection {
ERC1155_TO_ERC20,
ERC20_TO_ERC1155,
}


2 changes: 1 addition & 1 deletion sdk/packages/instaswap-core/src/tickMath.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Decimal } from 'decimal.js-light';
import JSBI from 'jsbi';
// import JSBI from 'jsbi';


export abstract class TickMath {
Expand Down
Loading

0 comments on commit 71071b2

Please sign in to comment.