Skip to content

Commit

Permalink
Change Delta B to Instantaneous + Fix Well Tests (#592)
Browse files Browse the repository at this point in the history
  • Loading branch information
publiuss authored Aug 7, 2023
2 parents c2543e2 + ce25909 commit f4de22d
Show file tree
Hide file tree
Showing 6 changed files with 103 additions and 76 deletions.
106 changes: 53 additions & 53 deletions protocol/abi/Beanstalk.json
Original file line number Diff line number Diff line change
Expand Up @@ -4484,6 +4484,59 @@
"stateMutability": "payable",
"type": "function"
},
{
"inputs": [
{
"internalType": "address",
"name": "tokenIn",
"type": "address"
},
{
"internalType": "address",
"name": "tokenOut",
"type": "address"
},
{
"internalType": "uint256",
"name": "amountIn",
"type": "uint256"
}
],
"name": "getAmountOut",
"outputs": [
{
"internalType": "uint256",
"name": "amountOut",
"type": "uint256"
}
],
"stateMutability": "view",
"type": "function"
},
{
"inputs": [
{
"internalType": "address",
"name": "tokenIn",
"type": "address"
},
{
"internalType": "address",
"name": "tokenOut",
"type": "address"
}
],
"name": "getMaxAmountIn",
"outputs": [
{
"internalType": "uint256",
"name": "amountIn",
"type": "uint256"
}
],
"stateMutability": "view",
"type": "function"
},
{
"anonymous": false,
"inputs": [
Expand Down Expand Up @@ -4570,59 +4623,6 @@
"stateMutability": "payable",
"type": "function"
},
{
"inputs": [
{
"internalType": "address",
"name": "tokenIn",
"type": "address"
},
{
"internalType": "address",
"name": "tokenOut",
"type": "address"
},
{
"internalType": "uint256",
"name": "amountIn",
"type": "uint256"
}
],
"name": "getAmountOut",
"outputs": [
{
"internalType": "uint256",
"name": "amountOut",
"type": "uint256"
}
],
"stateMutability": "view",
"type": "function"
},
{
"inputs": [
{
"internalType": "address",
"name": "tokenIn",
"type": "address"
},
{
"internalType": "address",
"name": "tokenOut",
"type": "address"
}
],
"name": "getMaxAmountIn",
"outputs": [
{
"internalType": "uint256",
"name": "amountIn",
"type": "uint256"
}
],
"stateMutability": "view",
"type": "function"
},
{
"inputs": [
{
Expand Down
20 changes: 18 additions & 2 deletions protocol/contracts/ecosystem/price/WellPrice.sol
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ pragma experimental ABIEncoderV2;

import {P} from "./P.sol";
import {SafeMath} from "@openzeppelin/contracts/math/SafeMath.sol";
import {IWell, IERC20} from "../../interfaces/basin/IWell.sol";
import {Call, IWell, IERC20} from "../../interfaces/basin/IWell.sol";
import {IBeanstalkWellFunction} from "../../interfaces/basin/IBeanstalkWellFunction.sol";
import {LibUsdOracle} from "../../libraries/Oracle/LibUsdOracle.sol";
import {LibWellMinting} from "../../libraries/Minting/LibWellMinting.sol";
Expand Down Expand Up @@ -74,9 +74,25 @@ contract WellPrice {
.mul(2)
.div(PRICE_PRECISION);

pool.deltaB = BEANSTALK.poolDeltaB(wellAddress);
pool.deltaB = getDeltaB(wellAddress, wellTokens, wellBalances);
pool.lpUsd = pool.liquidity.mul(WELL_DECIMALS).div(IERC20(wellAddress).totalSupply());
pool.lpBdv = BEANSTALK.bdv(wellAddress, WELL_DECIMALS);
}

function getDeltaB(address well, IERC20[] memory tokens, uint256[] memory reserves) internal view returns (int256 deltaB) {
Call memory wellFunction = IWell(well).wellFunction();
(uint256[] memory ratios, uint256 beanIndex, bool success) = LibWell.getRatiosAndBeanIndex(tokens);
// If the USD Oracle oracle call fails, we can't compute deltaB
if(!success) return 0;

uint256 beansAtPeg = IBeanstalkWellFunction(wellFunction.target).calcReserveAtRatioLiquidity(
reserves,
beanIndex,
ratios,
wellFunction.data
);

deltaB = int256(beansAtPeg) - int256(reserves[beanIndex]);
}

}
4 changes: 4 additions & 0 deletions protocol/contracts/mocks/well/MockSetComponentsWell.sol
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,10 @@ contract MockSetComponentsWell is MockToken {
_reserves = new uint256[](2);
}

function init() external {
_reserves = new uint256[](2);
}

Call[] public _pumps;
Call public _wellFunction;

Expand Down
7 changes: 4 additions & 3 deletions protocol/test/Silo.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -382,9 +382,10 @@ describe('Silo', function () {
depositID4 = '0x1BEA3CcD22F4EBd3d37d731BA31Eeca95713716DFFF000000000000000000111';
expect(await this.metadata.uri(depositID4)).to.eq(depositmetadata);

depositmetadata = await fs.readFileSync(__dirname + '/data/base64EncodedImageBeanEth.txt', 'utf-8');
depositID5 = '0x9bAaB117304f7D6517048e371025dB8f89a8DbE5FFFFFFFFFFFFF00000000002';
expect(await this.metadata.uri(depositID5)).to.eq(depositmetadata);
// TODO: reimplement once basin addresses are deployed.
// depositmetadata = await fs.readFileSync(__dirname + '/data/base64EncodedImageBeanEth.txt', 'utf-8');
// depositID5 = '0x9bAaB117304f7D6517048e371025dB8f89a8DbE5FFFFFFFFFFFFF00000000002';
// expect(await this.metadata.uri(depositID5)).to.eq(depositmetadata);

});

Expand Down
24 changes: 12 additions & 12 deletions protocol/test/beanstalkPrice.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -156,15 +156,15 @@ describe('BeanstalkPrice', function () {

expect(p.price).to.equal('1499997');
expect(p.liquidity).to.equal('3999995000000');
expect(p.deltaB).to.equal('133679332828');
expect(p.deltaB).to.equal('500000000000');

expect(c.price).to.equal('999999');
expect(c.liquidity).to.equal('1999999000000');
expect(c.deltaB).to.equal('0');

expect(w.price).to.equal('1999996');
expect(w.liquidity).to.equal('1999996000000');
expect(w.deltaB).to.equal('133679332828');
expect(w.deltaB).to.equal('500000000000');
})

it('deltaB > 0, wells and curve', async function () {
Expand Down Expand Up @@ -194,15 +194,15 @@ describe('BeanstalkPrice', function () {

expect(p.price).to.equal('1491246');
expect(p.liquidity).to.equal('4108725000000');
expect(p.deltaB).to.equal('183543345294');
expect(p.deltaB).to.equal('549891561002');

expect(c.price).to.equal('1008729');
expect(c.liquidity).to.equal('2108729000000');
expect(c.deltaB).to.equal('49891561002');

expect(w.price).to.equal('1999996');
expect(w.liquidity).to.equal('1999996000000');
expect(w.deltaB).to.equal('133651784292');
expect(w.deltaB).to.equal('500000000000');
})

it('deltaB < 0, curve only', async function () {
Expand Down Expand Up @@ -253,15 +253,15 @@ describe('BeanstalkPrice', function () {

expect(p.price).to.equal('749999');
expect(p.liquidity).to.equal('3999995000000');
expect(p.deltaB).to.equal('-224612602483');
expect(p.deltaB).to.equal('-1000000000000');

expect(c.price).to.equal('999999');
expect(c.liquidity).to.equal('1999999000000');
expect(c.deltaB).to.equal('0');

expect(w.price).to.equal('499999');
expect(w.liquidity).to.equal('1999996000000');
expect(w.deltaB).to.equal('-224612602483');
expect(w.deltaB).to.equal('-1000000000000');
})

it('deltaB < 0, wells and curve', async function () {
Expand Down Expand Up @@ -291,15 +291,15 @@ describe('BeanstalkPrice', function () {

expect(p.price).to.equal('751106');
expect(p.liquidity).to.equal('4090476600000');
expect(p.deltaB).to.be.within('-274563881303', '-274485381948')
expect(p.deltaB).to.be.eq('-1050108438998')

expect(c.price).to.equal('991346');
expect(c.liquidity).to.equal('2090480600000');
expect(c.deltaB).to.equal('-50108438998');

expect(w.price).to.equal('499999');
expect(w.liquidity).to.equal('1999996000000');
expect(w.deltaB).to.equal('-224376942950');
expect(w.deltaB).to.equal('-1000000000000');
})

it('well deltaB > 0, curve deltaB < 0', async function () {
Expand Down Expand Up @@ -329,15 +329,15 @@ describe('BeanstalkPrice', function () {

expect(p.price).to.equal('1484514');
expect(p.liquidity).to.equal('4090476600000');
expect(p.deltaB).to.equal('83488280312');
expect(p.deltaB).to.equal('449891561002');

expect(c.price).to.equal('991346');
expect(c.liquidity).to.equal('2090480600000');
expect(c.deltaB).to.equal('-50108438998');

expect(w.price).to.equal('1999996');
expect(w.liquidity).to.equal('1999996000000');
expect(w.deltaB).to.be.within('133569202852','133596719310');
expect(w.deltaB).to.be.eq('500000000000');
})

it('well deltaB < 0, curve deltaB > 0', async function () {
Expand Down Expand Up @@ -367,15 +367,15 @@ describe('BeanstalkPrice', function () {

expect(p.price).to.equal('761095');
expect(p.liquidity).to.equal('4108725000000');
expect(p.deltaB).to.be.within('-174485381948','-174406936344');
expect(p.deltaB).to.be.eq('-950108438998');

expect(c.price).to.equal('1008729');
expect(c.liquidity).to.equal('2108729000000');
expect(c.deltaB).to.equal('49891561002');

expect(w.price).to.equal('499999');
expect(w.liquidity).to.equal('1999996000000');
expect(w.deltaB).to.equal('-224298497346');
expect(w.deltaB).to.equal('-1000000000000');
})

});
Expand Down
18 changes: 12 additions & 6 deletions protocol/utils/well.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
const fs = require('fs');
const { BEAN, WETH, BEANSTALK_PUMP } = require('../test/utils/constants');
const { BEAN, WETH, BEANSTALK_PUMP, BEAN_ETH_WELL } = require('../test/utils/constants');
const { to6, to18 } = require('../test/utils/helpers');
const { getBeanstalk } = require('./contracts');
const { mintEth } = require('./mint');
Expand Down Expand Up @@ -100,7 +100,7 @@ async function deployWell(tokens, verbose = false, salt = ethers.constants.HashZ
if (verbose) console.log("Deployed Aquifer", aquifer.address);
const wellFunction = await deployWellContract('ConstantProduct2');
if (verbose) console.log("Deployed Well Function", wellFunction.address);
const pump = await deployGeoEmaAndCumSmaPump()
const pump = await deployMultiFlowPump()
if (verbose) console.log("Deployed Pump", pump.address);

const immutableData = await encodeWellImmutableData(
Expand Down Expand Up @@ -205,8 +205,8 @@ async function deployMockPump() {
return await ethers.getContractAt('MockPump', BEANSTALK_PUMP)
}

async function deployGeoEmaAndCumSmaPump() {
pump = await (await getWellContractFactory('GeoEmaAndCumSmaPump')).deploy(
async function deployMultiFlowPump() {
pump = await (await getWellContractFactory('MultiFlowPump')).deploy(
'0x3ffe0000000000000000000000000000', // 0.5
'0x3ffd555555555555553cbcd83d925070', // 0.333333333333333333
12,
Expand All @@ -218,7 +218,7 @@ async function deployGeoEmaAndCumSmaPump() {
BEANSTALK_PUMP,
await ethers.provider.getCode(pump.address),
]);
return await getWellContractAt('GeoEmaAndCumSmaPump', BEANSTALK_PUMP)
return await getWellContractAt('MultiFlowPump', BEANSTALK_PUMP)
}

async function deployMockWell() {
Expand All @@ -228,8 +228,14 @@ async function deployMockWell() {

let well = await (await ethers.getContractFactory('MockSetComponentsWell', await getWellDeployer())).deploy()
await well.deployed()
await network.provider.send("hardhat_setCode", [
BEAN_ETH_WELL,
await ethers.provider.getCode(well.address),
]);
well = await ethers.getContractAt('MockSetComponentsWell', BEAN_ETH_WELL)
await well.init()

pump = await deployGeoEmaAndCumSmaPump()
pump = await deployMultiFlowPump()

await well.setPumps([[pump.address, '0x']])
await well.setWellFunction([wellFunction.address, '0x'])
Expand Down

0 comments on commit f4de22d

Please sign in to comment.