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

Replaces next reserves by deltas in swap functions #42

Merged
109 commits merged into from
Mar 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
109 commits
Select commit Hold shift + click to select a range
308320b
feat: update IStrategy swap function to return swap deltas
clemlak Feb 29, 2024
8388c46
feat: refactor G3M swap to use deltas, fix swap fees affecting deltaL…
clemlak Feb 29, 2024
14cadee
feat: refactor DFMM swap to use deltas
clemlak Feb 29, 2024
26b0077
feat: update ConstantSum swap function to use deltas
clemlak Feb 29, 2024
7792647
chore: change imports order in LogNormal
clemlak Feb 29, 2024
d2de812
feat: update LogNormal strategy to use deltas in swap
clemlak Feb 29, 2024
0db0d97
test: update MockStrategy swap
clemlak Feb 29, 2024
2991993
feat: change G3M simulateSwap encoded data
clemlak Feb 29, 2024
c1ff462
feat: attempt to fix simulateSwap in LogNormalSolver
clemlak Feb 29, 2024
8ffa6ca
test: add skipped DFMM swap tests
clemlak Feb 29, 2024
693f6f1
test: update MockStrategy validateSwap function
clemlak Feb 29, 2024
981e1e3
chore: fix formulas in G3M README
clemlak Mar 4, 2024
b435f14
feat: add getPool in IDFMM interface
clemlak Mar 4, 2024
65ffe8a
feat: update simulateSwap in G3MSolver
clemlak Mar 4, 2024
e0f38d6
fix: G3MSolver simulateSwap function
clemlak Mar 4, 2024
253f010
test: fix test_G3M_swap_SwapsXforY
clemlak Mar 4, 2024
c6c23e4
test: add balance checks in test_G3M_swap_SwapsXforY
clemlak Mar 4, 2024
b66b0b5
test: update and fix G3MSwapTest tests
clemlak Mar 4, 2024
21ac38b
chore: attempt to fix ConstantSum README on GitHub
clemlak Mar 4, 2024
9ee8b31
chore: fix ConstantSum README display on Github
clemlak Mar 4, 2024
e0250f5
chore: remove unused import in StrategyLib
clemlak Mar 4, 2024
e083d3a
chore: update IDFMM swap interface
clemlak Mar 4, 2024
616cbc0
chore: remove unused import in LogNormal
clemlak Mar 4, 2024
056bbf0
chore: fix LogNormal README rendering on Github
clemlak Mar 4, 2024
c25293a
fix: deltaLiquidity computation in LogNormal validateSwap
clemlak Mar 5, 2024
0437f6b
test: update LogNormal swap test
clemlak Mar 5, 2024
1db1511
feat: update ConstantSumSolver to use deltas in simulateSwap
clemlak Mar 5, 2024
8d0372c
test: update ConstantSum swap tests
clemlak Mar 5, 2024
f69f9b2
chore: remove unused import in ConstantSum
clemlak Mar 5, 2024
03f7acb
test: remove Lex contract from ConstantSum tests
clemlak Mar 5, 2024
81b0f93
test: remove Lex contract from G3M tests
clemlak Mar 5, 2024
645e639
test: remove Lex contract from test SetUp contract
clemlak Mar 5, 2024
f76fa2a
test: remove Lex contract from LogNormal tests
clemlak Mar 5, 2024
9bfdd0b
test: remove Lex contract from SetUp test contracts
clemlak Mar 5, 2024
aa4916e
test: delete Lex contract
clemlak Mar 5, 2024
68d90eb
test: delete ArbiterToken test contract
clemlak Mar 5, 2024
755ef8a
test: delete ArbMath test contract
clemlak Mar 5, 2024
d444b97
test: delete AtomicV2 test contract
clemlak Mar 5, 2024
4cf186f
test: delete Coin test contract
clemlak Mar 5, 2024
93f78fe
test: delete PortfolioTracker test contract
clemlak Mar 5, 2024
8ff921b
chore: delete unused import in SetUp test contract
clemlak Mar 5, 2024
a9e4c10
test: add ConstantSum test SetUp contract
clemlak Mar 5, 2024
eadcfe7
test: add missing LPToken import in DFMMConstructorTest
clemlak Mar 5, 2024
8e99781
test: fix incorrect SetUp import in G3MSetUp
clemlak Mar 5, 2024
da2976e
test: update LogNormal setup
clemlak Mar 5, 2024
8a8fc4d
test: update ConstantSum test setup
clemlak Mar 5, 2024
f474817
chore: fix ConstantSumSetUp contract name
clemlak Mar 5, 2024
ec86383
chore: add test files for ConstantSum
clemlak Mar 5, 2024
1a06313
chore: add ConstantSumSolver test file
clemlak Mar 5, 2024
2955af0
chore: remove strategies folder and move tests at a higher level
clemlak Mar 5, 2024
2ad1b33
test: add test_ConstantSum_init_InitializesPool
clemlak Mar 5, 2024
c090f85
test: move tests into dedicated test folder
clemlak Mar 5, 2024
3e8991d
chore: move scripts into dedicated folder
clemlak Mar 5, 2024
dbbc290
test: add LPToken setup
clemlak Mar 6, 2024
d9bf889
test: move LPToken tests into initialize test file
clemlak Mar 6, 2024
89c9525
chore: add link to testing suite README in main README
clemlak Mar 6, 2024
ecd7f93
chore: convert imports to named imports
clemlak Mar 6, 2024
cd70ebd
test: fix test_dfmm_init_EmitsInitEvent
clemlak Mar 6, 2024
534c5de
feat: change LogNormal params to width and mean
clemlak Mar 6, 2024
c211070
feat: params update, tradingFunction in LogNormal
clemlak Mar 6, 2024
2076bcc
chore: add new imports rule in style guide
clemlak Mar 6, 2024
4ffc92d
chore: add recommended VS Code extensions
clemlak Mar 6, 2024
e93ce28
chore: add VS Code workspace settings
clemlak Mar 6, 2024
ac169aa
feat: update LogNormal parameters update, wip deltaLiquidity update
clemlak Mar 6, 2024
a598b38
feat: refactor LogNormal computations
clemlak Mar 6, 2024
6bf9e82
chore: update LogNormal NatSpec
clemlak Mar 6, 2024
ce930be
feat: remove unused params in LogNormal
clemlak Mar 6, 2024
6f40217
chore: remove unused mulidivUp function
clemlak Mar 6, 2024
9256a8e
feat: remove unused mulidiv function
clemlak Mar 6, 2024
5fb3f51
feat: update computeHalfSigmaSquared
clemlak Mar 6, 2024
7a27340
feat: change LogNormal to expect deltaLiquidity in swap
clemlak Mar 6, 2024
77754f2
feat: update D1 and D2 functions in LogNormalExtendedLib
clemlak Mar 6, 2024
c1fdedd
fix: wrong width internal param in LogNormal init
clemlak Mar 6, 2024
61098a6
test: update LogNormalParams in LogNormal test setup
clemlak Mar 6, 2024
0ede648
test: update LogNormalParams in LogNormal tests
clemlak Mar 6, 2024
70f5083
feat: update params update in LogNormalSolver
clemlak Mar 6, 2024
78fa654
feat: comment out LogNormal computations to avoid compilation errors …
clemlak Mar 6, 2024
8d2f139
fix: move lognormal arbitrage utils into test/utils directory
kinrezC Mar 6, 2024
8fa8f13
feat: add Strategy abstract contract
clemlak Mar 7, 2024
e6da465
feat: rename computeSwapConstant to tradingFunction in IStrategy
clemlak Mar 7, 2024
f622911
feat: add DeltaError in IStrategy
clemlak Mar 7, 2024
2718dfb
feat: add DFMM param in Strategy constructor
clemlak Mar 7, 2024
819699c
feat: update tradingFunction params in IStrategy
clemlak Mar 7, 2024
b1df224
feat: implement Strategy abstract contract in GeometricMean strategy
clemlak Mar 7, 2024
3c621b1
feat: update LogNormal strategy imports
clemlak Mar 7, 2024
b660097
feat: remove DeltaError duplicate in LogNormal
clemlak Mar 7, 2024
428bcde
feat: update delta X and Y calculations in Strategy
clemlak Mar 7, 2024
94f992f
feat: update delta X and Y calculations in G3M
clemlak Mar 7, 2024
b821460
feat: add delta X and Y calculations to LogNormal strategy
clemlak Mar 7, 2024
867c728
feat: ConstantSum now implements Strategy abstract contract
clemlak Mar 7, 2024
72b5852
fix: missing StrategyLib import in G3MExtendedLib
clemlak Mar 7, 2024
a5c38aa
test: implements Strategy into MockStrategy test contract
clemlak Mar 7, 2024
44164ec
feat: mark Strategy functions as virtual
clemlak Mar 7, 2024
dcc0105
feat: temporary invariant fix in LogNormalSolver
clemlak Mar 7, 2024
e1d349b
test: comment out LogNormal arbitraging tests
clemlak Mar 7, 2024
b1ddac9
test: update LogNormal params (no more tau, add width and mean)
clemlak Mar 7, 2024
8aadbf7
chore: remove unused params in G3M strategy
clemlak Mar 7, 2024
0447d7a
chore: remove unused parameters in ConstantSum strategy
clemlak Mar 7, 2024
d1b7087
test: remove unused parameters in MockStrategy
clemlak Mar 7, 2024
46b6ffe
feat: update simulateSwap in G3MSolver
clemlak Mar 7, 2024
0b475e4
fix: wrong constructor parameter for Strategy in LogNormal
clemlak Mar 7, 2024
04f6d58
feat: include deltaLiquidity in simulateSwap returned data in LogNorm…
clemlak Mar 7, 2024
33f0e19
feat: include deltaLiquidity in simulateSwap returned data in Constan…
clemlak Mar 7, 2024
d543985
test: update meand and width parameters in LogNormal test setup
clemlak Mar 7, 2024
c87df4a
fix: invariant computations in LogNormalSolver
clemlak Mar 7, 2024
1f149b7
fix: invariant computation in LogNormalSolver
clemlak Mar 7, 2024
902d648
feat: add wip validateAllocate in ConstantSum strategy
clemlak Mar 7, 2024
86aa450
test: add ConstantSum allocate test
clemlak Mar 7, 2024
dd777e8
feat: update simulateAllocate in ConstantSumSolver
clemlak Mar 7, 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
2 changes: 0 additions & 2 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,6 @@ optimized-out
kit/target
target

