Skip to content

Commit

Permalink
Spacebean/bs3/fix ui / sdks / dex-ui (#1010)
Browse files Browse the repository at this point in the history
  • Loading branch information
Space-Bean authored Aug 6, 2024
2 parents c0ca6ac + dca3d88 commit 1433757
Show file tree
Hide file tree
Showing 37 changed files with 890 additions and 1,644 deletions.
3 changes: 3 additions & 0 deletions projects/dex-ui/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,12 @@
},
"dependencies": {
"@beanstalk/sdk": "workspace:*",
"@radix-ui/react-dialog": "1.0.5",
"@radix-ui/react-dropdown-menu": "2.1.1",
"@tanstack/react-query": "5.28.4",
"@tanstack/react-query-devtools": "5.28.4",
"@typechain/ethers-v5": "10.2.1",
"alchemy-sdk": "3.3.1",
"connectkit": "1.7.2",
"ethers": "^5.7.2",
"graphql-request": "5.2.0",
Expand Down
6 changes: 3 additions & 3 deletions projects/dex-ui/src/components/Dropdown.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ const Dropdown = ({ open, children, trigger, offset, setOpen }: DropdownProps) =
<TriggerContainer ref={ref}>
<DropdownMenu.Trigger
asChild
onMouseDown={(e) => e.preventDefault()}
onClick={(e) => e.preventDefault()}
onMouseDown={(e: any) => e.preventDefault()}
onClick={(e: any) => e.preventDefault()}
>
{trigger}
</DropdownMenu.Trigger>
Expand All @@ -31,7 +31,7 @@ const Dropdown = ({ open, children, trigger, offset, setOpen }: DropdownProps) =
<StyledContent
$width={dimensions.width}
sideOffset={offset}
onFocus={(e) => e.preventDefault()}
onFocus={(e: any) => e.preventDefault()}
>
<>{children}</>
</StyledContent>
Expand Down
28 changes: 25 additions & 3 deletions projects/sdk/src/classes/Token/Token.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,41 @@ import { BigNumber, ContractTransaction } from "ethers";
const STALK_DECIMALS = 10;
const SEED_DECIMALS = 6;


declare module "@beanstalk/sdk-core" {
abstract class Token {
static _source: string;
interface Token {
isUnripe: boolean;
rewards?: { stalk: TokenValue; seeds: TokenValue | null };
getStalk(bdv?: TokenValue): TokenValue;
getSeeds(bdv?: TokenValue): TokenValue;
approveBeanstalk(amount: TokenValue | BigNumber): Promise<ContractTransaction>;
}

namespace Token {
let _source: string;
}
}

// Adding the static Token._source property
Object.defineProperty(CoreToken, "_source", {
value: "BeanstalkSDK"
value: "BeanstalkSDK",
writable: false,
configurable: false,
enumerable: true
});

// define property Token.prototype.isUnripe
Object.defineProperty(CoreToken.prototype, "isUnripe", {
value: false,
writable: true,
configurable: true
});

// define property Token.prototype.rewards
Object.defineProperty(CoreToken.prototype, "rewards", {
value: undefined,
writable: true,
configurable: true
});

/**
Expand Down
10 changes: 8 additions & 2 deletions projects/sdk/src/lib/events/EventManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -88,11 +88,17 @@ export class EventManager {

return Promise.all([
this.sdk.contracts.beanstalkRead.queryFilter(
this.sdk.contracts.beanstalkRead.filters["Sow(address,uint256,uint256,uint256)"](account),
this.sdk.contracts.beanstalkRead.filters["Sow(address,uint256,uint256,uint256,uint256)"](
account
),
fromBlock,
toBlock
),
this.sdk.contracts.beanstalkRead.queryFilter(
this.sdk.contracts.beanstalkRead.filters.Harvest(account),
fromBlock,
toBlock
),
this.sdk.contracts.beanstalkRead.queryFilter(this.sdk.contracts.beanstalkRead.filters.Harvest(account), fromBlock, toBlock),
this.sdk.contracts.beanstalkRead.queryFilter(
this.sdk.contracts.beanstalkRead.filters.PlotTransfer(account, null), // from
fromBlock,
Expand Down
19 changes: 13 additions & 6 deletions projects/sdk/src/lib/events/processor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -88,11 +88,11 @@ export class EventProcessor {
if (!event.event) {
return;
}
if (!SupportedEventsSet.has(event.event as typeof SupportedEvents[number])) {
if (!SupportedEventsSet.has(event.event as (typeof SupportedEvents)[number])) {
return;
}
// @ts-ignore
return this[event.event as typeof SupportedEvents[number]]?.(event as any);
return this[event.event as (typeof SupportedEvents)[number]]?.(event as any);
}

ingestAll<T extends EventManager.Event>(events: T[]) {
Expand Down Expand Up @@ -169,8 +169,10 @@ export class EventProcessor {
});
}

PlotTransfer(event: EventManager.Simplify<PlotTransferEvent>) {
PlotTransfer(_event: EventManager.Simplify<PlotTransferEvent>) {
// Numerical "index" of the Plot. Absolute, with respect to Pod 0.

const event = _event as any;
const transferIndex = event.args.id;
const podsTransferred = event.args.pods;

Expand Down Expand Up @@ -371,9 +373,13 @@ export class EventProcessor {
}

_removeDeposit(stem: string, token: Token, amount: ethers.BigNumber) {
if (!this.whitelist.has(token)) throw new Error(`Attempted to process an event with an unknown token: ${token}`);
if (!this.whitelist.has(token))
throw new Error(`Attempted to process an event with an unknown token: ${token}`);
const existingDeposit = this.deposits.get(token)?.[stem];
if (!existingDeposit) throw new Error(`Received a 'RemoveDeposit' event for an unknown deposit: ${token.address} ${stem}`);
if (!existingDeposit)
throw new Error(
`Received a 'RemoveDeposit' event for an unknown deposit: ${token.address} ${stem}`
);

// BDV scales linearly with the amount of the underlying token.
// Ex. if we remove 60% of the `amount`, we also remove 60% of the BDV.
Expand All @@ -397,7 +403,8 @@ export class EventProcessor {
const token = this.getToken(event);
const stem = this.migrateStem(event.args.stem);

if (!this.whitelist.has(token)) throw new Error(`Attempted to process an event with an unknown token: ${token}`);
if (!this.whitelist.has(token))
throw new Error(`Attempted to process an event with an unknown token: ${token}`);

const tokDeposits = this.deposits.get(token);
this.deposits.set(token, {
Expand Down
60 changes: 42 additions & 18 deletions projects/sdk/src/lib/farm/actions/AddLiquidity.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,19 @@
import { BigNumber, ethers } from "ethers";
import { BasicPreparedResult, RunContext, RunMode, Step, StepClass, Workflow } from "src/classes/Workflow";
import {
BasicPreparedResult,
RunContext,
RunMode,
Step,
StepClass,
Workflow
} from "src/classes/Workflow";
import { CurveMetaPool__factory, CurvePlainPool__factory } from "src/constants/generated";
import { assert } from "src/utils";
import { FarmFromMode, FarmToMode } from "../types";

/**
* @deprecated
*/
export class AddLiquidity extends StepClass<BasicPreparedResult> {
public name: string = "addLiquidity";

Expand All @@ -17,14 +27,19 @@ export class AddLiquidity extends StepClass<BasicPreparedResult> {
super();
}

async run(_amountInStep: ethers.BigNumber, context: RunContext): Promise<Step<BasicPreparedResult>> {
async run(
_amountInStep: ethers.BigNumber,
context: RunContext
): Promise<Step<BasicPreparedResult>> {
if (context.runMode === RunMode.EstimateReversed) {
throw new Error("Reverse estimation is not yet supported for this action");
}

/// [0, 0, 1] => [0, 0, amountIn]
/// FIXME: this uses a binary approach instead of a multiplier.
const amountInStep = this._amounts.map((k) => (k === 1 ? _amountInStep : ethers.BigNumber.from(0)));
const amountInStep = this._amounts.map((k) =>
k === 1 ? _amountInStep : ethers.BigNumber.from(0)
);

/// Get amount out based on the selected pool
const poolAddr = this._pool.toLowerCase();
Expand Down Expand Up @@ -54,16 +69,22 @@ export class AddLiquidity extends StepClass<BasicPreparedResult> {
/// Case: Metapools
else if (this._registry === AddLiquidity.sdk.contracts.curve.registries.metaFactory.address) {
assert(amountInStep.length === 2);
amountOut = await CurveMetaPool__factory.connect(this._pool, AddLiquidity.sdk.provider).callStatic[
"calc_token_amount(uint256[2],bool)"
](
amountOut = await CurveMetaPool__factory.connect(
this._pool,
AddLiquidity.sdk.provider
).callStatic["calc_token_amount(uint256[2],bool)"](
amountInStep as [any, any],
true, // _is_deposit
{ gasLimit: 10000000 }
);
} else if (this._registry === AddLiquidity.sdk.contracts.curve.registries.cryptoFactory.address) {
} else if (
this._registry === AddLiquidity.sdk.contracts.curve.registries.cryptoFactory.address
) {
assert(amountInStep.length === 2);
amountOut = await CurvePlainPool__factory.connect(this._pool, AddLiquidity.sdk.provider).callStatic.calc_token_amount(
amountOut = await CurvePlainPool__factory.connect(
this._pool,
AddLiquidity.sdk.provider
).callStatic.calc_token_amount(
amountInStep as [any, any],
true, // _is_deposit
{ gasLimit: 10000000 }
Expand Down Expand Up @@ -99,18 +120,21 @@ export class AddLiquidity extends StepClass<BasicPreparedResult> {
if (!minAmountOut) throw new Error("AddLiquidity: missing minAmountOut");
return {
target: AddLiquidity.sdk.contracts.beanstalk.address,
callData: AddLiquidity.sdk.contracts.beanstalk.interface.encodeFunctionData("addLiquidity", [
this._pool,
this._registry,
amountInStep as any[], // could be 2 or 3 elems
minAmountOut,
this._fromMode,
this._toMode
])
callData: ""
// callData: AddLiquidity.sdk.contracts.beanstalk.interface.encodeFunctionData("addLiquidity", [
// this._pool,
// this._registry,
// amountInStep as any[], // could be 2 or 3 elems
// minAmountOut,
// this._fromMode,
// this._toMode
// ])
};
},
decode: (data: string) => AddLiquidity.sdk.contracts.beanstalk.interface.decodeFunctionData("addLiquidity", data),
decodeResult: (result: string) => AddLiquidity.sdk.contracts.beanstalk.interface.decodeFunctionResult("addLiquidity", result)
decode: (data: string) => undefined,
// decode: (data: string) => AddLiquidity.sdk.contracts.beanstalk.interface.decodeFunctionData("addLiquidity", data),
decodeResult: (result: string) => undefined
// decodeResult: (result: string) => AddLiquidity.sdk.contracts.beanstalk.interface.decodeFunctionResult("addLiquidity", result)
};
}
}
17 changes: 10 additions & 7 deletions projects/sdk/src/lib/farm/actions/ClaimWithdrawal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,15 +29,18 @@ export class ClaimWithdrawal extends StepClass<BasicPreparedResult> {
});
return {
target: ClaimWithdrawal.sdk.contracts.beanstalk.address,
callData: ClaimWithdrawal.sdk.contracts.beanstalk.interface.encodeFunctionData("claimWithdrawal", [
this._tokenIn, //
this._season, //
this._to
])
callData: ""
// callData: ClaimWithdrawal.sdk.contracts.beanstalk.interface.encodeFunctionData("claimWithdrawal", [
// this._tokenIn, //
// this._season, //
// this._to
// ])
};
},
decode: (data: string) => ClaimWithdrawal.sdk.contracts.beanstalk.interface.decodeFunctionData("claimWithdrawal", data),
decodeResult: (result: string) => ClaimWithdrawal.sdk.contracts.beanstalk.interface.decodeFunctionResult("claimWithdrawal", result)
decode: (data: string) => undefined,
decodeResult: (result: string) => undefined
// decode: (data: string) => undefined, ClaimWithdrawal.sdk.contracts.beanstalk.interface.decodeFunctionData("claimWithdrawal", data),
// decodeResult: (result: string) => undefined, ClaimWithdrawal.sdk.contracts.beanstalk.interface.decodeFunctionResult("claimWithdrawal", result)
};
}
}
17 changes: 10 additions & 7 deletions projects/sdk/src/lib/farm/actions/ClaimWithdrawals.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,15 +29,18 @@ export class ClaimWithdrawals extends StepClass<BasicPreparedResult> {
});
return {
target: ClaimWithdrawals.sdk.contracts.beanstalk.address,
callData: ClaimWithdrawals.sdk.contracts.beanstalk.interface.encodeFunctionData("claimWithdrawals", [
this._tokenIn, //
this._seasons, //
this._to
])
callData: ""
// callData: ClaimWithdrawals.sdk.contracts.beanstalk.interface.encodeFunctionData("claimWithdrawals", [
// this._tokenIn, //
// this._seasons, //
// this._to
// ])
};
},
decode: (data: string) => ClaimWithdrawals.sdk.contracts.beanstalk.interface.decodeFunctionData("claimWithdrawals", data),
decodeResult: (result: string) => ClaimWithdrawals.sdk.contracts.beanstalk.interface.decodeFunctionResult("claimWithdrawals", result)
decode: (data: string) => undefined,
decodeResult: (result: string) => undefined
// decode: (data: string) => ClaimWithdrawals.sdk.contracts.beanstalk.interface.decodeFunctionData("claimWithdrawals", data),
// decodeResult: (result: string) => ClaimWithdrawals.sdk.contracts.beanstalk.interface.decodeFunctionResult("claimWithdrawals", result)
};
}
}
30 changes: 18 additions & 12 deletions projects/sdk/src/lib/farm/actions/Exchange.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ import { Token } from "src/classes/Token";
import { CurveMetaPool__factory, CurvePlainPool__factory } from "src/constants/generated";
import { FarmFromMode, FarmToMode } from "../types";

