Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

SIP-328 #1681

Merged
merged 4 commits into from
Jul 25, 2023
Merged

SIP-328 #1681

Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -228,4 +228,13 @@ interface IMarketManagerModule {
* @return minRatioD18 The current market-specific minimum liquidity ratio, denominated with 18 decimals of precision. (100% is represented by 1 followed by 18 zeros.)
*/
function getMinLiquidityRatio(uint128 marketId) external view returns (uint256 minRatioD18);

function getMarketPools(
uint128 marketId
) external returns (uint128[] memory inRangePoolIds, uint128[] memory outRangePoolIds);

function getMarketPoolDebtDistribution(
uint128 marketId,
uint128 poolId
) external returns (uint256 sharesD18, uint128 totalSharesD18, int128 valuePerShareD27);
}
53 changes: 53 additions & 0 deletions protocol/synthetix/contracts/modules/core/MarketManagerModule.sol
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,12 @@ import "@synthetixio/core-contracts/contracts/errors/AccessError.sol";
import "@synthetixio/core-contracts/contracts/ownership/OwnableStorage.sol";
import "@synthetixio/core-contracts/contracts/utils/SafeCast.sol";
import "@synthetixio/core-contracts/contracts/utils/ERC165Helper.sol";
import "@synthetixio/core-contracts/contracts/utils/HeapUtil.sol";

import "../../storage/Config.sol";
import "../../storage/Market.sol";
import "../../storage/MarketCreator.sol";
import "../../storage/Distribution.sol";

