From 9ee80f1c68c9f521dd48a8e101992e4d46d57a36 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Luis=20Fernando=20Ushi=C3=B1a?= Date: Tue, 26 Jul 2022 12:19:23 -0500 Subject: [PATCH 1/5] implementation of eventNewPokemon event --- PokemonFactory.sol | 38 ++++++++++++++++++++------------------ 1 file changed, 20 insertions(+), 18 deletions(-) diff --git a/PokemonFactory.sol b/PokemonFactory.sol index a3267da1..39223512 100644 --- a/PokemonFactory.sol +++ b/PokemonFactory.sol @@ -3,33 +3,35 @@ pragma solidity >=0.7.0 <0.9.0; contract PokemonFactory { - - struct Pokemon { - uint id; - string name; - } + struct Pokemon { + uint256 id; + string name; + } Pokemon[] private pokemons; - mapping (uint => address) public pokemonToOwner; - mapping (address => uint) ownerPokemonCount; + mapping(uint256 => address) public pokemonToOwner; + mapping(address => uint256) ownerPokemonCount; - function createPokemon (string memory _name, uint _id) public { - pokemons.push(Pokemon(_id, _name)); + event eventNewPokemon(Pokemon _pokemonCreated); + + function createPokemon(string memory _name, uint256 _id) public { + Pokemon memory pokemonCreated = Pokemon(_id, _name); + pokemons.push(pokemonCreated); pokemonToOwner[_id] = msg.sender; ownerPokemonCount[msg.sender]++; + + emit eventNewPokemon(pokemonCreated); } function getAllPokemons() public view returns (Pokemon[] memory) { - return pokemons; + return pokemons; } - - function getResult() public pure returns(uint product, uint sum){ - uint a = 1; - uint b = 2; - product = a * b; - sum = a + b; - } - + function getResult() public pure returns (uint256 product, uint256 sum) { + uint256 a = 1; + uint256 b = 2; + product = a * b; + sum = a + b; + } } From 390494f60d445449f84ba2b14a08d757de4eabe0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Luis=20Fernando=20Ushi=C3=B1a?= Date: Tue, 26 Jul 2022 12:28:51 -0500 Subject: [PATCH 2/5] validation with modifiers - challenge #2 --- PokemonFactory.sol | 20 +++++++++++++++++++- 1 file changed, 19 insertions(+), 1 deletion(-) diff --git a/PokemonFactory.sol b/PokemonFactory.sol index 39223512..ad747356 100644 --- a/PokemonFactory.sol +++ b/PokemonFactory.sol @@ -15,7 +15,11 @@ contract PokemonFactory { event eventNewPokemon(Pokemon _pokemonCreated); - function createPokemon(string memory _name, uint256 _id) public { + function createPokemon(string memory _name, uint256 _id) + public + NameMinimunTwoCharacters(_name) + IsGreaterThanZero(_id) + { Pokemon memory pokemonCreated = Pokemon(_id, _name); pokemons.push(pokemonCreated); pokemonToOwner[_id] = msg.sender; @@ -34,4 +38,18 @@ contract PokemonFactory { product = a * b; sum = a + b; } + + modifier IsGreaterThanZero(uint256 _id) { + require(_id > 0, "_id field should be greater than zero"); + _; + } + + modifier NameMinimunTwoCharacters(string memory _name) { + // this rule include bytes(_name).length > 0 that enssures "_name field should not be empty" + require( + bytes(_name).length >= 2, + "_name field should be have at least 2 characteres" + ); + _; + } } From 5dcc469bdf6a432291842b9578898b69fd11b5ee Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Luis=20Fernando=20Ushi=C3=B1a?= Date: Wed, 27 Jul 2022 11:31:55 -0500 Subject: [PATCH 3/5] add skills by pokemon --- PokemonFactory.sol | 71 ++++++++++++++++++++++++++++++++++++---------- 1 file changed, 56 insertions(+), 15 deletions(-) diff --git a/PokemonFactory.sol b/PokemonFactory.sol index ad747356..5f6a9430 100644 --- a/PokemonFactory.sol +++ b/PokemonFactory.sol @@ -6,6 +6,12 @@ contract PokemonFactory { struct Pokemon { uint256 id; string name; + Skill[] skills; + } + + struct Skill { + string name; + string description; } Pokemon[] private pokemons; @@ -13,32 +19,36 @@ contract PokemonFactory { mapping(uint256 => address) public pokemonToOwner; mapping(address => uint256) ownerPokemonCount; + // logic for pokemons + mapping(uint256 => uint256) public pokemonIndexed; + event eventNewPokemon(Pokemon _pokemonCreated); - function createPokemon(string memory _name, uint256 _id) - public - NameMinimunTwoCharacters(_name) - IsGreaterThanZero(_id) - { - Pokemon memory pokemonCreated = Pokemon(_id, _name); - pokemons.push(pokemonCreated); + // event eventAllPokemons(Pokemon[] _pokemonCreated); + + function createPokemon( + string memory _name, + uint256 _id, + string memory _skillName, + string memory _skillDescription + ) public NameMinimunTwoCharacters(_name) IsGreaterThanZero(_id) { + pokemons.push(); + uint256 lastIndex = pokemons.length - 1; + pokemons[lastIndex].id = _id; + pokemons[lastIndex].name = _name; + pokemons[lastIndex].skills.push(Skill(_skillName, _skillDescription)); + pokemonIndexed[_id] = lastIndex; + pokemonToOwner[_id] = msg.sender; ownerPokemonCount[msg.sender]++; - emit eventNewPokemon(pokemonCreated); + emit eventNewPokemon(pokemons[lastIndex]); } function getAllPokemons() public view returns (Pokemon[] memory) { return pokemons; } - function getResult() public pure returns (uint256 product, uint256 sum) { - uint256 a = 1; - uint256 b = 2; - product = a * b; - sum = a + b; - } - modifier IsGreaterThanZero(uint256 _id) { require(_id > 0, "_id field should be greater than zero"); _; @@ -52,4 +62,35 @@ contract PokemonFactory { ); _; } + + function addSkill( + uint256 _pokemonId, + string memory _skillName, + string memory _skillDescription + ) public IsGreaterThanZero(_pokemonId) { + // Solución al buscar directamente en un mapping + pokemons[pokemonIndexed[_pokemonId]].skills.push( + Skill(_skillName, _skillDescription) + ); + + // emit eventAllPokemons(pokemons); + // Solución al recorrer un for + // for (uint256 counter = 0; counter < pokemons.length; counter++) { + // if (pokemons[counter].id == _pokemonId) { + // pokemons[pokemonIndexed[_pokemonId]].skills.push( + // Skill(_skillName, _skillDescription) + // ); + + // emit eventAllPokemons(pokemons); + // } + // } + } + + modifier onlyExistingPokemon(uint256 _pokemonId) { + require( + bytes(pokemons[pokemonIndexed[_pokemonId]].name).length > 0, + "Please create a Pokemon to add skills." + ); + _; + } } From 9409e408a2ee486b2c20e6bb3f0f2a92061cbb9a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Luis=20Fernando=20Ushi=C3=B1a?= Date: Wed, 27 Jul 2022 12:26:47 -0500 Subject: [PATCH 4/5] add weakness - challenge #4 --- PokemonFactory.sol | 32 +++++++++++++++++++++++++------- 1 file changed, 25 insertions(+), 7 deletions(-) diff --git a/PokemonFactory.sol b/PokemonFactory.sol index 5f6a9430..6195f934 100644 --- a/PokemonFactory.sol +++ b/PokemonFactory.sol @@ -7,6 +7,7 @@ contract PokemonFactory { uint256 id; string name; Skill[] skills; + Aweakness[] aweakness; } struct Skill { @@ -14,6 +15,17 @@ contract PokemonFactory { string description; } + struct Aweakness { + WeaknessStatus weaknesName; + } + + enum WeaknessStatus { + Fire, + Psychic, + Flying, + Ice + } + Pokemon[] private pokemons; mapping(uint256 => address) public pokemonToOwner; @@ -30,13 +42,17 @@ contract PokemonFactory { string memory _name, uint256 _id, string memory _skillName, - string memory _skillDescription + string memory _skillDescription, + WeaknessStatus _weaknessStatus ) public NameMinimunTwoCharacters(_name) IsGreaterThanZero(_id) { pokemons.push(); uint256 lastIndex = pokemons.length - 1; pokemons[lastIndex].id = _id; pokemons[lastIndex].name = _name; - pokemons[lastIndex].skills.push(Skill(_skillName, _skillDescription)); + addSkill(_id, _skillName, _skillDescription); + // pokemons[lastIndex].skills.push(Skill(_skillName, _skillDescription)); + addAweakness(_id, _weaknessStatus); + // pokemons[lastIndex].aweakness.push(Aweakness(_weaknessStatus)); pokemonIndexed[_id] = lastIndex; pokemonToOwner[_id] = msg.sender; @@ -86,11 +102,13 @@ contract PokemonFactory { // } } - modifier onlyExistingPokemon(uint256 _pokemonId) { - require( - bytes(pokemons[pokemonIndexed[_pokemonId]].name).length > 0, - "Please create a Pokemon to add skills." + function addAweakness(uint256 _pokemonId, WeaknessStatus _weaknessStatus) + public + IsGreaterThanZero(_pokemonId) + { + // Solución al buscar directamente en un mapping + pokemons[pokemonIndexed[_pokemonId]].aweakness.push( + Aweakness(_weaknessStatus) ); - _; } } From 5f23d79eccc6740a4f1f25b9fb307981b8f3d3b4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Luis=20Fernando=20Ushi=C3=B1a?= Date: Wed, 27 Jul 2022 12:41:19 -0500 Subject: [PATCH 5/5] add readme --- README.md | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) create mode 100644 README.md diff --git a/README.md b/README.md new file mode 100644 index 00000000..f5511239 --- /dev/null +++ b/README.md @@ -0,0 +1,16 @@ +# Pokemon Factory + +Smart contract focused to implement Events, Validation, Types, in a programing way within a simulation of creation of Pokemon with his skills, weakness. + +To run this contract + +The best way to review this contract use `remix.ethereum.org` + +Create a new contract `copy/paste` this sample + +- `Ctrl+S` to compile +- Deploy +- Play on `createPokemon` button +- Then you can add new skills with `addSkill` button and weakness with `addAweakness` button + +Enjoy it!