Skip to content

Commit

Permalink
rename erc20token to enabledPaymentMethod
Browse files Browse the repository at this point in the history
  • Loading branch information
miquelcabot committed Dec 15, 2022
1 parent e57fbf7 commit d325d32
Showing 1 changed file with 35 additions and 24 deletions.
59 changes: 35 additions & 24 deletions contracts/dex/PaymentGateway.sol
Expand Up @@ -34,7 +34,7 @@ abstract contract PaymentGateway is Ownable {
address public masaToken; // MASA. It also needs to be enabled as payment method, if we want to pay in MASA

// enabled payment methods: ETH and ERC20 tokens
mapping(address => bool) public erc20token;
mapping(address => bool) public enabledPaymentMethod;
address[] public enabledPaymentMethods;

address public reserveWallet;
Expand Down Expand Up @@ -102,28 +102,33 @@ abstract contract PaymentGateway is Ownable {
masaToken = _masaToken;
}

/// @notice Adds a new ERC20 token as a valid payment method
/// @notice Adds a new token as a valid payment method
/// @dev The caller must have the owner to call this function
/// @param _erc20token New ERC20 token to add
function addErc20Token(address _erc20token) external onlyOwner {
require(_erc20token != address(0), "ZERO_ADDRESS");
require(!erc20token[_erc20token], "ALREADY_ADDED");
/// @param _paymentMethod New token to add
function enablePaymentMethod(address _paymentMethod) external onlyOwner {
require(_paymentMethod != address(0), "ZERO_ADDRESS");
require(!enabledPaymentMethod[_paymentMethod], "ALREADY_ADDED");

erc20token[_erc20token] = true;
enabledPaymentMethods.push(_erc20token);
enabledPaymentMethod[_paymentMethod] = true;
enabledPaymentMethods.push(_paymentMethod);
}

/// @notice Removes an ERC20 token as a valid payment method
/// @notice Removes a token as a valid payment method
/// @dev The caller must have the owner to call this function
/// @param _erc20token ERC20 token to remove
function removeErc20Token(address _erc20token) external onlyOwner {
require(_erc20token != address(0), "ZERO_ADDRESS");
require(erc20token[_erc20token], "NOT_EXISITING_ERC20TOKEN");
/// @param _paymentMethod Token to remove
function disablePaymentMethod(address _paymentMethod) external onlyOwner {
require(_paymentMethod != address(0), "ZERO_ADDRESS");
require(
enabledPaymentMethod[_paymentMethod],
"NOT_EXISITING_ERC20TOKEN"
);

erc20token[_erc20token] = false;
enabledPaymentMethod[_paymentMethod] = false;
for (uint256 i = 0; i < enabledPaymentMethods.length; i++) {
if (enabledPaymentMethods[i] == _erc20token) {
enabledPaymentMethods[i] = enabledPaymentMethods[enabledPaymentMethods.length - 1];
if (enabledPaymentMethods[i] == _paymentMethod) {
enabledPaymentMethods[i] = enabledPaymentMethods[
enabledPaymentMethods.length - 1
];
enabledPaymentMethods.pop();
break;
}
Expand All @@ -143,10 +148,14 @@ abstract contract PaymentGateway is Ownable {

/* ========== VIEWS ===================================================== */

/// @notice Returns all available ERC 20 tokens
/// @dev Returns the address of all available ERC 20 tokens
/// @return Array of all enabled ERC20 tokens
function getEnabledPaymentMethods() external view returns (address[] memory) {
/// @notice Returns all available payment methods
/// @dev Returns the address of all available payment methods
/// @return Array of all enabled payment methods
function getEnabledPaymentMethods()
external
view
returns (address[] memory)
{
return enabledPaymentMethods;
}

Expand All @@ -158,7 +167,7 @@ abstract contract PaymentGateway is Ownable {
returns (uint256)
{
require(
(token == wrappedNativeToken || erc20token[token]) &&
(token == wrappedNativeToken || enabledPaymentMethod[token]) &&
token != stableCoin,
"INVALID_TOKEN"
);
Expand Down Expand Up @@ -189,14 +198,16 @@ abstract contract PaymentGateway is Ownable {
(success, ) = payable(msg.sender).call{value: refund}("");
require(success);
}
} else if (paymentMethod == stableCoin && erc20token[paymentMethod]) {
} else if (
paymentMethod == stableCoin && enabledPaymentMethod[paymentMethod]
) {
// USDC
IERC20(paymentMethod).safeTransferFrom(
msg.sender,
reserveWallet,
amountInStableCoin
);
} else if (erc20token[paymentMethod]) {
} else if (enabledPaymentMethod[paymentMethod]) {
// ERC20 token, including MASA
uint256 swapAmout = _convertFromStableCoin(
paymentMethod,
Expand All @@ -218,7 +229,7 @@ abstract contract PaymentGateway is Ownable {
/// @param amountInMASA Price to be paid in MASA
function _payWithMASA(uint256 amountInMASA) internal {
// MASA
require(erc20token[masaToken], "INVALID_PAYMENT_METHOD");
require(enabledPaymentMethod[masaToken], "INVALID_PAYMENT_METHOD");

IERC20(masaToken).safeTransferFrom(
msg.sender,
Expand Down

0 comments on commit d325d32

Please sign in to comment.