Skip to content

Commit

Permalink
releaseFunds - COMPLETED
Browse files Browse the repository at this point in the history
  • Loading branch information
zajck committed Feb 20, 2023
1 parent 701720d commit 3d9fb9f
Show file tree
Hide file tree
Showing 3 changed files with 2,417 additions and 14 deletions.
19 changes: 10 additions & 9 deletions contracts/protocol/facets/ExchangeHandlerFacet.sol
Original file line number Diff line number Diff line change
Expand Up @@ -124,14 +124,15 @@ contract ExchangeHandlerFacet is IBosonExchangeHandler, BuyerBase, DisputeBase {
address tokenAddress = offer.exchangeToken;

// Get current buyer address. This is actually the seller in sequential commit. Need to do it before voucher is transferred
address _seller;
address seller;
uint256 buyerId = exchange.buyerId;
{
(, Buyer storage currentBuyer) = fetchBuyer(exchange.buyerId);
_seller = currentBuyer.wallet;
(, Buyer storage currentBuyer) = fetchBuyer(buyerId);
seller = currentBuyer.wallet;
}

if (_priceDiscovery.direction == Direction.Sell) {
require(_seller == msgSender(), NOT_VOUCHER_HOLDER);
require(seller == msgSender(), NOT_VOUCHER_HOLDER);
}

// First call price discovery and get actual price
Expand Down Expand Up @@ -174,7 +175,7 @@ contract ExchangeHandlerFacet is IBosonExchangeHandler, BuyerBase, DisputeBase {
// Update sequential commit
sequentialCommits.push(
SequentialCommit({
resellerId: exchange.buyerId,
resellerId: buyerId,
price: actualPrice,
protocolFeeAmount: protocolFeeAmount,
royaltyAmount: royaltyAmount
Expand All @@ -188,10 +189,10 @@ contract ExchangeHandlerFacet is IBosonExchangeHandler, BuyerBase, DisputeBase {
// Price discovery should send funds to the seller
// Nothing in escrow, need to pull everything from seller
if (tokenAddress == address(0)) {
FundsLib.transferFundsToProtocol(address(weth), _seller, escrowAmount);
FundsLib.transferFundsToProtocol(address(weth), seller, escrowAmount);
weth.withdraw(escrowAmount);
} else {
FundsLib.transferFundsToProtocol(tokenAddress, _seller, escrowAmount);
FundsLib.transferFundsToProtocol(tokenAddress, seller, escrowAmount);
}
}
} else {
Expand All @@ -202,7 +203,7 @@ contract ExchangeHandlerFacet is IBosonExchangeHandler, BuyerBase, DisputeBase {
}

uint256 payout = actualPrice - escrowAmount;
if (payout > 0) FundsLib.transferFundsFromProtocol(tokenAddress, payable(_seller), payout);
if (payout > 0) FundsLib.transferFundsFromProtocol(tokenAddress, payable(seller), payout);
}
}

Expand All @@ -221,7 +222,7 @@ contract ExchangeHandlerFacet is IBosonExchangeHandler, BuyerBase, DisputeBase {
if (_priceDiscovery.direction == Direction.Buy) {
return fulfilBuyOrder(_exchangeId, _exchangeToken, _priceDiscovery, _buyer, _initialSellerId);
} else {
return fulfilSellOrder(_exchangeId, _exchangeToken, _priceDiscovery, _initialSellerId);
// return fulfilSellOrder(_exchangeId, _exchangeToken, _priceDiscovery, _initialSellerId);
}
}

Expand Down
6 changes: 4 additions & 2 deletions contracts/protocol/libs/FundsLib.sol
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import { ProtocolLib } from "../libs/ProtocolLib.sol";
import { IERC20 } from "../../interfaces/IERC20.sol";
import { SafeERC20 } from "../../ext_libs/SafeERC20.sol";
import { Math } from "../../ext_libs/Math.sol";
import "hardhat/console.sol";

/**
* @title FundsLib
Expand Down Expand Up @@ -299,11 +300,12 @@ library FundsLib {

// escrowed for exchange between buyer i and i+1
{
uint256 escrowAmount = Math.max(sc.price, sc.protocolFeeAmount + sc.royaltyAmount + resellerBuyPrice) -
uint256 escrowAmount = Math.max(sc.price - sc.protocolFeeAmount - sc.royaltyAmount, resellerBuyPrice) -
resellerBuyPrice;

currentResellerAmount = (escrowAmount * effectivePriceMultiplier) / 10000 + nextResellerAmount;
nextResellerAmount = escrowAmount + nextResellerAmount - currentResellerAmount;
nextResellerAmount = escrowAmount - currentResellerAmount;
resellerBuyPrice = sc.price; // for next iteration
// uint256 nextResellerAmountTemp = escrowAmount - currentResellerAmount; // TODO: is it cheaper to make another memory variable and save one subtraction?
// currentResellerAmount += nextResellerAmount;
// nextResellerAmount = nextResellerAmountTemp;
Expand Down
Loading

0 comments on commit 3d9fb9f

Please sign in to comment.