Skip to content

Add test to cover Margin Only liquidations #2053

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

Open
wants to merge 14 commits into
base: main
Choose a base branch
from
Open
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
7 changes: 7 additions & 0 deletions auxiliary/PerpsRewardDistributor/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
cannon
contracts/generated
contracts/modules/test
contracts/Router.sol
deployments/hardhat
deployments/local
typechain-types
21 changes: 21 additions & 0 deletions auxiliary/PerpsRewardDistributor/LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2021 Synthetix

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
2 changes: 2 additions & 0 deletions auxiliary/PerpsRewardDistributor/cannonfile.test.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
version = "<%= package.version %>-testable"
include = ["cannonfile.toml"]
7 changes: 7 additions & 0 deletions auxiliary/PerpsRewardDistributor/cannonfile.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
name = "perps-reward-distributor"
version = "<%= package.version %>"
description = "Perps Reward Distributor implementation"

[contract.PerpsRewardDistributor]
artifact = "contracts/PerpsRewardDistributor.sol:PerpsRewardDistributor"
create2 = true
136 changes: 136 additions & 0 deletions auxiliary/PerpsRewardDistributor/contracts/PerpsRewardDistributor.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,136 @@
// SPDX-License-Identifier: MIT
pragma solidity >=0.8.11 <0.9.0;

import {IPerpRewardDistributor} from "./interfaces/IPerpsRewardDistributor.sol";
import {IRewardDistributor} from "@synthetixio/main/contracts/interfaces/external/IRewardDistributor.sol";
import {IRewardsManagerModule} from "@synthetixio/main/contracts/interfaces/IRewardsManagerModule.sol";
import {IERC165} from "@synthetixio/core-contracts/contracts/interfaces/IERC165.sol";
import {IERC20} from "@synthetixio/core-contracts/contracts/interfaces/IERC20.sol";

