-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
56 lines (51 loc) · 1.8 KB
/
index.js
File metadata and controls
56 lines (51 loc) · 1.8 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
const CONTRACT_ADDR = "0xa4Dbad9463BF3B107F27649f68a64740Ed1AEC3C";
class Demo{
constructor(_web3, _account) {
this.web3 = _web3;
this.account = _account;
this.contract = new this.web3.eth.Contract(CONTRACT_ABI, CONTRACT_ADDR);
}
async index_page() {
$("#addr").text(this.account);
let result = await this.contract.methods.query_user(this.account).call();
// (users[addr].exchanged,
// users[addr].ex_date,
// users[addr].ex_amount);
this.exchanged = result[0];
if(this.exchanged == true){
$("#exchange").prop('disabled', true);
$("#exchange_result").text("已经兑换过Token,1个地址只能兑换1次");
$("#ex_date").text(new Date(Number(result[1] * 1000)).toLocaleString());
$("#ex_amount").text(Number(this.web3.utils.fromWei(result[2])));
}
}
exchange() {
let amount = $("#amount").val();
this.contract.methods.exchange().send({value: this.web3.utils.toWei(amount), from: this.account}).then((tx) => {
$("#exchange_result").text("已经提交区块链确认,交易Hash:" + tx.transactionHash);
});
}
init() {
let that = this;
this.index_page();
$("#exchange").on("click", () => {
that.exchange();
});
}
}
window.addEventListener("load", async () => {
if (window.ethereum) {
try {
// Request account access if needed
window.ethereum.enable().then(accounts => {
let web3 = new Web3(window.ethereum);
let demo = new Demo(web3, accounts[0]);
demo.init();
});
} catch (error) {
alert(error);
}
} else {
alert("need dapp browser");
}
});