Skip to content

Commit

Permalink
Add fee function for AD action
Browse files Browse the repository at this point in the history
1
  • Loading branch information
MichaelKim20 committed Dec 28, 2024
1 parent 957752f commit 28ef415
Show file tree
Hide file tree
Showing 16 changed files with 296 additions and 56 deletions.
134 changes: 119 additions & 15 deletions packages/contracts/contracts/controllers/LoyaltyProvider.sol
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,8 @@ contract LoyaltyProvider is LoyaltyProviderStorage, Initializable, OwnableUpgrad
function initialize(
address _validatorAddress,
address _linkAddress,
address _currencyRateAddress
address _currencyRateAddress,
address _protocolFeeAddress
) external initializer {
__UUPSUpgradeable_init();
__Ownable_init_unchained();
Expand All @@ -75,6 +76,10 @@ contract LoyaltyProvider is LoyaltyProviderStorage, Initializable, OwnableUpgrad
currencyRateContract = ICurrencyRate(_currencyRateAddress);
isSetLedger = false;
isSetShop = false;

adActionProtocolFeeAccount = _protocolFeeAddress;
adActionAgentFee = DEFAULT_AD_ACTION_AGENT_FEE;
adActionProtocolFee = DEFAULT_AD_ACTION_PROTOCOL_FEE;
}

/// @notice 원장 컨트랙트를 등록한다.
Expand Down Expand Up @@ -207,7 +212,8 @@ contract LoyaltyProvider is LoyaltyProviderStorage, Initializable, OwnableUpgrad
data.currency,
data.purchaseId,
data.shopId,
data.sender
data.sender,
DMS.TAG_PROVIDE_PURCHASE
);
shopContract.addProvidedAmount(
data.shopId,
Expand All @@ -224,7 +230,8 @@ contract LoyaltyProvider is LoyaltyProviderStorage, Initializable, OwnableUpgrad
data.currency,
data.purchaseId,
data.shopId,
data.sender
data.sender,
DMS.TAG_PROVIDE_PURCHASE
);
} else {
ledgerContract.providePoint(
Expand All @@ -234,7 +241,8 @@ contract LoyaltyProvider is LoyaltyProviderStorage, Initializable, OwnableUpgrad
data.currency,
data.purchaseId,
data.shopId,
data.sender
data.sender,
DMS.TAG_PROVIDE_PURCHASE
);
}
shopContract.addProvidedAmount(
Expand Down Expand Up @@ -282,8 +290,10 @@ contract LoyaltyProvider is LoyaltyProviderStorage, Initializable, OwnableUpgrad
);

address sender;
bool isAgent;
if (recurve1 == _provider) {
sender = _provider;
isAgent = false;
} else {
address agent = ledgerContract.provisionAgentOf(_provider);
require(agent != address(0x0), "1501");
Expand All @@ -296,23 +306,54 @@ contract LoyaltyProvider is LoyaltyProviderStorage, Initializable, OwnableUpgrad
);
require(recurve2 == agent, "1501");
sender = agent;
isAgent = true;
}

address provider = _provider;
ledgerContract.providePoint(
_receiver,
_point,
_point,
DMS.DEFAULT_CURRENCY_SYMBOL,
"",
bytes32(0x0),
_provider
provider,
DMS.TAG_PROVIDE_AD
);

uint256 agentFee = 0;
if (isAgent) {
agentFee = DMS.zeroGWEI((_point * adActionAgentFee) / 10000);
ledgerContract.providePoint(
sender,
agentFee,
agentFee,
DMS.DEFAULT_CURRENCY_SYMBOL,
"",
bytes32(0x0),
provider,
DMS.TAG_PROVIDE_AD_FEE
);
}