import "@synthetixio/core-modules/contracts/storage/AssociatedSystem.sol";
import "@synthetixio/core-modules/contracts/storage/FeatureFlag.sol";
Expand All @@ -30,6 +32,8 @@ contract MarketManagerModule is IMarketManagerModule {
using SafeCastI256 for int256;
using Market for Market.Data;
using AssociatedSystem for AssociatedSystem.Data;
using Distribution for Distribution.Data;
using HeapUtil for HeapUtil.Data;

using DecimalMath for uint256;

Expand Down Expand Up @@ -111,6 +115,55 @@ contract MarketManagerModule is IMarketManagerModule {
return market.getDebtPerShare();
}

/**
* @inheritdoc IMarketManagerModule
*/
function getMarketPools(
uint128 marketId
)
external
override
returns (uint128[] memory inRangePoolIds, uint128[] memory outRangePoolIds)
{
Market.Data storage market = Market.load(marketId);
market.distributeDebtToPools(999999999);

HeapUtil.Data storage inRangePools = market.inRangePools;
inRangePoolIds = new uint128[](inRangePools.size());
for (uint i = 1; i <= inRangePools.size(); i++) {
HeapUtil.Node memory node = inRangePools.getByIndex(i);
inRangePoolIds[i - 1] = node.id;
}

HeapUtil.Data storage outRangePools = market.outRangePools;
outRangePoolIds = new uint128[](outRangePools.size());
for (uint i = 1; i <= outRangePools.size(); i++) {
HeapUtil.Node memory node = outRangePools.getByIndex(i);
outRangePoolIds[i - 1] = node.id;
}
}

/**
* @inheritdoc IMarketManagerModule
*/
function getMarketPoolDebtDistribution(
uint128 marketId,
uint128 poolId
)
external
override
returns (uint256 sharesD18, uint128 totalSharesD18, int128 valuePerShareD27)
{
Market.Data storage market = Market.load(marketId);
market.distributeDebtToPools(999999999);

Distribution.Data storage poolDistribution = market.poolsDebtDistribution;
sharesD18 = poolDistribution.getActorShares(poolId.toBytes32());

totalSharesD18 = market.poolsDebtDistribution.totalSharesD18;
valuePerShareD27 = market.poolsDebtDistribution.valuePerShareD27;
}

/**
* @inheritdoc IMarketManagerModule
*/
Expand Down
2 changes: 1 addition & 1 deletion protocol/synthetix/contracts/storage/Vault.sol
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ library Vault {
function updateCreditCapacity(
Data storage self,
uint256 collateralPriceD18
) internal returns (uint256 usdWeightD18, int256 totalDebtD18) {
) internal view returns (uint256 usdWeightD18, int256 totalDebtD18) {
VaultEpoch.Data storage epochData = currentEpoch(self);

usdWeightD18 = (epochData.collateralAmounts.totalAmount()).mulDecimal(collateralPriceD18);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import { bootstrapWithMockMarketAndPool } from '../../bootstrap';
import { MockMarket__factory } from '../../../../typechain-types/index';
import { verifyUsesFeatureFlag } from '../../verifications';
import { snapshotCheckpoint } from '@synthetixio/core-utils/utils/mocha/snapshot';
import { bn } from '../../../common';

describe('MarketManagerModule', function () {
const {
Expand Down Expand Up @@ -572,4 +573,116 @@ describe('MarketManagerModule', function () {
});
});
});

describe('getMarketPools()', () => {
before(restore);

before('add more staked pools', async () => {
// want a total of 3 staked pools
// create
await systems()
.Core.connect(owner)
.createPool(poolId + 1, await owner.getAddress());
await systems()
.Core.connect(owner)
.createPool(poolId + 2, await owner.getAddress());

// configure
await systems()
.Core.connect(owner)
.setPoolConfiguration(poolId, [
{
marketId: marketId(),
weightD18: ethers.utils.parseEther('1'),
maxDebtShareValueD18: ethers.utils.parseEther('0.1'),
},
]);
await systems()
.Core.connect(owner)
.setPoolConfiguration(poolId + 1, [
{
marketId: marketId(),
weightD18: ethers.utils.parseEther('1'),
maxDebtShareValueD18: ethers.utils.parseEther('0.2'),
},
]);
await systems()
.Core.connect(owner)
.setPoolConfiguration(poolId + 2, [
{
marketId: marketId(),
weightD18: ethers.utils.parseEther('1'),
maxDebtShareValueD18: ethers.utils.parseEther('0.3'),
},
]);

// delegate
await systems()
.Core.connect(user1)
.delegateCollateral(
accountId,
poolId + 1,
collateralAddress(),
depositAmount,
ethers.utils.parseEther('1')
);

await systems()
.Core.connect(user1)
.delegateCollateral(
accountId,
poolId + 2,
collateralAddress(),
depositAmount,
ethers.utils.parseEther('1')
);
});

it('inRangePools and outRangePools are returned correctly', async () => {
const result = await systems().Core.callStatic.getMarketPools(marketId());

assert.equal(result.inRangePoolIds.length, 3);
assert.equal(result.outRangePoolIds.length, 0);
});

it('distribute massive debt', async () => {
await MockMarket().connect(owner).setReportedDebt(bn(10000000000000));
});

it('inRangePools and outRangePools are returned correctly', async () => {
const result = await systems().Core.callStatic.getMarketPools(marketId());
assert.equal(result.inRangePoolIds.length, 0);
assert.equal(result.outRangePoolIds.length, 3);
});
});

describe('getMarketPoolDebtDistribution()', () => {
before(restore);

it('getMarketPoolDebtDistribution returns expected result', async () => {
const result = await systems().Core.callStatic.getMarketPoolDebtDistribution(
marketId(),
poolId
);

assertBn.equal(result.sharesD18, bn(1000));
assertBn.equal(result.totalSharesD18, bn(1000));
assertBn.equal(result.valuePerShareD27, bn(0));
});

it('distribute massive debt', async () => {
await MockMarket().connect(owner).setReportedDebt(bn(10000000000000));
});

it('getMarketPoolDebtDistribution returns expected result', async () => {
const result = await systems().Core.callStatic.getMarketPoolDebtDistribution(
marketId(),
poolId
);

assertBn.equal(result.sharesD18, bn(0));
assertBn.equal(result.totalSharesD18, bn(0));
assertBn.equal(result.valuePerShareD27, bn(1000000000));
});
});
});
Loading