TipsWithFee

Git Source

Inherits: ITipsWithFee, Initializable, IERC777Recipient

Logic to handle rewards that user can send to character and note.

The platform can set the commission ratio through the TipsWithFee contract, and draw a commission from the user's tips.
For TipCharacter User/Client should call send erc777 token to the TipsWithFee contract, with fromCharacterId, toCharacterId and receiver(a platform account) encoded in the data.
send interface is IERC777-send, and parameters encode refers AbiCoder-encode.
For TipCharacter4Note User should call send erc777 token to the TipsWithFee contract, with fromCharacterId, toCharacterId, toNoteId and receiver encoded in the data.
The platform account can set the commission ratio through setDefaultFeeFraction, setFeeFraction4Character and setFeeFraction4Note.

State Variables

ERC1820_REGISTRY

IERC1820Registry public constant ERC1820_REGISTRY = IERC1820Registry(0x1820a4B7618BdE71Dce8cdc73aAB6C95905faD24);

TOKENS_RECIPIENT_INTERFACE_HASH

bytes32 public constant TOKENS_RECIPIENT_INTERFACE_HASH = keccak256("ERC777TokensRecipient");

_web3Entry

address internal _web3Entry;

_token

address internal _token;

_feeFractions

mapping(address => uint256) internal _feeFractions;

_feeFractions4Character

mapping(address => mapping(uint256 => uint256)) internal _feeFractions4Character;

_feeFractions4Note

mapping(address => mapping(uint256 => mapping(uint256 => uint256))) internal _feeFractions4Note;

Functions

onlyFeeReceiver

modifier onlyFeeReceiver(address feeReceiver);

validateFraction

modifier validateFraction(uint256 fraction);

initialize

Initialize the contract, setting web3Entry address and token address.

function initialize(address web3Entry_, address token_) external override initializer;

Parameters

NameTypeDescription
web3Entry_addressAddress of web3Entry.
token_addressAddress of token.

setDefaultFeeFraction

Changes the default fee percentage of specific receiver.

The feeReceiver can be a platform account.

function setDefaultFeeFraction(address feeReceiver, uint256 fraction)
    external
    override
    onlyFeeReceiver(feeReceiver)
    validateFraction(fraction);

Parameters

NameTypeDescription
feeReceiveraddressThe fee receiver address.
fractionuint256The percentage measured in basis points. Each basis point represents 0.01%.

setFeeFraction4Character

Changes the fee percentage of specific <receiver, character>.

If feeFraction4Character is set, it will override the default fee fraction.

function setFeeFraction4Character(address feeReceiver, uint256 characterId, uint256 fraction)
    external
    override
    onlyFeeReceiver(feeReceiver)
    validateFraction(fraction);

Parameters

NameTypeDescription
feeReceiveraddressThe fee receiver address.
characterIduint256The character ID.
fractionuint256The percentage measured in basis points. Each basis point represents 0.01%.

setFeeFraction4Note

Changes the fee percentage of specific <receiver, note>.

If feeFraction4Note is set, it will override feeFraction4Character and the default fee fraction.

function setFeeFraction4Note(address feeReceiver, uint256 characterId, uint256 noteId, uint256 fraction)
    external
    override
    onlyFeeReceiver(feeReceiver)
    validateFraction(fraction);

Parameters

NameTypeDescription
feeReceiveraddressThe fee receiver address.
characterIduint256The character ID .
noteIduint256The note ID .
fractionuint256The percentage measured in basis points. Each basis point represents 0.01%.

tokensReceived

Called by an {IERC777} token contract whenever tokens are being moved or created into a registered account to (this contract).
The userData/operatorData should be an abi encoded bytes of fromCharacterId, toCharacter and toNoteId(optional) and receiver(platform account), so the length of data is 96 or 128.

function tokensReceived(
    address,
    address from,
    address to,
    uint256 amount,
    bytes calldata userData,
    bytes calldata operatorData
) external override(IERC777Recipient);

getWeb3Entry

Returns the address of web3Entry contract.

function getWeb3Entry() external view override returns (address);

Returns

NameTypeDescription
<none>addressThe address of web3Entry contract.

getToken

