RealEstate.sol
2.27 KB
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
pragma solidity ^0.4.23;
contract RealEstate {
struct Buyer {
address buyeraddress;
bytes32 name;
uint age;
}
event LogBuyRealEstate( // Event 발생 시에 이 또한 블록에도 등록이 된다
// Front에서 몇 번 유저가 몇 번 매물을 구매 했는지 띄워줄 것이다
// 따라서 두개의 속성이 필요함
address _buyer,
uint _id
);
mapping(uint => Buyer) public buyerInfo; // 매물의 아이디를 키값으로 하면 벨류 값으로 매입자 정보를 제공
address public owner; // public으로 만들면 getter가 자동으로 생성된다
address[10] public buyers; // 매물이 열 개니까 살 수 있는 것도 10개
constructor () public {
owner = msg.sender; // 컨트랙 배포 시 어떤 계정을 통해 배포가 된다
// 배포 시에 생성자를 호출이 되는데 생성자 안에 있는 이 라인을 읽음 (msg.sneder)
}
function buyRealEstate(uint _id, bytes32 _name, uint _age) public payable { // payable: 함수가 이더를 받아야 할 때 쓰는 것. 매입자가 매입을 했을 때 metamask가 뜨고 이 함수로 ETH를 보냄
require(_id >=0 && _id <= 9);
buyers[_id] = msg.sender; // 현재 이 함수를 사용하고 있는 것의 주소
// 또한 struct에 다른 정보들도 저장할 것임
buyerInfo[_id] = Buyer(msg.sender,_name,_age); // 매물을 매입한 것들에 대해 정보 입력. 배열과 Mapping해서 가져오는 것
owner.transfer(msg.value); // 매입가를 owner 주소로 송금. ether를 계정에서 계정 간 이동시 transfer를 쓴다
// msg.value는 이 함수를 통해 넘어온 value를 의미 msg.VALUE는 WEI만 허용하므로 front-end에서 wei to eth가 필요
emit LogBuyRealEstate(msg.sender, _id); // 이벤트 발생시키겠다는 구문
}
function getBuyerInfo(uint _id) public view returns (address,bytes32, uint){ // 매물 아이디로 검색
Buyer memory buyer = buyerInfo[_id];
return (buyer.buyeraddress, buyer.name, buyer.age);
}
function getAllbuyers() public view returns (address[10]){
return buyers;
}
}