Skip to content

Commit

Permalink
Spacebean/bs3/UI v2 (#1087)
Browse files Browse the repository at this point in the history
  • Loading branch information
Space-Bean authored Sep 14, 2024
2 parents 0009bc1 + 89558d5 commit 4ad7138
Show file tree
Hide file tree
Showing 43 changed files with 1,572 additions and 574 deletions.
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,8 @@
"ui:test": "yarn workspace ui test",
"test:browser": "yarn workspace tests test:browser",
"ex": "yarn workspace @beanstalk/examples x",
"anvil-arbitrum": "yarn cli:anvil-arbitrum --fork-block-number 250704543 --code-size-limit 50000",
"anvil-mainnet": "yarn cli:anvil-mainnet",
"anvil-arbitrum": "yarn cli:anvil-arbitrum --code-size-limit 50000 --fork-block-number 250704543",
"anvil-eth-mainnet": "yarn cli:anvil-eth-mainnet",
"anvil": "anvil --fork-url https://eth-mainnet.g.alchemy.com/v2/5ubn94zT7v7DnB5bNW1VOnoIbX5-AG2N --chain-id 1337",
"anvil4tests": "anvil --fork-url https://eth-mainnet.g.alchemy.com/v2/Kk7ktCQL5wz4v4AG8bR2Gun8TAASQ-qi --chain-id 1337 --fork-block-number 18629000"
},
Expand Down
2 changes: 1 addition & 1 deletion projects/cli/anvil.sh
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ else
fi