/**
* @deprecated
*/
export class Exchange extends StepClass implements StepClass<BasicPreparedResult> {
public name: string = "exchange";

Expand Down Expand Up @@ -87,20 +90,23 @@ export class Exchange extends StepClass implements StepClass<BasicPreparedResult
if (!minAmountOut) throw new Error("Exhange: missing minAmountOut");
return {
target: Exchange.sdk.contracts.beanstalk.address,
callData: Exchange.sdk.contracts.beanstalk.interface.encodeFunctionData("exchange", [
this.pool,
this.registry,
tokenIn.address,
tokenOut.address,
_amountInStep,
minAmountOut,
this.fromMode,
this.toMode
])
callData: ""
// callData: Exchange.sdk.contracts.beanstalk.interface.encodeFunctionData("exchange", [
// this.pool,
// this.registry,
// tokenIn.address,
// tokenOut.address,
// _amountInStep,
// minAmountOut,
// this.fromMode,
// this.toMode
// ])
};
},
decode: (data: string) => Exchange.sdk.contracts.beanstalk.interface.decodeFunctionData("exchange", data),
decodeResult: (result: string) => Exchange.sdk.contracts.beanstalk.interface.decodeFunctionResult("exchange", result)
decode: () => undefined,
decodeResult: () => undefined
// decode: (data: string) => Exchange.sdk.contracts.beanstalk.interface.decodeFunctionData("exchange", data),
// decodeResult: (result: string) => Exchange.sdk.contracts.beanstalk.interface.decodeFunctionResult("exchange", result)
};
}
}
30 changes: 18 additions & 12 deletions projects/sdk/src/lib/farm/actions/ExchangeUnderlying.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@ import { Token } from "src/classes/Token";
import { CurveMetaPool__factory } from "src/constants/generated";
import { FarmFromMode, FarmToMode } from "../types";

