Skip to content

Commit

Permalink
[Fix] Send claimed market fees to factory owner (#79)
Browse files Browse the repository at this point in the history
* [Fix] Send claimed market fees to factory owner

* claim directly from owner

* remove fund test
  • Loading branch information
arjun-io authored Aug 28, 2023
1 parent a996d43 commit 663fab9
Show file tree
Hide file tree
Showing 5 changed files with 5 additions and 45 deletions.
4 changes: 2 additions & 2 deletions packages/perennial/contracts/Market.sol
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ contract Market is IMarket, Instance, ReentrancyGuard {
function claimFee() external {
Global memory newGlobal = _global.read();

if (_claimFee(address(factory()), newGlobal.protocolFee)) newGlobal.protocolFee = UFixed6Lib.ZERO;
if (_claimFee(factory().owner(), newGlobal.protocolFee)) newGlobal.protocolFee = UFixed6Lib.ZERO;
if (_claimFee(address(IMarketFactory(address(factory())).oracleFactory()), newGlobal.oracleFee))
newGlobal.oracleFee = UFixed6Lib.ZERO;
if (_claimFee(coordinator, newGlobal.riskFee)) newGlobal.riskFee = UFixed6Lib.ZERO;
Expand All @@ -139,7 +139,7 @@ contract Market is IMarket, Instance, ReentrancyGuard {
_global.store(newGlobal);
}

/// @notice Helper function to handle a singular fee claim
/// @notice Helper function to handle a singular fee claim.
/// @param receiver The address to receive the fee
/// @param fee The amount of the fee to claim
function _claimFee(address receiver, UFixed6 fee) private returns (bool) {
Expand Down
7 changes: 0 additions & 7 deletions packages/perennial/contracts/MarketFactory.sol
Original file line number Diff line number Diff line change
Expand Up @@ -81,11 +81,4 @@ contract MarketFactory is IMarketFactory, Factory {

emit MarketCreated(newMarket, definition);
}

/// @notice Claims the protocol's fee from the given market
/// @param market The market to claim from
function fund(IMarket market) external {
if (!instances(IInstance(address(market)))) revert FactoryNotInstanceError();
market.claimFee();
}
}
1 change: 0 additions & 1 deletion packages/perennial/contracts/interfaces/IMarketFactory.sol
Original file line number Diff line number Diff line change
Expand Up @@ -25,5 +25,4 @@ interface IMarketFactory is IFactory {
function updateParameter(ProtocolParameter memory newParameter) external;
function updateOperator(address operator, bool newEnabled) external;
function create(IMarket.MarketDefinition calldata definition) external returns (IMarket);
function fund(IMarket market) external;
}
6 changes: 3 additions & 3 deletions packages/perennial/test/unit/market/Market.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13054,11 +13054,11 @@ describe('Market', () => {
})

it('claims fee (protocol)', async () => {
dsu.transfer.whenCalledWith(factory.address, PROTOCOL_FEE.mul(1e12)).returns(true)
dsu.transfer.whenCalledWith(owner.address, PROTOCOL_FEE.mul(1e12)).returns(true)

await expect(market.connect(factorySigner).claimFee())
await expect(market.connect(owner).claimFee())
.to.emit(market, 'FeeClaimed')
.withArgs(factory.address, PROTOCOL_FEE)
.withArgs(owner.address, PROTOCOL_FEE)

expect((await market.global()).protocolFee).to.equal(0)
expect((await market.global()).oracleFee).to.equal(ORACLE_FEE)
Expand Down
32 changes: 0 additions & 32 deletions packages/perennial/test/unit/marketfactory/MarketFactory.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -227,36 +227,4 @@ describe('MarketFactory', () => {
expect(await factory.operators(user.address, owner.address)).to.equal(false)
})
})

describe('#fund', async () => {
let marketAddress: string
let fakeMarket: FakeContract<IMarket>

beforeEach(async () => {
const marketDefinition = {
token: dsu.address,
oracle: oracle.address,
payoff: constants.AddressZero,
}

oracleFactory.instances.whenCalledWith(oracle.address).returns(true)

marketAddress = await factory.callStatic.create(marketDefinition)
await factory.connect(owner).create(marketDefinition)
fakeMarket = await smock.fake<IMarket>('IMarket', { address: marketAddress })
})

it('claims its fees', async () => {
await factory.connect(user).fund(marketAddress)

expect(fakeMarket.claimFee).to.have.been.called
})

it('reverts if not an instance', async () => {
await expect(factory.connect(user).fund(user.address)).to.be.revertedWithCustomError(
factory,
'FactoryNotInstanceError',
)
})
})
})

0 comments on commit 663fab9

Please sign in to comment.