# Determine which chain ID to use. Defaults to arbitrum local host
if [ "$chainIdType" = "mainnet-local" ]; then
if [ "$chainIdType" = "eth-mainnet" ]; then
chainId=$mainnet_local_chain_id
prefix="eth"
port=9545
Expand Down
10 changes: 5 additions & 5 deletions projects/cli/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@beanstalk/cli",
"version": "0.0.10",
"version": "0.0.20",
"description": "Beanstalk protocol development cli tool",
"license": "MIT",
"repository": {
Expand All @@ -17,10 +17,10 @@
"cli:publish": "yarn cli:build && yarn npm publish --access public",
"cli:build": "rimraf build && tsc && chmod u+x build/cli.js",
"g:bean": "yarn ts-node-esm src/cli.ts",
"cli:anvil-mainnet": "bash anvil.sh dev mainnet-local",
"cli:anvil-arbitrum": "bash anvil.sh dev arbitrum-local",
"cli:anvil4tests-mainnet": "bash anvil.sh test mainnet-local --fork-block-number 18629000",
"cli:anvil4tests-arbitrum": "bash anvil.sh test arbitrum-local --fork-block-number 18629000"
"cli:anvil-eth-mainnet": "bash anvil.sh dev eth-mainnet",
"cli:anvil-arbitrum": "bash anvil.sh dev arbitrum-mainnet",
"cli:anvil4tests-mainnet": "bash anvil.sh test eth-mainnet --fork-block-number 18629000",
"cli:anvil4tests-arbitrum": "bash anvil.sh test arbitrum-mainnet --fork-block-number 18629000"
},
"devDependencies": {
"@types/command-line-args": "^5.2.3",
Expand Down
52 changes: 52 additions & 0 deletions projects/examples/src/setup-ethMainnet.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
// import { BeanstalkSDK, DataSource, TestUtils } from "@beanstalk/sdk";
// import { ChainId } from "@beanstalk/sdk-core";
// import { Provider } from "@beanstalk/sdk-wells/dist/types/lib/WellsSDK";
// import { ethers } from "ethers";

// keeping these in the same file as ./setup for some reasons causes issues

// const RPC_URL = "http://127.0.0.1:9545";

// const network = {
// name: "local-eth-mainnet",
// chainId: ChainId.LOCALHOST_ETH,
// _defaultProvider: () => new ethers.providers.JsonRpcProvider(RPC_URL, network)
// };

// const provider = new ethers.providers.StaticJsonRpcProvider(RPC_URL, network);

// const connection = TestUtils.setupConnection(provider);

// const { signer, account } = connection;

// const sdk = new BeanstalkSDK({
// signer: signer,
// rpcUrl: RPC_URL,
// DEBUG: true
// });

// const chain = new TestUtils.BlockchainUtils(sdk);

// const impersonate = async (account) => {
// const stop = await chain.impersonate(account);
// const provider = ethers.getDefaultProvider(network) as Provider;
// const signer = await provider.getSigner(account);
// const sdk = new BeanstalkSDK({
// signer,
// source: DataSource.LEDGER,
// DEBUG: true
// });

// return { sdk, stop };
// };

// const ethMainnetUtils = {
// signer,
// account,
// provider,
// sdk,
// impersonate,
// chain
// };

// export default ethMainnetUtils;
26 changes: 2 additions & 24 deletions projects/examples/src/setup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,42 +3,20 @@ import { Provider } from "@beanstalk/sdk/dist/types/lib/BeanstalkSDK";
import { ethers } from "ethers";

const RPC_URL = "http://127.0.0.1:8545";
const LOCALHOST_ETH_RPC_URL = "http://127.0.0.1:9545";

const network = {
name: "local",
chainId: ChainId.LOCALHOST,
_defaultProvider: () => new ethers.providers.JsonRpcProvider(RPC_URL)
};

const ethMainnetNetwork = {
name: "local-eth-mainnet",
chainId: ChainId.LOCALHOST_ETH,
_defaultProvider: () => new ethers.providers.JsonRpcProvider(LOCALHOST_ETH_RPC_URL)
_defaultProvider: () => new ethers.providers.JsonRpcProvider(RPC_URL, network)
};

export const provider = new ethers.providers.StaticJsonRpcProvider(RPC_URL, network);
export const { signer, account } = TestUtils.setupConnection(provider);

export const localhostEthProvider = new ethers.providers.JsonRpcProvider(
LOCALHOST_ETH_RPC_URL,
ethMainnetNetwork
);
export const { signer: localhostEthSigner, account: localhostEthAccount } =
TestUtils.setupConnection(localhostEthProvider);
export const { signer, account } = TestUtils.setupConnection(provider);

export const sdk = new BeanstalkSDK({
signer,
rpcUrl: RPC_URL,
provider: provider,
source: DataSource.LEDGER,
DEBUG: true
});

export const ethSdk = new BeanstalkSDK({
signer: localhostEthSigner,
rpcUrl: LOCALHOST_ETH_RPC_URL,
provider: localhostEthProvider,
source: DataSource.LEDGER,
DEBUG: true
});
Expand Down
113 changes: 109 additions & 4 deletions projects/sdk/src/classes/Pool/BasinWell.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,122 @@
import { BasinWell__factory } from "src/constants/generated";
import { BasinWell__factory, BasinWell as BasinWellContract } from "src/constants/generated";
import { TokenValue } from "src/TokenValue";
import Pool, { Reserves } from "./Pool";
import { ERC20Token } from "../Token";
import { BeanstalkSDK } from "src/lib/BeanstalkSDK";

export class BasinWell extends Pool {
public readonly contract: BasinWellContract;

constructor(
sdk: BeanstalkSDK,
address: string,
lpToken: ERC20Token,
tokens: ERC20Token[],
metadata: {
name: string;
symbol: string;
logo: string;
color: string;
}
) {
super(sdk, address, lpToken, tokens, metadata);
this.contract = BasinWell__factory.connect(address, sdk.providerOrSigner);
}

public getContract() {
return BasinWell__factory.connect(this.address, Pool.sdk.providerOrSigner);
return this.contract;
}

public getReserves() {
Pool.sdk.debug(`BasinWell.getReserves(): ${this.address} ${this.name} on chain ${this.chainId}`);
Pool.sdk.debug(
`BasinWell.getReserves(): ${this.address} ${this.name} on chain ${this.chainId}`
);

return this.getContract()
.getReserves()
.then((result) => [TokenValue.fromBlockchain(result[0], 0), TokenValue.fromBlockchain(result[1], 0)] as Reserves);
.then(
(result) =>
[
TokenValue.fromBlockchain(result[0], 0),
TokenValue.fromBlockchain(result[1], 0)
] as Reserves
);
}

async getAddLiquidityOut(amounts: TokenValue[]) {
return this.contract
.getAddLiquidityOut(amounts.map((a) => a.toBigNumber()))
.then((result) => this.lpToken.fromBlockchain(result));
}

async getRemoveLiquidityOutEqual(amount: TokenValue) {
return this.contract
.getRemoveLiquidityOut(amount.toBigNumber())
.then((result) => this.tokens.map((token, i) => token.fromBlockchain(result[i])));
}

async getRemoveLiquidityOutOneToken(lpAmountIn: TokenValue, tokenOut: ERC20Token) {
return this.contract
.getRemoveLiquidityOneTokenOut(lpAmountIn.toBigNumber(), tokenOut.address)
.then((result) => tokenOut.fromBlockchain(result));
}

/**
* Get the @wagmi/core multicall params for removing liquidity
* @param lpAmountIn The amount of LP tokens to remove
* @returns @wagmi/core multicall calls for
* - removing equal amounts of liquidity
* - removing single sided liquidity as well.tokens[0]
* - removing single sided liquidity as well.tokens[1]
*/
static getRemoveLiquidityOutMulticallParams(well: BasinWell, lpAmountIn: TokenValue) {
const contract = {
address: well.address as `0x${string}`,
abi: removeLiquidityPartialABI
};

const removeEqual = {
...contract,
method: "getRemoveLiquidityOut",
args: [lpAmountIn.toBigNumber()]
};

const removeSingleSided0 = {
...contract,
method: "getRemoveLiquidityOneTokenOut",
args: [lpAmountIn.toBigNumber(), well.tokens[0].address as `0x${string}`]
};

const removeSingleSided1 = {
...contract,
method: "getRemoveLiquidityOneTokenOut",
args: [lpAmountIn.toBigNumber(), well.tokens[1].address as `0x${string}`]
};

return {
equal: removeEqual,
side0: removeSingleSided0,
side1: removeSingleSided1
};
}
}

const removeLiquidityPartialABI = [
{
inputs: [
{ internalType: "uint256", name: "lpAmountIn", type: "uint256" },
{ internalType: "contract IERC20", name: "tokenOut", type: "address" }
],
name: "getRemoveLiquidityOneTokenOut",
outputs: [{ internalType: "uint256", name: "tokenAmountOut", type: "uint256" }],
stateMutability: "view",
type: "function"
},
{
inputs: [{ internalType: "uint256", name: "lpAmountIn", type: "uint256" }],
name: "getRemoveLiquidityOut",
outputs: [{ internalType: "uint256[]", name: "tokenAmountsOut", type: "uint256[]" }],
stateMutability: "view",
type: "function"
}
] as const;
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
"type": "address"
}
],
"name": "getConstantProductWell",
"name": "getWell",
"outputs": [
{
"components": [
Expand Down
15 changes: 1 addition & 14 deletions projects/sdk/src/lib/silo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -194,19 +194,6 @@ export class Silo {
return this.siloConvert.convertEstimate(fromToken, toToken, fromAmount);
}

public async getDeposits(_account: string) {
const deposits = await Silo.sdk.contracts.beanstalk.getDepositsForAccount(_account);
}

public async getTokenDeposits(_account: string, token: Token) {
const tokenDeposits = Silo.sdk.contracts.beanstalk.getTokenDepositsForAccount(
_account,
token.address
);

const dict = {};
}

/**
*
* Return the Farmer's balance of a single whitelisted token.
Expand Down Expand Up @@ -236,7 +223,7 @@ export class Silo {
account,
_token.address
);
const depositsByToken = utils.parseDepositsByToken(Silo.sdk, farmerDeposits);
const depositsByToken = utils.parseDepositsByToken(Silo.sdk, [farmerDeposits]);

// The processor's return schema assumes we might have wanted to grab
// multiple tokens, so we have to grab the one we want
Expand Down
16 changes: 8 additions & 8 deletions projects/sdk/src/lib/silo/Withdraw.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,36 +36,36 @@ export class Withdraw {
const withdrawData = this.calculateWithdraw(token, amount, balance.deposits, season);
Withdraw.sdk.debug("silo.withdraw(): withdrawData", { withdrawData });

const seasons = withdrawData.crates.map((crate) => crate.stem.toString());
const stems = withdrawData.crates.map((crate) => crate.stem.toString());
const amounts = withdrawData.crates.map((crate) => crate.amount.toBlockchain());

let contractCall;

if (seasons.length === 0) {
if (stems.length === 0) {
throw new Error("Malformatted crates");
}

if (seasons.length === 1) {
if (stems.length === 1) {
Withdraw.sdk.debug("silo.withdraw(): withdrawDeposit()", {
address: token.address,
season: seasons[0],
stem: stems[0],
amount: amounts[0]
});
contractCall = Withdraw.sdk.contracts.beanstalk.withdrawDeposit(
token.address,
seasons[0],
stems[0],
amounts[0],
toMode
);
} else {
Withdraw.sdk.debug("silo.withdraw(): withdrawDeposits()", {
address: token.address,
seasons: seasons,
stems: stems,
amounts: amounts
});
contractCall = Withdraw.sdk.contracts.beanstalk.withdrawDeposits(
token.address,
seasons,
stems,
amounts,
toMode
);
Expand All @@ -89,4 +89,4 @@ export class Withdraw {
crates: pickedCrates.crates
};
}
}
}
13 changes: 6 additions & 7 deletions projects/sdk/src/lib/silo/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@ import { TokenValue } from "@beanstalk/sdk-core";
import { TokenSiloBalance, Deposit } from "./types";
import { assert } from "src/utils";
import { SiloGettersFacet } from "src/constants/generated/protocol/abi/Beanstalk";
import { MayArray } from "src/types";
import { isArray } from "src/utils/common";

export function sortCrates(state: TokenSiloBalance) {
state.deposits = state.deposits.sort(
Expand Down Expand Up @@ -42,8 +40,10 @@ export function sortCratesByBDVRatio(crates: Deposit[], direction: "asc" | "desc
export function pickCrates(
deposits: Deposit[],
amount: TokenValue,
token: Token,
currentSeason: number
// TODO: remove these
_token: Token,
// TODO: remove these
_currentSeason: number
) {
let totalAmount = TokenValue.ZERO;
let totalBDV = TokenValue.ZERO;
Expand Down Expand Up @@ -159,11 +159,10 @@ type TokenDepositsByStem = {

export function parseDepositsByToken(
sdk: BeanstalkSDK,
data: MayArray<SiloGettersFacet.TokenDepositIdStructOutput>
data: SiloGettersFacet.TokenDepositIdStructOutput[]
) {
const depositsByToken: Map<Token, TokenDepositsByStem> = new Map();
const datas = isArray(data) ? data : [data];
datas.forEach(({ token: tokenAddr, depositIds, tokenDeposits }) => {
data.forEach(({ token: tokenAddr, depositIds, tokenDeposits }) => {
const token = sdk.tokens.findByAddress(tokenAddr);
if (!token) return;

Expand Down
Loading

0 comments on commit 4ad7138

Please sign in to comment.