Skip to content

Commit

Permalink
Withdraw funds - support DR withdrawals
Browse files Browse the repository at this point in the history
  • Loading branch information
zajck committed May 17, 2023
1 parent 3511687 commit 373d7a1
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 28 deletions.
1 change: 1 addition & 0 deletions contracts/domain/BosonConstants.sol
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,7 @@ string constant INSUFFICIENT_AVAILABLE_FUNDS = "Insufficient available funds";
string constant NATIVE_NOT_ALLOWED = "Transfer of native currency not allowed";
string constant DR_FEE_NOT_RECEIVED = "DR fee not received";
string constant SELLER_NOT_COVERED = "Seller not covered";
string constant INVALID_ENTITY_ID = "Invalid entity id";

// Revert Reasons: Meta-Transactions related
string constant NONCE_USED_ALREADY = "Nonce used already";
Expand Down
2 changes: 1 addition & 1 deletion contracts/mock/MockDRFeeMutualizer.sol
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ contract MockDRFeeMutualizer is IDRFeeMutualizer {
*
*/
function isSellerCovered(address, address, uint256, address, bytes calldata) external pure returns (bool) {
true;
return true;
}

/**
Expand Down
64 changes: 37 additions & 27 deletions contracts/protocol/facets/FundsHandlerFacet.sol
Original file line number Diff line number Diff line change
Expand Up @@ -92,34 +92,8 @@ contract FundsHandlerFacet is IBosonFundsHandler, ProtocolBase {
address[] calldata _tokenList,
uint256[] calldata _tokenAmounts
) external override fundsNotPaused nonReentrant {
address payable sender = payable(msgSender());

// Address that will receive the funds
address payable destinationAddress;

// First check if the caller is a buyer
(bool exists, uint256 callerId) = getBuyerIdByWallet(sender);
if (exists && callerId == _entityId) {
// Caller is a buyer
destinationAddress = sender;
} else {
// Check if the caller is a clerk
(exists, callerId) = getSellerIdByClerk(sender);
if (exists && callerId == _entityId) {
// Caller is a clerk. In this case funds are transferred to the treasury address
(, Seller storage seller, ) = fetchSeller(callerId);
destinationAddress = seller.treasury;
} else {
(exists, callerId) = getAgentIdByWallet(sender);
if (exists && callerId == _entityId) {
// Caller is an agent
destinationAddress = sender;
} else {
// In this branch, caller is neither buyer, clerk or agent or does not match the _entityId
revert(NOT_AUTHORIZED);
}
}
}
address payable destinationAddress = getDestinationAddress(_entityId);

withdrawFundsInternal(destinationAddress, _entityId, _tokenList, _tokenAmounts);
}
Expand Down Expand Up @@ -257,4 +231,40 @@ contract FundsHandlerFacet is IBosonFundsHandler, ProtocolBase {
}
}
}

/**
* @notice Checks if message sender is associated with the entity id and returns the address that will receive the funds.
*
* Reverts if:
* - Entity id is 0
* - Caller is not associated with the entity id
*
* @param _entityId - id of entity for which funds should be withdrawn
*/
function getDestinationAddress(uint256 _entityId) internal view returns (address payable _destinationAddress) {
require(_entityId != 0, INVALID_ENTITY_ID);

address payable sender = payable(msgSender());
uint256 callerId;

(, callerId) = getBuyerIdByWallet(sender);
if (callerId == _entityId) return sender;

(, callerId) = getSellerIdByClerk(sender);
if (callerId == _entityId) {
(, Seller storage seller, ) = fetchSeller(callerId);
return seller.treasury;
}

(, callerId) = getDisputeResolverIdByAssistant(sender);
if (callerId == _entityId) {
(, DisputeResolver storage disputeResolver, ) = fetchDisputeResolver(callerId);
return disputeResolver.treasury;
}

(, callerId) = getAgentIdByWallet(sender);
if (callerId == _entityId) return sender;

revert(NOT_AUTHORIZED);
}
}

0 comments on commit 373d7a1

Please sign in to comment.