Returns the address of mira token contract.

function getToken() external view override returns (address);

Returns

NameTypeDescription
<none>addressThe address of mira token contract.

getFeeFraction

Returns the fee percentage of specific <receiver, note>.

It will return the first non-zero value by priority feeFraction4Note, feeFraction4Character and defaultFeeFraction.

function getFeeFraction(address feeReceiver, uint256 characterId, uint256 noteId)
    external
    view
    override
    returns (uint256);

Parameters

NameTypeDescription
feeReceiveraddressThe fee receiver address.
characterIduint256The character ID .
noteIduint256The note ID .

Returns

NameTypeDescription
<none>uint256fraction The percentage measured in basis points. Each basis point represents 0.01%.

getFeeAmount

Returns how much the fee is owed by <feeFraction, tipAmount>.

function getFeeAmount(address feeReceiver, uint256 characterId, uint256 noteId, uint256 tipAmount)
    external
    view
    override
    returns (uint256);

Parameters

NameTypeDescription
feeReceiveraddressThe fee receiver address.
characterIduint256The character ID .
noteIduint256The note ID .
tipAmountuint256

Returns

NameTypeDescription
<none>uint256The fee amount.

_tipCharacter

Tips a character by transferring amount tokens from the fromCharacterId account to toCharacterId account.
Emits the TipCharacter event.
User should call send erc777 token to the Tips contract, with fromCharacterId and toCharacterId encoded in the data.
send interface is IERC777-send, and parameters encode refers AbiCoder-encode.
Requirements:

  • The from account must be the character owner of `fromCharacterId.
function _tipCharacter(
    address from,
    uint256 fromCharacterId,
    uint256 toCharacterId,
    address token,
    uint256 amount,
    address feeReceiver
) internal;

Parameters

NameTypeDescription
fromaddressThe caller's account who sends token.
fromCharacterIduint256The token ID of character that calls this contract.
toCharacterIduint256The token ID of character that will receive the token.
tokenaddressAddress of token.
amountuint256Amount of token.
feeReceiveraddressFee receiver address.

_tipCharacterForNote

Tips a character's note by transferring amount tokens from the fromCharacterId account to toCharacterId account.
Emits the TipCharacterForNote event.
User should call send erc777 token to the Tips contract, with fromCharacterId, toCharacterId and toNoteId encoded in the data.
Requirements:

  • The from account must be the character owner of `fromCharacterId.
function _tipCharacterForNote(
    address from,
    uint256 fromCharacterId,
    uint256 toCharacterId,
    uint256 toNoteId,
    address token,
    uint256 amount,
    address feeReceiver
) internal;

Parameters

NameTypeDescription
fromaddressThe caller's account who sends token.
fromCharacterIduint256The token ID of character that calls this contract.
toCharacterIduint256The token ID of character that will receive the token.
toNoteIduint256The note ID.
tokenaddressAddress of token.
amountuint256Amount of token.
feeReceiveraddressFee receiver address.

_tip

function _tip(
    address from,
    uint256 fromCharacterId,
    uint256 toCharacterId,
    uint256 toNoteId,
    address token,
    uint256 amount,
    address feeReceiver
) internal;

_getFeeFraction

function _getFeeFraction(address feeReceiver, uint256 characterId, uint256 noteId) internal view returns (uint256);

_getFeeAmount

function _getFeeAmount(address feeReceiver, uint256 characterId, uint256 noteId, uint256 tipAmount)
    internal
    view
    returns (uint256);

_feeDenominator

Defaults to 10000 so fees are expressed in basis points.

function _feeDenominator() internal pure virtual returns (uint96);

Events

TipCharacter

Emitted when the assets are rewarded to a character.

event TipCharacter(
    uint256 indexed fromCharacterId,
    uint256 indexed toCharacterId,
    address token,
    uint256 amount,
    uint256 fee,
    address feeReceiver
);

TipCharacterForNote

Emitted when the assets are rewarded to a note.

event TipCharacterForNote(
    uint256 indexed fromCharacterId,
    uint256 indexed toCharacterId,
    uint256 indexed toNoteId,
    address token,
    uint256 amount,
    uint256 fee,
    address feeReceiver
);