Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

UI: When converting, ignore small ratio diffs and prioritize Season #533

Merged
merged 1 commit into from
Jul 3, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 2 additions & 3 deletions projects/ui/src/lib/Beanstalk/Silo/Convert.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
import BigNumber from 'bignumber.js';
import { defaultAbiCoder } from 'ethers/lib/utils';
import { Token } from '~/classes';
import { DepositCrate } from '~/state/farmer/silo';
import { sortCratesByBDVRatio, sortCratesBySeason } from './Utils';
import { sortConvertCratesByBest, sortCratesBySeason } from './Utils';
import { STALK_PER_SEED_PER_SEASON } from '~/util';

export enum ConvertKind {
Expand Down Expand Up @@ -47,7 +46,7 @@ export function selectCratesToConvert(
: /// LP -> BEAN: use the crates with the lowest [BDV/Amount] ratio first.
/// Since LP deposits can have varying BDV, the best option for the Farmer
/// is to increase the BDV of their existing lowest-BDV crates.
sortCratesByBDVRatio<DepositCrate>(depositedCrates, 'asc');
sortConvertCratesByBest<DepositCrate>(depositedCrates);

/// FIXME: symmetry with `Withdraw`
sortedCrates.some((crate) => {
Expand Down
30 changes: 30 additions & 0 deletions projects/ui/src/lib/Beanstalk/Silo/Utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,3 +25,33 @@ export function sortCratesByBDVRatio<T extends DepositCrate>(
return m * _b.minus(_a).toNumber();
});
}

/**
* Order crates by what's best to convert.
*
* Pre Silo V3, the season sorting is important to minimize potential stalk loss.
* After Silo V3, BDV ratio is the only thing that matters.
*/
export function sortConvertCratesByBest<T extends DepositCrate>(
crates: T[],
) {
return [...crates].sort((a, b) => {
// Calculate the BDV ratio for each crate.
// Fix to 4 decimal places to avoid small rounding differences from resorting.
const _a = Number(a.bdv.div(a.amount).toPrecision(4));
const _b = Number(b.bdv.div(b.amount).toPrecision(4));

// sorting a - b puts lowest ratio crates first, which is desirable
const delta = _a - _b;

// If the BDV ratio is the same, sort more recent seasons (higher numbers) first.
// All else equal, pre-Silo V3 users would rather convert more recent crates.
if (delta === 0) {
// sorting b.season - a.season sorts higher (more recent) seasons first,
// which is desirable
return b.season.minus(a.season).toNumber();
}

return delta;
});
}