token-gallery-contracts/contracts/LibSellable.sol

72 lines
2.5 KiB
Solidity

// SPDX-License-Identifier: Proprietary
pragma solidity ^0.7.2;
pragma experimental ABIEncoderV2;
import "./SafeMath.sol";
library LibSellable {
using SafeMath for uint256;
bytes32 constant SELLABLE_STORAGE_POSITION = keccak256("gallery.token.nft.LibSellable");
event FeeChanged(uint8 oldFee, uint8 newFee);
event RoyaltyChanged(uint8 oldRoyalty, uint8 newRoyalty);
event PayoutMethodChanged(bool oldPayout, bool newPayout);
struct SellableStorage {
uint8 fee; // percentage e.g. 5 = 5% yes only integers
uint8 royalty;
mapping (address => uint256) ownerEthBalances;
mapping (address => uint256) ownerTokenBalances;
address paymentTokenAddress;
bool tokenIsERC1363;
bool sendRightAway;
}
function setFee(uint8 _fee) internal {
require(_fee <= 10, "Fee must be <= 10");
SellableStorage storage s = sellableStorage();
require(s.fee != _fee, "No fee change");
emit FeeChanged(s.fee, _fee);
s.fee = _fee;
}
function setRoyalty(uint8 _royalty) internal {
require(_royalty <= 25, "Royalty must be <= 25");
SellableStorage storage s = sellableStorage();
require(s.royalty != _royalty, "No royalty change");
emit RoyaltyChanged(s.royalty, _royalty);
s.royalty = _royalty;
}
function getSplit(uint256 _price) view internal returns (uint256 _fee, uint256 _royalty, uint256 _profit) {
SellableStorage storage s = sellableStorage();
_fee = uint256(s.fee).mul(100).mul(_price).div(10_000);
_royalty = uint256(s.royalty).mul(100).mul(_price).div(10_000);
_profit = _price.sub(_fee).sub(_royalty);
}
function setPaymentToken(address _token, bool _isERC1363) internal {
SellableStorage storage s = sellableStorage();
require(s.paymentTokenAddress == address(0), "Payment token address must be unset to change");
s.paymentTokenAddress = _token;
s.tokenIsERC1363 = _isERC1363;
}
function setSendRightAWay(bool _sendRightAway) internal {
SellableStorage storage s = sellableStorage();
require(_sendRightAway != s.sendRightAway, "No payout method change");
emit PayoutMethodChanged(s.sendRightAway, _sendRightAway);
s.sendRightAway = _sendRightAway;
}
function sellableStorage() internal pure returns (SellableStorage storage ss) {
bytes32 position = SELLABLE_STORAGE_POSITION;
assembly {
ss.slot := position
}
}
}