uint256 protocolFee = DMS.zeroGWEI((_point * adActionProtocolFee) / 10000);
ledgerContract.providePoint(
adActionProtocolFeeAccount,
protocolFee,
protocolFee,
DMS.DEFAULT_CURRENCY_SYMBOL,
"",
bytes32(0x0),
provider,
DMS.TAG_PROVIDE_AD_PROTOCOL_FEE
);

ledgerContract.increaseNonce(sender);

uint256 amountToken = currencyRateContract.convertPointToToken(_point);
uint256 balancePoint = ledgerContract.pointBalanceOf(_provider);
uint256 balanceToken = ledgerContract.tokenBalanceOf(_provider);
emit ProvidedLoyaltyPointToAddress(_provider, _receiver, _point, amountToken, balancePoint, balanceToken);
uint256 amountToken = currencyRateContract.convertPointToToken(_point + agentFee + protocolFee);
uint256 balancePoint = ledgerContract.pointBalanceOf(provider);
uint256 balanceToken = ledgerContract.tokenBalanceOf(provider);
emit ProvidedLoyaltyPointToAddress(provider, _receiver, _point, amountToken, balancePoint, balanceToken);
}

function provideToPhone(address _provider, bytes32 _phoneHash, uint256 _point, bytes calldata _signature) external {
Expand All @@ -328,8 +369,10 @@ contract LoyaltyProvider is LoyaltyProviderStorage, Initializable, OwnableUpgrad
);

address sender;
bool isAgent;
if (recurve1 == _provider) {
sender = _provider;
isAgent = false;
} else {
address agent = ledgerContract.provisionAgentOf(_provider);
require(agent != address(0x0), "1501");
Expand All @@ -342,8 +385,10 @@ contract LoyaltyProvider is LoyaltyProviderStorage, Initializable, OwnableUpgrad
);
require(recurve2 == agent, "1501");
sender = agent;
isAgent = true;
}

address provider = _provider;
address receiver = linkContract.toAddress(_phoneHash);
if (receiver == address(0x00)) {
ledgerContract.provideUnPayablePoint(
Expand All @@ -353,7 +398,8 @@ contract LoyaltyProvider is LoyaltyProviderStorage, Initializable, OwnableUpgrad
DMS.DEFAULT_CURRENCY_SYMBOL,
"",
bytes32(0x0),
_provider
provider,
DMS.TAG_PROVIDE_AD
);
} else {
ledgerContract.providePoint(
Expand All @@ -363,14 +409,72 @@ contract LoyaltyProvider is LoyaltyProviderStorage, Initializable, OwnableUpgrad
DMS.DEFAULT_CURRENCY_SYMBOL,
"",
bytes32(0x0),
_provider
provider,
DMS.TAG_PROVIDE_AD
);
}

uint256 agentFee = 0;
if (isAgent) {
agentFee = DMS.zeroGWEI((_point * adActionAgentFee) / 10000);
ledgerContract.providePoint(
sender,
agentFee,
agentFee,
DMS.DEFAULT_CURRENCY_SYMBOL,
"",
bytes32(0x0),
provider,
DMS.TAG_PROVIDE_AD_FEE
);
}

uint256 protocolFee = DMS.zeroGWEI((_point * adActionProtocolFee) / 10000);
ledgerContract.providePoint(
adActionProtocolFeeAccount,
protocolFee,
protocolFee,
DMS.DEFAULT_CURRENCY_SYMBOL,
"",
bytes32(0x0),
provider,
DMS.TAG_PROVIDE_AD_PROTOCOL_FEE
);

ledgerContract.increaseNonce(sender);

uint256 amountToken = currencyRateContract.convertPointToToken(_point);
uint256 balancePoint = ledgerContract.pointBalanceOf(_provider);
uint256 balanceToken = ledgerContract.tokenBalanceOf(_provider);
emit ProvidedLoyaltyPointToPhone(_provider, _phoneHash, _point, amountToken, balancePoint, balanceToken);
uint256 amountToken = currencyRateContract.convertPointToToken(_point + agentFee + protocolFee);
uint256 balancePoint = ledgerContract.pointBalanceOf(provider);
uint256 balanceToken = ledgerContract.tokenBalanceOf(provider);
emit ProvidedLoyaltyPointToPhone(provider, _phoneHash, _point, amountToken, balancePoint, balanceToken);
}

