-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
148 lines (120 loc) · 4.44 KB
/
script.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
if (window.solanaWeb3) {
const { Connection, clusterApiUrl } = solanaWeb3;
function connectToSolana(network = 'devnet') {
const connection = new Connection(clusterApiUrl(network), 'confirmed');
console.log(`Connected to Solana ${network}`);
return connection;
}
const connection = connectToSolana();
}
function isBackpackInstalled() {
return window.backpack && window.backpack.isBackpack;
}
async function connectToBackpack() {
if (isBackpackInstalled()) {
try {
const resp = await window.backpack.connect();
console.log('Connected to Backpack wallet:', resp.publicKey.toString());
return resp.publicKey;
} catch (err) {
console.error('Error connecting to Backpack wallet:', err);
}
} else {
console.error('Backpack wallet is not installed');
}
}
const connectButton = document.getElementById('connectWallet');
if (connectButton) {
connectButton.addEventListener('click', async () => {
const publicKey = await connectToBackpack();
if (publicKey) {
console.log('Connected public key:', publicKey.toString());
// Perform additional actions with the connected wallet
}
});
}
let form = document.querySelector('form')
form.addEventListener('submit', function(){
event.preventDefault();
const formData = new FormData(form);
const publickey = formData.get('#publickey');
const amount = formData.get('#amount');
const tokenType = formData.get('#tokenType');
const loadingMessage = document.getElementById('loadingMessage');
const successMessage = document.getElementById('successMessage');
const errorMessage = document.getElementById('errorMessage');
if (!isValidPublicKey(publickey)){
alert('Invalid public key!');
return;
}
if (!isValidAmount(amount)){
alert('Invalid amount');
return;
}
if (!isValidTokenType(tokenType)) {
alert('Please select a token type!');
return;
}
async function connectToBackpack() {
if (isBackpackInstalled()) {
try {
const resp = await window.backpack.connect();
console.log('Connected to Backpack wallet:', resp.publicKey.toString());
publicKey = resp.publicKey;
return publicKey;
} catch (err) {
console.error('Error connecting to Backpack wallet:', err);
}
} else {
console.error('Backpack wallet is not installed');
}
}
async function sendTokens(receiverAddress, amount) {
try {
const fromPublicKey = publicKey;
const toPublicKey = new PublicKey(receiverAddress);
const lamports = amount * solanaWeb3.LAMPORTS_PER_SOL; // Convert SOL to lamports
// Create a transaction
const transaction = new Transaction().add(
SystemProgram.transfer({
fromPubkey: fromPublicKey,
toPubkey: toPublicKey,
lamports,
})
);
// Sign and send the transaction
const { signature } = await window.backpack.signAndSendTransaction(transaction);
console.log('Transaction sent with signature:', signature);
successMessage.style.display = 'block';
} catch (error) {
console.error('Error sending tokens:', error);
errorMessage.style.display = 'block';
} finally {
loadingMessage.style.display = 'none'; // Hide the loading message
}
}
const sendButton = document.getElementById('sendTokens');
if (sendButton) {
sendButton.addEventListener('click', async () => {
const receiverAddress = 'ReceiverSolanaPublicKeyHere'; // Replace with the actual receiver's public key
const amount = 0.1; // Amount in SOL
await sendTokens(receiverAddress, amount);
});
}
form.reset();
// console.log('form sumbited')
});
function isValidPublicKey(publicKey) {
const isValid = /^0x[a-fA-F0-9]{40}$/.test(publicKey);
return isValid;
}
function isValidAmount(amount) {
const amountValue = parseFloat(amount);
return !isNaN(amountValue) && amountValue > 0;
}
function isValidTokenType(tokenType) {
return tokenType !== "";
}
function isBackpackInstalled() {
return window.backpack && window.backpack.isBackpack;
}