contract PerpsRewardDistributor is IPerpRewardDistributor {
string private constant _version = "1.0.0";

bool private _initialized;

address private _rewardManager; // synthetix
address private _token;
string private _name;

uint128 private _poolId;
address private _perpMarket;
bool public shouldFailPayout;

error AlreadyInitialized();
error OnlyPerpMarket();
error OnlyRewardManager();

constructor() {
// Should be initialized by the factory. This prevents usage of the instance without proper initialization
_initialized = true;
}

/**
* Initialize the contract
* @notice This function should be called only once at clone node creation.
* @notice it can be called only once by anyone (not checking sender)
* @notice but will be created by the factory and initialized immediatly after creation, and consumed only if initialization succeeds
* @param rewardManager address of the reward manager (Synthetix core proxy)
* @param perpMarket address of the perp market
* @param poolId poolId of the pool
* @param token_ token address of the collateral
* @param name_ rewards distribution name
*/
function initialize(
address rewardManager,
address perpMarket,
uint128 poolId,
address token_,
string memory name_
) external {
if (_initialized) {
revert AlreadyInitialized();
}
_rewardManager = rewardManager;
_perpMarket = perpMarket;
_poolId = poolId;
_token = token_;
_name = name_;
}

function distributeRewards(address collateralType, uint256 amount) external {
onlyPerpMarket();
IRewardsManagerModule(_rewardManager).distributeRewards(
_poolId,
collateralType,
amount,
uint64(block.timestamp), // solhint-disable-line numcast/safe-cast
0
);
}

function setShouldFailPayout(bool _shouldFailedPayout) external {
onlyPerpMarket(); // perp market is in fact the owner
shouldFailPayout = _shouldFailedPayout;
}

function payout(
uint128 /* accountId */,
uint128 /* poolId */,
address /* collateralType */,
address sender,
uint256 amount
) external override returns (bool) {
onlyRewardManager();
IERC20(_token).transfer(sender, amount);
return !shouldFailPayout;
}

function onPositionUpdated(
uint128 /* accountId */,
uint128 /* poolId */,
address /* collateralType */,
uint256 /* newShares */
) external pure override {}

function token() external view override returns (address) {
return _token;
}

function name() external view override returns (string memory) {
return _name;
}

function getPoolId() external view override returns (uint128) {
return _poolId;
}

function version() external pure virtual override returns (string memory) {
return _version;
}

/**
* @inheritdoc IERC165
*/
function supportsInterface(
bytes4 interfaceId
) public view virtual override(IERC165) returns (bool) {
return
interfaceId == type(IRewardDistributor).interfaceId ||
interfaceId == type(IPerpRewardDistributor).interfaceId ||
interfaceId == this.supportsInterface.selector;
}

function onlyPerpMarket() internal view {
// solhint-disable-next-line meta-transactions/no-msg-sender
if (msg.sender != _perpMarket) {
revert OnlyPerpMarket();
}
}

function onlyRewardManager() internal view {
// solhint-disable-next-line meta-transactions/no-msg-sender
if (msg.sender != _rewardManager) {
revert OnlyRewardManager();
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
// SPDX-License-Identifier: MIT
pragma solidity >=0.8.11 <0.9.0;

import {IRewardDistributor} from "@synthetixio/main/contracts/interfaces/external/IRewardDistributor.sol";

interface IPerpRewardDistributor is IRewardDistributor {
/**
* @notice Returns the version of the PerpRewardDistributor.
* @return The Semver contract version as a string.
*/
function version() external view returns (string memory);

/**
* @notice Returns the id of the pool this was registered with.
*/
function getPoolId() external view returns (uint128);

/**
* @notice Initializes the PerpRewardDistributor with references, name, token to distribute etc.
*/
function initialize(
address rewardManager,
address perpMarket,
uint128 poolId_,
address token_,
string memory name_
) external;

/**
* @notice Set true to disable `payout` to revert on claim or false to allow.
*/
function setShouldFailPayout(bool _shouldFailedPayout) external;

/**
* @notice Creates a new distribution entry for LPs of `collateralType` to `amount` of tokens.
*/
function distributeRewards(address collateralType, uint256 amount) external;
}
23 changes: 23 additions & 0 deletions auxiliary/PerpsRewardDistributor/hardhat.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import commonConfig from '@synthetixio/common-config/hardhat.config';

import 'solidity-docgen';
import { templates } from '@synthetixio/docgen';

const config = {
...commonConfig,
docgen: {
exclude: [
'./interfaces/external',
'./modules',
'./mixins',
'./mocks',
'./utils',
'./storage',
'./Proxy.sol',
'./Router.sol',
],
templates,
},
};

export default config;
34 changes: 34 additions & 0 deletions auxiliary/PerpsRewardDistributor/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
{
"name": "@synthetixio/perps-reward-distributor",
"version": "3.3.17",
"description": "Perps Reward Distributor implementation",
"publishConfig": {
"access": "public"
},
"scripts": {
"test": "CANNON_REGISTRY_PRIORITY=local hardhat test",
"coverage": "hardhat coverage --network hardhat",
"clean": "hardhat clean",
"build": "yarn build:contracts",
"build:contracts": "hardhat cannon:build",
"build-testable": "CANNON_REGISTRY_PRIORITY=local hardhat cannon:build cannonfile.test.toml",
"compile-contracts": "hardhat compile",
"size-contracts": "hardhat compile && hardhat size-contracts",
"publish-contracts": "cannon publish perps-reward-distributor:$(node -p 'require(`./package.json`).version') --chain-id 13370 --quiet --tags $(node -p '/^\\d+\\.\\d+\\.\\d+$/.test(require(`./package.json`).version) ? `latest` : `dev`')",
"postpack": "yarn build && yarn publish-contracts",
"docgen": "hardhat docgen"
},
"keywords": [],
"author": "",
"license": "MIT",
"devDependencies": {
"@synthetixio/common-config": "workspace:*",
"@synthetixio/core-contracts": "workspace:*",
"@synthetixio/core-modules": "workspace:*",
"@synthetixio/docgen": "workspace:*",
"hardhat": "^2.19.5",
"solidity-docgen": "^0.6.0-beta.36",
"ts-node": "^10.9.2",
"typescript": "^5.3.3"
}
}
6 changes: 6 additions & 0 deletions auxiliary/PerpsRewardDistributor/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"extends": "../../utils/common-config/tsconfig.json",
"compilerOptions": {
"noEmit": true
}
}
7 changes: 7 additions & 0 deletions markets/perps-market/cannonfile.test.toml
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,9 @@ artifact = "PerpsMarketModule"
[contract.LiquidationModule]
artifact = "LiquidationModule"

[contract.CollateralConfigurationModule]
artifact = "CollateralConfigurationModule"

[contract.MarketConfigurationModule]
artifact = "MarketConfigurationModule"

Expand Down Expand Up @@ -76,6 +79,7 @@ contracts = [
"FeatureFlagModule",
"LiquidationModule",
"MarketConfigurationModule",
"CollateralConfigurationModule",
"GlobalPerpsMarketModule",
]

Expand Down Expand Up @@ -138,3 +142,6 @@ artifact = "contracts/mocks/MockPythERC7412Wrapper.sol:MockPythERC7412Wrapper"

[contract.FeeCollectorMock]
artifact = "contracts/mocks/FeeCollectorMock.sol:FeeCollectorMock"

[contract.MockPerpsRewardDistributor]
artifact = "contracts/mocks/MockPerpsRewardDistributor.sol:MockPerpsRewardDistributor"
4 changes: 4 additions & 0 deletions markets/perps-market/cannonfile.toml
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,9 @@ artifact = "PerpsMarketModule"
[contract.LiquidationModule]
artifact = "LiquidationModule"

[contract.CollateralConfigurationModule]
artifact = "CollateralConfigurationModule"

[contract.MarketConfigurationModule]
artifact = "MarketConfigurationModule"

Expand Down Expand Up @@ -87,6 +90,7 @@ contracts = [
"FeatureFlagModule",
"LiquidationModule",
"MarketConfigurationModule",
"CollateralConfigurationModule",
"GlobalPerpsMarketModule",
]

Expand Down
Loading