
Security News
vlt Launches "reproduce": A New Tool Challenging the Limits of Package Provenance
vlt's new "reproduce" tool verifies npm packages against their source code, outperforming traditional provenance adoption in the JavaScript ecosystem.
@stove-labs/arbitrage-bot-profit-finder-lite
Advanced tools
discovers arbitrage opportunities and calculates profit
In the process of arbitrage a trader speculates on the price differences between two markets, by taking advantage of the difference in asset prices and trades for a profit. The strategy is to buy crypto at a lower price on one exchange and sell immediately at a higher price on the other.
base token
balance before and after the arbitrage life-cycle.buy
and sell
swap. It completes when both swaps/trades have been finalized.An arbitrage opportunity presents itself when the same asset pair is traded on two different exchanges with varying prices. Of course we need to take into account fees as well, because those will cut into the profit opportunity.
DEX1 | DEX2 | |
---|---|---|
base token | low price in base token | high price in base token |
quote token | high price in quote token | low price in quote token |
DEX1 | DEX2 | |
---|---|---|
base token | buy | sell |
quote token |
The profit is the difference between the base token send (1) and receive (4).
DEX1 | DEX2 | |
---|---|---|
base token | (1) send | (4) receive |
quote token | (2) receive | (3) send |
Challenge is to find the optimal amount x, in order to calculate the required base token limits.
DEX1 | DEX2 | |
---|---|---|
base token | getAmountInGivenOut() | getAmountOutGivenIn() |
quote token | optimal amount x | amount amount x |
DEX1 | DEX2 | |
---|---|---|
base token | a1 | a2 |
quote token | b1 | b2 |
The following formulas are simplified for 0.3% fee (=1-997/1000). The final algorithm uses parameterized fees for each exchange independently.
getAmountInGivenOut=\frac{amountOut*1000*reserveIn}{reserveOut*1000-amountOut*997}
getAmountOutGivenIn=\frac{amountIn*997*reserveOut}{reserveIn*1000+amountIn*997}
DEX1 | DEX2 | |
---|---|---|
base token | Δa1 | Δa2 |
quote token | x | x |
\Delta a_1 = getAmountInGivenOut(x) = \frac{x*1000*a_1}{b_1*1000-x*f_1}
\Delta a_2 = getAmountOutGivenIn(x) = \frac{x*f_2*a_2}{b_2*1000+x*f_2}
Finally our profit function is:
f(x)=\Delta a_2 - \Delta a_1 = \frac{x*f_2*a_2}{b_2*1000+x*f_2} - \frac{x*1000*a_1}{b_1*1000-x*f_1}
And we want to find the biggest amount of x, where x < b1 and x < b2 (can't trade more quote tokens than there is in the pool).
We set the first derivative of this function to zero. Solving that yields us x1 and x2.
\frac{\delta}{\delta x}f(x)=f'(x)=0
Rearranging the function to fit the univariate quadratic function
f'(x)= ax^2 + bx + c = 0
Allows us to extract the coefficients and solve it with this formula
x_{1,2}=-\frac{b±\sqrt{b²-4ac}}{2a}
This is an example where base token is XTZ (y-axis) and quote token kUSD (x-axis). The optimal solution for quote token amount is x=23 kUSD that gives us a profit of 0.057 XTZ. Any other value would extract less arbitrage through the two trades.
The input for the profit finder algorithm is an array of two elements in the form of ExchangePrice[]
. It is the price of the same asset pair traded on two exchanges.
interface ExchangePrice {
baseToken: {
decimals: number;
...
};
baseTokenBalance: {
amount: string
};
quoteToken: {
decimals: number;
...
};
quoteTokenBalance: {
amount: string
};
fee: number;
spotPrice: string;
...
}
The output of the profit finder are Swap parameters for both DEXs that can be processed by the swap execution plugin.
interface ProfitOpportunity {
swaps: Swap[];
profit: {
baseTokenAmount: string;
};
}
interface Swap {
tokenIn: { ticker: string };
tokenOut: { ticker: string };
tokenInDecimals: number;
tokenOutDecimals: number;
type: SwapType; // buy or sell
amount: string;
limit: string;
limitWithoutSlippage: string;
ecosystemIdentifier: EcosystemIdentifier;
identifier: ExchangeIdentifier;
}
The ProfitFinderLitePlugin
first orders the prices in an ascending order. Then it needs to find the optimal quoteToken
amount for the trade. For that it uses the algorithm described above. The ideal quoteToken
amount is needed for the getAmountInGivenOut()
and getAmountOutGivenIn()
AMM methods to calculate the parameters for the swap trades.
class ProfitFinderLitePlugin implements ProfitFinderPlugin {
findProfits(prices: ExchangePrice[]): ProfitOpportunity {
throwForMissingDecimals(prices);
const ascendingSpotPrices = orderLowToHigh(prices);
const quoteTokenAmount = findOptimalQuoteTokenAmount(ascendingSpotPrices);
const profitOpportunity = createProfitOpportunity(
ascendingSpotPrices,
quoteTokenAmount,
this.config.profitSplitForSlippage
);
return profitOpportunity;
}
...
}
FAQs
discovers arbitrage opportunities and calculates profit
The npm package @stove-labs/arbitrage-bot-profit-finder-lite receives a total of 0 weekly downloads. As such, @stove-labs/arbitrage-bot-profit-finder-lite popularity was classified as not popular.
We found that @stove-labs/arbitrage-bot-profit-finder-lite demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 2 open source maintainers collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
vlt's new "reproduce" tool verifies npm packages against their source code, outperforming traditional provenance adoption in the JavaScript ecosystem.
Research
Security News
Socket researchers uncovered a malicious PyPI package exploiting Deezer’s API to enable coordinated music piracy through API abuse and C2 server control.
Research
The Socket Research Team discovered a malicious npm package, '@ton-wallet/create', stealing cryptocurrency wallet keys from developers and users in the TON ecosystem.