-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmintScript.js
More file actions
110 lines (101 loc) · 3.94 KB
/
Copy pathmintScript.js
File metadata and controls
110 lines (101 loc) · 3.94 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
let account;
let mintIndexForSale = 0;
let maxSaleAmount = 0;
let mintPrice = 0;
let mintStartBlockNumber = 0;
let mintLimitPerBlock = 0;
let blockNumber = 0;
let blockCnt = false;
function cntBlockNumber() {
if(!blockCnt) {
setInterval(function(){
blockNumber+=1;
document.getElementById("blockNubmer").innerHTML = "현재 블록: #" + blockNumber;
}, 1000);
blockCnt = true;
}
}
async function connect() {
const accounts = await klaytn.enable();
if (klaytn.networkVersion === 8217) {
console.log("메인넷");
} else if (klaytn.networkVersion === 1001) {
console.log("테스트넷");
} else {
alert("ERROR: 클레이튼 네트워크로 연결되지 않았습니다!");
return;
}
account = accounts[0];
caver.klay.getBalance(account)
.then(function (balance) {
document.getElementById("myWallet").innerHTML = `지갑주소: ${account}`
document.getElementById("myKlay").innerHTML = `잔액: ${caver.utils.fromPeb(balance, "KLAY")} KLAY`
});
await check_status();
}
async function check_status() {
const myContract = new caver.klay.Contract(ABI, CONTRACTADDRESS);
await myContract.methods.mintingInformation().call()
.then(function (result) {
console.log(result);
mintIndexForSale = parseInt(result[1]);
mintLimitPerBlock = parseInt(result[2]);
mintStartBlockNumber = parseInt(result[4]);
maxSaleAmount = parseInt(result[5]);
mintPrice = parseInt(result[6]);
document.getElementById("mintCnt").innerHTML = `${mintIndexForSale - 1} / ${maxSaleAmount}`;
document.getElementById("mintLimitPerBlock").innerHTML = `트랜잭션당 최대 수량: ${mintLimitPerBlock}개`;
document.getElementById('amount').max = mintLimitPerBlock;
document.getElementById("mintStartBlockNumber").innerHTML = `민팅 시작 블록: #${mintStartBlockNumber}`;
document.getElementById("mintPrice").innerHTML = `민팅 가격: ${caver.utils.fromPeb(mintPrice, "KLAY")} KLAY`;
})
.catch(function (error) {
console.log(error);
});
blockNumber = await caver.klay.getBlockNumber();
document.getElementById("blockNubmer").innerHTML = "현재 블록: #" + blockNumber;
cntBlockNumber();
}
async function whitelistMint() {
const accounts = await klaytn.enable();
if (klaytn.networkVersion === 8217) {
console.log("메인넷");
} else if (klaytn.networkVersion === 1001) {
console.log("테스트넷");
} else {
alert("ERROR: 클레이튼 네트워크로 연결되지 않았습니다!");
return;
}
account = accounts[0];
const leafNodes = whitelist.map(addr => keccak256(addr));
const merkleTree = new MerkleTree(leafNodes, keccak256, { sortPairs: true });
const addr = keccak256(account);
const hexProof = merkleTree.getHexProof(addr);
const myContract = new caver.klay.Contract(ABI, CONTRACTADDRESS);
const amount = document.getElementById('amount').value;
await check_status();
if (maxSaleAmount + 1 <= mintIndexForSale) {
alert("모든 물량이 소진되었습니다.");
return;
} else if (blockNumber <= mintStartBlockNumber) {
alert("아직 민팅이 시작되지 않았습니다.");
return;
}
const total_value = BigNumber(amount * mintPrice);
try {
const gasAmount = await myContract.methods.whitelistMint(amount, hexProof).estimateGas({
from: account,
gas: 6000000,
value: total_value
})
result = await myContract.methods.whitelistMint(amount, hexProof).send({
from: account,
gas: gasAmount,
value: total_value
})
console.log(result)
} catch (error) {
console.log(error);
alert("민팅에 실패하였습니다.");
}
}