forked from m10io/tokeninc-smart-contracts
-
Notifications
You must be signed in to change notification settings - Fork 0
/
TokenIOMerchant.sol
126 lines (104 loc) · 4.84 KB
/
TokenIOMerchant.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
pragma solidity 0.4.24;
import "./Ownable.sol";
import "./TokenIOStorage.sol";
import "./TokenIOLib.sol";
/*
COPYRIGHT 2018 Token, Inc.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@title TokenIOMerchant - Merchant Interface Smart Contract for Token, Inc.
@author Ryan Tate <[email protected]>, Sean Pollock <[email protected]>
@notice Contract uses generalized storage contract, `TokenIOStorage`, for
upgradeability of interface contract.
*/
contract TokenIOMerchant is Ownable {
/// @dev Set reference to TokenIOLib interface which proxies to TokenIOStorage
using TokenIOLib for TokenIOLib.Data;
TokenIOLib.Data lib;
/**
* @notice Constructor method for Merchant contract
* @param _storageContract Ethereum Address of TokenIOStorage contract
*/
constructor(address _storageContract) public {
/*
* @notice Set the storage contract for the interface
* @dev This contract will be unable to use the storage constract until
* @dev contract address is authorized with the storage contract
* @dev Once authorized, you can setRegisteredFirm and setRegisteredAuthority
*/
lib.Storage = TokenIOStorage(_storageContract);
/// @dev set owner to contract initiator
owner[msg.sender] = true;
}
/**
@notice Sets Merchant globals and fee paramters
@param feeContract Address of fee contract
@return { "success" : "Returns true if successfully called from another contract"}
*/
function setParams(
address feeContract
) onlyOwner public returns (bool success) {
require(lib.setFeeContract(feeContract),
"Error: Unable to set fee contract. Ensure contract is allowed by storage contract.");
return true;
}
/**
* @notice Gets fee parameters
* @return {
"bps":"Fee amount as a mesuare of basis points",
"min":"Minimum fee amount",
"max":"Maximum fee amount",
"flat":"Flat fee amount",
"contract":"Address of fee contract"
}
*/
function getFeeParams() public view returns (uint bps, uint min, uint max, uint flat, bytes feeMsg, address feeAccount) {
return (
lib.getFeeBPS(lib.getFeeContract(address(this))),
lib.getFeeMin(lib.getFeeContract(address(this))),
lib.getFeeMax(lib.getFeeContract(address(this))),
lib.getFeeFlat(lib.getFeeContract(address(this))),
lib.getFeeMsg(lib.getFeeContract(address(this))),
lib.getFeeContract(address(this))
);
}
/**
* @notice Calculates fee of a given transfer amount
* @param amount Amount to calculcate fee value
* @return {"fees": "Returns the calculated transaction fees based on the fee contract parameters"}
*/
function calculateFees(uint amount) public view returns (uint fees) {
return lib.calculateFees(lib.getFeeContract(address(this)), amount);
}
/**
* @notice Pay method for merchant interface
* @param currency Currency symbol of the token (e.g. USDx, JYPx, GBPx)
* @param merchant Ethereum address of merchant
* @param amount Amount of currency to send to merchant
* @param merchantPaysFees Provide option for merchant to pay the transaction fees
* @param data Optional data to be included when paying the merchant (e.g. item receipt)
* @return { "success" : "Returns true if successfully called from another contract"}
*/
function pay(string currency, address merchant, uint amount, bool merchantPaysFees, bytes data) public returns (bool success) {
uint fees = calculateFees(amount);
/// @dev note the spending amount limit is gross of fees
require(lib.setAccountSpendingAmount(msg.sender, lib.getFxUSDAmount(currency, amount)),
"Error: Unable to set account spending amount.");
require(lib.forceTransfer(currency, msg.sender, merchant, amount, data),
"Error: Unable to transfer funds to account");
address feeContract = lib.getFeeContract(address(this));
/// @dev If merchantPaysFees == true, the merchant will pay the fees to the fee contract;
if (merchantPaysFees) {
require(lib.forceTransfer(currency, merchant, feeContract, fees, lib.getFeeMsg(feeContract)),
"Error: Unable to transfer fees to fee contract.");
} else {
require(lib.forceTransfer(currency, msg.sender, feeContract, fees, lib.getFeeMsg(feeContract)),
"Error: Unable to transfer fees to fee contract.");
}
return true;
}
}