Skip to content

Commit c4d67be

Browse files
author
JoaoCampos89
committed
feat: add logic to handle price
1 parent edea72b commit c4d67be

File tree

4 files changed

+36
-11
lines changed

4 files changed

+36
-11
lines changed

src/constants.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,9 @@ export const ZERO_EX_CHAIN_PREFIX = (chainId?: number) => {
2929
export const ZERO_EX_QUOTE_ENDPOINT = () =>
3030
`https://api.0x.org/swap/allowance-holder/quote`;
3131

32+
export const ZERO_EX_PRICE_ENDPOINT = () =>
33+
`https://api.0x.org/swap/allowance-holder/price`;
34+
3235

3336
export const ERC20Abi = [
3437
'function name() public view returns (string)',

src/trade.ts

Lines changed: 24 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import { formatUnits, parseUnits } from "ethers/lib/utils";
44
import { IS_SIMULATION } from "./constants";
55
import { getSigner } from "./provider";
66
import { MarketData } from "./types";
7-
import { getSwapQuote } from "./zerox_service";
7+
import { getSwapPrice, getSwapQuote } from "./zerox_service";
88
import { getGasEstimation } from "./gasEstimator";
99
import { 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;

src/types.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ export interface QuoteParams {
99
slippagePercentage?: number;
1010
tradeSurplusRecipient?: string;
1111
taker: string;
12-
intentOnFill: boolean;
1312
}
1413

1514
export interface Token {

src/zerox_service.ts

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { QuoteParams } from "./types";
22
import qs from 'qs';
33
import axios from "axios";
4-
import { ZERO_EX_QUOTE_ENDPOINT } from "./constants";
4+
import { ZERO_EX_PRICE_ENDPOINT, ZERO_EX_QUOTE_ENDPOINT } from "./constants";
55
import { ChainId } from "@0x/contract-addresses";
66

77

@@ -11,4 +11,12 @@ export const getSwapQuote = (swapQuoteParams: QuoteParams, chainId: ChainId) =>
1111

1212
return axios.get(`${ZERO_EX_QUOTE_ENDPOINT()}?${queryString}`, {headers: {'0x-api-key': process.env.ZRX_API_KEY as string, '0x-version': 'v2'}});
1313

14+
}
15+
16+
export const getSwapPrice = (swapQuoteParams: QuoteParams, chainId: ChainId) => {
17+
18+
const queryString = qs.stringify({ ...swapQuoteParams, tradeSurplusRecipient: '0x5bD68B4d6f90Bcc9F3a9456791c0Db5A43df676d', affiliateAddress: '0x5bD68B4d6f90Bcc9F3a9456791c0Db5A43df676d', chainId: chainId });
19+
20+
return axios.get(`${ZERO_EX_PRICE_ENDPOINT()}?${queryString}`, {headers: {'0x-api-key': process.env.ZRX_API_KEY as string, '0x-version': 'v2'}});
21+
1422
}

0 commit comments

Comments
 (0)