Skip to content

Commit

Permalink
[Contract] Add a function that provides loyalty payment data
Browse files Browse the repository at this point in the history
  • Loading branch information
MichaelKim20 committed Nov 7, 2023
1 parent 5c92bde commit d685986
Show file tree
Hide file tree
Showing 6 changed files with 54 additions and 42 deletions.
2 changes: 1 addition & 1 deletion packages/contracts-lib/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "dms-osx-lib",
"version": "1.1.7",
"version": "1.1.8",
"description": "",
"main": "dist/bundle-cjs.js",
"module": "dist/bundle-esm.js",
Expand Down
82 changes: 44 additions & 38 deletions packages/contracts/contracts/Ledger.sol
Original file line number Diff line number Diff line change
Expand Up @@ -73,9 +73,9 @@ contract Ledger {
address account;
uint256 timestamp;
LoyaltyType loyaltyType;
uint256 amountPoint;
uint256 amountToken;
uint256 amountValue;
uint256 paidPoint;
uint256 paidToken;
uint256 paidValue;
uint256 feePoint;
uint256 feeToken;
uint256 feeValue;
Expand Down Expand Up @@ -399,6 +399,12 @@ contract Ledger {
else return false;
}

/// @notice 로얄티(포인트/토큰)을 구매데아타를 제공하는 함수
/// @param _paymentId 지불 아이디
function loyaltyPaymentOf(bytes32 _paymentId) public view returns (LoyaltyPaymentData memory) {
return loyaltyPayments[_paymentId];
}

/// @notice 로얄티(포인트/토큰)을 구매에 사용하는 함수
/// @dev 중계서버를 통해서 호출됩니다.
function createLoyaltyPayment(LoyaltyPaymentInputData calldata _data) public {
Expand Down Expand Up @@ -431,22 +437,22 @@ contract Ledger {
/// @notice 포인트를 구매에 사용하는 함수
function _createLoyaltyPaymentPoint(LoyaltyPaymentInputData memory _data) internal {
LoyaltyPaymentInputData memory data = _data;
uint256 purchasePoint = convertCurrencyToPoint(data.amount, data.currency);
uint256 purchaseToken = convertPointToToken(purchasePoint);
uint256 paidPoint = convertCurrencyToPoint(data.amount, data.currency);
uint256 paidToken = convertPointToToken(paidPoint);
uint256 feeValue = (data.amount * fee) / 100;
uint256 feePoint = convertCurrencyToPoint(feeValue, data.currency);
uint256 feeToken = convertPointToToken(feePoint);

require(pointBalances[data.account] >= purchasePoint + feePoint, "Insufficient balance");
require(pointBalances[data.account] >= paidPoint + feePoint, "Insufficient balance");
require(tokenBalances[foundationAccount] >= feeToken, "Insufficient foundation balance");

pointBalances[data.account] -= (purchasePoint + feePoint);
pointBalances[data.account] -= (paidPoint + feePoint);

// 재단의 토큰으로 교환해 지급한다.
tokenBalances[foundationAccount] -= feeToken;
tokenBalances[feeAccount] += feeToken;

shopCollection.addUsedPoint(data.shopId, purchasePoint, data.purchaseId);
shopCollection.addUsedPoint(data.shopId, paidPoint, data.purchaseId);

uint256 settlementPoint = shopCollection.getSettlementPoint(data.shopId);
if (settlementPoint > 0) {
Expand Down Expand Up @@ -476,9 +482,9 @@ contract Ledger {
account: data.account,
timestamp: block.timestamp,
loyaltyType: LoyaltyType.POINT,
amountPoint: purchasePoint,
amountToken: purchaseToken,
amountValue: data.amount,
paidPoint: paidPoint,
paidToken: paidToken,
paidValue: data.amount,
feePoint: feePoint,
feeToken: feeToken,
feeValue: feeValue,
Expand All @@ -493,9 +499,9 @@ contract Ledger {
payData.account,
payData.shopId,
payData.loyaltyType,
payData.amountPoint,
payData.amountToken,
payData.amountValue,
payData.paidPoint,
payData.paidToken,
payData.paidValue,
payData.feePoint,
payData.feeToken,
payData.feeValue,
Expand All @@ -507,19 +513,19 @@ contract Ledger {
function _createLoyaltyPaymentToken(LoyaltyPaymentInputData memory _data) internal {
LoyaltyPaymentInputData memory data = _data;

uint256 purchasePoint = convertCurrencyToPoint(data.amount, data.currency);
uint256 purchaseToken = convertPointToToken(purchasePoint);
uint256 paidPoint = convertCurrencyToPoint(data.amount, data.currency);
uint256 paidToken = convertPointToToken(paidPoint);
uint256 feeValue = (data.amount * fee) / 100;
uint256 feePoint = convertCurrencyToPoint(feeValue, data.currency);
uint256 feeToken = convertPointToToken(feePoint);

require(tokenBalances[data.account] >= purchaseToken + feeToken, "Insufficient balance");
require(tokenBalances[data.account] >= paidToken + feeToken, "Insufficient balance");

tokenBalances[data.account] -= (purchaseToken + feeToken);
tokenBalances[foundationAccount] += purchaseToken;
tokenBalances[data.account] -= (paidToken + feeToken);
tokenBalances[foundationAccount] += paidToken;
tokenBalances[feeAccount] += feeToken;

shopCollection.addUsedPoint(data.shopId, purchasePoint, data.purchaseId);
shopCollection.addUsedPoint(data.shopId, paidPoint, data.purchaseId);

uint256 settlementPoint = shopCollection.getSettlementPoint(data.shopId);
if (settlementPoint > 0) {
Expand Down Expand Up @@ -549,9 +555,9 @@ contract Ledger {
account: data.account,
timestamp: block.timestamp,
loyaltyType: LoyaltyType.TOKEN,
amountPoint: purchasePoint,
amountToken: purchaseToken,
amountValue: data.amount,
paidPoint: paidPoint,
paidToken: paidToken,
paidValue: data.amount,
feePoint: feePoint,
feeToken: feeToken,
feeValue: feeValue,
Expand All @@ -566,9 +572,9 @@ contract Ledger {
payData.account,
payData.shopId,
payData.loyaltyType,
payData.amountPoint,
payData.amountToken,
payData.amountValue,
payData.paidPoint,
payData.paidToken,
payData.paidValue,
payData.feePoint,
payData.feeToken,
payData.feeValue,
Expand Down Expand Up @@ -604,18 +610,18 @@ contract Ledger {
if (tokenBalances[feeAccount] >= payData.feeToken) {
tokenBalances[foundationAccount] += payData.feeToken;
tokenBalances[feeAccount] -= payData.feeToken;
pointBalances[data.account] += (payData.amountPoint + payData.feePoint);
shopCollection.subUsedPoint(payData.shopId, payData.amountPoint, payData.purchaseId);
pointBalances[data.account] += (payData.paidPoint + payData.feePoint);
shopCollection.subUsedPoint(payData.shopId, payData.paidPoint, payData.purchaseId);
emit CancelledLoyaltyPayment(
payData.paymentId,
payData.purchaseId,
payData.currency,
payData.account,
payData.shopId,
LoyaltyType.POINT,
payData.amountPoint,
payData.amountToken,
payData.amountValue,
payData.paidPoint,
payData.paidToken,
payData.paidValue,
payData.feePoint,
payData.feeToken,
payData.feeValue,
Expand All @@ -626,23 +632,23 @@ contract Ledger {
}
} else {
if (
(tokenBalances[foundationAccount] >= payData.amountToken) &&
(tokenBalances[foundationAccount] >= payData.paidToken) &&
(tokenBalances[feeAccount] >= payData.feeToken)
) {
tokenBalances[foundationAccount] -= payData.amountToken;
tokenBalances[foundationAccount] -= payData.paidToken;
tokenBalances[feeAccount] -= payData.feeToken;
pointBalances[data.account] += (payData.amountToken + payData.feeToken);
shopCollection.subUsedPoint(payData.shopId, payData.amountPoint, payData.purchaseId);
pointBalances[data.account] += (payData.paidToken + payData.feeToken);
shopCollection.subUsedPoint(payData.shopId, payData.paidPoint, payData.purchaseId);
emit CancelledLoyaltyPayment(
payData.paymentId,
payData.purchaseId,
payData.currency,
payData.account,
payData.shopId,
LoyaltyType.TOKEN,
payData.amountPoint,
payData.amountToken,
payData.amountValue,
payData.paidPoint,
payData.paidToken,
payData.paidValue,
payData.feePoint,
payData.feeToken,
payData.feeValue,
Expand Down
2 changes: 1 addition & 1 deletion packages/contracts/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "dms-osx-artifacts",
"version": "1.1.7",
"version": "1.1.8",
"description": "Smart contracts that decentralized point systems",
"files": [
"**/*.sol"
Expand Down
6 changes: 6 additions & 0 deletions packages/contracts/test/03-Ledger.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1239,6 +1239,12 @@ describe("Test for Ledger", () => {
});
const newFeeBalance = await ledgerContract.tokenBalanceOf(fee.address);
expect(newFeeBalance).to.deep.equal(oldFeeBalance.add(feeToken));

const paymentData = await ledgerContract.loyaltyPaymentOf(paymentId);
expect(paymentData.paymentId).to.deep.equal(paymentId);
expect(paymentData.purchaseId).to.deep.equal(purchase.purchaseId);
expect(paymentData.account).to.deep.equal(userWallets[purchase.userIndex].address);
expect(paymentData.paidValue).to.deep.equal(purchaseAmount);
});
});

Expand Down
2 changes: 1 addition & 1 deletion packages/faker/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@
"assert": "^2.0.0",
"axios": "^0.26.0",
"chai": "^4.3.7",
"dms-osx-artifacts": "^1.1.7",
"dms-osx-artifacts": "^1.1.8",
"dotenv": "^10.0.0",
"ethereum-waffle": "^4.0.10",
"ethers": "^5.7.0",
Expand Down
2 changes: 1 addition & 1 deletion packages/relay/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@
"chai-http": "^4.3.7",
"cors": "^2.8.5",
"del-osx-artifacts": "^1.1.1",
"dms-osx-artifacts": "^1.1.7",
"dms-osx-artifacts": "^1.1.8",
"dotenv": "^10.0.0",
"ethereum-waffle": "^4.0.10",
"ethers": "^5.7.0",
Expand Down

0 comments on commit d685986

Please sign in to comment.