Skip to content

Commit

Permalink
bugfix: sp update when withdraw
Browse files Browse the repository at this point in the history
  • Loading branch information
artistic709 committed Nov 5, 2023
1 parent d5ad1de commit bf4121f
Showing 1 changed file with 8 additions and 6 deletions.
14 changes: 8 additions & 6 deletions src/DysonToGo.sol
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,8 @@ contract DysonToGo is IERC721Receiver {
uint public adminFeeRatio;
uint public spPool;
uint public dysonPool;
uint public spPending;
uint public spSnapshot; //sp in farm at last update
uint public spPending; //sp to be added to pool
uint public lastUpdateTime;
uint public updatePeriod = 5 hours; //depends on agent tier
uint private unlocked = 1;
Expand Down Expand Up @@ -175,7 +176,7 @@ contract DysonToGo is IERC721Receiver {

function update() external lock returns (uint sp) {
sp = _update();
spPending = sp;
spSnapshot = sp;
}

function _update() internal returns (uint spInFarm) {
Expand All @@ -184,8 +185,9 @@ contract DysonToGo is IERC721Receiver {
lastUpdateTime = block.timestamp;
}
spInFarm = IFarm(FARM).balanceOf(address(this));
if(spInFarm < spPending) {
if(spInFarm < spSnapshot) {
spPool += spPending;
spPending = 0;
uint newBalance = IERC20(DYSON).balanceOf(address(this));
if (newBalance > dysonPool) {
uint dysonAdded = newBalance - dysonPool;
Expand All @@ -209,7 +211,8 @@ contract DysonToGo is IERC721Receiver {
output = IPair(pair).deposit1(address(this), input, minOutput, time);
uint spAfter = IFarm(FARM).balanceOf(address(this));
uint spAdded = spAfter - spBefore;
spPending = spAfter;
spPending += spAdded;
spSnapshot = spAfter;
Position storage position = positions[pair][msg.sender][positionsCount[pair][msg.sender]];
position.index = noteCount;
position.spAmount = spAdded;
Expand All @@ -229,14 +232,14 @@ contract DysonToGo is IERC721Receiver {
}

function _claimDyson(address to, uint sp) internal returns (uint dysonAmount) {
spSnapshot = _update();
dysonAmount = dysonPool * sp / (spPool + sp);
spPool -= sp;
dysonPool -= dysonAmount;
DYSON.safeTransfer(to, dysonAmount);
}

function withdraw(address pair, uint index, address to) external lock returns (uint token0Amt, uint token1Amt, uint dysonAmt) {
_update();
Position storage position = positions[pair][msg.sender][index];
require(position.hasDepositedAsset, "not deposited");
position.hasDepositedAsset = false;
Expand All @@ -246,7 +249,6 @@ contract DysonToGo is IERC721Receiver {
}

function withdrawETH(address pair, uint index, address to) external lock returns (uint token0Amt, uint token1Amt, uint dysonAmt) {
_update();
Position storage position = positions[pair][msg.sender][index];
require(position.hasDepositedAsset, "not deposited");
position.hasDepositedAsset = false;
Expand Down

0 comments on commit bf4121f

Please sign in to comment.