add isEthPair test for xyk curve

This commit is contained in:
0xacedia
2022-07-31 10:28:00 +01:00
parent 1635f1d814
commit a8b5e34969
3 changed files with 131 additions and 35 deletions
+5 -4
View File
@@ -2,10 +2,11 @@ ExponentialCurveTest:test_getBuyInfoExample() (gas: 9089)
ExponentialCurveTest:test_getSellInfoExample() (gas: 9022)
LinearCurveTest:test_getBuyInfoExample() (gas: 8628)
LinearCurveTest:test_getSellInfoExample() (gas: 8477)
XykCurveTest:test_buyCalculatesFee() (gas: 143)
XykCurveTest:test_buyCalculatesProtocolFee() (gas: 186)
XykCurveTest:test_buyReturnsSpotPrice() (gas: 165)
XykCurveTest:test_isETHPair() (gas: 120)
XykCurveTest:test_buyCalculatesFee() (gas: 165)
XykCurveTest:test_buyCalculatesProtocolFee() (gas: 208)
XykCurveTest:test_getBuyInfoCannotHave0NumItems() (gas: 6488)
XykCurveTest:test_getBuyInfoReturnsSpotPrice() (gas: 164)
XykCurveTest:test_isETHPair() (gas: 262286)
XykCurveTest:test_sellReturnsSpotPrice() (gas: 187)
PAFExponentialCurveEnumerableERC20Test:testFail_callMint721() (gas: 19205)
PAFExponentialCurveEnumerableERC20Test:testFail_changeAssetRecipientForTrade() (gas: 10757)
+10 -28
View File
@@ -8,27 +8,13 @@ import {LSSVMPair} from "../LSSVMPair.sol";
import {IERC721} from "@openzeppelin/contracts/token/ERC721/IERC721.sol";
import {LSSVMPairCloner} from "../lib/LSSVMPairCloner.sol";
import {LSSVMPairERC20} from "../LSSVMPairERC20.sol";
import {ILSSVMPairFactoryLike} from "../LSSVMPairFactory.sol";
/*
@author 0xacedia
@notice Bonding curve logic for an x*y=k curve.
*/
contract XykCurve is ICurve, CurveErrorCodes {
address factory;
address public immutable enumerableETHTemplate;
address public immutable missingEnumerableETHTemplate;
constructor(
address _factory,
address _enumerableETHTemplate,
address _missingEnumerableETHTemplate
) {
factory = _factory;
enumerableETHTemplate = _enumerableETHTemplate;
missingEnumerableETHTemplate = _missingEnumerableETHTemplate;
}
/**
@dev See {ICurve-validateDelta}
*/
@@ -81,9 +67,10 @@ contract XykCurve is ICurve, CurveErrorCodes {
}
// get the pair's nft and eth/erc20 balance
IERC721 nft = IERC721(LSSVMPair(msg.sender).nft());
LSSVMPair pair = LSSVMPair(msg.sender);
IERC721 nft = IERC721(pair.nft());
uint256 nftBalance = nft.balanceOf(msg.sender);
uint256 tokenBalance = isETHPair(msg.sender)
uint256 tokenBalance = isETHPair(pair)
? msg.sender.balance
: LSSVMPairERC20(msg.sender).token().balanceOf(msg.sender);
@@ -104,18 +91,13 @@ contract XykCurve is ICurve, CurveErrorCodes {
error = Error.OK;
}
function isETHPair(address query) public view returns (bool) {
function isETHPair(LSSVMPair pair) public pure returns (bool) {
ILSSVMPairFactoryLike.PairVariant variant = pair.pairVariant();
return
LSSVMPairCloner.isETHPairClone(
factory,
enumerableETHTemplate,
query
) ||
LSSVMPairCloner.isETHPairClone(
factory,
missingEnumerableETHTemplate,
query
);
variant ==
ILSSVMPairFactoryLike.PairVariant.MISSING_ENUMERABLE_ETH ||
variant == ILSSVMPairFactoryLike.PairVariant.ENUMERABLE_ETH;
}
/**
+116 -3
View File
@@ -6,6 +6,15 @@ import {FixedPointMathLib} from "solmate/utils/FixedPointMathLib.sol";
import {XykCurve} from "../../bonding-curves/XykCurve.sol";
import {CurveErrorCodes} from "../../bonding-curves/CurveErrorCodes.sol";
import {LSSVMPairFactory} from "../../LSSVMPairFactory.sol";
import {LSSVMPairEnumerableETH} from "../../LSSVMPairEnumerableETH.sol";
import {LSSVMPairMissingEnumerableETH} from "../../LSSVMPairMissingEnumerableETH.sol";
import {LSSVMPairEnumerableERC20} from "../../LSSVMPairEnumerableERC20.sol";
import {LSSVMPairMissingEnumerableERC20} from "../../LSSVMPairMissingEnumerableERC20.sol";
import {LSSVMPairCloner} from "../../lib/LSSVMPairCloner.sol";
import {LSSVMPair} from "../../LSSVMPair.sol";
import {IERC721} from "@openzeppelin/contracts/token/ERC721/IERC721.sol";
import {ERC20} from "solmate/tokens/ERC20.sol";
import {Hevm} from "../utils/Hevm.sol";
@@ -15,14 +24,118 @@ contract XykCurveTest is DSTest {
uint256 constant MIN_PRICE = 1 gwei;
XykCurve curve;
LSSVMPairFactory factory;
LSSVMPairEnumerableETH enumerableETHTemplate;
LSSVMPairMissingEnumerableETH missingEnumerableETHTemplate;
LSSVMPairEnumerableERC20 enumerableERC20Template;
LSSVMPairMissingEnumerableERC20 missingEnumerableERC20Template;
function setUp() public {
curve = new XykCurve(address(0), address(0), address(0));
enumerableETHTemplate = new LSSVMPairEnumerableETH();
missingEnumerableETHTemplate = new LSSVMPairMissingEnumerableETH();
enumerableERC20Template = new LSSVMPairEnumerableERC20();
missingEnumerableERC20Template = new LSSVMPairMissingEnumerableERC20();
factory = new LSSVMPairFactory(
enumerableETHTemplate,
missingEnumerableETHTemplate,
enumerableERC20Template,
missingEnumerableERC20Template,
payable(0),
0
);
curve = new XykCurve();
}
function test_isETHPair() public {}
function test_getBuyInfoCannotHave0NumItems() public {
// arrange
uint256 numItems = 0;
function test_buyReturnsSpotPrice() public {}
// act
(CurveErrorCodes.Error error, , , , ) = curve.getBuyInfo(
0,
0,
numItems,
0,
0
);
// assert
assertEq(
uint256(error),
uint256(CurveErrorCodes.Error.INVALID_NUMITEMS),
"Should have returned invalid num items error"
);
}
function test_isETHPair() public {
// arrange
address enumerableETHPair = LSSVMPairCloner.cloneETHPair(
address(enumerableETHTemplate),
factory,
curve,
IERC721(address(0)),
uint8(2)
);
address missingEnumerableETHPair = LSSVMPairCloner.cloneETHPair(
address(missingEnumerableETHTemplate),
factory,
curve,
IERC721(address(0)),
uint8(2)
);
address enumerableERC20Pair = LSSVMPairCloner.cloneERC20Pair(
address(enumerableERC20Template),
factory,
curve,
IERC721(address(0)),
uint8(2),
ERC20(address(0))
);
address missingEnumerableERC20Pair = LSSVMPairCloner.cloneERC20Pair(
address(missingEnumerableERC20Template),
factory,
curve,
IERC721(address(0)),
uint8(2),
ERC20(address(0))
);
// act
bool isEnumerableETHPairETHPair = curve.isETHPair(
LSSVMPair(enumerableETHPair)
);
bool isMissingEnumerableETHPairETHPair = curve.isETHPair(
LSSVMPair(missingEnumerableETHPair)
);
bool isEnumerableERC20PairETHPair = curve.isETHPair(
LSSVMPair(enumerableERC20Pair)
);
bool isMissingEnumerableERC20PairETHPair = curve.isETHPair(
LSSVMPair(missingEnumerableERC20Pair)
);
// assert
assertTrue(
isEnumerableETHPairETHPair,
"Enumerable ETH pair should be detected as an ETH pair"
);
assertTrue(
isMissingEnumerableETHPairETHPair,
"Missing enumerable ETH pair should be detected as an ETH pair"
);
assertTrue(
!isEnumerableERC20PairETHPair,
"Enumerable ERC20 pair should not be detected as an ETH pair"
);
assertTrue(
!isMissingEnumerableERC20PairETHPair,
"Missing enumerable ERC20 pair should not be detected as an ETH pair"
);
}
function test_getBuyInfoReturnsSpotPrice() public {}
function test_sellReturnsSpotPrice() public {}