Files
web/apps/base-docs/base-learn/docs/structs/structs-exercise.md
Brian Doyle 566e5f5832 Rename Base Camp to Base Learn (#649)
* Rename Base Camp to Base Learn

* Rename Base Camp to Base Learn

* Update learn link

* change wallet type property from camel to snake case (#643)

* Update api key requirement, minor style updates (#642)

* Fix type and clarify inheritance ex (#655)

* Document Reth snapshot URLs (#651)

* feat(ecosystem): New additions to Ecosystem page (#647)

* feat(ecosystem): New additions to Ecosystem page

* chore(Ecosystem): Add image for Dynamic

* Update preparing-for-fault-proofs-on-base-sepolia.md (#633)

Update preparing-for-fault-proofs-on-base-sepolia

* Fix conflict

* fix conflict

---------

Co-authored-by: Brendan from DeFi <brendan.forster@coinbase.com>
Co-authored-by: Danyal Prout <danyal.prout@coinbase.com>
Co-authored-by: wbnns <hello@wbnns.com>
Co-authored-by: Olexandr Radovenchyk <radole1203@gmail.com>
2024-07-23 17:44:11 -04:00

2.8 KiB

title, description, hide_table_of_contents
title description hide_table_of_contents
Structs Exercise Exercise - Demonstrate your knowledge of structs. false

Create a contract that adheres to the following specifications.


Contract

Create a contract called GarageManager. Add the following in storage:

  • A public mapping called garage to store a list of Cars (described below), indexed by address

Add the following types and functions.

Car Struct

Implement a struct called Car. It should store the following properties:

  • make
  • model
  • color
  • numberOfDoors

Add Car Garage

Add a function called addCar that adds a car to the user's collection in the garage. It should:

  • Use msg.sender to determine the owner
  • Accept arguments for make, model, color, and number of doors, and use those to create a new instance of Car
  • Add that Car to the garage under the user's address

Get All Cars for the Calling User

Add a function called getMyCars. It should return an array with all of the cars owned by the calling user.

Get All Cars for Any User

Add a function called getUserCars. It should return an array with all of the cars for any given address.

Update Car

Add a function called updateCar. It should accept a uint for the index of the car to be updated, and arguments for all of the Car types.

If the sender doesn't have a car at that index, it should revert with a custom error BadCarIndex and the index provided.

Otherwise, it should update that entry to the new properties.

Reset My Garage

Add a public function called resetMyGarage. It should delete the entry in garage for the sender.


Submit your Contract and Earn an NFT Badge! (BETA)

:::info

Hey, where'd my NFT go!?

Testnets are not permanent! Base Goerli will soon be sunset, in favor of Base Sepolia.

As these are separate networks with separate data, your NFTs will not transfer over.

Don't worry! We've captured the addresses of all NFT owners on Base Goerli and will include them when we release the mechanism to transfer these NFTs to mainnet later this year! You can also redeploy on Sepolia and resubmit if you'd like!

:::

import CafeUnitTest from '../../../src/components/CafeUnitTest/index.jsx'


⚠️ Spoiler Alert: Open only if tests fail

Ensure your variable sizes align with their intended use, and consider the nuances of packing in Solidity. Resources: Solidity - Layout in Storage, Variables in Struct