Skip to content

Commit

Permalink
Merge branch 'reseed-testnet-deployment' into beanstalk3-ui-sdks
Browse files Browse the repository at this point in the history
  • Loading branch information
Space-Bean authored Sep 20, 2024
2 parents 065307e + aff6543 commit 2ad6fbf
Show file tree
Hide file tree
Showing 24 changed files with 2,440 additions and 21,144 deletions.
2 changes: 1 addition & 1 deletion protocol/contracts/beanstalk/migration/L1RecieverFacet.sol
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ contract L1RecieverFacet is ReentrancyGuard {
bytes32 internal constant DEPOSIT_MERKLE_ROOT =
0x53d64bba5389504aeff2d680bd0d5a4a27f7c535578915ccfbc64f4aef1656ee;
bytes32 internal constant PLOT_MERKLE_ROOT =
0x26da8c597f94901bab9ecbe1f491f3d953d358c7221680a27dc76e19318de3d1;
0x828c73c8e073801828c564f95bafd55c6a0485f244d4581e638c90ca678d3a28;
bytes32 internal constant INTERNAL_BALANCE_MERKLE_ROOT =
0x0ed5cf8908ddecd7d9285e5809315d007bfd5bd6d71f4c7db1131569180d23e2;
bytes32 internal constant FERTILIZER_MERKLE_ROOT =
Expand Down
5 changes: 0 additions & 5 deletions protocol/hardhat.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -547,11 +547,6 @@ module.exports = {
chainId: 5,
url: process.env.GOERLI_RPC || "",
timeout: 100000
},
reseedArbitrum: {
url: "https://virtual.arbitrum.rpc.tenderly.co/10991114-8428-4e06-88dc-426e4e5673da",
chainId: 42161,
timeout: 10000000000000
}
},
etherscan: {
Expand Down
11 changes: 7 additions & 4 deletions protocol/reseed/data/exports/contract-circulating20736200.json
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
{
"beanstalk": {
"beans": "32952499881747",
"unripeBeans": "107495547853813",
"unripeLp": "67632589795645"
"beans": "29976763899684",
"unripeBeans": "96200825182974",
"unripeLp": "67632589795645",
"ethLp": "1323213132988957601130",
"wstethLp": "185335736965741266416875",
"bs3crvLp": "93024994011998500979055"
},
"pools": {
"beanweth": {
Expand All @@ -18,4 +21,4 @@
"3crv": "20668624046092866087554"
}
}
}
}
4 changes: 2 additions & 2 deletions protocol/reseed/data/exports/storage-system20736200.json
Original file line number Diff line number Diff line change
Expand Up @@ -619,7 +619,7 @@
"unripeSettings": {
"0x1BEA054dddBca12889e07B3E076f511Bf1d27543": {
"underlyingToken": "0xBEA0005B8599265D41256905A9B3073D397812E4",
"balanceOfUnderlying": "0x19f4f0551a45"
"balanceOfUnderlying": "0x174018a57256"
},
"0x1BEA059c3Ea15F6C10be1c53d70C75fD1266D788": {
"underlyingToken": "0xBEA0093f626Ce32dd6dA19617ba4e7aA0c3228e8",
Expand Down Expand Up @@ -941,7 +941,7 @@
"activeFertilizer": "0x106b5be",
"fertilizedIndex": "0x5249320f8fa",
"unfertilizedIndex": "0x57266060fa3c",
"fertilizedPaidIndex": "0x4ba57633e2d",
"fertilizedPaidIndex": "0x5249320f8fa",
"fertFirst": "0x145c1f",
"fertLast": "0x5b8d80",
"bpf": "0x53342",
Expand Down
28 changes: 14 additions & 14 deletions protocol/reseed/data/global.json
Original file line number Diff line number Diff line change
Expand Up @@ -233,7 +233,7 @@
"17216958",
"5654645373178",
"95822337342012",
"5198376549933",
"5654645373178",
"1334303",
"6000000",
"340802",
Expand All @@ -255,34 +255,34 @@
],
[
[
"3569521145236",
"3582258810468"
"93730848969040",
"21505944545778"
],
[
"890052022584",
"119822073895"
"64256517403254",
"17207954363487"
],
[
"122660960",
"3003870654"
"3569521145236",
"3582258810468"
],
[
"966131510422",
"88320676626"
"122660960",
"3003870654"
],
[
"93730848969040",
"21505944545778"
"890052022584",
"119822073895"
],
[
"64256517403254",
"17207954363487"
"966131510422",
"88320676626"
]
],
[
[
"0xBEA0005B8599265D41256905A9B3073D397812E4",
"28539794823749"
"25564058841686"
],
[
"0xBEA0093f626Ce32dd6dA19617ba4e7aA0c3228e8",
Expand Down
4 changes: 2 additions & 2 deletions protocol/reseed/data/r2/L2_initial_supply.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
[
"32952499881747",
"107495547853813",
"29976763899684",
"96200825182974",
"67632589795645"
]
24 changes: 14 additions & 10 deletions protocol/reseed/dataConverts/convertGlobal.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,17 @@ const { convertToBigNum } = require("../../utils/read.js");
function parseGlobals(inputFilePath, outputFilePath) {
const data = JSON.parse(fs.readFileSync(inputFilePath, "utf8"));

// Sort silo tokens alphabetically
const sortedSiloTokens = Object.keys(
data.silo?.balances || { "0x0000000000000000000000000000000000000000": {} }
).sort();

// Create an object of sorted balances
const sortedBalances = sortedSiloTokens.reduce((acc, token) => {
acc[token] = data.silo?.balances[token] || { deposited: "0", depositedBdv: "0" };
return acc;
}, {});

const result = [
// SystemInternalBalances
[
Expand Down Expand Up @@ -35,16 +46,9 @@ function parseGlobals(inputFilePath, outputFilePath) {
data.silo?.earnedBeans ? convertToBigNum(data.silo.earnedBeans) : "0",
data.orderLockedBeans ? convertToBigNum(data.orderLockedBeans) : "0",
// all silo tokens
Object.keys(
data.silo?.balances || { "0x0000000000000000000000000000000000000000": {} }
).sort(), // Sort alphabetically so that the 2 Unripe tokens that start with 0x1BEA appear first
// This is assumed in ReseedGlobal.sol
// all silo balances
Object.values(
data.silo?.balances || {
"0x0000000000000000000000000000000000000000": { deposited: "0", depositedBdv: "0" }
}
).map((balance) => [
sortedSiloTokens,
// all silo balances (now sorted to match tokens)
Object.values(sortedBalances).map((balance) => [
convertToBigNum(balance.deposited),
convertToBigNum(balance.depositedBdv)
]),
Expand Down
128 changes: 104 additions & 24 deletions protocol/reseed/reseedAddLiquidityAndTransfer.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@ const {
} = require("../test/hardhat/utils/constants.js");
const { to18, to6, toX } = require("../test/hardhat/utils/helpers.js");
const { impersonateToken } = require("../scripts/impersonate.js");
const { getWellContractAt } = require("../utils/well.js");
const fs = require("fs");
const { toBN } = require("../utils");

const WellAddresses = [
"0xBEA00A3F7aaF99476862533Fe7DcA4b50f6158cB", // BEAN/WETH
Expand All @@ -29,50 +31,128 @@ const NonBeanToken = [
L2_USDT // USDT
];

async function setBalance(
contract, // token contract address
account, // account address
amount, // amount to set
slot // storage slot to set
) {
const index = ethers.utils.solidityKeccak256(["uint256", "uint256"], [account, slot]);
const balance = ethers.utils.hexlify(
ethers.utils.zeroPad(ethers.BigNumber.from(amount).toHexString(), 32)
);
await hre.network.provider.send("hardhat_setStorageAt", [contract, index, balance]);
}

async function reseedAddLiquidityAndTransfer(account, L2Beanstalk, mock = true, verbose = true) {
const INIT_WELL_BALANCES = "./reseed/data/r2/L2_well_balances.json";
[balancesInBeanEthWell, balancesInBeanWstEthWell, balancesInBeanStableWell] = JSON.parse(
await fs.readFileSync(INIT_WELL_BALANCES)
);

// divide balancesInBeanStableWell[1] by 1e12:
balancesInBeanStableWell[1] = Math.floor(balancesInBeanStableWell[1] / 1e12).toString();
const slots = [
51, // WETH
1, // wstETH
51, // WEETH
51, // WBTC
9, // USDC
51 // USDT
];

const L1_beanstalkBalances = [
"1323213132988957601130", // ethLP
"185335736965741266416875", // wstethLP
"93024994011998500979055" // 3crvLP
];

// note: the amounts
// were updated with the amounts in `L2_well_balances.json`

// calculate the LP token supply for each well:
// note: this is an estimation and is used for the sake of testing.

const cp2 = await getWellContractAt(
"ConstantProduct2",
"0xBA5104f2df98974A83CD10d16E24282ce6Bb647f"
);

const beanEthLpTokenSupply = await cp2.calcLpTokenSupply(balancesInBeanEthWell, "0x");
console.log("beanEthLpTokenSupply", beanEthLpTokenSupply);
const beanWstEthLpTokenSupply = await cp2.calcLpTokenSupply(balancesInBeanWstEthWell, "0x");
console.log("beanWstEthLpTokenSupply", beanWstEthLpTokenSupply);
// const beanStableLpTokenSupply = await cp2.calcLpTokenSupply(balancesInBeanStableWell, "0x");
// console.log("beanStableLpTokenSupply", beanStableLpTokenSupply);

// get the % of the total LP token supply that each well has:
// note: the bean3crv was manually calculated given the migration to the well.
const beanEthLpTokenSupplyPercentage = L1_beanstalkBalances[0] / beanEthLpTokenSupply;
const beanWstEthLpTokenSupplyPercentage = L1_beanstalkBalances[1] / beanWstEthLpTokenSupply;
const beanStableLpTokenSupplyPercentage = 0.998832;

const scaleFactor = to18("1"); // Scale factor to avoid decimals

// Convert percentages to BigNumber with scaling
const beanEthLpTokenSupplyPercentageBN = toBN((beanEthLpTokenSupplyPercentage * 1e18).toString());
const beanWstEthLpTokenSupplyPercentageBN = toBN(
(beanWstEthLpTokenSupplyPercentage * 1e18).toString()
);
const beanStableLpTokenSupplyPercentageBN = toBN(
(beanStableLpTokenSupplyPercentage * 1e18).toString()
);

// Scale balances using the percentages
balancesInBeanEthWell = await balancesInBeanEthWell.map((balance) => {
const balanceBN = toBN(balance.toString());
return balanceBN.mul(beanEthLpTokenSupplyPercentageBN).div(scaleFactor).toString(); // Scale down
});

balancesInBeanWstEthWell = await balancesInBeanWstEthWell.map((balance) => {
const balanceBN = toBN(balance.toString());
return balanceBN.mul(beanWstEthLpTokenSupplyPercentageBN).div(scaleFactor).toString(); // Scale down
});

balancesInBeanStableWell = await balancesInBeanStableWell.map((balance) => {
const balanceBN = toBN(balance.toString());
return balanceBN.mul(beanStableLpTokenSupplyPercentageBN).div(scaleFactor).toString(); // Scale down
});

const nonBeanAmounts = [
to18("20"), // BEAN/WETH
to18("2556"), // BEAN/WstETH
to18("16"), // BEAN/WEEETH
toX("8", 8), // BEAN/WBTC (8 decimals)
to6("190000"), // BEAN/USDC
to6("190000") // BEAN/USDT
balancesInBeanEthWell[1], // BEAN/WETH
balancesInBeanWstEthWell[1], // BEAN/WstETH
to6("0"), // BEAN/WEEETH
toX("0", 8), // BEAN/WBTC (8 decimals)
balancesInBeanStableWell[1], // BEAN/USDC
to6("0") // to6("190000") // BEAN/USDT
];

const beanAmounts = [
to6("108357"), // BEAN/WETH
to6("14642617"), // BEAN/WstETH
to6("100000"), // BEAN/WEEETH
to6("1000000"), // BEAN/WBTC
to6("1000000"), // BEAN/USDC
to6("1000000") // BEAN/USDT
balancesInBeanEthWell[0], // BEAN/WETH
balancesInBeanWstEthWell[0], // BEAN/WstETH
to6("0"), // to6("100000"), // BEAN/WEEETH
to6("0"), // to6("1000000"), // BEAN/WBTC
balancesInBeanStableWell[0], // BEAN/USDC
to6("0") // to6("1000000") // BEAN/USDT
];
console.log("-----------------------------------");
console.log("add liquidity to wells and transfers to l2 beanstalk.\n");
const beanAddress = "0xBEA0005B8599265D41256905A9B3073D397812E4";
await impersonateToken(beanAddress, 6);
const bean = await ethers.getContractAt("MockToken", beanAddress);

await impersonateToken("0xBEA0005B8599265D41256905A9B3073D397812E4", 6);
const bean = await ethers.getContractAt(
"MockToken",
"0xBEA0005B8599265D41256905A9B3073D397812E4"
);
// add liquidity and transfer to L2 Beanstalk:
for (let i = 0; i < WellAddresses.length; i++) {
console.log(`-----------------------------------`);
const well = await ethers.getContractAt("IWell", WellAddresses[i], account);
const wellERC20 = await ethers.getContractAt("IERC20", WellAddresses[i], account);
const token = await ethers.getContractAt("MockToken", NonBeanToken[i], account);
const decimals = await token.decimals();
await impersonateToken(NonBeanToken[i], decimals);
const token = await ethers.getContractAt("IERC20", NonBeanToken[i], account);

if (mock) {
// mint tokens to add liquidity:
console.log(`Minting tokens for ${WellAddresses[i]} and ${NonBeanToken[i]}`);
await token.mint(account.address, nonBeanAmounts[i]);
await bean.mint(account.address, beanAmounts[i] + to6("1000000"));
await setBalance(NonBeanToken[i], account.address, nonBeanAmounts[i], slots[i]);
await setBalance(beanAddress, account.address, beanAmounts[i], 0); // storage slot 0 for MockToken
}

console.log(`Approving tokens for ${WellAddresses[i]} and ${NonBeanToken[i]}`);
await token.connect(account).approve(well.address, MAX_UINT256);
await bean.connect(account).approve(well.address, MAX_UINT256);
Expand Down
Loading

0 comments on commit 2ad6fbf

Please sign in to comment.