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 366 implementation #2145

Open
wants to merge 59 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 38 commits
Commits
Show all changes
59 commits
Select commit Hold shift + click to select a range
2209136
wip sip366
leomassazza May 8, 2024
45dfd41
wip 366
leomassazza May 9, 2024
cebc9c4
wip 366 start fixing tests
leomassazza May 9, 2024
f41d109
fix, wrong params for pair
leomassazza May 9, 2024
e3ab470
wip: add per collateral accounting
leomassazza May 13, 2024
18de031
Test MarketManagerModule
leomassazza May 13, 2024
de561f0
Fix LiquidationModule test
leomassazza May 13, 2024
1f49264
wip: fix tests
leomassazza May 13, 2024
5a28067
checkpoint: fixing vault module test (wip)
leomassazza May 13, 2024
13a5120
checkpoint: use delta for intent declarations
leomassazza May 13, 2024
b2cb01c
checkpoint: added some events
leomassazza May 13, 2024
7280c58
Link to liquidation
leomassazza May 13, 2024
64030e3
checkpoint: more tests fixed and bugs smashed
leomassazza May 14, 2024
3f68d81
Fix tests
leomassazza May 15, 2024
c60b1c4
fix other project tests
leomassazza May 16, 2024
6ae5345
fix another tests on bfp
leomassazza May 16, 2024
8289f11
Only owner can delete all intents
leomassazza May 17, 2024
c6b5332
Make window time flexible with configuration changes
leomassazza May 20, 2024
55cf924
wip: timing tests
leomassazza May 20, 2024
383d27e
test edge cases
leomassazza May 20, 2024
18a4308
Reduce AccountDelegationIntents footprint
leomassazza May 21, 2024
a47d06c
Cleanup and comments
leomassazza May 21, 2024
12db077
update storage.dump
leomassazza May 22, 2024
1027f7f
add skipped test - multiple markets with diff timing
leomassazza May 22, 2024
3a4ac69
deps fix
leomassazza May 22, 2024
ed9d256
undo deps fix
leomassazza May 22, 2024
fc9cf62
Merge branch 'SIP-366-implementation' of https://github.com/Synthetix…
leomassazza May 22, 2024
f0531e2
fix deps
leomassazza May 22, 2024
d03d3ca
typos + remove account owner check on delete expired intents
leomassazza May 22, 2024
0cf5549
PR review fixes (partial)
leomassazza May 27, 2024
6c54a5d
fix: ensure intentId to process is still valid for the account (not r…
leomassazza May 27, 2024
3c9f5be
fix pair using wrong key
leomassazza May 27, 2024
50b6b61
fix tests
leomassazza May 27, 2024
f62dad5
propagate storage change
leomassazza May 27, 2024
901f542
rename function (wip)
leomassazza May 27, 2024
30953ee
add two new views
leomassazza May 28, 2024
05cd81f
some fixes
leomassazza May 30, 2024
2e5ddcd
add views test + small fix
leomassazza Jun 4, 2024
9e836d5
PR Fixes -> SIP 366 (#2171)
leomassazza Jun 26, 2024
cb02ac7
Merge branch 'main' into SIP-366-implementation
leomassazza Jun 26, 2024
4061dec
fix bfp tests
leomassazza Jun 26, 2024
f9ba8da
more fixes on bfp (roll back changes)
leomassazza Jun 26, 2024
cc0bd12
add missing empty line
leomassazza Jun 27, 2024
62de774
add more info to view
leomassazza Jun 27, 2024
9a44955
fix wrong sign + a small refactor
leomassazza Jun 27, 2024
6272a49
fix test
leomassazza Jun 27, 2024
ac3ad55
rename FFs
leomassazza Jul 1, 2024
c11d354
fix tests
leomassazza Jul 1, 2024
c4fa5d3
Merge branch 'main' into SIP-366-implementation
leomassazza Jul 2, 2024
0a2d08e
push storage dump
leomassazza Jul 3, 2024
b190bfc
Merge branch 'main' into SIP-366-implementation
leomassazza Jul 15, 2024
dde2c7d
Merge branch 'main' into SIP-366-implementation
leomassazza Jul 18, 2024
4ff64db
Reduce delegationIntent footprint
leomassazza Jul 19, 2024
cd8a7f9
fix missing references to intent.id
leomassazza Jul 19, 2024
eb05eba
Merge branch 'main' into SIP-366-implementation
leomassazza Jul 27, 2024
1f46654
Merge branch 'main' into SIP-366-implementation
leomassazza Aug 13, 2024
f960f81
fix test
leomassazza Aug 13, 2024
712921c
Merge branch 'main' into SIP-366-implementation
leomassazza Aug 27, 2024
dcb9ea4
Merge branch 'main' into SIP-366-implementation
leomassazza Aug 30, 2024
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
13 changes: 7 additions & 6 deletions markets/bfp-market/storage.dump.sol
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ library Account {
AccountRBAC.Data rbac;
uint64 lastInteraction;
uint64 __slotAvailableForFutureUse;
uint128 __slot2AvailableForFutureUse;
uint128 currentDelegationIntentsEpoch;
mapping(address => Collateral.Data) collaterals;
}
function load(uint128 id) internal pure returns (Data storage account) {
Expand Down Expand Up @@ -228,11 +228,13 @@ library Market {
mapping(uint128 => MarketPoolInfo.Data) pools;
DepositedCollateral[] depositedCollateral;
mapping(address => uint256) maximumDepositableD18;
uint32 minDelegateTime;
uint32 __unusedLegacyStorageSlot;
uint32 undelegateCollateralDelay;
uint32 undelegateCollateralWindow;
uint32 delegateCollateralDelay;
uint32 delegateCollateralWindow;
uint32 __reservedForLater1;
uint64 __reservedForLater2;
uint64 __reservedForLater3;
uint64 __reservedForLater4;
uint256 minLiquidityRatioD18;
}
struct DepositedCollateral {
Expand Down Expand Up @@ -280,7 +282,6 @@ library OracleManager {

// @custom:artifact @synthetixio/main/contracts/storage/Pool.sol:Pool
library Pool {
bytes32 private constant _CONFIG_SET_MARKET_MIN_DELEGATE_MAX = "setMarketMinDelegateTime_max";
struct Data {
uint128 id;
string name;
Expand Down Expand Up @@ -388,7 +389,7 @@ library VaultEpoch {
Distribution.Data accountsDebtDistribution;
ScalableMapping.Data collateralAmounts;
mapping(uint256 => int256) consolidatedDebtAmountsD18;
mapping(uint128 => uint64) lastDelegationTime;
mapping(uint128 => uint64) __unused_legacy_slot;
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,13 +37,15 @@ import {
import { ethers } from 'ethers';
import { calcFillPrice } from '../../calculations';
import { shuffle } from 'lodash';
import { delegateCollateral } from '@synthetixio/main/test/common';

describe('OrderModule', () => {
const bs = bootstrap(genBootstrap());
const {
systems,
restore,
provider,
owner,
keeper,
collateralsWithoutSusd,
markets,
Expand Down Expand Up @@ -1372,7 +1374,7 @@ describe('OrderModule', () => {
});

it('should accurately account for utilization when holding for a long time', async () => {
const { BfpMarketProxy, Core } = systems();
const { BfpMarketProxy } = systems();

const { collateral, collateralDepositAmount, trader, marketId, market } = await depositMargin(
bs,
Expand Down Expand Up @@ -1402,7 +1404,10 @@ describe('OrderModule', () => {
const { stakedAmount, staker, stakerAccountId, collateral: stakedCollateral, id } = pool();
// Unstake 50% of the delegated amount on the core side, this should lead to a increased utilization rate.
const newDelegated = wei(stakedAmount).mul(0.5).toBN();
await Core.connect(staker()).delegateCollateral(
await delegateCollateral(
systems,
owner(),
staker(),
stakerAccountId,
id,
stakedCollateral().address,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import {
withExplicitEvmMine,
} from '../../helpers';
import { calcUtilization, calcUtilizationRate } from '../../calculations';
import { delegateCollateral } from '@synthetixio/main/test/common';

describe('PerpMarketFactoryModule Utilization', () => {
const bs = bootstrap(genBootstrap());
Expand Down Expand Up @@ -152,7 +153,10 @@ describe('PerpMarketFactoryModule Utilization', () => {
stakedCollateral().address
);
const stakedCollateralAddress = stakedCollateral().address;
await Core.connect(staker()).delegateCollateral(
await delegateCollateral(
systems,
owner(),
staker(),
stakerAccountId,
poolId,
stakedCollateralAddress,
Expand Down Expand Up @@ -222,7 +226,10 @@ describe('PerpMarketFactoryModule Utilization', () => {
stakedCollateral().address
);
const stakedCollateralAddress = stakedCollateral().address;
await Core.connect(staker()).delegateCollateral(
await delegateCollateral(
systems,
owner(),
staker(),
stakerAccountId,
poolId,
stakedCollateralAddress,
Expand Down Expand Up @@ -377,7 +384,7 @@ describe('PerpMarketFactoryModule Utilization', () => {
});
describe('getUtilizationDigest', async () => {
it('should return utilization data', async () => {
const { BfpMarketProxy, Core } = systems();
const { BfpMarketProxy } = systems();
const market = genOneOf(markets());

const { marketId, trader, collateral, collateralDepositAmount } = await depositMargin(
Expand Down Expand Up @@ -412,16 +419,15 @@ describe('PerpMarketFactoryModule Utilization', () => {
await fastForwardBySec(provider(), genNumber(1, SECONDS_ONE_DAY));
// Decrease amount of staked collateral on the core side.
const stakedCollateralAddress = stakedCollateral().address;
await withExplicitEvmMine(
() =>
Core.connect(staker()).delegateCollateral(
stakerAccountId,
poolId,
stakedCollateralAddress,
wei(stakedAmount).mul(0.9).toBN(),
bn(1)
),
provider()
await delegateCollateral(
systems,
owner(),
staker(),
stakerAccountId,
poolId,
stakedCollateralAddress,
wei(stakedAmount).mul(0.9).toBN(),
bn(1)
);

const utilizationDigest2 = await BfpMarketProxy.getUtilizationDigest(marketId);
Expand Down
1 change: 1 addition & 0 deletions markets/legacy-market/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
"@synthetixio/common-config": "workspace:*",
"@synthetixio/core-utils": "workspace:*",
"@synthetixio/docgen": "workspace:*",
"@synthetixio/main": "workspace:*",
"@synthetixio/wei": "^2.74.4",
"ethers": "^5.7.2",
"hardhat": "^2.19.5",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { wei } from '@synthetixio/wei';
import { snapshotCheckpoint } from '../utils';
import { fastForward } from '@synthetixio/core-utils/utils/hardhat/rpc';
import assertRevert from '@synthetixio/core-utils/utils/assertions/assert-revert';
import { delegateCollateral } from '@synthetixio/main/test/common';

async function getImpersonatedSigner(
provider: ethers.providers.JsonRpcProvider,
Expand Down Expand Up @@ -274,15 +275,18 @@ describe('LegacyMarket (iosiro)', function () {
await v3System
.connect(whaleAccount)
.deposit(whaleAccountId, collateralType, whaleDelegationAmount);
await v3System
.connect(whaleAccount)
.delegateCollateral(
whaleAccountId,
otherPoolId,
collateralType,
whaleDelegationAmount,
wei(1).toBN()
);
await delegateCollateral(
() => ({
Core: v3System,
}),
owner,
whaleAccount,
whaleAccountId,
otherPoolId,
collateralType,
whaleDelegationAmount,
wei(1).toBN()
);
await v3System
.connect(whaleAccount)
.mintUsd(whaleAccountId, otherPoolId, collateralType, wei(3333).toBN());
Expand Down Expand Up @@ -325,9 +329,18 @@ describe('LegacyMarket (iosiro)', function () {
const amountToWithdraw = accountCollateralDetails.totalDeposited;

await fastForward(605000, cannonProvider);
await v3System
.connect(attacker)
.delegateCollateral(accountID, poolId, collateralType, 0, wei(1).toBN());
await delegateCollateral(
() => ({
Core: v3System,
}),
owner,
attacker,
accountID,
poolId,
collateralType,
wei(0).toBN(),
wei(1).toBN()
);
await v3System.connect(attacker).withdraw(accountID, collateralType, amountToWithdraw);
// attacker restakes in v2
await snxV2.connect(attacker).issueMaxSynths();
Expand Down
21 changes: 12 additions & 9 deletions markets/legacy-market/test/integration/LegacyMarket.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import { LegacyMarket } from '../../typechain-types/contracts/LegacyMarket';
import Wei, { wei } from '@synthetixio/wei';
import assertEvent from '@synthetixio/core-utils/utils/assertions/assert-event';
import { snapshotCheckpoint } from '../utils';
import { delegateCollateral } from '@synthetixio/main/test/common';

async function getImpersonatedSigner(
provider: ethers.providers.JsonRpcProvider,
Expand Down Expand Up @@ -347,15 +348,17 @@ describe('LegacyMarket', function () {
await v3System.connect(owner).deposit(accountId, snxToken.address, delegateAmount);

// invest in the pool
await v3System
.connect(owner)
.delegateCollateral(
accountId,
await v3System.getPreferredPool(),
snxToken.address,
delegateAmount,
ethers.utils.parseEther('1')
);
await delegateCollateral(
() => ({
Core: v3System,
}),
owner,
accountId,
await v3System.getPreferredPool(),
snxToken.address,
delegateAmount,
ethers.utils.parseEther('1')
);

// sanity
assertBn.equal(
Expand Down
13 changes: 7 additions & 6 deletions markets/perps-market/storage.dump.sol
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ library Account {
AccountRBAC.Data rbac;
uint64 lastInteraction;
uint64 __slotAvailableForFutureUse;
uint128 __slot2AvailableForFutureUse;
uint128 currentDelegationIntentsEpoch;
mapping(address => Collateral.Data) collaterals;
}
function load(uint128 id) internal pure returns (Data storage account) {
Expand Down Expand Up @@ -227,11 +227,13 @@ library Market {
mapping(uint128 => MarketPoolInfo.Data) pools;
DepositedCollateral[] depositedCollateral;
mapping(address => uint256) maximumDepositableD18;
uint32 minDelegateTime;
uint32 __unusedLegacyStorageSlot;
uint32 undelegateCollateralDelay;
uint32 undelegateCollateralWindow;
uint32 delegateCollateralDelay;
uint32 delegateCollateralWindow;
uint32 __reservedForLater1;
uint64 __reservedForLater2;
uint64 __reservedForLater3;
uint64 __reservedForLater4;
uint256 minLiquidityRatioD18;
}
struct DepositedCollateral {
Expand Down Expand Up @@ -279,7 +281,6 @@ library OracleManager {

// @custom:artifact @synthetixio/main/contracts/storage/Pool.sol:Pool
library Pool {
bytes32 private constant _CONFIG_SET_MARKET_MIN_DELEGATE_MAX = "setMarketMinDelegateTime_max";
struct Data {
uint128 id;
string name;
Expand Down Expand Up @@ -387,7 +388,7 @@ library VaultEpoch {
Distribution.Data accountsDebtDistribution;
ScalableMapping.Data collateralAmounts;
mapping(uint256 => int256) consolidatedDebtAmountsD18;
mapping(uint128 => uint64) lastDelegationTime;
mapping(uint128 => uint64) __unused_legacy_slot;
}
}

Expand Down
22 changes: 12 additions & 10 deletions markets/perps-market/test/integration/Insolvent.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import Wei, { wei } from '@synthetixio/wei';
import { ethers } from 'ethers';
import { fastForwardTo, getTime } from '@synthetixio/core-utils/utils/hardhat/rpc';
import assertBn from '@synthetixio/core-utils/utils/assertions/assert-bignumber';
import { delegateCollateral } from '@synthetixio/main/test/common';

const _SECONDS_IN_DAY = 24 * 60 * 60;

Expand All @@ -18,7 +19,7 @@ const interestRateParams = {
};

describe('Position - interest rates', () => {
const { systems, perpsMarkets, superMarketId, provider, trader1, keeper, staker } =
const { systems, perpsMarkets, superMarketId, provider, owner, trader1, keeper, staker } =
bootstrapMarkets({
interestRateParams: {
lowUtilGradient: interestRateParams.lowUtilGradient.toBN(),
Expand Down Expand Up @@ -83,15 +84,16 @@ describe('Position - interest rates', () => {
systems().CollateralMock.address
);
// very low amount to make market insolvent
await systems()
.Core.connect(staker())
.delegateCollateral(
1,
1,
systems().CollateralMock.address,
wei(currentCollateralAmount).mul(wei(0.1)).toBN(),
ethers.utils.parseEther('1')
);
await delegateCollateral(
systems,
owner(),
staker(),
1,
1,
systems().CollateralMock.address,
wei(currentCollateralAmount).mul(wei(0.1)).toBN(),
ethers.utils.parseEther('1')
);
});

// trader 1
Expand Down
Loading