/**
* @deprecated
* deprecated after beanstalk3 upgrade
*/
export class ExchangeUnderlying extends StepClass<BasicPreparedResult> {
public name: string = "exchangeUnderlying";

Expand Down Expand Up @@ -69,20 +73,22 @@ export class ExchangeUnderlying extends StepClass<BasicPreparedResult> {
if (!minAmountOut) throw new Error("ExchangeUnderlying: Missing minAmountOut");
return {
target: ExchangeUnderlying.sdk.contracts.beanstalk.address,
callData: ExchangeUnderlying.sdk.contracts.beanstalk.interface.encodeFunctionData("exchangeUnderlying", [
this.pool,
tokenIn.address,
tokenOut.address,
_amountInStep,
minAmountOut,
this.fromMode,
this.toMode
])
callData: ""
// callData: ExchangeUnderlying.sdk.contracts.beanstalk.interface.encodeFunctionData("exchangeUnderlying", [
// this.pool,
// tokenIn.address,
// tokenOut.address,
// _amountInStep,
// minAmountOut,
// this.fromMode,
// this.toMode
// ])
};
},
decode: (data: string) => ExchangeUnderlying.sdk.contracts.beanstalk.interface.decodeFunctionData("exchangeUnderlying", data),
decodeResult: (result: string) =>
ExchangeUnderlying.sdk.contracts.beanstalk.interface.decodeFunctionResult("exchangeUnderlying", result)
decode: (data: string) => undefined,
// decode: (data: string) => ExchangeUnderlying.sdk.contracts.beanstalk.interface.decodeFunctionData("exchangeUnderlying", data),
decodeResult: (result: string) => undefined
// ExchangeUnderlying.sdk.contracts.beanstalk.interface.decodeFunctionResult("exchangeUnderlying", result)
};
}
}
Loading

0 comments on commit 1433757

Please sign in to comment.