NewbieVilla

Git Source

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

NameTypeDescription
web3Entry_addressAddress of web3Entry contract.
xsyncOperator_addressAddress of xsyncOperator.
token_addressAddress of ERC777 token.
admin_addressAddress of admin.
tips_addressAddress 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 have ADMIN_ROLE but not character's keeper.
function tipCharacter(uint256 fromCharacterId, uint256 toCharacterId, uint256 amount) external;

Parameters

NameTypeDescription
fromCharacterIduint256The token ID of character that calls this contract.
toCharacterIduint256The token ID of character that will receive the token.
amountuint256Amount 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 have ADMIN_ROLE but not character's keeper.
function tipCharacterForNote(uint256 fromCharacterId, uint256 toCharacterId, uint256 toNoteId, uint256 amount)
    external;

Parameters

NameTypeDescription
fromCharacterIduint256The token ID of character that calls this contract.
toCharacterIduint256The token ID of character that will receive the token.
toNoteIduint256The note ID.
amountuint256Amount 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 timestamp
  • proof 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

NameTypeDescription
toaddressReceiver of the withdrawn character.
characterIduint256The token id of the character to withdraw.
nonceuint256Random nonce used to generate the proof.
expiresuint256Expire time of the proof, Unix timestamp in seconds.
proofbytesThe 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

NameTypeDescription
operatoraddress
<none>address
tokenIduint256
databytesbytes 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

NameTypeDescription
<none>address
<none>address
toaddressThe Newbie Villa contract address.
amountuint256The amount of token sent.
userDatabytesThe abi-encoded bytes of fromCharacterId and toCharacter.
operatorDatabytesThe 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

NameTypeDescription
characterIduint256The character ID to query.

Returns

NameTypeDescription
<none>uint256uint256 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

NameTypeDescription
characterIduint256The character ID to query.

Returns

NameTypeDescription
<none>addressaddress The address of the keeper.

getToken

Returns the address of mira token contract.

function getToken() external view returns (address);

Returns

NameTypeDescription
<none>addressThe 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);