-
Notifications
You must be signed in to change notification settings - Fork 4
/
index.js
147 lines (131 loc) · 5.02 KB
/
index.js
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
import Web3 from 'web3'
import moment from 'moment'
import contractJSON from './build/contracts/AravindICOContract.json'
import tokenJSON from './build/contracts/AravindTestToken.json'
const web3 = new Web3(new Web3.providers.HttpProvider("https://ropsten.infura.io/your-api-key"));
const contract = new web3.eth.Contract(contractJSON.abi, "0xcc4de437aedb95381f6b33fc4aaafd50f1164dba")
const token = new web3.eth.Contract(tokenJSON.abi, "0x7fa2c3910277b78654ef751950311e91a3276b36")
/*Fetch all the details from contracts and update the UI*/
fetchFromContracts().then(contractData => {
$('#loadingAnim').hide()
!contractData.hasEnded ? $("#status").html('Aravind Token sale is open') :
$("#status").html('Aravind Token sale has ended')
$("#amount").append('<span>' + contractData.moneyRaised + ' Ether </span>')
$("#supply").append('<span>' + contractData.totalSupply + ' ART </span>')
$("#token-address").html(token._address)
$("#token-name").html(contractData.tokenName)
$("#token-symbol").html(contractData.tokenSymbol)
$("#token-decimals").html(contractData.tokenDecimals)
$("#rate").html(contractData.tokenRate)
$("#contract-address").html(contractData.tokenOwner)
$("#wallet-address").html(contractData.walletAddress)
$("#countdown").countdown(contractData.endTime, function (event) {
$(this).html(
event.strftime(
`<div class="timer-wrapper"><div class="time">%D</div><span class="text">days</span></div><div
class="timer-wrapper"><div class="time">%H</div><span class="text">hrs</span></div><div
class="timer-wrapper"><div class="time">%M</div><span class="text">mins</span></div><div
class="timer-wrapper"><div class="time">%S</div><span class="text">sec</span></div>`
)
);
});
});
/*Async function for red the contract properties*/
async function fetchFromContracts() {
let dataObj = {}
dataObj.moneyRaised = await fetchMoneyRaised();
dataObj.endTime = await fetchEndTime();
dataObj.totalSupply = await fetchTotalSupply();
dataObj.walletAddress = await fetchWalletAddress();
dataObj.tokenName = await fetchTokenName();
dataObj.tokenSymbol = await fetchTokenSymbol();
dataObj.tokenDecimals = await fetchTokenDecimals();
dataObj.tokenOwner = await fetchTokenOwner();
dataObj.tokenRate = await fetchTokenRate();
dataObj.hasEnded = await fetchContractStatus();
return dataObj;
}
function fetchMoneyRaised(){
return new Promise(resolve => {
contract.methods.weiRaised().call().then(function(etherBalance){
resolve(web3.utils.fromWei(etherBalance, 'ether'))
})
})
}
function fetchEndTime() {
return new Promise(resolve => {
contract.methods.endTime().call().then(function(endTime){
resolve(__WEBPACK_IMPORTED_MODULE_1_moment___default.a.unix(endTime).toDate())
})
})
}
function fetchTotalSupply() {
return new Promise(resolve => {
token.methods.totalSupply().call().then(function(totalSupply){
resolve(web3.utils.fromWei(totalSupply, 'ether'))
})
})
}
function fetchWalletAddress() {
return new Promise(resolve => {
contract.methods.wallet().call().then(function(walletAddress){
resolve(walletAddress)
})
})
}
function fetchTokenName() {
return new Promise(resolve => {
token.methods.name().call().then(function(tokenName){
resolve(tokenName)
})
})
}
function fetchTokenSymbol() {
return new Promise(resolve => {
token.methods.symbol().call().then(function(symbol){
resolve(symbol)
})
})
}
function fetchTokenDecimals() {
return new Promise(resolve => {
token.methods.decimals().call().then(function(decimals){
resolve(decimals)
})
})
}
function fetchTokenOwner() {
return new Promise(resolve => {
token.methods.owner().call().then(function(owner){
resolve(owner)
})
})
}
function fetchTokenRate() {
return new Promise(resolve => {
contract.methods.rate().call().then(function(rate){
resolve(rate)
})
})
}
function fetchContractStatus() {
return new Promise(resolve => {
contract.methods.hasEnded().call().then(function(ended){
resolve(ended)
})
})
}
/*Check token balance button click function to fetch balance*/
$( document ).ready(function() {
$("#balance").on( "click", function() {
if(web3.utils.isAddress($("#account-num").val())) {
token.methods.balanceOf($("#account-num").val()).call().then(function(res){
$('#check-bal').html('<p style="font-size: 14px;color: #fff;font-weight: bold;">You have '
+ web3.utils.fromWei(res, 'ether') + ' ART Tokens</p>')
})
} else {
$('#check-bal').html(`<p style="font-size: 14px;color: #fff;font-weight: bold;">
Please enter a valid address</p>`)
}
})
});