Skip to content
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

Catch execute errors then popup #325

Merged
merged 2 commits into from
May 31, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion packages/keychain/src/components/Auth/Login.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ export function Login({

setIsLoading(false);
},
[log, onSuccess],
[chainId, context, expiresAt, isSlot, log, onSuccess],
);

return (
Expand Down
24 changes: 3 additions & 21 deletions packages/keychain/src/components/Execute/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import {
constants,
Call as StarknetCall,
InvocationsDetails,
uint256,
} from "starknet";
import { Fees } from "./Fees";
import { formatEther } from "viem";
Expand Down Expand Up @@ -54,7 +53,6 @@ export function Execute({
const [error, setError] = useState<Error>();
const [isLoading, setLoading] = useState<boolean>(false);
const [ethBalance, setEthBalance] = useState<bigint>();
const [ethApproved, setEthApproved] = useState<bigint>();
const [lowEth, setLowEth] = useState<boolean>(false);

const account = controller.account(chainId);
Expand Down Expand Up @@ -104,20 +102,6 @@ export function Execute({
return;
}

const approve: StarknetCall[] = calls.filter(
(call) =>
call.contractAddress === CONTRACT_ETH && call.entrypoint === "approve",
);

if (approve.length > 0) {
setEthApproved(
uint256.uint256ToBN({
low: approve[0].calldata[1],
high: approve[0].calldata[2],
}),
);
}

account
.estimateInvokeFee(calls, transactionsDetail)
.then((fees) => {
Expand All @@ -130,7 +114,6 @@ export function Execute({
}, [
account,
controller,
setEthApproved,
setError,
setFees,
calls,
Expand All @@ -139,14 +122,14 @@ export function Execute({
]);

useEffect(() => {
if (!ethBalance || !ethApproved) {
if (!ethBalance || !fees) {
return;
}

if (ethBalance < ethApproved) {
if (ethBalance < fees.max) {
setLowEth(true);
}
}, [ethBalance, ethApproved]);
}, [ethBalance, fees]);

const onSubmit = useCallback(async () => {
setLoading(true);
Expand Down Expand Up @@ -202,7 +185,6 @@ export function Execute({
chainId={chainId}
fees={fees}
balance={ethBalance && format(ethBalance)}
approved={ethApproved && format(ethApproved)}
/>

<PortalFooter>
Expand Down
26 changes: 17 additions & 9 deletions packages/keychain/src/pages/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -211,15 +211,23 @@ const Index: NextPage = () => {
});
}

const res = await account.execute(
calls,
session,
transactionsDetail,
);
return {
code: ResponseCodes.SUCCESS,
...res,
};
try {
const res = await account.execute(
calls,
session,
transactionsDetail,
);

return {
code: ResponseCodes.SUCCESS,
...res,
};
} catch (e) {
return {
code: ResponseCodes.NOT_ALLOWED,
message: e.message,
};
}
},
),
),
Expand Down
5 changes: 5 additions & 0 deletions packages/keychain/src/utils/account.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import {
TransactionFinalityStatus,
InvocationsDetails,
num,
TransactionExecutionStatus,
} from "starknet";
import {
AccountContractDocument,
Expand Down Expand Up @@ -197,6 +198,10 @@ class Account extends BaseAccount {
TransactionFinalityStatus.ACCEPTED_ON_L1,
TransactionFinalityStatus.ACCEPTED_ON_L2,
],
errorStates: [
TransactionExecutionStatus.REJECTED,
TransactionExecutionStatus.REVERTED,
],
})
.catch(() => {
this.resetNonce();
Expand Down
Loading