Skip to content

Commit

Permalink
Merge pull request #59 from VenusProtocol/add-fixed-pairs-option
Browse files Browse the repository at this point in the history
Add fixed pairs option
  • Loading branch information
coreyar authored Aug 27, 2024
2 parents 3392084 + 24fabbb commit 0c56bc9
Show file tree
Hide file tree
Showing 5 changed files with 64 additions and 23 deletions.
56 changes: 39 additions & 17 deletions packages/cli/source/commands/convert.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,16 @@ export const options = zod.object({
)
.optional()
.default(false),
fixedPairs: zod
.boolean()
.describe(
option({
description: "Use fixed pairs to query available pools",
alias: "f",
}),
)
.optional()
.default(false),
minIncomeBp: zod
.number()
.describe(
Expand All @@ -116,8 +126,19 @@ interface Props {
* Command to search for and execute token conversions based on parameters
*/
export default function Convert({ options }: Props) {
const { minTradeUsd, maxTradeUsd, simulate, assetIn, assetOut, converter, profitable, loop, debug, minIncomeBp } =
options;
const {
minTradeUsd,
maxTradeUsd,
simulate,
assetIn,
assetOut,
converter,
profitable,
loop,
debug,
minIncomeBp,
fixedPairs,
} = options;

const { exit } = useApp();
const { write: writeStdErr } = useStderr();
Expand All @@ -127,25 +148,25 @@ export default function Convert({ options }: Props) {

useEffect(() => {
const network = getEnvValue("NETWORK");
const tokenConverter = new TokenConverter({
subscriber: dispatch,
simulate: !!simulate,
swapProvider: network?.includes("bsc") ? PancakeSwapProvider : UniswapProvider,
});

const convert = async () => {
const tokenConverter = new TokenConverter({
subscriber: dispatch,
simulate: !!simulate,
swapProvider: network?.includes("bsc") ? PancakeSwapProvider : UniswapProvider,
const potentialConversions = await tokenConverter.queryConversions({
assetIn,
assetOut,
converter,
releaseFunds: false,
});

do {
const potentialConversions = await tokenConverter.queryConversions({
assetIn,
assetOut,
converter,
releaseFunds: false,
});

if (potentialConversions.length === 0) {
setError("No Potential Trades Found");
}
if (potentialConversions.length === 0) {
setError("No Potential Trades Found");
}

do {
for (const t of potentialConversions) {
let amountOut = t.assetOut.balance;

Expand All @@ -166,6 +187,7 @@ export default function Convert({ options }: Props) {
t.assetOut.address,
t.assetIn.address,
amountOut,
fixedPairs,
);

const { trade, amount, minIncome } = arbitrageArgs || {
Expand Down
3 changes: 3 additions & 0 deletions packages/keeper-bots/src/converter-bot/TokenConverter.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,7 @@ describe("Token Converter", () => {
addresses.WBNB,
addresses.USDC,
1000000000000000000000n,
false,
);

expect(trade.inputToken).toEqual({
Expand All @@ -205,6 +206,7 @@ describe("Token Converter", () => {
numerator: 8904975230019520420n,
},
});

expect(trade.outputToken).toEqual({
address: "0xcf6bb5389c92bdda8a3747ddb454cb7a64626c63",
amount: {
Expand Down Expand Up @@ -233,6 +235,7 @@ describe("Token Converter", () => {
addresses.WBNB,
addresses.USDC,
750000000000000000n,
false,
);
expect(subscriberMock).toHaveBeenCalledWith({
type: "GetBestTrade",
Expand Down
13 changes: 10 additions & 3 deletions packages/keeper-bots/src/converter-bot/TokenConverter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,9 @@ export class TokenConverter extends BotBase {
swapFrom: Address,
swapTo: Address,
amount: bigint,
fixedPairs?: boolean,
): Promise<[TradeRoute, bigint]> {
return this.swapProvider.getBestTrade(tokenConverter, swapFrom, swapTo, amount);
return this.swapProvider.getBestTrade(tokenConverter, swapFrom, swapTo, amount, fixedPairs);
}

/**
Expand Down Expand Up @@ -385,13 +386,19 @@ export class TokenConverter extends BotBase {
* @param amountOut Amount of asset out to receive from the token converter
* @returns {trade: SmartRouterTrade, amount: bigint, minIncome: bigint }
*/
async prepareConversion(tokenConverter: Address, assetOut: Address, assetIn: Address, amountOut: bigint) {
async prepareConversion(
tokenConverter: Address,
assetOut: Address,
assetIn: Address,
amountOut: bigint,
fixedPairs?: boolean,
) {
let error;
let trade;
let amount;

try {
[trade, amount] = await this.getBestTrade(tokenConverter, assetOut, assetIn, amountOut);
[trade, amount] = await this.getBestTrade(tokenConverter, assetOut, assetIn, amountOut, fixedPairs);
} catch (e) {
error = (e as Error).message;
} finally {
Expand Down
13 changes: 10 additions & 3 deletions packages/keeper-bots/src/providers/pancake-swap.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,14 +80,20 @@ class PancakeSwapProvider extends SwapProvider {
* @param swapFromToken
* @param swapToToken
*/
getCandidatePools = async (swapFromToken: Token, swapToToken: Token) => {
getCandidatePools = async (swapFromToken: Token, swapToToken: Token, fixedPairs: boolean) => {
const now = new Date();
if (!this._candidatePools.data.length || this._candidatePools.expirationTimestamp < now) {
const candidatePools = await SmartRouter.getV3CandidatePools({
onChainProvider: () => this.publicClient,
subgraphProvider: () => this.v3PancakeSubgraphClient,
currencyA: swapFromToken,
currencyB: swapToToken,
pairs: fixedPairs
? [
[swapFromToken, swapToToken],
[swapToToken, swapFromToken],
]
: undefined,
});

now.setMinutes(15 + new Date().getMinutes());
Expand All @@ -105,6 +111,7 @@ class PancakeSwapProvider extends SwapProvider {
swapFrom: Address,
swapTo: Address,
amount: bigint,
fixedPairs: boolean = false,
): Promise<[TradeRoute, bigint]> {
const swapFromToken = await this.getToken(swapFrom);
const swapToToken = await this.getToken(swapTo);
Expand All @@ -122,7 +129,7 @@ class PancakeSwapProvider extends SwapProvider {
});

try {
const candidatePools = await this.getCandidatePools(swapFromToken, swapToToken);
const candidatePools = await this.getCandidatePools(swapFromToken, swapToToken, fixedPairs);

const response = await SmartRouter.getBestTrade(
CurrencyAmount.fromRawAmount(swapToToken, updatedAmountIn[1]),
Expand Down Expand Up @@ -179,7 +186,7 @@ class PancakeSwapProvider extends SwapProvider {
},
});

return this.getBestTrade(tokenConverter, swapFrom, swapTo, (updatedAmountIn[1] * 75n) / 100n);
return this.getBestTrade(tokenConverter, swapFrom, swapTo, (updatedAmountIn[1] * 75n) / 100n, fixedPairs);
}
return [trade, updatedAmountIn[0]];
}
Expand Down
2 changes: 2 additions & 0 deletions packages/keeper-bots/src/providers/swap-provider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,8 @@ class SwapProvider {
swapTo: Address,
// eslint-disable-next-line
amount: bigint,
// eslint-disable-next-line
fixedPairs?: boolean,
): Promise<[TradeRoute, bigint]> {
throw new Error("Not Implemented Error");
}
Expand Down

0 comments on commit 0c56bc9

Please sign in to comment.