NewbieVilla
Inherits: Initializable, AccessControlEnumerable, IERC721Receiver, IERC777Recipient
Implementation of a contract to keep characters for others. The keepers and addresses with the ADMIN_ROLE are expected to issue the proof to users. Then users could use the proof to withdraw the corresponding character.
State Variables
ADMIN_ROLE
bytes32 public constant ADMIN_ROLE = keccak256("ADMIN_ROLE");
ERC1820_REGISTRY
IERC1820Registry public constant ERC1820_REGISTRY = IERC1820Registry(0x1820a4B7618BdE71Dce8cdc73aAB6C95905faD24);
TOKENS_RECIPIENT_INTERFACE_HASH
bytes32 public constant TOKENS_RECIPIENT_INTERFACE_HASH = keccak256("ERC777TokensRecipient");
web3Entry
address public web3Entry;
xsyncOperator
address public xsyncOperator;
_token
address internal _token;
_balances
mapping(uint256 => uint256) internal _balances;
_tips
address internal _tips;
_keepers
mapping(uint256 characterId => address keeper) private _keepers;
Functions
notExpired
modifier notExpired(uint256 expires);
initialize
Initialize the Newbie Villa contract.
msg.sender will be granted DEFAULT_ADMIN_ROLE
.
function initialize(address web3Entry_, address xsyncOperator_, address token_, address admin_, address tips_)
external
reinitializer(3);
Parameters
Name | Type | Description |
---|---|---|
web3Entry_ | address | Address of web3Entry contract. |
xsyncOperator_ | address | Address of xsyncOperator. |
token_ | address | Address of ERC777 token. |
admin_ | address | Address of admin. |
tips_ | address | Address of Tips contract. |
tipCharacter
Tips a character by transferring amount
tokens
from account with required permission to Tips
contract.
Admin will 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
msg.sender
must be character's keeper or haveADMIN_ROLE
but not character's keeper.
function tipCharacter(uint256 fromCharacterId, uint256 toCharacterId, uint256 amount) external;
Parameters
Name | Type | Description |
---|---|---|
fromCharacterId | uint256 | The token ID of character that calls this contract. |
toCharacterId | uint256 | The token ID of character that will receive the token. |
amount | uint256 | Amount of token. |
tipCharacterForNote
Tips a character's note by transferring amount
tokens
from account with required permission to Tips
contract.
Admin will call send
erc777 token to the Tips contract, with fromCharacterId
,
toCharacterId
and toNoteId
encoded in the data
.
send
interface is
IERC777-send,
and parameters encode refers
AbiCoder-encode.
Requirements:
- The
msg.sender
must be character's keeper or haveADMIN_ROLE
but not character's keeper.
function tipCharacterForNote(uint256 fromCharacterId, uint256 toCharacterId, uint256 toNoteId, uint256 amount)
external;
Parameters
Name | Type | Description |
---|---|---|
fromCharacterId | uint256 | The token ID of character that calls this contract. |
toCharacterId | uint256 | The token ID of character that will receive the token. |
toNoteId | uint256 | The note ID. |
amount | uint256 | Amount of token. |
withdraw
Withdraw character#characterId
to to
using the nonce, expires and the proof.
Emits the Withdraw
event.
*Proof is the signature from character keepers or someone with the ADMIN_ROLE. The message to sign is
the packed data of this contract's address, characterId
, nonce
and expires
.
Here's an example to generate a proof:
digest = ethers.utils.arrayify(
ethers.utils.solidityKeccak256(
["address", "uint256", "uint256", "uint256"],
[newbieVilla.address, characterId, nonce, expires]
)
);
proof = await owner.signMessage(digest);
Requirements: :
expires
is greater than the current timestampproof
is signed by the keeper or address with the ADMIN_ROLE*
function withdraw(address to, uint256 characterId, uint256 nonce, uint256 expires, bytes calldata proof)
external
notExpired(expires);
Parameters
Name | Type | Description |
---|---|---|
to | address | Receiver of the withdrawn character. |
characterId | uint256 | The token id of the character to withdraw. |
nonce | uint256 | Random nonce used to generate the proof. |
expires | uint256 | Expire time of the proof, Unix timestamp in seconds. |
proof | bytes | The proof using to withdraw the character. |
onERC721Received
*Whenever a character tokenId
is transferred to this contract via {IERC721-safeTransferFrom}
by operator
from from
, this function is called. data
will be decoded as an address and set as
the operator of the character. If the data
is empty, the operator
will be default operator of the
character.
Requirements: :
msg.sender
must be address of Web3Entry.*
function onERC721Received(address operator, address, uint256 tokenId, bytes calldata data)
external
override
returns (bytes4);
Parameters
Name | Type | Description |
---|---|---|
operator | address | |
<none> | address | |
tokenId | uint256 | |
data | bytes | bytes encoded from the operator address to set for the incoming character. |
tokensReceived
Receives tokens. Only specific tokens are accepted, so be careful not to send tokens to this address randomly.
The userData/operatorData should be an abi-encoded bytes of fromCharacterId
and toCharacter
,
which are both uint256 type, so the length of data is 64.
function tokensReceived(
address,
address,
address to,
uint256 amount,
bytes calldata userData,
bytes calldata operatorData
) external override(IERC777Recipient);
Parameters
Name | Type | Description |
---|---|---|
<none> | address | |
<none> | address | |
to | address | The Newbie Villa contract address. |
amount | uint256 | The amount of token sent. |
userData | bytes | The abi-encoded bytes of fromCharacterId and toCharacter . |
operatorData | bytes | The abi-encoded bytes of fromCharacterId and toCharacter . |
balanceOf
Returns the amount of tokens owned by characterId
.
function balanceOf(uint256 characterId) external view returns (uint256);
Parameters
Name | Type | Description |
---|---|---|
characterId | uint256 | The character ID to query. |
Returns
Name | Type | Description |
---|---|---|
<none> | uint256 | uint256 The amount of tokens owned by the character. |
getKeeper
Returns the address of keeper by characterId
.
function getKeeper(uint256 characterId) external view returns (address);
Parameters
Name | Type | Description |
---|---|---|
characterId | uint256 | The character ID to query. |
Returns
Name | Type | Description |
---|---|---|
<none> | address | address The address of the keeper. |
getToken
Returns the address of mira token contract.
function getToken() external view returns (address);
Returns
Name | Type | Description |
---|---|---|
<none> | address | The address of mira token contract. |
_hasPermission
It will return true if account
is character's keeper or has ADMIN_ROLE
but not character's keeper.
function _hasPermission(address account, uint256 characterId) internal view returns (bool);
Events
Withdraw
Emitted when the web3Entry character nft is withdrawn.
event Withdraw(address to, uint256 characterId, address token, uint256 amount);