-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathitems.js
87 lines (82 loc) · 3.44 KB
/
items.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
import {itemsContract, tokenContract} from "./contract.js";
import { getWalletAddress } from "./wallet.js";
const buyItem = async (buy_button) => {
const initialText = buy_button.textContent;
buy_button.textContent = "Loading..."
const item = buy_button.parentElement;
// const tokenID = 1;
const tokenID = item.getElementsByClassName("nft-id")[0]?.textContent;
const wallet = await getWalletAddress();
const items = await itemsContract.methods.items(tokenID).call();
console.log("TOKEN ID", tokenID)
console.log("ITEMS", items);
const price = items.price;
let tx;
let txData;
if (items.itemType === "0") {
tx = itemsContract.methods.buyItem(tokenID, 1);
txData = {
from: wallet,
value: price
}
} else if (items.itemType === "1") {
const tokensAvailable = await tokenContract.methods.balanceOf(wallet).call();
if (Number(tokensAvailable) < price) {
alert(`Not enough AGOS! You need ${Math.round(price / 1e18)} AGOS to make this transaction`);
return
}
const allowance = await tokenContract.methods.allowance(wallet, itemsContract._address).call()
if (Number(allowance) < price) {
const maxUint = "115792089237316195423570985008687907853269984665640564039457584007913129639935"; // = 2^256 - 1, from https://etherscan.io/tx/0xad66b94f5bae8221c2e862680cdcb9e1ff24183db0579de1618d40892a39ffa4
const approveTx = tokenContract.methods.approve(itemsContract._address, maxUint);
const approveTxData = { from: wallet };
const estimatedGas = await approveTx.estimateGas(approveTxData).catch((e) => {
buy_button.textContent = initialText;
})
await approveTx.send({...approveTxData, gasLimit: estimatedGas + 5000}).catch((e) => {
buy_button.textContent = initialText;
if (e.code === 4001) {
throw e;
}
})
}
tx = itemsContract.methods.claimItem(tokenID, 1);
txData = {
from: wallet
}
}
const estimatedGas = await tx.estimateGas(txData).catch((e) => {
buy_button.textContent = initialText;
});
await tx.send({...txData, gasLimit: estimatedGas ?? 100000 + 5000}).then((r) => {
console.log(r);
buy_button.textContent = initialText;
}).catch((e) => {
buy_button.textContent = initialText;
})
};
const setRemainingTotalAmounts = async (buy_button) => {
const item = buy_button.parentElement;
const tokenID = item.getElementsByClassName("nft-id")[0]?.textContent;
const itemInfo = await itemsContract.methods.items(tokenID).call();
const [remaining, total] = [itemInfo.maxSupply - itemInfo.mintedSupply, itemInfo.maxSupply];
const remainingElem = item.getElementsByClassName("remaining-amount")[0];
const totalElem = item.getElementsByClassName("total-amount")[0];
remainingElem.textContent = remaining;
totalElem.textContent = total;
}
export const insertItemLinks = () => {
const items = document.getElementsByClassName("buy-button");
if (items) {
Array.from(items).forEach(async (item) => {
item.onclick = async () => {
await buyItem(item)
}
try {
await setRemainingTotalAmounts(item);
} catch (e) {
console.log(e, item)
}
})
}
}