-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
👷 add claim v2 which lets frontend wallet make the transaction
- Loading branch information
Showing
4 changed files
with
215 additions
and
68 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,61 +1,119 @@ | ||
export async function claim(address, amount) { | ||
if (!Telegram.WebApp.initDataUnsafe.user) { | ||
console.error("User is not available"); | ||
return; | ||
} | ||
|
||
const data = { | ||
id: Telegram.WebApp.initDataUnsafe.user.id, | ||
user: Telegram.WebApp.initDataUnsafe.user.username, | ||
type: "claim", | ||
address, | ||
amount, | ||
}; | ||
|
||
console.log("Claiming data", data); | ||
|
||
try { | ||
const response = await fetch( | ||
"https://tg-api.konnektoren.help/api/v1/claim", | ||
{ | ||
method: "POST", | ||
headers: { | ||
"Content-Type": "application/json", | ||
}, | ||
body: JSON.stringify(data), | ||
}, | ||
); | ||
|
||
if (!response.ok) { | ||
throw new Error("Network response was not ok"); | ||
if (!Telegram.WebApp.initDataUnsafe.user) { | ||
console.error("User is not available"); | ||
return; | ||
} | ||
|
||
const result = await response.json(); | ||
|
||
if (result.success) { | ||
Telegram.WebApp.showPopup( | ||
{ | ||
title: "Claimed successfully", | ||
message: "Success! View transaction", | ||
buttons: [ | ||
{ id: "link", type: "default", text: "Open Explorer" }, | ||
{ type: "cancel" }, | ||
], | ||
}, | ||
function (btn) { | ||
if (btn === "link") { | ||
Telegram.WebApp.openLink(result.explorer_url); | ||
} | ||
}, | ||
); | ||
return Promise.resolve(result.explorer_url); | ||
} else { | ||
alert("Claim failed."); | ||
return Promise.reject(result); | ||
const data = { | ||
id: Telegram.WebApp.initDataUnsafe.user.id, | ||
user: Telegram.WebApp.initDataUnsafe.user.username, | ||
type: "claim", | ||
address, | ||
amount, | ||
}; | ||
|
||
console.log("Claiming data", data); | ||
|
||
try { | ||
const response = await fetch( | ||
"https://tg-api.konnektoren.help/api/v1/claim", | ||
{ | ||
method: "POST", | ||
headers: { | ||
"Content-Type": "application/json", | ||
}, | ||
body: JSON.stringify(data), | ||
}, | ||
); | ||
|
||
if (!response.ok) { | ||
throw new Error("Network response was not ok"); | ||
} | ||
|
||
const result = await response.json(); | ||
|
||
if (result.success) { | ||
Telegram.WebApp.showPopup( | ||
{ | ||
title: "Claimed successfully", | ||
message: "Success! View transaction", | ||
buttons: [ | ||
{id: "link", type: "default", text: "Open Explorer"}, | ||
{type: "cancel"}, | ||
], | ||
}, | ||
function (btn) { | ||
if (btn === "link") { | ||
Telegram.WebApp.openLink(result.explorer_url); | ||
} | ||
}, | ||
); | ||
return Promise.resolve(result.explorer_url); | ||
} else { | ||
alert("Claim failed."); | ||
return Promise.reject(result); | ||
} | ||
} catch (error) { | ||
console.error("Error:", error); | ||
alert("Error occurred while claiming."); | ||
return Promise.reject(error); | ||
} | ||
} | ||
|
||
export async function claim_v2(address, amount) { | ||
/* | ||
if (!Telegram.WebApp.initDataUnsafe.user) { | ||
console.error("User is not available"); | ||
return Promise.reject(new Error("User is not available")); | ||
} | ||
const data = { | ||
id: Telegram.WebApp.initDataUnsafe.user.id, | ||
user: Telegram.WebApp.initDataUnsafe.user.username, | ||
type: "claim", | ||
address, | ||
amount, | ||
}; | ||
*/ | ||
|
||
const data = { | ||
id: 1, | ||
user: "user", | ||
type: "claim", | ||
address, | ||
amount | ||
}; | ||
|
||
console.log("Claiming data", data); | ||
|
||
try { | ||
const response = await fetch( | ||
//"https://tg-api.konnektoren.help/api/v2/claim", | ||
"http://localhost:3000/api/v2/claim", | ||
{ | ||
method: "POST", | ||
headers: { | ||
"Content-Type": "application/json", | ||
}, | ||
body: JSON.stringify(data), | ||
}, | ||
); | ||
|
||
if (!response.ok) { | ||
throw new Error("Network response was not ok"); | ||
} | ||
|
||
const result = await response.json(); | ||
|
||
if (result.success) { | ||
return result; | ||
} else { | ||
alert("Claim failed."); | ||
return Promise.reject(result); | ||
} | ||
} catch (error) { | ||
console.error("Error:", error); | ||
alert("Error occurred while claiming."); | ||
return Promise.reject(error); | ||
} | ||
} catch (error) { | ||
console.error("Error:", error); | ||
alert("Error occurred while claiming."); | ||
return Promise.reject(error); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters