/
CouponMarketplaceVia.sol
161 lines (117 loc) 路 5.93 KB
/
CouponMarketplaceVia.sol
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
pragma solidity ^0.5.0;
import "./SnowflakeVia.sol";
import "./interfaces/SnowflakeResolverInterface.sol";
import "./zeppelin/math/SafeMath.sol";
import "./interfaces/CouponMarketplaceResolverInterface.sol";
import "./interfaces/SnowflakeInterface.sol";
import "./resolvers/CouponMarketplaceResolver.sol";
import "./ein/util/SnowflakeEINOwnable.sol";
import "./interfaces/marketplace/CouponInterface.sol";
import "./interfaces/marketplace/ItemInterface.sol";
import "./marketplace/features/CouponFeature.sol";
import "./marketplace/features/ItemFeature.sol";
contract CouponMarketplaceVia is SnowflakeVia, SnowflakeEINOwnable {
//We may need to define this in a seperate file... uncertain if this will quite work being in two different locations
// This is now defined within the CouponMarketplaceResolverInterface.sol, let's test this...
// enum CouponType { AMOUNT_OFF, PERCENTAGE_OFF, BUY_X_QTY_GET_Y_FREE , BUY_X_QTY_FOR_Y_AMNT }
using SafeMath for uint256;
address public CouponMarketplaceResolverAddress;
constructor(address _snowflakeAddress) SnowflakeVia(_snowflakeAddress) public {
//Constructing parents
_constructSnowflakeEINOwnable(_snowflakeAddress);
//Regular constructor
//setSnowflakeAddress(_snowflakeAddress);
}
// end recipient is an EIN
function snowflakeCall(
address /* resolver */,
uint /* einFrom */,
uint /* einTo */,
uint /* amount */,
bytes memory snowflakeCallBytes
) public senderIsSnowflake {
address(this).call(snowflakeCallBytes);
}
//Name of this function is perhaps a little misleading, since amount has already been transferred, we're just calcing coupon here
//TODO: Should we have the NeoCouponMarketplaceResolverAddress exist, or just take the address resolver passed here? Completely forgot we were give this, and now this param is being unused
function processTransaction(/*address resolver, */uint itemID, uint einBuyer, uint einSeller, uint amount, uint couponID) public senderIsSnowflake returns (bool) {
//Initialize NeoCouponMarketplaceResolverAddress
CouponMarketplaceResolver mktResolver = CouponMarketplaceResolver(CouponMarketplaceResolverAddress);
ItemFeature itemFeature = ItemFeature(mktResolver.ItemFeatureAddress());
//Initialize Snowflake
SnowflakeInterface snowflake = SnowflakeInterface(snowflakeAddress);
//Declare our total
uint total = amount;
//Since couponID == 0 means we do not have a coupon, we check this first
if(couponID != 0){
uint amountRefund;
//TODO: Break this out into its own function thingie
//Get coupon info from our coupon feature contract
//Personal note; supposedly, these ()s are needed. Wait, is this always true when calling outside of contract? I swear that I've accessed some directly...?
CouponFeature couponFeature = CouponFeature(mktResolver.CouponFeatureAddress());
(CouponInterface.CouponType couponType, uint256 amountOff, uint expirationDate) = couponFeature.getCouponSimple(couponID);
//Ensure coupon is not expired
require(now < expirationDate, "Coupon is expired!");
//If couponType is amountOff...
if(couponType == CouponInterface.CouponType.AMOUNT_OFF){
total = _applyCouponAmountOff(total, amountOff);
amountRefund = amountOff;
}
/*
//In an event, let's push the transaction or something
emit CouponProcessed(total, amountRefund, couponType, amountOff, expirationDate);
//Send coupon to burner address
couponFeature.burnAddress(couponID);
//Send item to buyer
itemFeature.transferFromAddress(einSeller, einBuyer, itemID);
//Send our total charged to buyer addr via snowflake
snowflake.transferSnowflakeBalance(einSeller, total);
//Finally, let's return their amount... (for security reasons, we follow Checks-Effect-Interaction pattern and modify state last...)
snowflake.transferSnowflakeBalance(einBuyer, amountRefund);
*/
} else {
//Send item to buyer
itemFeature.transferFromAddress(einSeller, einBuyer, itemID);
//Send our total charged to buyer addr via snowflake
snowflake.transferSnowflakeBalance(einSeller, total);
}
}
function _applyCouponAmountOff(uint256 total, uint256 amountOff) pure private returns (uint256) {
require(total >= amountOff, "Coupon amount is higher than total");
uint256 newTotal = total.sub(amountOff);
return newTotal;
}
// end recipient is an EIN, no from field
function snowflakeCall(
address /* resolver */,
uint /* einTo */,
uint /* amount */,
bytes memory /* snowflakeCallBytes */
) public senderIsSnowflake {
revert("Not Implemented.");
}
// end recipient is an address
function snowflakeCall(
address /* resolver */,
uint /* einFrom */,
address payable /* to */,
uint /* amount */,
bytes memory /* snowflakeCallBytes */
) public senderIsSnowflake {
revert("Not Implemented.");
}
// end recipient is an address, no from field
function snowflakeCall(
address /* resolver */,
address payable /* to */,
uint /* amount */,
bytes memory /* snowflakeCallBytes */
) public senderIsSnowflake {
revert("Not Implemented.");
}
function setCouponMarketplaceResolverAddress(address _CouponMarketplaceResolverAddress) public onlyEINOwner returns (bool) {
CouponMarketplaceResolverAddress = _CouponMarketplaceResolverAddress;
return true;
}
event CouponProcessed(uint total, uint amountRefunded, CouponInterface.CouponType couponType, uint256 amountOff, uint expirationDate);
}