You don't have access to this class

Keep learning! Join and start boosting your career

Aprovecha el precio especial y haz tu profesi贸n a prueba de IA

Antes: $249

Currency
$209
Suscr铆bete

Termina en:

2 D铆as
8 Hrs
32 Min
49 Seg

Crear un market place de achievements

9/15
Resources

How to combine achievements and coins in a game through a Marketplace?

In the world of video game development, the integration of reward systems can be a fundamental pillar to keep players' participation and interest active. Imagine being able to link an achievement system with an in-game currency: achievements act as goals or trophies, and coins as rewards. Let's delve into how you can implement a decentralized Marketplace to buy and sell in-game achievements using smart contracts.

How to solve the challenge of achievements and coin issuance?

The first step to achieve integration was to increase the issuance of coins if a player has already acquired an achievement. This mechanism is verified thanks to the balanceOf function in the contracts, which tells us how many tokens an account owns. If the balance is greater than zero, it means that the player already has an achievement, and therefore the reward is doubled.

How to model a decentralized Marketplace?

To create an achievement marketplace, a separate contract is needed to handle the transactions. This contract will reference the interfaces of fungible (ERC20) and non-fungible (ERC721) tokens, which optimizes the contract by making it more lightweight.

In the Marketplace, the key functions are publishing and purchasing:

  • Publish: Validate that the ID of the token to be published is not already registered and that the publish value is greater than zero.
  • Purchase: Ensure that the user has sufficient funds and that the token is published and available for sale.

Example of implementation of validations and transfers.

The Marketplace contract must handle approvals to transfer tokens when selling achievements. Next, check if the selling account has authorized the Marketplace to manage the token.

// Check if the Marketplace has permission to transfer the achievementrequire(getApproved(tokenId) == address(this), "No permission to transfer");

Upon completion of the validations, the token transfer and registration of the transaction is performed.

// Transfer funds from buyer to sellerIERC20(tokenContract).transferFrom(msg.sender, ownerAddress, salePrice);
// Transfer achievement from seller to buyerIERC721(achievementContract).transferFrom(ownerAddress, msg.sender, tokenId);

Test implementation and future challenges

With the contract deployed, it is critical to assign the correct addresses through a constructor, ensuring correct initialization of variables. Test these operations by ensuring the proper permissions are in place before each transaction.

Finally, we propose an exciting challenge: transform this Marketplace into an auction model, thus elevating the platform to a new level of interactivity and strategy. Review these concepts and continue learning - the world of blockchain-based game development awaits you!

Contributions 1

Questions 0

Sort by:

Want to see more contributions, questions and answers from the community?

Antes de conocer el m茅todo balanceOf opt茅 por crear un mapping para registrar los logros obtenidos y as铆 usarlo para validar antes de entregar los token ERC20.

	// variables
	mapping (address => uint) logrosObtenidos;

Luego registro los logros cada vez que se emiten los token ERC721:

        // Give achievement ERC721
        partidasGanadas[partidas[idPartida].ganador]++;
        if (partidasGanadas[partidas[idPartida].ganador] == 5) {
            achievement.emitir(partidas[idPartida].ganador);
            logrosObtenidos[partidas[idPartida].ganador]++;
        }

Finalmente entrego los token ERC20 si se cumple la condici贸n indicada:

        // Give extra ERC20 token because has achievement
        if (logrosObtenidos[partidas[idPartida].ganador] > 0) {
            moneda.emitir(partidas[idPartida].ganador, 2);
        }

Definitivamente usar balanceOf es m谩s 贸ptimo 馃槂