@@ -4,7 +4,7 @@ import { formatUnits, parseUnits } from "ethers/lib/utils";
44import { IS_SIMULATION } from "./constants" ;
55import { getSigner } from "./provider" ;
66import { MarketData } from "./types" ;
7- import { getSwapQuote } from "./zerox_service" ;
7+ import { getSwapPrice , getSwapQuote } from "./zerox_service" ;
88import { getGasEstimation } from "./gasEstimator" ;
99import { setAllowanceToAllowanceHolder } from "./erc20" ;
1010
@@ -31,7 +31,7 @@ export const executeTrade = async (market: MarketData) => {
3131 }
3232 const sellAmount = parseUnits ( randomBuyAmount . toFixed ( 6 ) , market . quoteTokenBalance . token . decimals ) . toString ( ) ;
3333 if ( Number ( randomBuyAmount ) > market . minBuyUnit ) {
34- const quote = await getSwapQuote ( { sellAmount, sellToken, buyToken, taker, intentOnFill : true , slippagePercentage } , market . chainId ) ;
34+ const quote = await getSwapQuote ( { sellAmount, sellToken, buyToken, taker, slippagePercentage } , market . chainId ) ;
3535
3636 if ( market . maxGasValueInGwei && quote . data . gasPrice ) {
3737 const wei = ethers . utils . parseUnits ( String ( market . maxGasValueInGwei ) , 'gwei' ) ;
@@ -48,7 +48,7 @@ export const executeTrade = async (market: MarketData) => {
4848 }
4949
5050
51- console . log ( `doing a buy a amount of ${ randomBuyAmount } base token` )
51+ console . log ( `doing a buy a amount of ${ randomBuyAmount } quote token` )
5252
5353
5454 if ( ! IS_SIMULATION ) {
@@ -72,23 +72,37 @@ export const executeTrade = async (market: MarketData) => {
7272
7373 } else {
7474 try {
75- const buyToken = market . baseTokenBalance . token . address ;
76- const sellToken = market . quoteTokenBalance . token . address ;
75+ const buyToken = market . quoteTokenBalance . token . address ;
76+ const sellToken = market . baseTokenBalance . token . address ;
7777 const slippagePercentage = market . slippagePercentage ;
7878 let randomSellAmount = market . minSellUnit + market . maxSellUnit * Math . random ( ) ;
7979 if ( randomSellAmount > market . maxSellUnit ) {
8080 randomSellAmount = market . maxSellUnit ;
8181 }
82- const sellAmount = parseUnits ( randomSellAmount . toFixed ( 6 ) , market . quoteTokenBalance . token . decimals ) . toString ( ) ;
83- const quote = await getSwapQuote ( { sellAmount, sellToken, buyToken, taker, intentOnFill : false , slippagePercentage } , market . chainId ) ;
82+ const unitSellAmount = parseUnits ( '1' , market . baseTokenBalance . token . decimals ) . toString ( ) ;
83+ console . log ( unitSellAmount )
84+
85+
86+ // get price
87+ const price = ( await getSwapPrice ( { sellAmount : unitSellAmount , sellToken, buyToken, taker, slippagePercentage} , market . chainId ) ) . data
88+
89+ const buyAmountPriceUnits = formatUnits ( price . buyAmount , market . quoteTokenBalance . token . decimals ) ;
90+ const sellAmountPriceUnits = formatUnits ( price . sellAmount , market . baseTokenBalance . token . decimals ) ;
91+
92+
93+ const basePrice = Number ( sellAmountPriceUnits ) / Number ( buyAmountPriceUnits ) ;
94+
95+ const sellAmount = parseUnits ( ( randomSellAmount * basePrice ) . toFixed ( 6 ) , market . baseTokenBalance . token . decimals ) . toString ( ) ;
96+
97+ const quote = await getSwapQuote ( { sellAmount, sellToken, buyToken, taker, slippagePercentage } , market . chainId ) ;
8498
8599
86100
87101 if ( BigNumber . from ( quote . data . sellAmount ) . lt ( market . baseTokenBalance . balance ) ) {
88102 //const sellAmount = market.baseTokenBalance.balance.toString();
89103
90104
91- const quote = await getSwapQuote ( { sellAmount, sellToken, buyToken, taker, intentOnFill : true , slippagePercentage } , market . chainId ) ;
105+ // const quote = await getSwapQuote({ sellAmount, sellToken, buyToken, taker, slippagePercentage }, market.chainId);
92106 if ( market . maxGasValueInGwei && quote . data . gasPrice ) {
93107 const wei = ethers . utils . parseUnits ( String ( market . maxGasValueInGwei ) , 'gwei' ) ;
94108 if ( BigNumber . from ( quote . data . gasPrice ) . gte ( wei ) ) {
@@ -97,7 +111,8 @@ export const executeTrade = async (market: MarketData) => {
97111 }
98112 }
99113
100- console . log ( `doing a sell a amount of ${ randomSellAmount } base token` )
114+ console . log ( `doing a sell a amount of ${ randomSellAmount } quote token` )
115+ console . log ( quote . data ?. issues ?. allowance )
101116 if ( quote . data ?. issues ?. allowance ) {
102117 console . log ( 'setting allowance on sell token' ) ;
103118 const { actual, spender} = quote . data . issues ?. allowance ;
0 commit comments