# VSCode
.vscode
.env
report/main.aux
report/main.pyg
Expand Down
6 changes: 6 additions & 0 deletions .vscode/extensions.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"recommendations": [
"remisa.mathover",
"nomicfoundation.hardhat-solidity"
]
}
10 changes: 10 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"editor.formatOnSave": true,
"solidity.formatter": "forge",
"[solidity]": {
"editor.defaultFormatter": "NomicFoundation.hardhat-solidity"
},
"mathover.matchRegex": "\\$\\$(.*?)\\$\\$",
"mathover.forwardSkip": 2,
"mathover.backwardSkip": 2
}
2 changes: 1 addition & 1 deletion CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,11 @@ A few rules:

## Style Guide


### Imports

1. Use named imports as much as possible.
2. Import external dependencies first (libs, etc).
3. Import from root `src/DFMM.sol` instead of `../DFMM.sol`.

### Tests

Expand Down
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,8 @@ To run all the tests, simply run the following command:
forge t
```

Learn more about our testing suite [here](./test/README.md).

### Deployment

You can deploy the contracts with the following command:
Expand Down
File renamed without changes.
161 changes: 49 additions & 112 deletions src/ConstantSum/ConstantSum.sol
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,9 @@ pragma solidity ^0.8.13;

import "./ConstantSumLib.sol";
import "src/interfaces/IDFMM.sol";
import "src/interfaces/IStrategy.sol";
import "forge-std/Test.sol";
import { Strategy, IStrategy } from "src/Strategy.sol";

contract ConstantSum is IStrategy {
contract ConstantSum is Strategy {
using FixedPointMathLib for uint256;

struct InternalParams {
Expand All @@ -21,27 +20,17 @@ contract ConstantSum is IStrategy {
address controller;
}

/// @inheritdoc IStrategy
address public dfmm;

/// @inheritdoc IStrategy
string public constant name = "ConstantSum";

mapping(uint256 => InternalParams) public internalParams;

constructor(address dfmm_) {
dfmm = dfmm_;
}

modifier onlyDFMM() {
if (msg.sender != dfmm) revert NotDFMM();
_;
}
constructor(address dfmm_) Strategy(dfmm_) { }

function init(
address,
uint256 poolId,
IDFMM.Pool calldata pool,
IDFMM.Pool calldata,
bytes calldata data
)
public
Expand Down Expand Up @@ -71,67 +60,7 @@ contract ConstantSum is IStrategy {
return (valid, invariant, reserveX, reserveY, totalLiquidity);
}

function validateSwap(
address,
uint256 poolId,
IDFMM.Pool calldata pool,
bytes calldata data
)
external
view
returns (
bool valid,
int256 invariant,
int256 liquidityDelta,
uint256 nextRx,
uint256 nextRy,
uint256 nextL
)
{
ConstantSumParams memory params =
abi.decode(getPoolParams(poolId), (ConstantSumParams));

(nextRx, nextRy, nextL) = abi.decode(data, (uint256, uint256, uint256));

uint256 minLiquidityDelta;
uint256 amountIn;
uint256 fees;
if (nextRx > pool.reserveX) {
amountIn = nextRx - pool.reserveX;
fees = amountIn.mulWadUp(params.swapFee);
minLiquidityDelta += fees;
} else if (nextRy > pool.reserveY) {
amountIn = nextRy - pool.reserveY;
fees = amountIn.mulWadUp(params.swapFee);
minLiquidityDelta += fees.divWadUp(params.price);
} else {
revert("invalid swap: inputs x and y have the same sign!");
}

liquidityDelta = int256(nextL) - int256(pool.totalLiquidity);
assert(liquidityDelta >= int256(minLiquidityDelta));

invariant =
ConstantSumLib.tradingFunction(nextRx, nextRy, nextL, params.price);

valid = -EPSILON < invariant && invariant < EPSILON;
}

function computeSwapConstant(
uint256 poolId,
bytes memory data
) external view returns (int256) {
(uint256 reserveX, uint256 reserveY, uint256 totalLiquidity) =
abi.decode(data, (uint256, uint256, uint256));
return ConstantSumLib.tradingFunction(
reserveX,
reserveY,
totalLiquidity,
abi.decode(getPoolParams(poolId), (ConstantSumParams)).price
);
}

// This should literally always work lol
/// @inheritdoc IStrategy
function validateAllocate(
address,
uint256 poolId,
Expand All @@ -140,6 +69,7 @@ contract ConstantSum is IStrategy {
)
external
view
override
returns (
bool valid,
int256 invariant,
Expand All @@ -151,50 +81,20 @@ contract ConstantSum is IStrategy {
(deltaX, deltaY, deltaLiquidity) =
abi.decode(data, (uint256, uint256, uint256));

invariant = ConstantSumLib.tradingFunction(
invariant = tradingFunction(
pool.reserveX + deltaX,
pool.reserveX + deltaY,
pool.reserveY + deltaY,
pool.totalLiquidity + deltaLiquidity,
abi.decode(getPoolParams(poolId), (ConstantSumParams)).price
getPoolParams(poolId)
);

valid = -EPSILON < invariant && invariant < EPSILON;
}

// This should literally always work lol
function validateDeallocate(
address,
uint256 poolId,
IDFMM.Pool calldata pool,
bytes calldata data
)
external
view
returns (
bool valid,
int256 invariant,
uint256 deltaX,
uint256 deltaY,
uint256 deltaLiquidity
)
{
(deltaX, deltaY, deltaLiquidity) =
abi.decode(data, (uint256, uint256, uint256));

invariant = ConstantSumLib.tradingFunction(
pool.reserveX - deltaX,
pool.reserveY - deltaY,
pool.totalLiquidity - deltaLiquidity,
abi.decode(getPoolParams(poolId), (ConstantSumParams)).price
);

valid = -EPSILON < invariant && invariant < EPSILON;
valid = -(EPSILON) < invariant && invariant < EPSILON;
}

function update(
address sender,
uint256 poolId,
IDFMM.Pool calldata pool,
IDFMM.Pool calldata,
bytes calldata data
) external onlyDFMM {
if (sender != internalParams[poolId].controller) revert InvalidSender();
Expand All @@ -217,12 +117,49 @@ contract ConstantSum is IStrategy {
}
}

function getPoolParams(uint256 poolId) public view returns (bytes memory) {
function getPoolParams(uint256 poolId)
public
view
override
returns (bytes memory)
{
ConstantSumParams memory params;

params.price = internalParams[poolId].price;
params.swapFee = internalParams[poolId].swapFee;

return abi.encode(params);
}

function tradingFunction(
uint256 reserveX,
uint256 reserveY,
uint256 totalLiquidity,
bytes memory params
) public pure override returns (int256) {
return ConstantSumLib.tradingFunction(
reserveX,
reserveY,
totalLiquidity,
abi.decode(params, (ConstantSumParams)).price
);
}

function _computeDeltaXGivenDeltaL(
uint256 deltaLiquidity,
IDFMM.Pool calldata,
bytes memory
) internal view virtual override returns (uint256) {
// TODO: Implement this.
return deltaLiquidity;
}

function _computeDeltaYGivenDeltaL(
uint256 deltaLiquidity,
IDFMM.Pool calldata,
bytes memory
) internal view virtual override returns (uint256) {
// TODO: Implement this.
return deltaLiquidity;
}
}
Loading
Loading