Create an NFT-based Game
ERC721 based game
Non-Fungible Tokens, or NFTs in short, have recently gained significant popularity due to the rising trend in the blockchain gaming space. Ever since their inception, NFTs became really popular among the crypto community, thanks to successful projects like Cryptokitties and CryptoPunks, etc.
Cryptokitties is one of the most hyped NFT projects within the DeFi and blockchain gaming space. CryptoKitties are digital, collectible cats built on the Ethereum blockchain, and they can be bought and sold using Ethereum, and bred to create new cats with several traits and varying characteristics.
CryptoKitties is basically a digital pet community centred around collecting, training, raising, and battling fantasy creatures that are known as ‘CryptoFantom’. Each CryptoFantom has unique genetic data stored on Opera.
A CryptoFantom can possess two different genes with different body parts. This smart contract serves as an example, and can be a good starting point for anyone who is looking to build NFT based products on the Fantom blockchain platform.

Prerequisite

To Run these examples please follow the following steps:
Install Truffle:
1
$ npm install -g truffle
Copied!
Clone the project:
1
$ git clone https://github.com/Fantom-foundation/crypto-fantom-game.git
Copied!
This project is for test purpose only, it contains only the basic functions and minimum requirements.
run npm install to install dependencies
1
$ npm install
Copied!
1
Contract: Game
2
✓ should NOT mint if not admin (47ms)
3
✓ should mint if admin (154ms)
4
✓ should NOT transfer if balance is 0 (104ms)
5
✓ should NOT transfer if not owner (79ms)
6
✓ safeTransferFrom() should NOT transfer if recipient contract does not implement erc721recipient interface (95ms)
7
✓ transferFrom() should transfer (70ms)
8
✓ transferFrom() should transfer (70ms)
9
✓ safeTransferFrom() should transfer (62ms)
10
✓ should transfer token when approved (117ms)
11
✓ should transfer token when approved (87ms)
12
13
Contract: Game
14
✓ should NOT mint if not admin
15
✓ should mint if admin (161ms)
16
✓ should breed (167ms)
17
18
13 passing (4s)
Copied!

Deploy Locally and Interact with frontend

Start by running Ganache-CLI. It runs a personal Ethereum blockchain on which you can use to run tests, execute commands. Run this command:
1
$ ganache-cli --deterministic -i 1337
Copied!
You should get this output:
1
Ganache CLI v6.10.0-beta.2 (ganache-core: 2.11.0-beta.0)
2
3
Available Accounts
4
==================
5
(0) 0x90F8bf6A479f320ead074411a4B0e7944Ea8c9C1 (100 ETH)
6
(1) 0xFFcf8FDEE72ac11b5c542428B35EEF5769C409f0 (100 ETH)
7
(2) 0x22d491Bde2303f2f43325b2108D26f1eAbA1e32b (100 ETH)
8
(3) 0xE11BA2b4D45Eaed5996Cd0823791E0C93114882d (100 ETH)
9
(4) 0xd03ea8624C8C5987235048901fB614fDcA89b117 (100 ETH)
10
(5) 0x95cED938F7991cd0dFcb48F0a06a40FA1aF46EBC (100 ETH)
11
(6) 0x3E5e9111Ae8eB78Fe1CC3bb8915d5D461F3Ef9A9 (100 ETH)
12
(7) 0x28a8746e75304c0780E011BEd21C72cD78cd535E (100 ETH)
13
(8) 0xACa94ef8bD5ffEE41947b4585a84BdA5a3d3DA6E (100 ETH)
14
(9) 0x1dF62f291b2E969fB0849d99D9Ce41e2F137006e (100 ETH)
15
16
...
17
...
18
Listening on 127.0.0.1:8545
Copied!
You can either import the mnemonic into metamask or at least the first account (you should have the private key) into Metamask.
Add local network to Metamask
Then deploy our contract by running this command:
1
$ truffle migrate --network testrpc --reset
Copied!
You should get this output:
1
Compiling your contracts...
2
===========================
3
✔ Fetching solc version list from solc-bin. Attempt #1
4
> Everything is up to date, there is nothing to compile.
5
6
7
8
Starting migrations...
9
======================
10
> Network name: 'testrpc'
11
> Network id: 1337
12
> Block gas limit: 6721975 (0x6691b7)
13
14
15
1_initial_migration.js
16
======================
17
18
Replacing 'Migrations'
19
----------------------
20
> transaction hash: 0x28099872c6feeceab4033e63e9a3c48e73f315e2c61feef4a5bf7ac947c3329e
21
> Blocks: 0 Seconds: 0
22
> contract address: 0xe78A0F7E598Cc8b0Bb87894B0F60dD2a88d6a8Ab
23
> block number: 1
24
> block timestamp: 1608736619
25
> account: 0x90F8bf6A479f320ead074411a4B0e7944Ea8c9C1
26
> balance: 99.99665588
27
> gas used: 167206 (0x28d26)
28
> gas price: 20 gwei
29
> value sent: 0 ETH
30
> total cost: 0.00334412 ETH
31
32
33
> Saving migration to chain.
34
> Saving artifacts
35
-------------------------------------
36
> Total cost: 0.00334412 ETH
37
38
39
2_deploy_contracts.js
40
=====================
41
42
Replacing 'CryptoFantom'
43
------------------------
44
> transaction hash: 0x8a3dd0bc5d57a8f6dcba777ded4b0a2038fe6f8c7c6684e8546cd49a40220a9d
45
> Blocks: 0 Seconds: 0
46
> contract address: 0xCfEB869F69431e42cdB54A4F4f105C19C080A601
47
> block number: 3
48
> block timestamp: 1608736619
49
> account: 0x90F8bf6A479f320ead074411a4B0e7944Ea8c9C1
50
> balance: 99.9661064
51
> gas used: 1485231 (0x16a9af)
52
> gas price: 20 gwei
53
> value sent: 0 ETH
54
> total cost: 0.02970462 ETH
55
56
57
> Saving migration to chain.
58
> Saving artifacts
59
-------------------------------------
60
> Total cost: 0.02970462 ETH
61
62
63
Summary
64
=======
65
> Total deployments: 2
66
> Final cost: 0.03304874 ETH
Copied!
Then you can finally run:
1
$ npm run start
Copied!
1
Compiled successfully!
2
3
You can now view app in the browser.
4
5
Local: http://localhost:3000/
6
On Your Network: http://192.168.102.7:3000/
7
8
Note that the development build is not optimized.
9
To create a production build, use npm run build.
10
Copied!

Issues

You may encounter some problems with the current version of Metamask which does not detect the correct nonce, you can switch from network to Mainnet and then back to Local that solves most of the issues until a correction is made.
Last modified 11mo ago