web3-course/l6
Vladimir Domnich 1fa8f126da Refactor player inventory management
Signed-off-by: Vladimir Domnich <v.domnich@yadro.com>
2023-11-21 10:58:08 +04:00
..
merchant Fix buying items from merchants 2023-11-20 16:05:27 +04:00
player Refactor player inventory management 2023-11-21 10:58:08 +04:00
.gitignore Simple version of l6 materials 2023-11-19 23:12:42 +04:00
data.json Add initial version of l6 materials 2023-11-19 11:31:41 +04:00
go.mod Add initial version of l6 materials 2023-11-19 11:31:41 +04:00
go.sum Add initial version of l6 materials 2023-11-19 11:31:41 +04:00
README.md Fix buying items from merchants 2023-11-20 16:05:27 +04:00

Prerequisites

To run this example we will need:

  • Wallet with sufficient GAS on it. In our example we will be using wallet with address NP8wjGz3Wvxe4gUAkTbK2McR95Y4LM2jMW.
  • Instance of frostfs-aio running with oracle enabled.

Running the example

In this section we will assume that:

  • Our wallet is located next to web3-course directory.
  • We run all commands in directory l6.

1. Compile contracts

To compile the contracts we run the following commands:

$ neo-go contract compile -in lottery/lottery_contract.go --config lottery/config.yml --out lottery/lottery.nef --manifest lottery/config.json

$ neo-go contract compile -in player/player_contract.go --config player/config.yml --out player/player.nef --manifest player/config.json

2. Assign contracts to the group

Now we will create a wallet for a group that will contain both contracts. It is possible to skip this step and use wallet frostfs-aio/morph/node-wallet.json, but for the sake of purity it is recommended to use a separate wallet.

To create a wallet, we run the following command. When prompted, enter any name for the account, we will use l6-game as group name for this example:

$ neo-go wallet init -w ../../group-wallet.json -a
Enter the name of the account > l6-game
Enter new password >
Confirm password >

Copy account address from the command's output, we will need it in a moment:

{
 	"version": "1.0",
 	"accounts": [
 		{
 			"address": "NgdcCGcR7QveKn9yjQgpemtgtn5zLn33b6",  <-- NOTE: take this value
 			...

Now we need to assign group to contracts' manifests with the following commands (pay attention that sender is address of our wallet with GAS, but --wallet and --address are referring to the group wallet that we've just created):

$ neo-go contract manifest add-group -n lottery/lottery.nef -m lottery/config.json --sender NP8wjGz3Wvxe4gUAkTbK2McR95Y4LM2jMW --wallet ../../group-wallet.json --address NYThT8RYFhkBzJfzMwCe67A63p2WoDev9X

$ neo-go contract manifest add-group -n player/player.nef -m player/config.json --sender NP8wjGz3Wvxe4gUAkTbK2McR95Y4LM2jMW --wallet ../../group-wallet.json --address NYThT8RYFhkBzJfzMwCe67A63p2WoDev9X

Now our contracts are ready to be deployed.

3. Deploy contracts

Now we can deploy lottery contract:

$ neo-go contract deploy -r http://localhost:30333 -w ../../wallet.json --in lottery/lottery.nef --manifest lottery/config.json

Copy contract hash from the command output and convert it to LE address, for example:

...
Contract: 8b81042613a3df06b4ed0479407ab5107a4a7aaf

$  neo-go util convert '8b81042613a3df06b4ed0479407ab5107a4a7aaf'
BE ScriptHash to Address	NYdbfd7nqhPCuS9dBhQKYkr8u1UKCnJykp
LE ScriptHash to Address	NbuowFNug7QCPM3zYc8HGH5ZsWbHBdGLR9  <-- NOTE: take this value
...

Now we can deploy player contract, it takes hash of lottery contract as a parameter to its' _deploy method:

$ neo-go contract deploy -r http://localhost:30333 -w ../../wallet.json --in player/player.nef --manifest player/config.json [ hash160:NbuowFNug7QCPM3zYc8HGH5ZsWbHBdGLR9 ]

Copy hash of the player contract from the command's output to use it in the next sections:

...
Contract: e7e4910b3a83ce0eff180a3f5462030391fde830  <-- NOTE: take this value

This time we won't need to convert it to any other format and will be able to use it "as is".

4. Create a new player

To create a new player, invoke newPlayer function on the player contract and specify name of the player. In example below we create a player with the name demo:

neo-go contract invokefunction -r http://localhost:30333 -w ../../wallet.json e7e4910b3a83ce0eff180a3f5462030391fde830 newPlayer string:demo
neo-go contract invokefunction -r http://localhost:30333 -w ../../wallet.json 8d099cb315c03c1f040d8912f1f2a84e569ca50c buyItem string:demo string:Shortbow -- 'NP8wjGz3Wvxe4gUAkTbK2McR95Y4LM2jMW:None'