Skip to content

Commit c68f490

Browse files
committed
okkk
1 parent 2afcf72 commit c68f490

File tree

4 files changed

+250
-14
lines changed

4 files changed

+250
-14
lines changed

supabase/functions/withdrawRequest/.npmrc

Whitespace-only changes.
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"imports": {}
3+
}
Lines changed: 158 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,158 @@
1+
import "jsr:@supabase/functions-js/edge-runtime.d.ts";
2+
import { createClient } from "jsr:@supabase/supabase-js@2.48";
3+
import { corsHeaders } from "../_shared/cors.ts";
4+
5+
Deno.serve(async (req) => {
6+
const supabase = createClient(
7+
Deno.env.get('SUPABASE_URL'),
8+
Deno.env.get('SUPABASE_SERVICE_ROLE_KEY'),
9+
);
10+
11+
const headers = { ...corsHeaders };
12+
13+
if (req.method === 'OPTIONS') {
14+
return new Response(null, {
15+
status: 204,
16+
headers: {
17+
...headers
18+
}
19+
});
20+
}
21+
22+
const authHeader = req.headers.get('authorization');
23+
if (!authHeader) {
24+
return new Response(JSON.stringify({ response: 'Authorization header missing' }), {
25+
status: 401,
26+
headers: {
27+
...headers
28+
}
29+
});
30+
}
31+
32+
const token = authHeader.split(' ')[1];
33+
const { data: user, error } = await supabase.auth.getUser(token);
34+
if (error || !user) {
35+
return new Response(JSON.stringify({ response: 'Invalid JWT' }), {
36+
status: 401,
37+
headers: {
38+
...headers
39+
}
40+
});
41+
}
42+
43+
let uid: string | null = null;
44+
let network: string | null = null;
45+
let amount: number | null = null;
46+
let address: string | null = null;
47+
48+
try {
49+
const body = await req.json();
50+
uid = body.uid || null;
51+
amount = parseInt(body.amount) || null;
52+
network = body.network || null;
53+
address = body.address || null;
54+
} catch (error) {
55+
console.error("Failed to parse JSON body", error);
56+
return new Response(JSON.stringify({ response: "Failed to parse request body" + error }), {
57+
status: 400,
58+
headers: {
59+
...headers
60+
}
61+
});
62+
}
63+
64+
if (!uid || !amount || !network || !address) {
65+
return new Response(JSON.stringify({ response: "UID is required" }), {
66+
status: 400,
67+
headers: {
68+
...headers
69+
}
70+
});
71+
}
72+
73+
let fee: number = 0;
74+
if ((network === "spd" && ((amount < 10) || (amount > 10000))) || (network === "lgn" && ((amount < 2000) || (amount > 10000))) || (network === "btc" && ((amount < 20000) || (amount > 1000000))) || (network === "bnb" && ((amount < 100) || (amount > 100000000)))) {
75+
return new Response(JSON.stringify({ response: "Requested withdrawal amount is invalid." }), {
76+
status: 400,
77+
headers: {
78+
...headers
79+
}
80+
});
81+
}
82+
if (network === "spd") fee = String(amount).length;
83+
else if (network === "lgn") fee = 100;
84+
else if (network === "btc") fee = 3000;
85+
86+
const { data: balance, error: balanceError } = await supabase.from("udata")
87+
.select("balance_sats").eq("user_id", uid).single();
88+
89+
if (balanceError || !balance) return new Response(JSON.stringify({ response: "Something went wrong." }), {
90+
status: 400,
91+
headers: {
92+
...headers
93+
}
94+
});
95+
96+
if (balance.balance_sats < (amount + fee)) return new Response(JSON.stringify({ response: "Insufficient funds." }), {
97+
status: 400,
98+
headers: {
99+
...headers
100+
}
101+
});
102+
103+
104+
try {
105+
let from: string = "";
106+
const { data: recuser, error: userExistsErrorn } = await supabase
107+
.from("users")
108+
.select("username")
109+
.eq("id", uid)
110+
.single();
111+
if (userExistsErrorn || !recuser) {
112+
return new Response(JSON.stringify({ response: `User ${uid} does not exist in the 'users' table.` }), {
113+
status: 400,
114+
headers: {
115+
...headers
116+
}
117+
});
118+
} else {
119+
from = recuser.username;
120+
}
121+
122+
const { error: balanceupError } = await supabase.from("udata")
123+
.update({ "balance_sats": balance.balance_sats - amount - fee }).eq("user_id", uid).single();
124+
if (balanceupError) return new Response(JSON.stringify({ response: "Something went wrong." }), {
125+
status: 400,
126+
headers: {
127+
...headers
128+
}
129+
});
130+
131+
const { error: logError } = await supabase
132+
.from("logs")
133+
.insert([{ created_by: from, type: "WITHDRAWAL_REQUEST", attributes: "network->" + network + "\nfee->" + fee, message: "amount->"+amount+"\naddress->"+address }]);
134+
if (logError) {
135+
return new Response(JSON.stringify({ response: `Internal server error.` }), {
136+
status: 500,
137+
headers: {
138+
...headers
139+
}
140+
});
141+
}
142+
143+
return new Response(JSON.stringify({ response: "Withdrawal request was submitted!<br>Please wait patiently before we process it.", sc:true }), {
144+
status: 200,
145+
headers: {
146+
...headers
147+
}
148+
});
149+
} catch (error) {
150+
console.error("Error processing request", error);
151+
return new Response(JSON.stringify({ response: "Internal Server Error." }), {
152+
status: 500,
153+
headers: {
154+
...headers
155+
}
156+
});
157+
}
158+
});

