integrate withdraw base and quote to final settlement claim
This commit is contained in:
@@ -17,6 +17,7 @@ import {Storage} from "./Storage.sol";
|
||||
import {Settlement} from "./Settlement.sol";
|
||||
import {Pricing} from "./Pricing.sol";
|
||||
|
||||
|
||||
/**
|
||||
* @title LiquidityProvider
|
||||
* @author DODO Breeder
|
||||
@@ -58,6 +59,11 @@ contract LiquidityProvider is Storage, Pricing, Settlement {
|
||||
_;
|
||||
}
|
||||
|
||||
modifier dodoNotClosed() {
|
||||
require(!_CLOSED_, "DODO_CLOSED");
|
||||
_;
|
||||
}
|
||||
|
||||
// ============ Routine Functions ============
|
||||
|
||||
function withdrawBase(uint256 amount) external returns (uint256) {
|
||||
@@ -138,7 +144,12 @@ contract LiquidityProvider is Storage, Pricing, Settlement {
|
||||
|
||||
// ============ Withdraw Functions ============
|
||||
|
||||
function withdrawQuoteTo(address to, uint256 amount) public preventReentrant returns (uint256) {
|
||||
function withdrawQuoteTo(address to, uint256 amount)
|
||||
public
|
||||
preventReentrant
|
||||
dodoNotClosed
|
||||
returns (uint256)
|
||||
{
|
||||
// calculate capital
|
||||
(, uint256 quoteTarget) = getExpectedTarget();
|
||||
uint256 totalQuoteCapital = getTotalQuoteCapital();
|
||||
@@ -166,7 +177,12 @@ contract LiquidityProvider is Storage, Pricing, Settlement {
|
||||
return amount.sub(penalty);
|
||||
}
|
||||
|
||||
function withdrawBaseTo(address to, uint256 amount) public preventReentrant returns (uint256) {
|
||||
function withdrawBaseTo(address to, uint256 amount)
|
||||
public
|
||||
preventReentrant
|
||||
dodoNotClosed
|
||||
returns (uint256)
|
||||
{
|
||||
// calculate capital
|
||||
(uint256 baseTarget, ) = getExpectedTarget();
|
||||
uint256 totalBaseCapital = getTotalBaseCapital();
|
||||
@@ -196,7 +212,12 @@ contract LiquidityProvider is Storage, Pricing, Settlement {
|
||||
|
||||
// ============ Withdraw all Functions ============
|
||||
|
||||
function withdrawAllQuoteTo(address to) public preventReentrant returns (uint256) {
|
||||
function withdrawAllQuoteTo(address to)
|
||||
public
|
||||
preventReentrant
|
||||
dodoNotClosed
|
||||
returns (uint256)
|
||||
{
|
||||
uint256 withdrawAmount = getLpQuoteBalance(msg.sender);
|
||||
uint256 capital = getQuoteCapitalBalanceOf(msg.sender);
|
||||
|
||||
@@ -216,7 +237,7 @@ contract LiquidityProvider is Storage, Pricing, Settlement {
|
||||
return withdrawAmount.sub(penalty);
|
||||
}
|
||||
|
||||
function withdrawAllBaseTo(address to) public preventReentrant returns (uint256) {
|
||||
function withdrawAllBaseTo(address to) public preventReentrant dodoNotClosed returns (uint256) {
|
||||
uint256 withdrawAmount = getLpBaseBalance(msg.sender);
|
||||
uint256 capital = getBaseCapitalBalanceOf(msg.sender);
|
||||
|
||||
|
||||
@@ -12,6 +12,7 @@ import {SafeMath} from "../lib/SafeMath.sol";
|
||||
import {SafeERC20} from "../lib/SafeERC20.sol";
|
||||
import {DecimalMath} from "../lib/DecimalMath.sol";
|
||||
import {Types} from "../lib/Types.sol";
|
||||
import {IDODOLpToken} from "../intf/IDODOLpToken.sol";
|
||||
import {IERC20} from "../intf/IERC20.sol";
|
||||
import {Storage} from "./Storage.sol";
|
||||
|
||||
@@ -114,16 +115,27 @@ contract Settlement is Storage {
|
||||
require(_CLOSED_, "DODO_NOT_CLOSED");
|
||||
require(!_CLAIMED_[msg.sender], "ALREADY_CLAIMED");
|
||||
_CLAIMED_[msg.sender] = true;
|
||||
uint256 quoteAmount = DecimalMath.mul(
|
||||
getBaseCapitalBalanceOf(msg.sender),
|
||||
_BASE_CAPITAL_RECEIVE_QUOTE_
|
||||
);
|
||||
uint256 baseAmount = DecimalMath.mul(
|
||||
getQuoteCapitalBalanceOf(msg.sender),
|
||||
_QUOTE_CAPITAL_RECEIVE_BASE_
|
||||
|
||||
uint256 quoteCapital = getQuoteCapitalBalanceOf(msg.sender);
|
||||
uint256 baseCapital = getBaseCapitalBalanceOf(msg.sender);
|
||||
|
||||
uint256 quoteAmount = _TARGET_QUOTE_TOKEN_AMOUNT_.mul(quoteCapital).div(
|
||||
getTotalQuoteCapital()
|
||||
);
|
||||
uint256 baseAmount = _TARGET_BASE_TOKEN_AMOUNT_.mul(baseCapital).div(getTotalBaseCapital());
|
||||
|
||||
_TARGET_QUOTE_TOKEN_AMOUNT_ = _TARGET_QUOTE_TOKEN_AMOUNT_.sub(quoteAmount);
|
||||
_TARGET_BASE_TOKEN_AMOUNT_ = _TARGET_BASE_TOKEN_AMOUNT_.sub(baseAmount);
|
||||
|
||||
quoteAmount = quoteAmount.add(DecimalMath.mul(baseCapital, _BASE_CAPITAL_RECEIVE_QUOTE_));
|
||||
baseAmount = baseAmount.add(DecimalMath.mul(quoteCapital, _QUOTE_CAPITAL_RECEIVE_BASE_));
|
||||
|
||||
_baseTokenTransferOut(msg.sender, baseAmount);
|
||||
_quoteTokenTransferOut(msg.sender, quoteAmount);
|
||||
|
||||
IDODOLpToken(_BASE_CAPITAL_TOKEN_).burn(msg.sender, baseCapital);
|
||||
IDODOLpToken(_QUOTE_CAPITAL_TOKEN_).burn(msg.sender, quoteCapital);
|
||||
|
||||
emit ClaimAssets(msg.sender, baseAmount, quoteAmount);
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -327,8 +327,6 @@ describe("Admin", () => {
|
||||
.send(ctx.sendParam(ctx.Deployer));
|
||||
|
||||
await ctx.DODO.methods.claimAssets().send(ctx.sendParam(lp1));
|
||||
await ctx.DODO.methods.withdrawAllBase().send(ctx.sendParam(lp1));
|
||||
await ctx.DODO.methods.withdrawAllQuote().send(ctx.sendParam(lp1));
|
||||
|
||||
assert.equal(
|
||||
await ctx.BASE.methods.balanceOf(lp1).call(),
|
||||
@@ -357,16 +355,7 @@ describe("Admin", () => {
|
||||
assert.equal(await ctx.DODO.methods._R_STATUS_().call(), "0");
|
||||
|
||||
await ctx.DODO.methods.claimAssets().send(ctx.sendParam(lp1));
|
||||
assert.equal(
|
||||
await ctx.BASE.methods.balanceOf(lp1).call(),
|
||||
decimalStr("90")
|
||||
);
|
||||
assert.equal(
|
||||
await ctx.QUOTE.methods.balanceOf(lp1).call(),
|
||||
"9551951805416248746110"
|
||||
);
|
||||
await ctx.DODO.methods.withdrawAllBase().send(ctx.sendParam(lp1));
|
||||
await ctx.DODO.methods.withdrawAllQuote().send(ctx.sendParam(lp1));
|
||||
|
||||
assert.equal(
|
||||
await ctx.BASE.methods.balanceOf(lp1).call(),
|
||||
decimalStr("94.995")
|
||||
@@ -394,16 +383,7 @@ describe("Admin", () => {
|
||||
assert.equal(await ctx.DODO.methods._R_STATUS_().call(), "0");
|
||||
|
||||
await ctx.DODO.methods.claimAssets().send(ctx.sendParam(lp1));
|
||||
assert.equal(
|
||||
await ctx.BASE.methods.balanceOf(lp1).call(),
|
||||
decimalStr("95")
|
||||
);
|
||||
assert.equal(
|
||||
await ctx.QUOTE.methods.balanceOf(lp1).call(),
|
||||
decimalStr("9000")
|
||||
);
|
||||
await ctx.DODO.methods.withdrawAllBase().send(ctx.sendParam(lp1));
|
||||
await ctx.DODO.methods.withdrawAllQuote().send(ctx.sendParam(lp1));
|
||||
|
||||
assert.equal(
|
||||
await ctx.BASE.methods.balanceOf(lp1).call(),
|
||||
decimalStr("105")
|
||||
@@ -434,9 +414,18 @@ describe("Admin", () => {
|
||||
.send(ctx.sendParam(ctx.Deployer));
|
||||
await assert.rejects(
|
||||
ctx.DODO.methods.finalSettlement().send(ctx.sendParam(ctx.Deployer)),
|
||||
/ DODO_CLOSED/
|
||||
/DODO_CLOSED/
|
||||
);
|
||||
|
||||
await assert.rejects(
|
||||
ctx.DODO.methods.withdrawAllBase().send(ctx.sendParam(lp1)),
|
||||
/DODO_CLOSED/
|
||||
)
|
||||
await assert.rejects(
|
||||
ctx.DODO.methods.withdrawAllQuote().send(ctx.sendParam(lp1)),
|
||||
/DODO_CLOSED/
|
||||
)
|
||||
|
||||
await ctx.DODO.methods.claimAssets().send(ctx.sendParam(lp2));
|
||||
await assert.rejects(
|
||||
ctx.DODO.methods.claimAssets().send(ctx.sendParam(lp2)),
|
||||
|
||||
Reference in New Issue
Block a user