-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #125 from primitivefinance/beta/prep
Beta/prep
- Loading branch information
Showing
39 changed files
with
934 additions
and
179 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
// SPDX-License-Identifier: GPL-3.0-only | ||
pragma solidity ^0.8.0; | ||
|
||
import "./setup/InvariantTargetContract.sol"; | ||
|
||
contract InvariantAllocate is InvariantTargetContract { | ||
constructor(address hyper_, address asset_, address quote_) InvariantTargetContract(hyper_, asset_, quote_) {} | ||
|
||
event SentTokens(uint amount); | ||
|
||
function send_erc20(uint amount) external { | ||
__asset__.mint(address(__hyper__), amount); | ||
emit SentTokens(amount); | ||
} | ||
|
||
function allocate_unallocate(uint deltaLiquidity) external { | ||
vm.assume(deltaLiquidity > 0); | ||
vm.assume(deltaLiquidity < 2 ** 127); | ||
// TODO: Add use max flag support. | ||
|
||
// Preconditions | ||
HyperPool memory pool = getPool(address(__hyper__), __poolId__); | ||
assertTrue(pool.blockTimestamp != 0, "Pool not initialized"); | ||
assertTrue(pool.lastPrice != 0, "Pool not created with a price"); | ||
|
||
(uint expectedDeltaAsset, uint expectedDeltaQuote) = __hyper__.getReserveDelta(__poolId__, deltaLiquidity); | ||
__asset__.mint(address(this), expectedDeltaAsset); | ||
__quote__.mint(address(this), expectedDeltaQuote); | ||
|
||
// Execution | ||
HyperState memory prev = getState(); | ||
(uint deltaAsset, uint deltaQuote) = __hyper__.allocate(__poolId__, deltaLiquidity); | ||
HyperState memory post = getState(); | ||
|
||
// Postconditions | ||
{ | ||
assertEq(deltaAsset, expectedDeltaAsset, "pool-delta-asset"); | ||
assertEq(deltaQuote, expectedDeltaQuote, "pool-delta-quote"); | ||
assertEq(post.totalPoolLiquidity, prev.totalPoolLiquidity + deltaLiquidity, "pool-total-liquidity"); | ||
assertTrue(post.totalPoolLiquidity > prev.totalPoolLiquidity, "pool-liquidity-increases"); | ||
assertEq( | ||
post.callerPositionLiquidity, | ||
prev.callerPositionLiquidity + deltaLiquidity, | ||
"position-liquidity-increases" | ||
); | ||
|
||
assertEq(post.reserveAsset, prev.reserveAsset + expectedDeltaAsset, "reserve-asset"); | ||
assertEq(post.reserveQuote, prev.reserveQuote + expectedDeltaQuote, "reserve-quote"); | ||
assertEq(post.physicalBalanceAsset, prev.physicalBalanceAsset + expectedDeltaAsset, "physical-asset"); | ||
assertEq(post.physicalBalanceQuote, prev.physicalBalanceQuote + expectedDeltaQuote, "physical-quote"); | ||
|
||
uint feeDelta0 = post.feeGrowthAssetPosition - prev.feeGrowthAssetPosition; | ||
uint feeDelta1 = post.feeGrowthAssetPool - prev.feeGrowthAssetPool; | ||
assertTrue(feeDelta0 == feeDelta1, "asset-growth"); | ||
|
||
uint feeDelta2 = post.feeGrowthQuotePosition - prev.feeGrowthQuotePosition; | ||
uint feeDelta3 = post.feeGrowthQuotePool - prev.feeGrowthQuotePool; | ||
assertTrue(feeDelta2 == feeDelta3, "quote-growth"); | ||
} | ||
|
||
// Unallocate | ||
uint timestamp = block.timestamp + __hyper__.JUST_IN_TIME_LIQUIDITY_POLICY(); | ||
vm.warp(timestamp); | ||
__hyper__.setTimestamp(uint128(timestamp)); | ||
(uint unallocatedAsset, uint unallocatedQuote) = __hyper__.unallocate(__poolId__, deltaLiquidity); | ||
|
||
{ | ||
HyperState memory end = getState(); | ||
assertEq(unallocatedAsset, deltaAsset); | ||
assertEq(unallocatedQuote, deltaQuote); | ||
assertEq(end.reserveAsset, prev.reserveAsset); | ||
assertEq(end.reserveQuote, prev.reserveQuote); | ||
assertEq(end.totalPoolLiquidity, prev.totalPoolLiquidity); | ||
assertEq(end.totalPositionLiquidity, prev.totalPositionLiquidity); | ||
assertEq(end.callerPositionLiquidity, prev.callerPositionLiquidity); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
// SPDX-License-Identifier: GPL-3.0-only | ||
pragma solidity ^0.8.0; | ||
|
||
import "./setup/InvariantTargetContract.sol"; | ||
|
||
bytes32 constant SLOT_LOCKED = bytes32(uint(5)); | ||
|
||
contract InvariantGlobal is InvariantTargetContract { | ||
constructor(address hyper_, address asset_, address quote_) InvariantTargetContract(hyper_, asset_, quote_) {} | ||
|
||
function invariant_global() public { | ||
bytes32 locked = vm.load(address(__hyper__), SLOT_LOCKED); | ||
assertEq(uint(locked), 1, "invariant-locked"); | ||
|
||
(bool prepared, bool settled) = __hyper__.__account__(); | ||
assertTrue(!prepared, "invariant-prepared"); | ||
assertTrue(settled, "invariant-settled"); | ||
|
||
uint balance = address(__hyper__).balance; | ||
assertEq(balance, 0, "invariant-ether"); | ||
|
||
(uint reserve, uint physical, uint balances) = getBalances(address(__asset__)); | ||
assertTrue(physical >= reserve + balances, "invariant-asset-physical-balance"); | ||
|
||
(reserve, physical, balances) = getBalances(address(__quote__)); | ||
assertTrue(physical >= reserve + balances, "invariant-quite-physical-balance"); | ||
} | ||
|
||
function getBalances(address token) internal view returns (uint reserve, uint physical, uint balances) { | ||
reserve = getReserve(address(__hyper__), token); | ||
physical = getPhysicalBalance(address(__hyper__), token); | ||
balances = getBalanceSum(address(__hyper__), token, ctx.users()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
// SPDX-License-Identifier: GPL-3.0-only | ||
pragma solidity ^0.8.0; | ||
|
||
import "./setup/TestE2ESetup.sol"; | ||
import "./setup/TestInvariantSetup.sol"; | ||
|
||
import {InvariantAllocate} from "./InvariantAllocate.sol"; | ||
|
||
contract TestE2EInvariant is TestInvariantSetup, TestE2ESetup { | ||
InvariantAllocate internal _allocate; | ||
|
||
function setUp() public override { | ||
super.setUp(); | ||
|
||
_allocate = new InvariantAllocate(address(__hyper__), address(__asset__), address(__quote__)); | ||
|
||
addTargetContract(address(_allocate)); | ||
} | ||
|
||
function invariant_global_account() public { | ||
(bool prepared, bool settled) = __hyper__.__account__(); | ||
assertTrue(!prepared, "invariant-prepared"); | ||
assertTrue(settled, "invariant-settled"); | ||
} | ||
} |
Oops, something went wrong.