function setAdActionAgentFee(uint32 _fee) external {
require(_fee <= MAX_AD_ACTION_AGENT_FEE, "1521");
require(_msgSender() == owner(), "1050");
adActionAgentFee = _fee;
}

function getAdActionAgentFee() external view returns (uint32) {
return adActionAgentFee;
}

function setAdActionProtocolFee(uint32 _fee) external {
require(_fee <= MAX_AD_ACTION_PROTOCOL_FEE, "1521");
require(_msgSender() == owner(), "1050");
adActionProtocolFee = _fee;
}

function getAdActionProtocolFee() external view returns (uint32) {
return adActionProtocolFee;
}

function getAdActionProtocolFeeAccount() external view returns (address) {
return adActionProtocolFeeAccount;
}

function changeAdActionProtocolFeeAccount(address _account) external {
require(_msgSender() == owner(), "1050");
adActionProtocolFeeAccount = _account;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@ import "../interfaces/IShop.sol";
import "../interfaces/ILedger.sol";

contract LoyaltyProviderStorage {
uint32 public constant DEFAULT_AD_ACTION_AGENT_FEE = 200;
uint32 public constant MAX_AD_ACTION_AGENT_FEE = 500;
uint32 public constant DEFAULT_AD_ACTION_PROTOCOL_FEE = 300;
uint32 public constant MAX_AD_ACTION_PROTOCOL_FEE = 500;
IValidator internal validatorContract;
IPhoneLinkCollection internal linkContract;
ICurrencyRate internal currencyRateContract;
Expand All @@ -21,4 +25,8 @@ contract LoyaltyProviderStorage {

bool internal isSetLedger;
bool internal isSetShop;

uint32 internal adActionAgentFee;
uint32 internal adActionProtocolFee;
address internal adActionProtocolFeeAccount;
}
6 changes: 4 additions & 2 deletions packages/contracts/contracts/interfaces/ILedger.sol
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ interface ILedger {
string calldata _currency,
string calldata _purchaseId,
bytes32 _shopId,
address _sender
address _sender,
uint256 _tag
) external;

function provideUnPayablePoint(
Expand All @@ -20,7 +21,8 @@ interface ILedger {
string calldata _currency,
string calldata _purchaseId,
bytes32 _shopId,
address _sender
address _sender,
uint256 _tag
) external;

function refund(
Expand Down
36 changes: 23 additions & 13 deletions packages/contracts/contracts/ledger/Ledger.sol
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,8 @@ contract Ledger is LedgerStorage, Initializable, OwnableUpgradeable, UUPSUpgrade
bytes32 shopId,
address provider,
uint256 consumedToken,
uint256 providerBalanceToken
uint256 providerBalanceToken,
uint256 tag
);

/// @notice 포인트가 지급될 때 발생되는 이벤트
Expand All @@ -45,7 +46,8 @@ contract Ledger is LedgerStorage, Initializable, OwnableUpgradeable, UUPSUpgrade
bytes32 shopId,
address provider,
uint256 consumedToken,
uint256 providerBalanceToken
uint256 providerBalanceToken,
uint256 tag
);

event Refunded(
Expand Down Expand Up @@ -223,9 +225,10 @@ contract Ledger is LedgerStorage, Initializable, OwnableUpgradeable, UUPSUpgrade
string calldata _currency,
string calldata _purchaseId,
bytes32 _shopId,
address _sender
address _sender,
uint256 _tag
) external override onlyProvider {
_provideUnPayablePoint(_phone, _loyaltyPoint, _loyaltyValue, _currency, _purchaseId, _shopId, _sender);
_provideUnPayablePoint(_phone, _loyaltyPoint, _loyaltyValue, _currency, _purchaseId, _shopId, _sender, _tag);
}

function _provideUnPayablePoint(
Expand All @@ -235,7 +238,8 @@ contract Ledger is LedgerStorage, Initializable, OwnableUpgradeable, UUPSUpgrade
string calldata _currency,
string calldata _purchaseId,
bytes32 _shopId,
address _sender
address _sender,
uint256 _tag
) internal {
uint256 consumedToken = 0;
if (_sender == systemAccount) {
Expand All @@ -249,6 +253,7 @@ contract Ledger is LedgerStorage, Initializable, OwnableUpgradeable, UUPSUpgrade
}
uint256 balance = unPayablePointBalances[_phone];
uint256 providerTokenBalance = tokenBalances[_sender];
uint256 tag = _tag;
emit ProvidedUnPayablePoint(
_phone,
_loyaltyPoint,
Expand All @@ -259,7 +264,8 @@ contract Ledger is LedgerStorage, Initializable, OwnableUpgradeable, UUPSUpgrade
_shopId,
_sender,
consumedToken,
providerTokenBalance
providerTokenBalance,
tag
);
}

Expand All @@ -277,9 +283,10 @@ contract Ledger is LedgerStorage, Initializable, OwnableUpgradeable, UUPSUpgrade
string calldata _currency,
string calldata _purchaseId,
bytes32 _shopId,
address _sender
address _sender,
uint256 _tag
) external override onlyProvider {
_providePoint(_account, _loyaltyPoint, _loyaltyValue, _currency, _purchaseId, _shopId, _sender);
_providePoint(_account, _loyaltyPoint, _loyaltyValue, _currency, _purchaseId, _shopId, _sender, _tag);
}

function _providePoint(
Expand All @@ -289,7 +296,8 @@ contract Ledger is LedgerStorage, Initializable, OwnableUpgradeable, UUPSUpgrade
string calldata _currency,
string calldata _purchaseId,
bytes32 _shopId,
address _sender
address _sender,
uint256 _tag
) internal {
uint256 consumedToken = 0;
if (_sender == systemAccount) {
Expand All @@ -304,6 +312,7 @@ contract Ledger is LedgerStorage, Initializable, OwnableUpgradeable, UUPSUpgrade
}
uint256 balance = pointBalances[_account];
uint256 providerTokenBalance = tokenBalances[_sender];
uint256 tag = _tag;
emit ProvidedPoint(
_account,
_loyaltyPoint,
Expand All @@ -314,7 +323,8 @@ contract Ledger is LedgerStorage, Initializable, OwnableUpgradeable, UUPSUpgrade
_shopId,
_sender,
consumedToken,
providerTokenBalance
providerTokenBalance,
tag
);
}

Expand Down Expand Up @@ -395,7 +405,7 @@ contract Ledger is LedgerStorage, Initializable, OwnableUpgradeable, UUPSUpgrade
/// @param _fee % 단위 입니다.
function setPaymentFee(uint32 _fee) external override {
require(_fee <= MAX_PAYMENT_FEE, "1521");
require(_msgSender() == paymentFeeAccount, "1050");
require(_msgSender() == owner(), "1050");
paymentFee = _fee;
}

Expand Down Expand Up @@ -488,13 +498,13 @@ contract Ledger is LedgerStorage, Initializable, OwnableUpgradeable, UUPSUpgrade
}

function changeFeeAccount(address _account) external {
require(_msgSender() == paymentFeeAccount, "1050");
require(_msgSender() == owner(), "1050");

paymentFeeAccount = _account;
}

function changeProtocolFeeAccount(address _account) external {
require(_msgSender() == protocolFeeAccount, "1050");
require(_msgSender() == owner(), "1050");

protocolFeeAccount = _account;
}
Expand Down
Loading

0 comments on commit 28ef415

Please sign in to comment.