How to build a Web3 Play-to-Earn P2E game: smart contracts

This is part 2 of the series. If you’ve missed the 1st part, you can check it here. The first part was about the game’s architecture and how to connect Blockchain with game’s backed in an overview.

The main goal of this contract is to contain the state of the players in the game. Players should deposit BOV tokens beforehand in order to mint new nft or replace / update their stats. As an additional feature this contract has a possibility to work with Ulti tokens as well – it is possible to buy card packs from a game (buyCardPack).

In our example NFT Web3 Trading Card Game: there's PVE mode and PVP mode
In our example NFT Web3 Trading Card Game theres PVE mode and PVP mode

Contract access

Contract contains information about the amount of BOV available for players in the game. It has access to the marketplace in order to create or replace user nft as well as buying it for Ultis. It also gives an opportunity to get BOV reward and withdraw it from the game.

Access level

There are some methods that cannot be called by the user directly such as mint, reroll and reward. In order to use them, the player should have hashed a message that was signed by the admin. Methods like deposit, withdraw are open publicly so players can use them directly. Also there are only admin access methods like setMarketplace, setToken that allow to change addresses of the token and marketplace game is interacting with.

In order to sign a message server should prepare a hash message that should be signed. There two ways to do it

1: create this message on server by itself

2: call getMintHashMessage with parameters needed and get it from the contract itself

In the second option  there should be sent correct parameters and called with correct method for each occasion (for mint there should be called getMintHashMessage)

Mint Reroll params description:

Mint has mintData parameters that have next structure MintData [uint256 id] and should be called as [[12]] on the client side. Same is for the rerroll parameters it also has BurnData [uint256 id, uint256 amount] or [[1,1]] on the client side.

Methods:

Deposit:

Parameters: uint256 amount

Access level: public

Description:

In order to make any mint or reroll player should have some tokens on his personal account. 

Example:

deposit(100)

Withdraw:

Parameters: uint256 amount

Access level: public

Description:

Players can withdraw tokens from the game to the player wallet. Cannot be withdraw more than player has on the account

Example:

withdraw(100)

GetUserNonce:

Parameters: address userAddress

Access level: public

Description:

In order to protect a signature so it wouldn’t be possible to call the same method multiple times with the same signature the nonce was introduced. Nonce will be slowly growing after each method that requires admin signature

Example:

const nonce = getUserNonce('0x7eF644345F0e68706de60E89c4090F976E01e972')

GetMintHashMessage:

Parameters: uint256 price, MintData[] data, uint256 nonce

Access level: public

Description: This is a special method to get hash message for a mint method there should be same parameters as for the mint call

Example:

const nonce = getUserNonce('0x7eF644345F0e68706de60E89c4090F976E01e972')
const hashMessage = getMintHashMessage(10, [[1]], nonce)

Mint:

Parameters: uint256 price, MintData[] data, bytes signature

Access level: admin signed signature

Description:

Players can mint nft in a game that will be automatically visible on the marketplace. In order to get hash message there should be called getMintHashMessage and it should be signed by admin

Example:

const nonce = getUserNonce(0x7eF644345F0e68706de60E89c4090F976E01e972);
const hashMessage = getMintHashMessage(10, [[1]], nonce)
const { signature } = web3.eth.accounts.sign(hashMessage, privateKey)

mint(10, [[1]], signature)

Reroll:

Parameters: uint256 price, BurnData[] data, MintData[] data, bytes signature

Access level: admin signed signature

Description:

Players can mint and burn nft in a game that will be automatically visible and hidden on the marketplace. In order to get a hash message there should be called getRerollHashMessage and it should be signed by admin to get it signature

Example:

const nonce = getUserNonce(0x7eF644345F0e68706de60E89c4090F976E01e972);
const hashMessage = getRerollHashMessage(10, [[1, 1]], [[2]], nonce)
const { signature } = web3.eth.accounts.sign(hashMessage, privateKey)

reroll(10, [[1]], [[2]], signature)

Reward:

Parameters: uint256 amount, bytes signature

Access level: admin signed signature

Description:

Players can get rewards for completing quests. In order to get a hash message there should be called getRewardHashMessage and it should be signed by admin to get it signature and be provided by user

Example:

const nonce = getUserNonce(0x7eF644345F0e68706de60E89c4090F976E01e972);
const hashMessage = getRerollHashMessage(10, [[1]], [[2]], nonce)
const { signature } = web3.eth.accounts.sign(hashMessage, privateKey)
reroll(10, signature)

BuyCardPack:

In our example Web3 NFT Trading Card Game - players buy Card Packs with in-game currency which is also a tradable ERC20 Token.
In our example Web3 NFT Trading Card Game players buy Card Packs with in game currency which is also a tradable ERC20 Token

Parameters: uint256 price, MintData[] data, bytes signature

Access level: admin signed signature

Description:

Players can buy a new card pack for an Ulti. In order to get a hash message there should be called getBuyCardPackHashMessage with the same parameters + user nonce and it should be signed by admin to get it signature and be provided by user

Example:

const nonce = getUserNonce(0x7eF644345F0e68706de60E89c4090F976E01e972);
const hashMessage = getBuyCardPackHashMessage(10, [[1]], [[2]], nonce)
const { signature } = web3.eth.accounts.sign(hashMessage, privateKey)

buyCardPack(10, [[1]], signature)

OpenCardPack:

After opening the Card Packs, players can see them in the inventory. Each card is an NFT which is a ERC1155 token. NFT's can also be leveled up using ERC20 token (also in-game currency)
After opening the Card Packs players can see them in the inventory Each card is an NFT which is a ERC1155 token NFTs can also be leveled up using ERC20 token also in game currency

Parameters: uint256 price, BurnData[] data, MintData[] data, bytes signature

Access level: admin signed signature

Description:

Players can buy a new card pack for an Ulti. In order to get a hash message there should be called getBuyCardPackHashMessage with the same parameters + user nonce and it should be signed by admin to get it signature and be provided by user

Example:

const nonce = getUserNonce(0x7eF644345F0e68706de60E89c4090F976E01e972);
const hashMessage = getBuyCardPackHashMessage(10, [[1]], [[2]], nonce)
const { signature } = web3.eth.accounts.sign(hashMessage, privateKey)

buyCardPack(10, [[1]], signature)

Admin Level Methods:

SetMarketplace:

Parameters: address marketplace_

Access level: admin only

Description:

Contract has a link to the 1155 contract in order to mint or burn tokens from the marketplace, can be replaced/changed by admin only

Example:

setMarketPlace(0x7eF644345F0e68706de60E89c4090F976E01e972);

SetToken:

Parameters: address token_

Access level: admin only

Description:

Contract has a link to the erc20 contract in order to mint or burn tokens from the BOV there should be a correct link, can be replaced/changed by admin only

Example:

setMarketPlace(0x7eF644345F0e68706de60E89c4090F976E01e972);
author avatar
Fungies
Fungies.io helps game developers create their own storefronts or marketplaces to sell directly to players. Web2 and Web3 compatible.

 

Fungies.io helps game developers create their own storefronts or marketplaces to sell directly to players. Web2 and Web3 compatible.

Post a comment

Your email address will not be published. Required fields are marked *