Final

What have you done? You've played with Spacemuffins... 🙄 You're an Amiral! In the following tutorial, you are going to start assigning Spacecrypts to pilots.

Project

All spacecrypts must have a pilot. At first, the spacecrypt is in the factory, but when it leaves, its ownership might be transfered from one pilot to another. It's a wild cryptospace out there.

Within SpacecryptFactory contract, you must ensure that such Spacecrypts management is possible. Therefore, you should:

  • Declare a new mapping from pilots (address) to its amount of spacecrypts (uint)
  • Take a look at hasSpaceships modifier, guess what it does
  • Finally, declare transferSpacecrypt, a public function transfering an amount (uint) of spacecrypts from the sender to another pilot (address). It takes two arguments, one address and one uint, in this order

Exercise
Correct!
False!

Loading...

Improve your own spacecrypt factory down there
pragma solidity ^0.4.24; contract SpacecryptFactory { bytes32 public name = 'SuperCryptor 2000'; address public owner = msg.sender; uint public numberOfSpacecrypts = 0; // Declare pilotsToSpacecrypts here :) modifier onlyOwner() { require(msg.sender == owner); _; } // Wow, a brand new modifier! modifier hasSpaceships(address addr, uint amount) { require(amount <= pilotToSpacecrypts[addr]); _; } function produce(uint numberOfUnits) public onlyOwner { numberOfSpacecrypts += numberOfUnits; pilotToSpacecrypts[owner] += numberOfUnits; } // transferSpacecrypt function should be there // you can use hasSpaceships to check if the sender can transfer // it will use your declared mapping function transferFactory(address newOwner) public { owner = newOwner; } }

Going further

Previous contract was an introduction. The daily life of pilots may prevent them from doing the transfer. Most of them prefer to allow trusty people to acquire their Spacecrypt in case they are unable to do so. To match their needs, you will need to use allowedTransfer. You need to implement the following:

  • allowance to check how much spaceship a pilot has authorised another pilot to retrieve
  • approve which is called by a pilot to allow another one
  • transferSpaceshipFrom to execute a previously allowed trade. Be careful about the amount of spaceships possessed by the pilot

Exercise
Correct!
False!

Loading...

Improve your own spacecrypt factory down there
pragma solidity ^0.4.24; contract SpacecryptFactory { bytes32 public name = 'SuperCryptor 2000'; address public owner = msg.sender; mapping (address => uint256) public pilotToSpacecrypts; uint public numberOfSpacecrypts = 0; // An even more fantastic mapping mapping (address => mapping (address => uint256)) private allowedTransfer; modifier onlyOwner() { require(msg.sender == owner); _; } modifier hasSpaceships(address addr, uint amount) { require(amount <= pilotToSpacecrypts[addr]); _; } function produce(uint numberOfUnits) public onlyOwner { numberOfSpacecrypts += numberOfUnits; pilotToSpacecrypts[owner] += numberOfUnits; } function transferSpacecrypt(address to, uint amount) public hasBalance(msg.sender, amount) { pilotToSpacecrypts[msg.sender] = pilotToSpacecrypts[msg.sender] - amount; pilotToSpacecrypts[to] = pilotToSpacecrypts[to] + amount; } // write `allowance` function here // don't forget it's mutability parameter // `approve` function set on pilot allowance // it takes an `address` and a `uint` // to `transferSpacecryptFrom`, the sender should be allow by the pilot (using require ;)) and have enough // it takes `address` of the transfering pilot, `address` of the receiving pilot and an amount (`uint`) function transferFactory(address newOwner) public { owner = newOwner; } }

Congratulations, you've been able to start production of Spacecrypt! Your plan to conquer the crypto space has started 😈

This contract has been inspired by ERC20.sol. It is a model to help you design ERC20 token contract in the future.

results matching ""

    No results matching ""