u/wallet.js

Lines changed: 89 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -144,49 +144,117 @@ function updateFee(num, onNode, dedc) {
144144
num.value = numv;
145145
}
146146

147-
function finalizeWithdraw(amount, status) {
148-
149-
}
150-
151147
function updateNetwork(to) {
148+
var nodes = [document.getElementById("wnspd"), document.getElementById("wnlgn"), document.getElementById("wnbtc"), document.getElementById("wnbnb")];
152149
var mn = document.getElementById("wtminimum");
153150
var mx = document.getElementById("wtmaximum");
154151
var fee = document.getElementById("withdrawfee");
155152
var amount = document.getElementById("withdrawamount");
156153
var spad = document.getElementById("speedad");
157-
var ded = document.getElementById('withdrawdeduct')
154+
var bnad = document.getElementById("binancead");
155+
var ded = document.getElementById('withdrawdeduct');
156+
for (i of nodes) i.style.display = "none";
157+
spad.style.display = "none";
158+
bnad.style.display = "none";
158159
if (to === "spd") {
159160
mn.innerHTML = "10";
160161
mx.innerHTML = "10000";
161162
fee.innerHTML = "2";
162163
amount.min = 10;
163164
amount.max = 10000;
164165
spad.style.display = "block";
166+
nodes[0].style.display = "flex";
165167
} else if (to === "lgn") {
166168
mn.innerHTML = "2000";
167169
mx.innerHTML = "10000";
168170
fee.innerHTML = "100";
169171
amount.min = 2000;
170172
amount.max = 100000;
171-
spad.style.display = "none";
173+
nodes[1].style.display = "flex";
172174
} else if (to === "btc") {
173175
mn.innerHTML = "20000";
174176
mx.innerHTML = "1000000";
175177
fee.innerHTML = "3000";
176178
amount.min = 20000;
177179
amount.max = 1000000;
178-
spad.style.display = "none";
180+
nodes[2].style.display = "flex";
179181
} else if (to === "bnb") {
180182
mn.innerHTML = "100";
181183
mx.innerHTML = "100000000";
182184
fee.innerHTML = "0";
183185
amount.min = 100;
184186
amount.max = 100000000;
185-
spad.style.display = "none";
187+
bnad.style.display = "block";
188+
nodes[3].style.display = "flex";
186189
}
187190
updateFee(amount, fee, ded);
188191
}
189192

