-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
98 lines (82 loc) · 3.21 KB
/
Copy pathscript.js
File metadata and controls
98 lines (82 loc) · 3.21 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
const connectButton = document.getElementById('connectButton');
const mintButton = document.getElementById('mintButton');
const tokenAmountInput = document.getElementById('tokenAmount');
const minTokens = 21000;
const rpcUrl = 'https://test-rpc-node-http.svmscan.io/';
let web3 = new Web3(Web3.givenProvider || rpcUrl);
const decimals = 8;
const savmchainId = 3110;
window.ethereum.on('connect', () => {
getAccounts((accounts) => {
if (accounts.length !== 0) {
connectButton.disabled = true;
mintButton.disabled = false;
}
})
})
connectButton.addEventListener('click', () => {
// Connect to MetaMask
if (window.ethereum) {
window.ethereum.request({ method: 'eth_requestAccounts' })
.then(() => {
connectButton.disabled = true;
mintButton.disabled = false;
})
.catch(err => {
console.error(err);
});
} else {
alert("MetaMask is not installed!");
}
});
function getChain(cb) {
window.ethereum.request({ method: 'eth_chainId' }).then((id) => {
const convertedId = web3.utils.hexToNumber(id);
cb(convertedId === savmchainId);
});
}
const contractAddress = '0x7B1998888821909e2900A82fA30a9486d8B8cC76'; // Replace with your contract address
const tokenContract = new web3.eth.Contract(tokenABI, contractAddress);
document.addEventListener('DOMContentLoaded', () => {
getTokenSupply();
});
function getTokenSupply() {
tokenContract.methods.totalSupply().call()
.then(supply => {
// Convert to 8 decimals
const supplyBN = new web3.utils.BN(supply); // Convert supply to a Big Number
const decimalsBN = new web3.utils.BN(10).pow(new web3.utils.BN(decimals)); // Calculate 10^decimals
const convertedValue = supplyBN.div(decimalsBN); // Divide supply by 10^decimals
const supplySpan = document.getElementById('supply');
supplySpan.textContent = convertedValue.toString();
})
.catch(err => {
console.error(err);
});
}
function getAccounts(cb) {
window.ethereum.request({ method: 'eth_accounts' }).then(cb);
}
mintButton.addEventListener('click', () => {
const amount = tokenAmountInput.value;
const amountBN = new web3.utils.BN(amount); // Convert amount to a Big Number
const decimalsBN = new web3.utils.BN(10).pow(new web3.utils.BN(decimals)); // Calculate 10^decimals
const amountInTokenUnits = amountBN.mul(decimalsBN); // Multiply amount by 10^decimals
getChain((correctChain) => {
if (!correctChain) return alert('Wrong Chain!\nUse SatoshiVM Testnet');
window.ethereum.request({ method: 'eth_accounts' })
.then(accounts => {
if (accounts.length === 0) {
console.error("No accounts found. Ensure MetaMask is connected.");
return;
}
tokenContract.methods.mint(amountInTokenUnits).send({ from: accounts[0] });
})
.then(result => {
console.log(`Minted ${amount} tokens.`);
})
.catch(err => {
console.error(err);
});
})
});