Skip to content

Commit

Permalink
feat: async masp params loading (anoma#305)
Browse files Browse the repository at this point in the history
  • Loading branch information
mateuszjasiuk authored Jun 14, 2023
1 parent f6c908f commit eb16888
Show file tree
Hide file tree
Showing 14 changed files with 229 additions and 94 deletions.
38 changes: 37 additions & 1 deletion apps/extension/src/App/App.components.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,34 @@
import styled, { createGlobalStyle } from "styled-components";
import styled, { createGlobalStyle, css } from "styled-components";

export const GlobalStyles = createGlobalStyle`
html, body {
background-color: #000;
}
`;

export const Spinner = css`
content: "";
position: absolute;
top: 0;
left: 0;
right: 0;
bottom: 0;
border-top-color: ${(props) => props.theme.colors.primary.main};
border-radius: 50%;
animation: button-loading-spinner 1s ease infinite;
@keyframes button-loading-spinner {
from {
transform: rotate(0turn);
}
to {
transform: rotate(1turn);
}
}
}
`;

export const AppContainer = styled.div`
display: flex;
justify-content: space-between;
Expand Down Expand Up @@ -42,6 +65,19 @@ export const Heading = styled.h1`
padding-left: 12px;
`;

export const HeadingLoader = styled.div`
position: relative;
width: 20px;
height: 20px;
&.is-loading::after {
width: 16px;
height: 16px;
border: 2px solid transparent;
${Spinner}
}
`;

export const HeadingButtons = styled.div`
display: flex;
justify-content: flex-end;
Expand Down
71 changes: 50 additions & 21 deletions apps/extension/src/App/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import { useUntil } from "@anoma/hooks";

import { Ports } from "router";
import {
LoadMaspParamsMsg,
FetchAndStoreMaspParamsMsg,
HasMaspParamsMsg,
QueryAccountsMsg,
} from "provider/messages";
Expand All @@ -24,6 +24,7 @@ import {
Heading,
HeadingButtons,
SettingsButton,
HeadingLoader,
} from "./App.components";
import { TopLevelRoute } from "./types";
import { LockWrapper } from "./LockWrapper";
Expand All @@ -47,10 +48,13 @@ export const App: React.FC = () => {
const navigate = useNavigate();
const [isLocked, setIsLocked] = useState(true);
const [status, setStatus] = useState<Status>();
const [maspStatus, setMaspStatus] = useState<{
status: Status;
info: string;
}>({ status: Status.Completed, info: "" });
const [accounts, setAccounts] = useState<DerivedAccount[]>([]);
const [parentAccount, setParentAccount] = useState<DerivedAccount>();
const [error, setError] = useState("");
const [statusInfo, setStatusInfo] = useState("");
const requester = useRequester();

const fetchAccounts = async (): Promise<void> => {
Expand Down Expand Up @@ -94,26 +98,11 @@ export const App: React.FC = () => {
predFn: async () => {
setStatus(Status.Pending);
try {
setStatusInfo("Fetching accounts...");
const accounts = await requester.sendMessage(
Ports.Background,
new QueryAccountsMsg()
);

setStatusInfo("Checking MASP parameters...");
const hasMaspParams = await requester.sendMessage(
Ports.Background,
new HasMaspParamsMsg()
);

if (!hasMaspParams) {
setStatusInfo("Fetching MASP parameters...");
await requester.sendMessage(
Ports.Background,
new LoadMaspParamsMsg()
);
}

setAccounts(accounts);
return true;
} catch (e) {
Expand All @@ -122,7 +111,6 @@ export const App: React.FC = () => {
}
},
onSuccess: () => {
setStatusInfo("");
setStatus(Status.Completed);
},
onFail: () => {
Expand All @@ -134,6 +122,43 @@ export const App: React.FC = () => {
[]
);

// Fetch Masp params if they don't exist
useEffect(() => {
if (status === Status.Completed) {
(async () => {
const hasMaspParams = await requester.sendMessage(
Ports.Background,
new HasMaspParamsMsg()
);

if (!hasMaspParams) {
setMaspStatus({
status: Status.Pending,
info: "Fetching MASP parameters...",
});
try {
await requester.sendMessage(
Ports.Background,
new FetchAndStoreMaspParamsMsg()
);

setMaspStatus({
status: Status.Completed,
info: "",
});
} catch (e) {
setMaspStatus({
status: Status.Failed,
info: `Fetching MASP parameters failed: ${e}`,
});
//TODO: Notify user in a better way
console.error(e);
}
}
})();
}
}, [status]);

useEffect(() => {
if (redirect) {
// Provide a redirect in the case of transaction/connection approvals
Expand Down Expand Up @@ -164,6 +189,12 @@ export const App: React.FC = () => {
<ContentContainer>
<TopSection>
<Heading>Anoma Browser Extension</Heading>
<HeadingLoader
className={
maspStatus.status === Status.Pending ? "is-loading" : ""
}
title={maspStatus.info}
/>
<HeadingButtons>
{parentAccount && (
<SettingsButton
Expand All @@ -177,9 +208,7 @@ export const App: React.FC = () => {
<Routes>
<Route
path="*"
element={
<Loading status={status} info={statusInfo} error={error} />
}
element={<Loading status={status} error={error} />}
/>
<Route path={TopLevelRoute.Setup} element={<Setup />} />
<Route
Expand Down
23 changes: 3 additions & 20 deletions apps/extension/src/App/Loading/Loading.components.ts
Original file line number Diff line number Diff line change
@@ -1,30 +1,13 @@
import { Spinner } from "App/App.components";
import styled from "styled-components";

export const LoadingContainer = styled.div`
&.is-loading::after {
content: "";
position: absolute;
width: 32px;
height: 32px;
top: 0;
left: 0;
right: 0;
bottom: 0;
margin: auto;
border: 4px solid transparent;
border-top-color: ${(props) => props.theme.colors.primary.main};
border-radius: 50%;
animation: button-loading-spinner 1s ease infinite;
@keyframes button-loading-spinner {
from {
transform: rotate(0turn);
}
to {
transform: rotate(1turn);
}
}
margin: auto;
${Spinner}
}
`;

Expand Down
4 changes: 2 additions & 2 deletions apps/extension/src/App/Loading/Loading.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,12 @@ type Props = {
error?: string;
};

const Loading: React.FC<Props> = ({ error, status, info }) => {
const Loading: React.FC<Props> = ({ error, status }) => {
return (
<LoadingContainer className={error ? "" : "is-loading"}>
{(status === Status.Failed && (
<LoadingError>Error: {error}</LoadingError>
)) || <p>{info ?? "Loading..."}</p>}
)) || <p>Fetching accounts...</p>}
</LoadingContainer>
);
};
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import styled, { css } from "styled-components";

export const Spinner = css`
content: "";
position: absolute;
top: 0;
left: 0;
right: 0;
bottom: 0;
border-top-color: ${(props) => props.theme.colors.primary.main};
border-radius: 50%;
animation: button-loading-spinner 1s ease infinite;
@keyframes button-loading-spinner {
from {
transform: rotate(0turn);
}
to {
transform: rotate(1turn);
}
}
}
`;

export const InfoHeader = styled.div`
display: flex;
align-items: center;
gap: 10px;
`;

export const InfoLoader = styled.div`
position: relative;
width: 20px;
height: 20px;
&::after {
width: 16px;
height: 16px;
border: 2px solid transparent;
${Spinner}
}
`;
29 changes: 27 additions & 2 deletions apps/extension/src/Approvals/ApproveTransfer/ConfirmTransfer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ import { useRequester } from "hooks/useRequester";
import { SubmitApprovedTransferMsg } from "background/approvals";
import { Address } from "App/Accounts/AccountListing.components";
import { closeCurrentTab } from "utils";
import { FetchAndStoreMaspParamsMsg, HasMaspParamsMsg } from "provider";
import { InfoHeader, InfoLoader } from "./ConfirmTransfer.components";

type Props = {
msgId: string;
Expand All @@ -26,20 +28,40 @@ export const ConfirmTransfer: React.FC<Props> = ({ msgId, address }) => {
const [password, setPassword] = useState("");
const [error, setError] = useState<string>();
const [status, setStatus] = useState<Status>();
const [statusInfo, setStatusInfo] = useState<string>("");

const handleApprove = async (): Promise<void> => {
setStatus(Status.Pending);
const hasMaspParams = await requester.sendMessage(
Ports.Background,
new HasMaspParamsMsg()
);

if (!hasMaspParams) {
setStatusInfo("Fetching MASP parameters...");
try {
await requester.sendMessage(
Ports.Background,
new FetchAndStoreMaspParamsMsg()
);
} catch (e) {
setError(`Fetching MASP parameters failed: ${e}`);
setStatus(Status.Failed);
}
}
try {
// TODO: use executeUntil here!
setStatusInfo("Decrypting keys and submitting transfer...");
await requester.sendMessage(
Ports.Background,
new SubmitApprovedTransferMsg(msgId, address, password)
);
setStatus(Status.Completed);
} catch (e) {
setError("Unable to authenticate Tx!");
setStatus(Status.Failed);
}
setStatusInfo("");
setStatus(Status.Completed);
return;
};

Expand All @@ -54,7 +76,10 @@ export const ConfirmTransfer: React.FC<Props> = ({ msgId, address }) => {
return (
<ApprovalContainer>
{status === Status.Pending && (
<p>Decrypting keys and submitting transfer...</p>
<InfoHeader>
<InfoLoader />
<p>{statusInfo}</p>
</InfoHeader>
)}
{status === Status.Failed && (
<p>
Expand Down
14 changes: 7 additions & 7 deletions apps/extension/src/background/keyring/handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ import {
SubmitBondMsg,
SubmitUnbondMsg,
SubmitIbcTransferMsg,
LoadMaspParamsMsg,
FetchAndStoreMaspParamsMsg,
HasMaspParamsMsg,
} from "provider/messages";

Expand Down Expand Up @@ -100,10 +100,10 @@ export const getHandler: (service: KeyRingService) => Handler = (service) => {
);
case DeleteAccountMsg:
return handleDeleteAccountMsg(service)(env, msg as DeleteAccountMsg);
case LoadMaspParamsMsg:
return handleLoadMaspParamsMsg(service)(
case FetchAndStoreMaspParamsMsg:
return handleFetchAndStoreMaspParamsMsg(service)(
env,
msg as LoadMaspParamsMsg
msg as FetchAndStoreMaspParamsMsg
);
case HasMaspParamsMsg:
return handleHasMaspParamsMsg(service)(env, msg as HasMaspParamsMsg);
Expand Down Expand Up @@ -296,11 +296,11 @@ const handleDeleteAccountMsg: (
};
};

const handleLoadMaspParamsMsg: (
const handleFetchAndStoreMaspParamsMsg: (
service: KeyRingService
) => InternalHandler<LoadMaspParamsMsg> = (service) => {
) => InternalHandler<FetchAndStoreMaspParamsMsg> = (service) => {
return async (_, _msg) => {
return await service.loadMaspParams();
return await service.fetchAndStoreMaspParams();
};
};

Expand Down
Loading

0 comments on commit eb16888

Please sign in to comment.