193+
async function finalizeWithdraw(amount, network, status) {
194+
var address = document.getElementById("withdrawaddress" + network).value;
195+
status.innerHTML = "Please wait...";
196+
if (!address || !address.value) {
197+
if ((network === "spd") || (network === "btc")) status.innerHTML = "Please enter a valid address.";
198+
if (network === "lgn") status.innerHTML = "Please enter a valid invoice.";
199+
if ((network === "bnb")(/^\d+$/.test(address))) status.innerHTML = "Please enter a valid Binance ID.";
200+
return;
201+
}
202+
if ((network === "spd") && ((address.includes("@speed.app") && !/[0-9A-Za-z\._]+/.test(address.substring(0, address.length - 10))) || !/[0-9A-Za-z\._]+/.test(address))) {
203+
status.innerHTML = "Please enter a valid Speed Wallet address.";
204+
return;
205+
}
206+
if ((network === "lgn") && !address.startsWith("lnbc")) {
207+
status.innerHTML = "Please enter a valid invoice.";
208+
return;
209+
}
210+
if ((network === "bnb") && !/^\d+$/.test(address)) {
211+
status.innerHTML = "Please enter a valid Binance ID.";
212+
return;
213+
}
214+
215+
if ((network === "spd" && ((amount < 10) || (amount > 10000))) || (network === "lgn" && ((amount < 2000) || (amount > 10000))) || (network === "btc" && ((amount < 20000) || (amount > 1000000))) || (network === "bnb" && ((amount < 100) || (amount > 100000000)))) {
216+
status.innerHTML = "Please enter a valid amount.";
217+
}
218+
219+
var bls = await getBalance((await supabase.auth.getSession()).data.session?.user.id);
220+
if (!bls) {
221+
location.href = "login.html";
222+
return;
223+
}
224+
bls = bls[2];
225+
226+
var fee = 0;
227+
if (network === "spd") fee = amount.length;
228+
if (network === "lgn") fee = 100;
229+
if (network === "btc") fee = 3000;
230+
231+
if (bls < (amount + fee)) {
232+
status.innerHTML = "Insufficient funds."
233+
return;
234+
}
235+
236+
await fetch('https://jwpvozanqtemykhdqhvk.supabase.co/functions/v1/withdrawRequest', {
237+
method: 'POST',
238+
headers: {
239+
'Content-Type': 'application/json',
240+
'authorization': `Bearer ${(await supabase.auth.getSession()).data.session?.access_token}`
241+
},
242+
body: JSON.stringify({ uid: (await supabase.auth.getSession()).data.session?.user.id, network: network, amount: amount, address: address })
243+
})
244+
.then(response => response.json())
245+
.then(data => {
246+
if (!data.sc) {
247+
status.innerHTML = "Error: " + data.response;
248+
} else {
249+
status.innerHTML = data.response;
250+
loadWallet();
251+
}
252+
})
253+
.catch((error) => {
254+
console.error('Error invoking function:', error);
255+
});
256+
}
257+
190258
function withdraw() {
191259
popup("Withdraw Bitcoin Satoshi",
192260
`
@@ -204,6 +272,10 @@ function withdraw() {
204272
< speed wallet ad >
205273
</div>
206274
275+
<div id="binancead" style="display:none">
276+
< binance ad >
277+
</div>
278+
207279
<p>
208280
Minimum Withdrawal: <span id="wtminimum">10</span> Satoshis<br>
209281
Maximum Withdrawal: <span id="wtmaximum">10000</span> Satoshis<br>
@@ -219,22 +291,25 @@ function withdraw() {
219291
<label for="withdrawaddressspd">Speed Wallet Address:</label>
220292
<input id="withdrawaddressspd" type="text" placeholder="johndoe@speed.app">
221293
</div>
222-
<div class="input" id="wnlgn">
294+
<div class="input" id="wnlgn" style="display:none">
223295
<label for="withdrawaddresslgn">Lightning Invoice:</label>
296+
<div class="halve">
224297
<input id="withdrawaddresslgn" type="text" placeholder="lnbc1500n1pw9q5g0pp5g0g0g0g0g0g0g0g0g0g0g0g">
225-
</div>
226-
<div class="input" id="wnbtc">
298+
<button onclick="popup('Lightning Invoice','Do not make the invoice contain a set amount of money to withdraw!<br>Doing so may result in your transaction being rejected.')">?</button>
299+
</div></div>
300+
<div class="input" id="wnbtc" style="display:none">
227301
<label for="withdrawaddressbtc">Bitcoin Address:</label>
228302
<input id="withdrawaddressbtc" type="text" placeholder="1A1zP1eP5QGefi2DMPTfTL5SLmv7DivfNa">
229303
</div>
230-
<div class="input" id="wnbnb">
304+
<div class="input" id="wnbnb" style="display:none">
231305
<label for="withdrawaddressbnb">Binance ID:</label>
232306
<input id="withdrawaddressbnb" type="text" placeholder="12345678">
233307
</div>
234308
<p>Total to be deducted from your wallet: <span id="withdrawdeduct" style="font-weight: bold">12</span> Satoshis</p>
235-
<p style="color: #ccc; text-align: center;">Withdrawals are processed manually.<br>If you don't receive your Satoshis within 7 days, please contact the support.</p>
309+
<p style="color: #ccc; text-align: center;">Withdrawals are processed manually.<br>If you don't receive your Satoshis within 7 days, please contact the support.<br>
310+
If your withdrawal gets rejected, you will be fully refunded (fee included).</p>
236311
<p id="withdrawstatus" style="font-weight: bold;text-align:center"></p>
237-
<button class="fullwidth" onclick="finalizeWithdraw()">Request Withdrawal</button>
312+
<button class="fullwidth" onclick="finalizeWithdraw(document.getElementById('withdrawamount').value, document.getElementById('withdrawnetwork').value, document.getElementById('withdrawstatus'))">Request Withdrawal</button>
238313
<p style="margin:0">
239314
`
240315
);

0 commit comments

Comments
 (0)