diff --git a/contracts/DODOPrivatePool/impl/DPPAdmin.sol b/contracts/DODOPrivatePool/impl/DPPAdmin.sol index 756a6f1..5b2bd2c 100644 --- a/contracts/DODOPrivatePool/impl/DPPAdmin.sol +++ b/contracts/DODOPrivatePool/impl/DPPAdmin.sol @@ -38,10 +38,9 @@ contract DPPAdmin is InitializableOwnable { //For Rebase Token function sync() external notFreezed onlyOwner { - IDPP(_DPP_).sync(); + IDPP(_DPP_).ratioSync(); } - function setFreezeTimestamp(uint256 timestamp) external notFreezed onlyOwner { _FREEZE_TIMESTAMP_ = timestamp; } @@ -111,17 +110,18 @@ contract DPPAdmin is InitializableOwnable { operator == _OPERATOR_), "RESET FORBIDDEN!" ); - return IDPP(_DPP_).reset( - msg.sender, - newLpFeeRate, - newMtFeeRate, - newI, - newK, - baseOutAmount, - quoteOutAmount, - minBaseReserve, - minQuoteReserve - ); + return + IDPP(_DPP_).reset( + msg.sender, + newLpFeeRate, + newMtFeeRate, + newI, + newK, + baseOutAmount, + quoteOutAmount, + minBaseReserve, + minQuoteReserve + ); } // ============ Admin Version Control ============ diff --git a/contracts/DODOPrivatePool/impl/DPPVault.sol b/contracts/DODOPrivatePool/impl/DPPVault.sol index bb520af..a1bd0cf 100644 --- a/contracts/DODOPrivatePool/impl/DPPVault.sol +++ b/contracts/DODOPrivatePool/impl/DPPVault.sol @@ -23,10 +23,7 @@ contract DPPVault is DPPStorage { // ============ Events ============ - event Reset( - uint256 newLpFeeRate, - uint256 newMtFeeRate - ); + event Reset(uint256 newLpFeeRate, uint256 newMtFeeRate); // ============ View Functions ============ @@ -35,14 +32,22 @@ contract DPPVault is DPPStorage { quoteReserve = _QUOTE_RESERVE_; } - function getUserFeeRate(address user) external view returns (uint256 lpFeeRate, uint256 mtFeeRate) { + function getUserFeeRate(address user) + external + view + returns (uint256 lpFeeRate, uint256 mtFeeRate) + { lpFeeRate = _LP_FEE_RATE_MODEL_.getFeeRate(user); mtFeeRate = _MT_FEE_RATE_MODEL_.getFeeRate(user); } - function getUserTradePermission(address user) external view returns (bool isBuyAllow, bool isSellAllow) { + function getUserTradePermission(address user) + external + view + returns (bool isBuyAllow, bool isSellAllow) + { isBuyAllow = (!_BUYING_CLOSE_ && _TRADE_PERMISSION_.isAllowed(user)); - isSellAllow = (!_SELLING_CLOSE_ && _TRADE_PERMISSION_.isAllowed(user)); + isSellAllow = (!_SELLING_CLOSE_ && _TRADE_PERMISSION_.isAllowed(user)); } // ============ Get Input ============ @@ -57,7 +62,7 @@ contract DPPVault is DPPStorage { // ============ Set States ============ - function sync() external preventReentrant onlyOwner { + function ratioSync() external preventReentrant onlyOwner { uint256 baseBalance = _BASE_TOKEN_.balanceOf(address(this)); uint256 quoteBalance = _QUOTE_TOKEN_.balanceOf(address(this)); if (baseBalance != _BASE_RESERVE_) { @@ -95,7 +100,10 @@ contract DPPVault is DPPStorage { uint256 minBaseReserve, uint256 minQuoteReserve ) public preventReentrant onlyOwner returns (bool) { - require(_BASE_RESERVE_ >= minBaseReserve && _QUOTE_RESERVE_ >= minQuoteReserve, "Reserve amount is not enough"); + require( + _BASE_RESERVE_ >= minBaseReserve && _QUOTE_RESERVE_ >= minQuoteReserve, + "RESERVE_AMOUNT_IS_NOT_ENOUGH" + ); _LP_FEE_RATE_MODEL_.setFeeRate(newLpFeeRate); _MT_FEE_RATE_MODEL_.setFeeRate(newMtFeeRate); _I_.set(newI); diff --git a/contracts/DODOPrivatePool/intf/IDPP.sol b/contracts/DODOPrivatePool/intf/IDPP.sol index df7810c..a45ffd5 100644 --- a/contracts/DODOPrivatePool/intf/IDPP.sol +++ b/contracts/DODOPrivatePool/intf/IDPP.sol @@ -45,7 +45,7 @@ interface IDPP { function setSell(bool open) external; - function sync() external; + function ratioSync() external; //============================== diff --git a/test/CrowdPooling/CPSettle.test.ts b/test/CrowdPooling/CPSettle.test.ts index 03e7e58..b211128 100644 --- a/test/CrowdPooling/CPSettle.test.ts +++ b/test/CrowdPooling/CPSettle.test.ts @@ -108,5 +108,26 @@ describe("Funding", () => { assert.equal(await ctx.QUOTE.methods.balanceOf(ctx.CP.options.address).call(), decimalStr("49900")) }) + it("bid zero", async () => { + await ctx.EVM.increaseTime(86400 * 2) + + await logGas(ctx.CP.methods.settle(), ctx.sendParam(ctx.Deployer), "settle") + + var poolAddress = await ctx.CP.methods._POOL_().call() + var pool = getContractWithAddress(DVM_NAME, poolAddress) + + assert.equal(await pool.methods.getMidPrice().call(), decimalStr("10")) + assert.equal(await ctx.CP.methods._AVG_SETTLED_PRICE_().call(), decimalStr("10")) + + assert.equal(await ctx.CP.methods._UNUSED_QUOTE_().call(), "0") + assert.equal(await ctx.CP.methods._UNUSED_BASE_().call(), "0") + + assert.equal(await ctx.BASE.methods.balanceOf(poolAddress).call(), decimalStr("10000")) + assert.equal(await ctx.BASE.methods.balanceOf(ctx.CP.options.address).call(), "0") + + assert.equal(await ctx.QUOTE.methods.balanceOf(poolAddress).call(), "0") + assert.equal(await ctx.QUOTE.methods.balanceOf(ctx.CP.options.address).call(), "0") + }) + }) }) \ No newline at end of file diff --git a/test/DPP/reset.test.ts b/test/DPP/reset.test.ts index a65fd6f..69239a0 100644 --- a/test/DPP/reset.test.ts +++ b/test/DPP/reset.test.ts @@ -32,7 +32,7 @@ async function init(ctx: DPPContext): Promise { await ctx.DPP.methods.reset(lp, lpFeeRate, mtFeeRate, iValue, kValue, "0", "0", "0", "0").send(ctx.sendParam(ctx.Deployer)) } -describe("DPP Trader", () => { +describe("DPP Reset", () => { let snapshotId: string; let ctx: DPPContext; @@ -68,8 +68,10 @@ describe("DPP Trader", () => { assert.equal(state.Q0, decimalStr("2000")) assert.equal(state.i, iValue) assert.equal(state.K, kValue) - assert.equal(await ctx.DPP.methods.getLpFeeRate(lp).call(), lpFeeRate) - assert.equal(await ctx.DPP.methods.getMtFeeRate(lp).call(), mtFeeRate) + + var feeRate = await ctx.DPP.methods.getUserFeeRate(lp).call() + assert.equal(feeRate[0], lpFeeRate) + assert.equal(feeRate[1], mtFeeRate) }); it("reset with asset output", async () => { @@ -86,8 +88,10 @@ describe("DPP Trader", () => { assert.equal(state.Q0, decimalStr("900")) assert.equal(state.i, iValue) assert.equal(state.K, kValue) - assert.equal(await ctx.DPP.methods.getLpFeeRate(lp).call(), lpFeeRate) - assert.equal(await ctx.DPP.methods.getMtFeeRate(lp).call(), mtFeeRate) + + var feeRate = await ctx.DPP.methods.getUserFeeRate(lp).call() + assert.equal(feeRate[0], lpFeeRate) + assert.equal(feeRate[1], mtFeeRate) }) it("reset without asset input/output", async () => { @@ -104,8 +108,10 @@ describe("DPP Trader", () => { assert.equal(state.Q0, decimalStr("1000")) assert.equal(state.i, iValue) assert.equal(state.K, kValue) - assert.equal(await ctx.DPP.methods.getLpFeeRate(lp).call(), lpFeeRate) - assert.equal(await ctx.DPP.methods.getMtFeeRate(lp).call(), mtFeeRate) + + var feeRate = await ctx.DPP.methods.getUserFeeRate(lp).call() + assert.equal(feeRate[0], lpFeeRate) + assert.equal(feeRate[1], mtFeeRate) }) }); @@ -138,18 +144,30 @@ describe("DPP Trader", () => { it("i or k can not out of range", async () => { await truffleAssert.reverts( - ctx.DPP.methods.reset(lp, "0", "0", "0", decimalStr("1"), "0", "0" ,"0", "0").send(ctx.sendParam(ctx.Deployer)), "I_OUT_OF_RANGE" + ctx.DPP.methods.reset(lp, "0", "0", "0", decimalStr("1"), "0", "0", "0", "0").send(ctx.sendParam(ctx.Deployer)), "I_OUT_OF_RANGE" ) await truffleAssert.reverts( ctx.DPP.methods.reset(lp, "0", "0", "10000000000000000000000000000000000000", decimalStr("1"), "0", "0", "0", "0").send(ctx.sendParam(ctx.Deployer)), "I_OUT_OF_RANGE" ) - await truffleAssert.reverts( - ctx.DPP.methods.reset(lp, "0", "0", decimalStr("1"), "1", "0", "0", "0", "0").send(ctx.sendParam(ctx.Deployer)), "K_OUT_OF_RANGE" - ) await truffleAssert.reverts( ctx.DPP.methods.reset(lp, "0", "0", decimalStr("1"), decimalStr("2"), "0", "0", "0", "0").send(ctx.sendParam(ctx.Deployer)), "K_OUT_OF_RANGE" ) }) + + it("revert if someone trade before reset", async () => { + var baseReserve = await ctx.DPP.methods._BASE_RESERVE_().call() + var quoteReserve = await ctx.DPP.methods._QUOTE_RESERVE_().call() + + await ctx.BASE.methods.mint(ctx.DPP.options.address, decimalStr("1")).send(ctx.sendParam(ctx.Deployer)) + await ctx.DPP.methods.sellBase(ctx.Deployer).send(ctx.sendParam(ctx.Deployer)) + + await truffleAssert.reverts(ctx.DPP.methods.reset(lp, "0", "0", decimalStr("1"), decimalStr("2"), "0", "0", baseReserve, quoteReserve).send(ctx.sendParam(ctx.Deployer)), "RESERVE_AMOUNT_IS_NOT_ENOUGH") + + await ctx.QUOTE.methods.mint(ctx.DPP.options.address, decimalStr("200")).send(ctx.sendParam(ctx.Deployer)) + await ctx.DPP.methods.sellQuote(ctx.Deployer).send(ctx.sendParam(ctx.Deployer)) + + await truffleAssert.reverts(ctx.DPP.methods.reset(lp, "0", "0", decimalStr("1"), decimalStr("2"), "0", "0", baseReserve, quoteReserve).send(ctx.sendParam(ctx.Deployer)), "RESERVE_AMOUNT_IS_NOT_ENOUGH") + }) }) }); \ No newline at end of file diff --git a/test/DPP/trader.test.ts b/test/DPP/trader.test.ts index 3e181b3..9505804 100644 --- a/test/DPP/trader.test.ts +++ b/test/DPP/trader.test.ts @@ -26,7 +26,7 @@ async function init(ctx: DPPContext): Promise { await ctx.transferBaseToDPP(lp, decimalStr("10")) await ctx.transferQuoteToDPP(lp, decimalStr("1000")) - await ctx.DPP.methods.reset(lp, decimalStr("0.002"), decimalStr("0.001"), decimalStr("100"), decimalStr("0.1"), "0", "0").send(ctx.sendParam(ctx.Deployer)) + await ctx.DPP.methods.reset(lp, decimalStr("0.002"), decimalStr("0.001"), decimalStr("100"), decimalStr("0.1"), "0", "0", "0", "0").send(ctx.sendParam(ctx.Deployer)) console.log("deposit") } @@ -73,11 +73,11 @@ describe("DPP Trader", () => { await ctx.DPP.methods.sellQuote(trader).send(ctx.sendParam(trader)) var balances = await ctx.getBalances(trader) - assert.equal(balances.traderBase, "10986174542266106306") + assert.equal(balances.traderBase, "10986174542266106307") assert.equal(balances.traderQuote, decimalStr("900")) assert.equal(balances.DPPBase, "9012836315765723075") assert.equal(balances.DPPQuote, decimalStr("1100")) - assert.equal(balances.maintainerBase, "989141968170619") + assert.equal(balances.maintainerBase, "989141968170618") assert.equal(balances.maintainerQuote, "0") // buy at R>1 @@ -85,11 +85,11 @@ describe("DPP Trader", () => { await ctx.DPP.methods.sellQuote(trader).send(ctx.sendParam(trader)) balances = await ctx.getBalances(trader) - assert.equal(balances.traderBase, "11946772292527553371") + assert.equal(balances.traderBase, "11946772292527553373") assert.equal(balances.traderQuote, decimalStr("800")) assert.equal(balances.DPPBase, "8051275077289369844") assert.equal(balances.DPPQuote, decimalStr("1200")) - assert.equal(balances.maintainerBase, "1952630183076785") + assert.equal(balances.maintainerBase, "1952630183076783") assert.equal(balances.maintainerQuote, "0") // sell at R>1 and R not change state @@ -97,42 +97,42 @@ describe("DPP Trader", () => { await ctx.DPP.methods.sellBase(trader).send(ctx.sendParam(trader)) balances = await ctx.getBalances(trader) - assert.equal(balances.traderBase, "10946772292527553371") - assert.equal(balances.traderQuote, "903421814651005338948") + assert.equal(balances.traderBase, "10946772292527553373") + assert.equal(balances.traderQuote, "903421814651005338950") assert.equal(balances.DPPBase, "9051275077289369844") - assert.equal(balances.DPPQuote, "1096474452335302579468") - assert.equal(balances.maintainerBase, "1952630183076785") - assert.equal(balances.maintainerQuote, "103733013692081584") + assert.equal(balances.DPPQuote, "1096474452335302579467") + assert.equal(balances.maintainerBase, "1952630183076783") + assert.equal(balances.maintainerQuote, "103733013692081583") // sell at R>1 and R change state await ctx.transferBaseToDPP(trader, decimalStr("2")) await ctx.DPP.methods.sellBase(trader).send(ctx.sendParam(trader)) balances = await ctx.getBalances(trader) - assert.equal(balances.traderBase, "8946772292527553371") - assert.equal(balances.traderQuote, "1102638273848343281091") + assert.equal(balances.traderBase, "8946772292527553373") + assert.equal(balances.traderQuote, "1102638273848343281094") assert.equal(balances.DPPBase, "11051275077289369844") - assert.equal(balances.DPPQuote, "897058177231046545106") - assert.equal(balances.maintainerBase, "1952630183076785") - assert.equal(balances.maintainerQuote, "303548920610173803") + assert.equal(balances.DPPQuote, "897058177231046545105") + assert.equal(balances.maintainerBase, "1952630183076783") + assert.equal(balances.maintainerQuote, "303548920610173801") var PMMStat = await ctx.DPP.methods.getPMMState().call() assert.equal(PMMStat.R, "2") assert.equal(PMMStat.B0, "10005950249348099200") }); - it.only("first sell and then buy", async () => { + it("first sell and then buy", async () => { // sell at R=1 await ctx.transferBaseToDPP(trader, decimalStr("1")) await ctx.DPP.methods.sellBase(trader).send(ctx.sendParam(trader)) var balances = await ctx.getBalances(trader) assert.equal(balances.traderBase, decimalStr("9")) - assert.equal(balances.traderQuote, "1098617454226610630662") + assert.equal(balances.traderQuote, "1098617454226610630663") assert.equal(balances.DPPBase, decimalStr("11")) assert.equal(balances.DPPQuote, "901283631576572307521") assert.equal(balances.maintainerBase, "0") - assert.equal(balances.maintainerQuote, "98914196817061817") + assert.equal(balances.maintainerQuote, "98914196817061816") // buy at R>1 await ctx.transferBaseToDPP(trader, decimalStr("1")) @@ -140,35 +140,35 @@ describe("DPP Trader", () => { balances = await ctx.getBalances(trader) assert.equal(balances.traderBase, decimalStr("8")) - assert.equal(balances.traderQuote, "1194677229252755337107") + assert.equal(balances.traderQuote, "1194677229252755337109") assert.equal(balances.DPPBase, decimalStr("12")) assert.equal(balances.DPPQuote, "805127507728936984519") assert.equal(balances.maintainerBase, "0") - assert.equal(balances.maintainerQuote, "195263018307678374") + assert.equal(balances.maintainerQuote, "195263018307678372") // sell at R>1 and R not change state await ctx.transferQuoteToDPP(trader, decimalStr("100")) await ctx.DPP.methods.sellQuote(trader).send(ctx.sendParam(trader)) balances = await ctx.getBalances(trader) - assert.equal(balances.traderBase, "9034218146510053389") - assert.equal(balances.traderQuote, "1094677229252755337107") - assert.equal(balances.DPPBase, "10964744523353025795") + assert.equal(balances.traderBase, "9034218146510053391") + assert.equal(balances.traderQuote, "1094677229252755337109") + assert.equal(balances.DPPBase, "10964744523353025794") assert.equal(balances.DPPQuote, "905127507728936984519") - assert.equal(balances.maintainerBase, "1037330136920816") - assert.equal(balances.maintainerQuote, "195263018307678374") + assert.equal(balances.maintainerBase, "1037330136920815") + assert.equal(balances.maintainerQuote, "195263018307678372") // sell at R>1 and R change state await ctx.transferQuoteToDPP(trader, decimalStr("200")) await ctx.DPP.methods.sellQuote(trader).send(ctx.sendParam(trader)) balances = await ctx.getBalances(trader) - assert.equal(balances.traderBase, "11026382738483432810") - assert.equal(balances.traderQuote, "894677229252755337107") + assert.equal(balances.traderBase, "11026382738483432812") + assert.equal(balances.traderQuote, "894677229252755337109") assert.equal(balances.DPPBase, "8970581772310465451") assert.equal(balances.DPPQuote, "1105127507728936984519") - assert.equal(balances.maintainerBase, "3035489206101739") - assert.equal(balances.maintainerQuote, "195263018307678374") + assert.equal(balances.maintainerBase, "3035489206101737") + assert.equal(balances.maintainerQuote, "195263018307678372") var PMMStat = await ctx.DPP.methods.getPMMState().call() @@ -182,30 +182,30 @@ describe("DPP Trader", () => { await ctx.transferQuoteToDPP(trader, decimalStr("200")) // buy failed - await truffleAssert.reverts(ctx.DPP.methods.flashLoan("1946763594380080788", "0", trader, "0x").send(ctx.sendParam(trader)), "FLASH_LOAN_FAILED") + await truffleAssert.reverts(ctx.DPP.methods.flashLoan("1946763594380080790", "0", trader, "0x").send(ctx.sendParam(trader)), "FLASH_LOAN_FAILED") // buy succeed - await ctx.DPP.methods.flashLoan("1946763594380080787", "0", trader, "0x").send(ctx.sendParam(trader)) + await ctx.DPP.methods.flashLoan("1946763594380080789", "0", trader, "0x").send(ctx.sendParam(trader)) // trader balances assert.equal( await ctx.BASE.methods.balanceOf(trader).call(), - "11946763594380080787" + "11946763594380080789" ); // sell await ctx.transferBaseToDPP(trader, decimalStr("1")) // sell failed - await truffleAssert.reverts(ctx.DPP.methods.flashLoan("0", "103421810640399874604", trader, "0x").send(ctx.sendParam(trader)), "FLASH_LOAN_FAILED") + await truffleAssert.reverts(ctx.DPP.methods.flashLoan("0", "103421810640399874606", trader, "0x").send(ctx.sendParam(trader)), "FLASH_LOAN_FAILED") // sell succeed - await ctx.DPP.methods.flashLoan("0", "103421810640399874603", trader, "0x").send(ctx.sendParam(trader)) + await ctx.DPP.methods.flashLoan("0", "103421810640399874605", trader, "0x").send(ctx.sendParam(trader)) // trader balances assert.equal( await ctx.QUOTE.methods.balanceOf(trader).call(), - "903421810640399874603" + "903421810640399874605" ); }) @@ -213,11 +213,9 @@ describe("DPP Trader", () => { var gasPriceLimitContract = getContractWithAddress(EXTERNAL_VALUE_NAME, await ctx.DPP.methods._GAS_PRICE_LIMIT_().call()) await gasPriceLimitContract.methods.set(gweiStr("10")).send(ctx.sendParam(ctx.Deployer)) - await truffleAssert.reverts( ctx.DPP.methods.sellQuote(trader).send({ from: trader, gas: 300000, gasPrice: gweiStr("200") }), "GAS_PRICE_EXCEED" ) - }) }); }); \ No newline at end of file