-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsimpleTokenBookStore.sol
81 lines (61 loc) · 2.13 KB
/
simpleTokenBookStore.sol
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
// SPDX-License-Identifier: MIT
pragma solidity 0.8.19;
contract Token{
//Token Owner
mapping (uint => address) tokenOwner;
// function to create a token
function createToken() public returns (uint) {
uint tokenID = block.timestamp;
tokenOwner[tokenID] = msg.sender;
return tokenID;
}
//change Owner
function changeTokenOwner(uint _tokenID,address newOwner) public {
tokenOwner[_tokenID] = newOwner;
}
}
contract BookStore {
struct Book {
string title;
uint8 price;
address owner;
bool sold;
}
//Using a dynamic array for storing book lists
uint[] booklist;
mapping (uint => Book) Books;
// fun setting book details
// set title price owner
function setBookDetails(uint _slno,string memory _title, uint8 _price) public {
Books[_slno] = Book(_title, _price, msg.sender,false);
booklist.push(_slno);
}
//Fn overloading: for registering an already sold book
function setBookDetails(uint _slno,string memory _title, uint8 _price, bool _soldornot) public {
Books[_slno] = Book(_title, _price, msg.sender,_soldornot);
booklist.push(_slno);
}
// buys - ownership transfer
// buyer provider ether > price
function buyBook(uint _slno) public payable {
require( msg.sender != Books[_slno].owner,"You are already the owner");
require(Books[_slno].sold,"Book is already sold");
if ((msg.value / 1 ether) == Books[_slno].price) {
Books[_slno].owner = msg.sender;
Books[_slno].sold = true;
} else if ((msg.value / 1 ether) > Books[_slno].price) {
Books[_slno].owner = msg.sender;
uint256 rm = msg.value - Books[_slno].price * 1 ether;
Books[_slno].sold = true;
payable(msg.sender).transfer(rm);
} else {
revert();
}
}
function getBookDetails(uint _slno) public view returns (Book memory) {
return (Books[_slno]);
}
function getBooklist() public view returns(uint [] memory) {
return booklist;
}
}