From 285cca664a3bf71b2c7a0b96cfc8ea727ba60bea Mon Sep 17 00:00:00 2001 From: Clement Date: Mon, 14 Aug 2023 15:36:09 +0200 Subject: [PATCH] add contracts --- contracts/FavoriteNumber.sol | 15 +++++++++ contracts/MusiciansManager.sol | 40 ++++++++++++++++++++++++ contracts/Owner.sol | 18 +++++++++++ contracts/Wallet.sol | 56 ++++++++++++++++++++++++++++++++++ 4 files changed, 129 insertions(+) create mode 100644 contracts/FavoriteNumber.sol create mode 100644 contracts/MusiciansManager.sol create mode 100644 contracts/Owner.sol create mode 100644 contracts/Wallet.sol diff --git a/contracts/FavoriteNumber.sol b/contracts/FavoriteNumber.sol new file mode 100644 index 0000000..ee45577 --- /dev/null +++ b/contracts/FavoriteNumber.sol @@ -0,0 +1,15 @@ +// SPDX-License-Identifier: MIT +pragma solidity >=0.4.22 <0.9.0; + +contract FavoriteNumber { + + uint favoriteNumber; + + function setFavoriteNumber(uint _favoriteNumber) public { + favoriteNumber = _favoriteNumber; + } + + function getFavoriteNumber() public view returns (uint) { + return favoriteNumber; + } +} diff --git a/contracts/MusiciansManager.sol b/contracts/MusiciansManager.sol new file mode 100644 index 0000000..f160fb6 --- /dev/null +++ b/contracts/MusiciansManager.sol @@ -0,0 +1,40 @@ +// SPDX-License-Identifier: MIT +pragma solidity >=0.4.22 <0.9.0; + +import "./Owner.sol"; + +contract MusiciansManager is Owner{ + + event musicianCreated(string _artistName); + event trackAdded(string _artistName, string _title); + event getTheTracks(Track[] _tracks); + + struct Track { + string title; + uint duration; + } + + struct Musician { + string name; + Track[] tracks; + } + + mapping (address => Musician) musicians; + + function addMusician(address _address, string memory _name) public isOwner() { + require(bytes(musicians[_address].name).length == 0, "Musician already exists"); + musicians[_address].name = _name; + emit musicianCreated(_name); + } + + function addTrack(address _address, string memory _title, uint _duration) external isOwner() { + require(bytes(musicians[_address].name).length > 0, "Musician does not exist"); + Track memory thisTrack = Track(_title, _duration); + musicians[_address].tracks.push(thisTrack); + emit trackAdded(musicians[_address].name, _title); + } + + function getTracks(address _address) external { + emit getTheTracks(musicians[_address].tracks); + } +} diff --git a/contracts/Owner.sol b/contracts/Owner.sol new file mode 100644 index 0000000..721511c --- /dev/null +++ b/contracts/Owner.sol @@ -0,0 +1,18 @@ +// SPDX-License-Identifier: MIT +pragma solidity >=0.4.22 <0.9.0; + +contract Owner{ + + address owner; + + constructor(){ + owner = msg.sender; + } + + modifier isOwner(){ + require(msg.sender == owner, "sender is not the owner"); + _; + } +} + + diff --git a/contracts/Wallet.sol b/contracts/Wallet.sol new file mode 100644 index 0000000..bfbd87c --- /dev/null +++ b/contracts/Wallet.sol @@ -0,0 +1,56 @@ +// SPDX-License-Identifier: MIT +pragma solidity >=0.4.22 <0.9.0; + + +import "./Owner.sol"; + +contract Wallet is Owner{ + + event received(uint _newAmount); + + event sended(uint _amount); + + struct Payment{ + uint amount; + uint timeStamp; + } + + struct Balance{ + uint totalBalance; + uint numPayment; + mapping(uint => Payment) payments; + } + + mapping(address => Balance) wallet; + + function getBalances() public isOwner() view returns(uint){ + return address(this).balance; + } + + function getBalance() public view returns(uint){ + return wallet[msg.sender].totalBalance; + } + + function withdrawAllMoney(address payable _to) public{ + uint amount = wallet[msg.sender].totalBalance; + wallet[msg.sender].totalBalance = 0; + _to.transfer(amount); + } + + function withdrawMoney(address payable _to, uint val) public{ + uint thisBalance = wallet[msg.sender].totalBalance; + require(thisBalance >= val,"not enough balance"); + //envoyer a l'addr _to + wallet[msg.sender].totalBalance -= val; + _to.transfer(val); + emit sended(val); + } + + receive() payable external{ + Payment memory thisPayment = Payment(msg.value, block.timestamp); + wallet[msg.sender].totalBalance += msg.value; + wallet[msg.sender].payments[wallet[msg.sender].numPayment] = thisPayment; + wallet[msg.sender].numPayment++; + emit received(wallet[msg.sender].totalBalance); + } +} \ No newline at end of file