app.js
4.11 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
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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
App = {
web3Provider: null,
contracts: {},
init: function() {
$.getJSON('../real-estate.json', function(data){
var list = $('#list');
var template = $('#template');
for(i=0;i<data.length;i++){
template.find('img').attr('src',data[i].picture);
template.find('.id').text(data[i].id);
template.find('.type').text(data[i].type);
template.find('.area').text(data[i].area);
template.find('.price').text(data[i].price);
list.append(template.html());
}
});
return App.initWeb3();
},
initWeb3: function() {
if (typeof web3 !== 'undefined'){ // web3 가 존재하면
App.web3Provider = web3.currentProvider;
web3 = new Web3(web3.currentProvider); // 공급자의 정보를 바탕으로 주입
}
else
{
App.web3Provider = new web3.providers.HttpProvider('http://localhoat:8545'); // 주입된 것이 없다면 로컬 RPC 서버에 연결하여 공급자의 정보를 가져옴
web3 = new Web3(web3.web3Provider);
}
return App.initContract();
},
initContract: function() {
$.getJSON('RealEstate.json', function(data){
App.contracts.RealEstate = TruffleContract(data);
App.contracts.RealEstate.setProvider(App.web3Provider);
return App.loadRealEstates();
});
},
buyRealEstate: function() {
var id = $('#id').val();
var name = $('#name').val();
var price = $('#price').val();
var age = $('#age').val();
web3.eth.getAccounts(function(error, accounts){
if(error){
console.log(error);
}
var account = accounts[0];
App.contracts.RealEstate.deployed().then(function(instance){
var nameUtf8Encoded = utf8.encode(name);
return instance.buyRealEstate(id,web3.toHex(nameUtf8Encoded), age, {from: account, value: price});
}).then(function(){
$('#name').val('');
$('#age').val('');
$('#buyModal').modal('hide');
}).catch(function(err){
console.log(err.message);
});
});
},
loadRealEstates: function() {
App.contracts.RealEstate.deployed().then(function(instance){
return instance.getAllbuyers.call();
}).then(function(buyers){
for(i = 0; i < buyers.length; i++) {
if (buyers[i] !== '0x0000000000000000000000000000000000000000') { // 빈 주소가 없다면 매물이 팔린 것
var imgType = $('.panel-realEstate').eq(i).find('img').attr('src').substr(7);
switch(imgType) {
case 'apartment.jpg':
$('.panel-realEstate').eq(i).find('img').attr('src','images/apartment_sold.jpg')
break;
case 'townhouse.jpg':
$('.panel-realEstate').eq(i).find('img').attr('src','images/townhouse_sold.jpg')
break;
case 'house.jpg':
$('.panel-realEstate').eq(i).find('img').attr('src','images/house_sold.jpg')
break;
}
$('.panel-realEstate').eq(i).find('.btn-buy').text('매각').attr('disabled', true);
$('.panel-realEstate').eq(i).find('.btn-buyerInfo').removeAttr('style');
}
}
}).catch(function(err){
console.log(err.message);
})
},
listenToEvents: function() {
}
};
$(function() {
$(window).load(function() {
App.init();
});
$('#buyModal').on('show.bs.modal', function(e){
var id = $(e.relatedTarget).parent().find('.id').text();
var price = web3.toWei(parseFloat($(e.relatedTarget).parent().find('.price').text() || 0), "ether");
$(e.currentTarget).find('#id').val(id);
$(e.currentTarget).find('#price').val(price);
});
$('#buyerInfoModal').on('show.bs.modal', function(e){
var id = $(e.relatedTarget).parent().find('.id').text();
App.contracts.RealEstate.deployed().then(function(instance){
return instance.getBuyerInfo.call(id);
}).then(function(buyerInfo){
$(e.currentTarget).find('#buyerAddress').text(buyerInfo[0]);
$(e.currentTarget).find('#buyerName').text(web3.toUtf8(buyerInfo[1]));
$(e.currentTarget).find('#buyerAge').text(buyerInfo[2]);
}).catch(function(err){
console.log(err.message);
})
});
});