-
Notifications
You must be signed in to change notification settings - Fork 3
/
tokenMarketplace.php
255 lines (219 loc) · 9.89 KB
/
tokenMarketplace.php
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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
<?php
require_once("includes/header.php");
if(!User::isLoggedIn()) {
header("Location: signIn.php");
}
?>
<div class="column">
<h2>Buy / Sell DTC <img src="assets/images/icons/coin.svg" width="50px"></h2>
<form method="POST">
<p>
<h6>
Introducing "DecenTube Coin" (DTC) !<br><br>
Coin Buy Price: <span class="buy-token-price"></span> ETH per coin (₹ 5 per coin)<br>
Coin Sell Price: <span class="sell-token-price"></span> ETH per coin (₹ 4.75 per coin)<br><br>
Your last buy transaction was made on: <span class="last-buy-timestamp"></span><br>
Your last sell transaction was made on: <span class="last-sell-timestamp"></span><br><br>
Terms of Use:<br>
- A maximum of 1000 coins can be bought or sold in a single transaction.<br>
- Every buy/sell transaction can be made after a duration of 7 days since the previous transaction.
<br><br>
Add the following token address to your MetaMask wallet: <span class="token-address"></span><br>
Token Symbol: DTC<br>
Decimals: 3 <br>
<br><br>
You currently have : <span class="dtc-balance">0</span> DTC <br><br>
Total amount of coins: <span class="tokens-available"></span><br>
Amount of coins sold: <span class="tokens-sold"></span><br>
</h6>
</p>
<br>
<div class="form-group">
<span class="ico-sale-percent"></span> % of coins sold. Hurry! Invest in DTC before it gets too late
<div class="progress">
<div id="progress" class="progress-bar progress-bar-striped active progress-bar-animated" aria-valuemin="0" aria-valuemax="100">
</div>
</div>
</div>
<div class="form-group">
<div class="input-group">
<input id="numberOfTokens" class="form-control input-lg" type="number" name="number" value="1" min="1" pattern="[0-9]" max="1000">
</input>
<span class="input-group-btn">
<button type="submit" class="btn btn-primary btn-lg" onclick="buyTokens(); return false;">Buy Tokens</button>
</span>
<span class="input-group-btn">
<button type="submit" class="btn btn-primary btn-lg" onclick="sellTokens(); return false;">Sell Tokens</button>
</span>
</div>
</div>
</form>
</div>
<script>
function getUrlVars() {
var vars = {};
var parts = window.location.href.replace(/[?&]+([^=&]+)=([^&]*)/gi, function(m,key,value) {
vars[key] = value;
});
return vars;
}
window.addEventListener('load', async () => {
loadWeb3();
loadBlockchainData();
});
async function loadWeb3() {
// Modern dapp browsers...
if (window.ethereum) {
window.web3 = new Web3(ethereum);
try {
// Request account access if needed
console.log("window.ethereum");
ethereum.enable();
ethereum.autoRefreshOnNetworkChange = false;
ethereum.on('accountsChanged', function () {
//location.reload();
});
ethereum.on('networkChanged', function () {
//location.reload();
});
} catch (error) {
console.log(error); // User denied account access...
}
}
// Legacy dapp browsers...
else if (window.web3) {
window.web3 = new Web3(web3.currentProvider);
console.log("window.currentProvider");
// Acccounts always exposed
}
// Non-dapp browsers...
else {
var provider = new Web3.providers.HttpProvider('https://ropsten.infura.io/v3/ee7f14f8e4ef43dab2769edfcda8445f')
window.web3 = new Web3(provider)
alert('Non-Ethereum browser detected. You should consider trying MetaMask!');
console.log('Non-Ethereum browser detected. You should consider trying MetaMask!');
}
console.log("version");
var version = web3.version;
console.log(version);
// var currentAccount = await web3.eth.getAccounts();
// console.log(currentAccount);
}
async function loadBlockchainData() {
//Load account
window.currentAccount = await web3.eth.getAccounts();
window.currentAccount = window.currentAccount[0];
// $.getJSON( "assets/contracts/VideosSharing.json", function( json ) {
// console.log( "JSON Data received, name is " + json.contractName);
// window.VideosSharingContract = json;
// });
window.VideosSharingContract = <?php require_once("includes/VideosSharingJSON.php");?>;
console.log( "JSON Contract Data received");
window.networkId = await web3.eth.net.getId()
window.networkData = window.VideosSharingContract.networks[networkId];
if (window.networkData) {
window.videossharing = new web3.eth.Contract(window.VideosSharingContract.abi, window.networkData.address);
if(window.currentAccount == getUserEthAddress()){
await displayERC20Balance();
}
loadTokenPrices();
loadUserBalance();
updateProgressBar();
loadLastTxnTimes();
} else {
window.alert('Please switch to Ropsten Test Network in your MetaMask Wallet');
}
}
function getUserEthAddress() {
return '<?php echo $userLoggedInObj->getEthAddress(); ?>';
}
async function displayERC20Balance() {
var currBal = await window.videossharing.methods.balanceOf(window.currentAccount).call();
//currBal = parseInt(currBal) / 1000;
document.getElementsByClassName("balanceDisplay")[0].innerHTML = currBal + ' DTC';
}
async function loadTokenPrices() {
buyPrice = await window.videossharing.methods.tokenBuyPrice().call();
buyPrice = window.web3.utils.fromWei(buyPrice, 'ether');
sellPrice = await window.videossharing.methods.tokenSellPrice().call();
sellPrice = window.web3.utils.fromWei(sellPrice, 'ether');
$(".buy-token-price").text(buyPrice);
$(".sell-token-price").text(sellPrice);
}
async function loadUserBalance() {
userBalance = await window.videossharing.methods.balanceOf(window.currentAccount).call();
$(".dtc-balance").text(userBalance);
}
async function loadLastTxnTimes() {
lastBuyTime = await window.videossharing.methods.getBuyTimeStampValue(window.currentAccount).call();
lastSellTime = await window.videossharing.methods.getSellTimeStampValue(window.currentAccount).call();
if (lastBuyTime == '0') {
$(".last-buy-timestamp").text("NIL");
} else {
lastBuyTimeText = convertTimestampToDateTime(lastBuyTime);
$(".last-buy-timestamp").text(lastBuyTimeText);
}
if (lastSellTime == '0') {
$(".last-sell-timestamp").text("NIL");
} else {
lastSellTimeText = convertTimestampToDateTime(lastSellTime);
$(".last-sell-timestamp").text(lastSellTimeText);
}
}
function convertTimestampToDateTime(timestamp) {
var months_arr = ['Jan','Feb','Mar','Apr','May','Jun','Jul','Aug','Sep','Oct','Nov','Dec'];
var date = new Date(timestamp*1000);
var year = date.getFullYear();
var month = months_arr[date.getMonth()];
var day = date.getDate();
var hours = date.getHours();
var minutes = "0" + date.getMinutes();
var seconds = "0" + date.getSeconds();
var convdataTime = month+'-'+day+'-'+year+' '+hours + ':' + minutes.substr(-2) + ':' + seconds.substr(-2);
return convdataTime;
}
var rCatch;
async function buyTokens() {
var numberOfTokens = $('#numberOfTokens').val();
console.log("Buying " + numberOfTokens + " tokens");
buyPrice = await window.videossharing.methods.tokenBuyPrice().call();
await window.videossharing.methods.buyTokens(numberOfTokens).send(
{ from: window.currentAccount,
value: numberOfTokens * buyPrice
},function(error, result) {
if(error) {
alert("ERROR: Buy transaction failed :"+error.message);
}
else {
alert("Buy Transaction successful "+ result);
rCatch = result;
}
});
}
async function sellTokens() {
var numberOfTokens = $('#numberOfTokens').val();
console.log("Selling " + numberOfTokens + " tokens");
await window.videossharing.methods.sellTokens(numberOfTokens).send(
{ from: window.currentAccount},function(error, result) {
if(error) {
alert("ERROR: Sell transaction failed :"+error.message);
}
else {
alert("Sell Transaction successful "+result);
}
});
}
async function updateProgressBar() {
tokensSold = await window.videossharing.methods.tokensSold().call();
tokensSold = parseInt(tokensSold);
$('.token-address').html(window.networkData.address);
$('.tokens-sold').html(tokensSold);
$('.tokens-available').html(500000);
var progressPercent = 100 * (tokensSold / 500000);
$('#progress').css('width', progressPercent + '%');
$('.ico-sale-percent').html(progressPercent);
}
</script>
<?php
require_once("includes/footer.php");
?>