Skip to content

Commit

Permalink
Merge branch 'main' into zero-shares
Browse files Browse the repository at this point in the history
  • Loading branch information
noisekit authored Jun 24, 2024
2 parents 5bea45e + 9300b52 commit 8a753ca
Show file tree
Hide file tree
Showing 55 changed files with 742 additions and 323 deletions.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
2 changes: 1 addition & 1 deletion auxiliary/ERC4626ToAssetsRatioOracle/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
"@synthetixio/docgen": "workspace:*",
"@synthetixio/main": "workspace:*",
"@synthetixio/wei": "^2.74.4",
"@usecannon/cli": "2.13.6",
"@usecannon/cli": "2.15.2",
"ethers": "^5.7.2",
"hardhat": "^2.19.5",
"solidity-docgen": "^0.6.0-beta.36"
Expand Down
2 changes: 1 addition & 1 deletion auxiliary/RewardsDistributor/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
"forge-coverage": "forge coverage --report lcov"
},
"devDependencies": {
"@usecannon/cli": "2.13.6",
"@usecannon/cli": "2.15.2",
"forge-std": "github:foundry-rs/forge-std#master"
},
"depcheck": {
Expand Down
2 changes: 1 addition & 1 deletion auxiliary/RewardsDistributorExternal/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
"forge-coverage": "forge coverage --report lcov"
},
"devDependencies": {
"@usecannon/cli": "2.13.6",
"@usecannon/cli": "2.15.2",
"forge-std": "github:foundry-rs/forge-std#master"
},
"depcheck": {
Expand Down
2 changes: 1 addition & 1 deletion auxiliary/WstEthToStEthRatioOracle/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
"@synthetixio/docgen": "workspace:*",
"@synthetixio/main": "workspace:*",
"@synthetixio/wei": "^2.74.4",
"@usecannon/cli": "2.13.6",
"@usecannon/cli": "2.15.2",
"ethers": "^5.7.2",
"hardhat": "^2.19.5",
"solidity-docgen": "^0.6.0-beta.36"
Expand Down
5 changes: 5 additions & 0 deletions markets/bfp-market/cannonfile.test.toml
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,10 @@ args = []
artifact = "contracts/modules/SettlementHookModule.sol:SettlementHookModule"
args = []

[contract.SplitAccountConfigurationModule]
artifact = "contracts/modules/SplitAccountConfigurationModule.sol:SplitAccountConfigurationModule"
args = []

# --- Distributor contract --- #

[contract.PerpRewardDistributor]
Expand Down Expand Up @@ -104,6 +108,7 @@ contracts = [
"LiquidationModule",
"PerpRewardDistributorFactoryModule",
"SettlementHookModule",
"SplitAccountConfigurationModule",
"__TestHelperModule",
]

Expand Down
5 changes: 5 additions & 0 deletions markets/bfp-market/cannonfile.toml
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,10 @@ args = []
artifact = "contracts/modules/SettlementHookModule.sol:SettlementHookModule"
args = []

[contract.SplitAccountConfigurationModule]
artifact = "contracts/modules/SplitAccountConfigurationModule.sol:SplitAccountConfigurationModule"
args = []

# --- Distributor contract --- #

[contract.PerpRewardDistributor]
Expand Down Expand Up @@ -94,6 +98,7 @@ contracts = [
"LiquidationModule",
"PerpRewardDistributorFactoryModule",
"SettlementHookModule",
"SplitAccountConfigurationModule",
]

# --- Proxy post deployment --- #
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
//SPDX-License-Identifier: MIT
pragma solidity >=0.8.11 <0.9.0;

import {SettlementHookConfiguration} from "../storage/SettlementHookConfiguration.sol";

interface ISplitAccountConfigurationModule {
// --- Events --- //

/// @notice Emitted when split account whitelist is configured.
/// @param from Address of configurer
/// @param hooks Number of addresses configured
event SplitAccountConfigured(address indexed from, uint256 hooks);

// --- Mutations --- //

/// @notice Configures a list of addresses to be whitelisted for splitAccount.
/// @param addresses An array of addresses to whitelist
function setEndorsedSplitAccounts(address[] memory addresses) external;

// --- Views --- //

/// @notice Returns addresses allowed to split account.
/// @return addresses list of addresses
function getEndorsedSplitAccounts() external view returns (address[] memory addresses);

/// @notice Returns whether the specified hook is whitelisted.
/// @param addr Address of hook to assert
/// @return isEndoresedForSplitAccount True if whitelisted, false otherwise
function isEndorsedForSplitAccount(address addr) external view returns (bool);
}
4 changes: 4 additions & 0 deletions markets/bfp-market/contracts/modules/PerpAccountModule.sol
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import {Margin} from "../storage/Margin.sol";
import {AddressRegistry} from "../storage/AddressRegistry.sol";
import {PerpMarketConfiguration} from "../storage/PerpMarketConfiguration.sol";
import {SettlementHookConfiguration} from "../storage/SettlementHookConfiguration.sol";
import {SplitAccountConfiguration} from "../storage/SplitAccountConfiguration.sol";

/* solhint-disable meta-transactions/no-msg-sender */

Expand Down Expand Up @@ -229,6 +230,9 @@ contract PerpAccountModule is IPerpAccountModule {
Position.Data storage toPosition = market.positions[toId];
Position.Data storage fromPosition = market.positions[fromId];

if (SplitAccountConfiguration.load().whitelisted[msg.sender] != true) {
revert ErrorUtil.Unauthorized(msg.sender);
}
// Cannot split more than what's available.
if (proportion > DecimalMath.UNIT) {
revert ErrorUtil.AccountSplitProportionTooLarge();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
//SPDX-License-Identifier: MIT
pragma solidity >=0.8.11 <0.9.0;

import {IERC165} from "@synthetixio/core-contracts/contracts/interfaces/IERC165.sol";
import {OwnableStorage} from "@synthetixio/core-contracts/contracts/ownership/OwnableStorage.sol";
import {ERC165Helper} from "@synthetixio/core-contracts/contracts/utils/ERC165Helper.sol";
import {ERC2771Context} from "@synthetixio/core-contracts/contracts/utils/ERC2771Context.sol";
import {ISplitAccountConfigurationModule} from "../interfaces/ISplitAccountConfigurationModule.sol";
import {SplitAccountConfiguration} from "../storage/SplitAccountConfiguration.sol";
import {ErrorUtil} from "../utils/ErrorUtil.sol";

contract SplitAccountConfigurationModule is ISplitAccountConfigurationModule {
// --- Mutations --- //

/**
* @inheritdoc ISplitAccountConfigurationModule
*/
function setEndorsedSplitAccounts(address[] memory addresses) external {
OwnableStorage.onlyOwner();

SplitAccountConfiguration.GlobalData storage config = SplitAccountConfiguration.load();
uint256 existingAddresses = config.whitelistedAddresses.length;
for (uint256 i = 0; i < existingAddresses; ) {
delete config.whitelisted[config.whitelistedAddresses[i]];
unchecked {
++i;
}
}
delete config.whitelistedAddresses;

uint256 addressesLength = addresses.length;
address currentAddress;
for (uint256 i = 0; i < addressesLength; ) {
currentAddress = addresses[i];
if (currentAddress == address(0)) {
revert ErrorUtil.ZeroAddress();
}

config.whitelisted[currentAddress] = true;
unchecked {
++i;
}
}
config.whitelistedAddresses = addresses;

emit SplitAccountConfigured(ERC2771Context._msgSender(), addressesLength);
}

// --- Views --- //

/**
* @inheritdoc ISplitAccountConfigurationModule
*/
function getEndorsedSplitAccounts() external view returns (address[] memory addresses) {
SplitAccountConfiguration.GlobalData storage config = SplitAccountConfiguration.load();
return config.whitelistedAddresses;
}

/**
* @inheritdoc ISplitAccountConfigurationModule
*/
function isEndorsedForSplitAccount(address addr) external view returns (bool) {
SplitAccountConfiguration.GlobalData storage config = SplitAccountConfiguration.load();
return config.whitelisted[addr];
}
}
5 changes: 4 additions & 1 deletion markets/bfp-market/contracts/storage/Position.sol
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,10 @@ library Position {
uint256 minimumCredit = market.getMinimumCredit(marketConfig, oraclePrice, addresses);
int256 delegatedCollateralValueUsd = market.getDelegatedCollateralValueUsd(addresses);

if (delegatedCollateralValueUsd < minimumCredit.toInt()) {
if (
delegatedCollateralValueUsd + market.depositedCollateral[addresses.sUsd].toInt() <
minimumCredit.toInt()
) {
revert ErrorUtil.InsufficientLiquidity();
}
}
Expand Down
25 changes: 25 additions & 0 deletions markets/bfp-market/contracts/storage/SplitAccountConfiguration.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
//SPDX-License-Identifier: MIT
pragma solidity >=0.8.11 <0.9.0;

library SplitAccountConfiguration {
// --- Constants --- //

bytes32 private constant GLOBAL_DATA_SLOT_NAME =
keccak256(abi.encode("io.synthetix.bfp-market.SplitAccountConfiguration"));

// --- Storage --- //

struct GlobalData {
/// {address => isEnabled}.
mapping(address => bool) whitelisted;
/// Array of whitelisted addresses (use whitelisted mapping).
address[] whitelistedAddresses;
}

function load() internal pure returns (SplitAccountConfiguration.GlobalData storage d) {
bytes32 s = GLOBAL_DATA_SLOT_NAME;
assembly {
d.slot := s
}
}
}
3 changes: 3 additions & 0 deletions markets/bfp-market/contracts/utils/ErrorUtil.sol
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,9 @@ library ErrorUtil {
/// @notice Thrown when an invalid Core as passed was specified.
error InvalidCoreAddress(address synthetixCore);

/// @notice Thrown when an account is not authorized to perform an action.
error Unauthorized(address caller);

/// @notice Thrown when a specified hook is not whitelisted, or does not match spec, or otherwise.
error InvalidHook(address hook);

Expand Down
2 changes: 1 addition & 1 deletion markets/bfp-market/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
"@synthetixio/wei": "^2.74.4",
"@types/lodash": "^4.14.199",
"@types/mocha-each": "2.0.1",
"@usecannon/cli": "2.13.6",
"@usecannon/cli": "2.15.2",
"ethers": "^5.7.2",
"hardhat": "^2.19.5",
"lodash": "^4.17.21",
Expand Down
15 changes: 15 additions & 0 deletions markets/bfp-market/storage.dump.sol
Original file line number Diff line number Diff line change
Expand Up @@ -950,6 +950,21 @@ library SettlementHookConfiguration {
}
}

// @custom:artifact contracts/storage/SplitAccountConfiguration.sol:SplitAccountConfiguration
library SplitAccountConfiguration {
bytes32 private constant GLOBAL_DATA_SLOT_NAME = keccak256(abi.encode("io.synthetix.bfp-market.SplitAccountConfiguration"));
struct GlobalData {
mapping(address => bool) whitelisted;
address[] whitelistedAddresses;
}
function load() internal pure returns (SplitAccountConfiguration.GlobalData storage d) {
bytes32 s = GLOBAL_DATA_SLOT_NAME;
assembly {
d.slot := s
}
}
}

// @custom:artifact contracts/utils/Flags.sol:Flags
library Flags {
bytes32 public constant CREATE_ACCOUNT = "createAccount";
Expand Down
Loading

0 comments on commit 8a753ca

Please sign in to comment.