Skip to content

Commit

Permalink
[WIP]: Converts (#1108)
Browse files Browse the repository at this point in the history
  • Loading branch information
Space-Bean authored Sep 23, 2024
2 parents d5714c8 + 50692ad commit 1bf7494
Show file tree
Hide file tree
Showing 22 changed files with 1,096 additions and 633 deletions.
29 changes: 29 additions & 0 deletions projects/sdk/src/classes/Pool/BasinWell.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,21 @@ export class BasinWell extends Pool {
);
}

// Ensure tokens are in the correct order
async updateTokenIndexes() {
const data = await this.getContract().tokens();
if (!data || data.length !== 2) {
throw new Error(`could not validate well tokens for ${this.name}`);
}

const first = data[0].toLowerCase();
const thisFirst = this.tokens[0].address.toLowerCase();

if (first !== thisFirst) {
this.tokens.reverse();
}
}

async getAddLiquidityOut(amounts: TokenValue[]): Promise<TokenValue> {
return this.getContract()
.getAddLiquidityOut(amounts.map((a) => a.toBigNumber()))
Expand All @@ -49,4 +64,18 @@ export class BasinWell extends Pool {
.getRemoveLiquidityOneTokenOut(lpAmountIn.toBigNumber(), tokenOut.address)
.then((result) => tokenOut.fromBlockchain(result));
}

getBeanWellTokenIndexes() {
// assumes tokens are in correct order
const beanIndex = this.tokens.findIndex((token) => token.equals(BasinWell.sdk.tokens.BEAN));

if (beanIndex < 0) {
throw new Error(`Bean token not found in well ${this.name}`);
}

return {
bean: beanIndex,
nonBean: beanIndex === 0 ? 1 : 0
};
}
}
2 changes: 1 addition & 1 deletion projects/sdk/src/constants/addresses.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ export const addresses = {
// ----------------------------------------
BEANSTALK_PRICE: Address.make({
[ChainId.ETH_MAINNET]: "0x4BEd6cb142b7d474242d87F4796387DEB9E1E1B4",
[ChainId.ARBITRUM_MAINNET]: "0xC218F5a782b0913931DCF502FA2aA959b36Ac9E7"
[ChainId.ARBITRUM_MAINNET]: "0xA560c3aFcEb9a046573bf6F401134a6837f6D321"
}),
JUNCTION: Address.make({
[ChainId.ETH_MAINNET]: "0x16a903b66403d3de69db50e6d1ad0b07490b740a",
Expand Down
2 changes: 1 addition & 1 deletion projects/sdk/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ export type {
} from "src/lib/depot";

export type {
ZeroExQuoteParams,
MinimumViableSwapQuote,
ZeroExQuoteResponse,
ZeroExAPIRequestParams
} from "src/lib/matcha/types";
Expand Down
64 changes: 0 additions & 64 deletions projects/sdk/src/lib/farm/actions/PipelineConvert.ts

This file was deleted.

2 changes: 0 additions & 2 deletions projects/sdk/src/lib/farm/actions/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ import { UniswapV3Swap } from "./UniswapV3Swap";
import { DevDebug } from "./_DevDebug";
import { LidoWrapSteth } from "./LidoWrapSteth";
import { LidoUnwrapWstETH } from "./LidoUnwrapWstETH";
import { PipelineConvert } from "./PipelineConvert";

export {
// Approvals
Expand All @@ -45,7 +44,6 @@ export {
ClaimWithdrawal,
TransferDeposits,
TransferDeposit,
PipelineConvert,

// Lido
LidoWrapSteth,
Expand Down
40 changes: 35 additions & 5 deletions projects/sdk/src/lib/matcha/types.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,3 @@
export interface ZeroExQuoteParams extends ZeroExAPIRequestParams {
mode: "exactInput" | "exactOutput";
enabled: boolean;
}

export interface ZeroExAPIRequestParams {
/**
* The ERC20 token address of the token you want to sell. It is recommended to always use the token address
Expand Down Expand Up @@ -148,6 +143,41 @@ interface ZeroExSource {
proportion: string;
}

export interface MinimumViableSwapQuote {
/**
* The ERC20 token address of the token you want to receive in quote.
*/
buyTokenAddress: string;
/**
* The ERC20 token address of the token you want to sell with quote.
*/
sellTokenAddress: string;
/**
* The address of the contract to send call data to.
*/
to: string;
/**
* The target contract address for which the user needs to have an allowance in order to be able to complete the swap.
*/
allowanceTarget: string;
/**
* The amount of buyToken (in buyToken units) that would be bought in this swap.
*/
buyAmount: string;
/**
* The amount of sellToken (in sellToken units) that would be sold in this swap.
*/
sellAmount: string;
/**
* The amount of ether (in wei) that should be sent with the transaction.
*/
value: string;
/**
* The call data
*/
data: string;
}

/**
* Response type from 0x quote-v1 swap API.
*
Expand Down
5 changes: 4 additions & 1 deletion projects/sdk/src/lib/silo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,17 +18,19 @@ import {
} from "./silo/types";
import { Transfer } from "./silo/Transfer";
import { Convert, ConvertDetails } from "./silo/Convert";
import { PipelineConvert } from "./silo/PipelineConvert";

export class Silo {
static sdk: BeanstalkSDK;
private depositBuilder: DepositBuilder;
siloWithdraw: Withdraw;
siloTransfer: Transfer;
siloConvert: Convert;
pipelineConvert: PipelineConvert;

// 1 Seed grows 1 / 10_000 Stalk per Season.
// 1/10_000 = 1E-4
// FIXME
// BS3TODO: FIXME.
static STALK_PER_SEED_PER_SEASON = TokenValue.fromHuman(1e-4, 10);

constructor(sdk: BeanstalkSDK) {
Expand All @@ -37,6 +39,7 @@ export class Silo {
this.siloWithdraw = new Withdraw(sdk);
this.siloTransfer = new Transfer(sdk);
this.siloConvert = new Convert(sdk);
this.pipelineConvert = new PipelineConvert(sdk);
}

public calculateGrownStalk = utils.calculateGrownStalkStems;
Expand Down
22 changes: 11 additions & 11 deletions projects/sdk/src/lib/silo/Convert.ts
Original file line number Diff line number Diff line change
Expand Up @@ -145,16 +145,17 @@ export class Convert {
currentSeason: number
): ConvertDetails {
if (deposits.length === 0) throw new Error("No crates to withdraw from");
const sortedCrates = toToken.isLP
? /// BEAN -> LP: oldest crates are best. Grown stalk is equivalent
/// on both sides of the convert, but having more seeds in older crates
/// allows you to accrue stalk faster after convert.
/// Note that during this convert, BDV is approx. equal after the convert.
sortCratesByStem(deposits, "asc")
: /// 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(deposits, "asc");
const sortedCrates =
!fromToken.isLP && toToken.isLP
? /// BEAN -> LP: oldest crates are best. Grown stalk is equivalent
/// on both sides of the convert, but having more seeds in older crates
/// allows you to accrue stalk faster after convert.
/// Note that during this convert, BDV is approx. equal after the convert.
sortCratesByStem(deposits, "asc")
: /// X -> LP: 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(deposits, "asc");

const pickedCrates = pickCrates(sortedCrates, fromAmount, fromToken, currentSeason);

Expand Down Expand Up @@ -192,7 +193,6 @@ export class Convert {
throw new Error("SDK: Deprecated conversion pathway");
}

// BS3TODO: is this encoding correct ?
if (fromToken.equals(toToken)) {
return ConvertEncoder.lambdaLambda(amountIn.toBlockchain(), fromToken.address);
}
Expand Down
Loading

0 comments on commit 1bf7494

Please sign in to comment.