-
Notifications
You must be signed in to change notification settings - Fork 9
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: add an approval/funding UI #30
base: development
Are you sure you want to change the base?
Changes from 4 commits
bc37549
6719d29
dc3aaaa
68e1715
a220ca3
f6a4e17
4571b5e
287505d
a3caa79
9634f81
edd0772
3befb07
03b9dcf
6fd952e
9d1ffb8
19f3bbf
5b93446
5f48675
18adb4e
1337208
3c77a03
6478209
4605da3
e2149ad
89b0f04
ba36a2c
6bec149
1f81afa
8ea7037
7878d48
fa44c95
f421786
c526d60
b5c844a
f801b95
3ffcf15
bded1b3
8f8fa38
c22af00
c0ca66d
228f7d1
834b8dd
1bd7ca8
8105aab
9299ea3
23f3344
f04f3d1
656e862
58edb53
c906697
78fb771
b525724
326dbc2
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -10,15 +10,18 @@ const currentAllowanceAmount = document.querySelector(".current-allowance-amount | |
const approveButton = document.querySelector(".approve-button") as HTMLButtonElement; | ||
const revokeButton = document.querySelector(".revoke-button") as HTMLButtonElement; | ||
|
||
const red = "1px solid red"; | ||
const green = "1px solid #5af55a"; | ||
const grey = "1px solid rgba(255, 255, 255, 0.1)"; | ||
|
||
function isValidAddress(): boolean { | ||
//reset color | ||
addressInput.style.border = grey; | ||
|
||
const isValid = /^0x[a-fA-F0-9]{40}$/.test(addressInput.value); | ||
|
||
if (isValid) { | ||
addressInput.style.border = "1px solid #5af55a"; | ||
} else if (addressInput.value === "") { | ||
addressInput.style.border = "1px solid rgba(255, 255, 255, 0.1)"; | ||
} else { | ||
addressInput.style.border = "1px solid red"; | ||
if (!isValid && addressInput.value !== "") { | ||
addressInput.style.border = red; | ||
} | ||
|
||
return isValid; | ||
|
@@ -28,33 +31,41 @@ function isValidAmount(): boolean { | |
const isValid = !isNaN(Number(amountInput.value)) && Number(amountInput.value) > 0; | ||
|
||
if (isValid) { | ||
amountInput.style.border = "1px solid #5af55a"; | ||
amountInput.style.border = green; | ||
} else if (amountInput.value === "") { | ||
amountInput.style.border = "1px solid rgba(255, 255, 255, 0.1)"; | ||
amountInput.style.border = grey; | ||
} else { | ||
amountInput.style.border = "1px solid red"; | ||
amountInput.style.border = red; | ||
} | ||
|
||
return isValid; | ||
} | ||
|
||
export function isApprovalButtonsValid() { | ||
export async function isApprovalButtonsValid() { | ||
currentAllowanceAmount.textContent = "..."; | ||
const isConnected = appState.getIsConnectedState(); | ||
const isAddressValid = isValidAddress(); | ||
const isAmountValid = isValidAmount(); | ||
|
||
approveButton.disabled = !(isConnected && isAddressValid && isAmountValid); | ||
revokeButton.disabled = !(isConnected && isAddressValid); | ||
approveButton.disabled = true; | ||
revokeButton.disabled = true; | ||
|
||
if (isAddressValid && isConnected) { | ||
void getCurrentAllowance(); | ||
const isSuccess = await getCurrentAllowance(); | ||
if (isSuccess) { | ||
approveButton.disabled = !isAmountValid; | ||
revokeButton.disabled = false; | ||
addressInput.style.border = green; | ||
} else { | ||
addressInput.style.border = red; | ||
} | ||
} | ||
} | ||
|
||
async function getCurrentAllowance() { | ||
async function getCurrentAllowance(): Promise<boolean> { | ||
if (!provider) { | ||
console.error("Provider is not initialized"); | ||
return; | ||
return false; | ||
} | ||
|
||
const tokenAddress = addressInput.value; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. you might wanna check if the address is ERC20 because if put a random address then it throws a revert error and it can be confusing to the users There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. managed that, thanks |
||
|
@@ -68,10 +79,10 @@ async function getCurrentAllowance() { | |
const allowance = await tokenContract.allowance(userAddress, permit2Address); | ||
const formattedAllowance = ethers.utils.formatUnits(allowance, decimals); | ||
currentAllowanceAmount.textContent = formattedAllowance + " " + symbol; | ||
return allowance; | ||
return true; | ||
} catch (error) { | ||
console.error("Error fetching allowance:", error); | ||
renderErrorInModal(error as Error); | ||
currentAllowanceAmount.textContent = "not a valid token"; | ||
return false; | ||
} | ||
} | ||
|
||
|
@@ -109,7 +120,7 @@ async function onApproveClick() { | |
renderErrorInModal(error as Error); | ||
} finally { | ||
approveButton.textContent = originalText; | ||
isApprovalButtonsValid(); // re-check the state to restore buttons correctly | ||
await isApprovalButtonsValid(); // re-check the state to restore buttons correctly | ||
} | ||
} | ||
|
||
|
@@ -145,11 +156,11 @@ async function onRevokeClick() { | |
renderErrorInModal(error as Error); | ||
} finally { | ||
revokeButton.textContent = originalText; | ||
isApprovalButtonsValid(); // re-check state to restore buttons correctly | ||
await isApprovalButtonsValid(); // re-check state to restore buttons correctly | ||
} | ||
} | ||
|
||
export function setupButtonValidityListener() { | ||
export async function setupButtonValidityListener() { | ||
amountInput.addEventListener("change", isApprovalButtonsValid); | ||
addressInput.addEventListener("change", isApprovalButtonsValid); | ||
} |
zugdev marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
This file was deleted.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
now every time I click out of the amount input, the buttons get disabled until allowance is fetched and it's a weird UX
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
but thats what you meant by checking if its an erc20 token, should I add a spinner to indicate loading perhaps?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I thought you will do the check when approve button is clicked and then it would show the error modal.