Skip to content

Commit

Permalink
chore: coding style and jsdocs
Browse files Browse the repository at this point in the history
  • Loading branch information
samsiegart committed Sep 11, 2024
1 parent a46b386 commit abe267e
Show file tree
Hide file tree
Showing 14 changed files with 100 additions and 44 deletions.
26 changes: 15 additions & 11 deletions src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -127,17 +127,21 @@ const WalletButton = () => {
setConnectWalletIndicator(false);
};

return address ? (
<a
target="block-explorer"
href={explorerHref}
title="Block Explorer"
className="btn-header no-underline"
>
{address}
<FiExternalLink className="my-1 ml-2 -mr-1 h-4 w-5" />
</a>
) : (
if (address) {
return (
<a
target="block-explorer"
href={explorerHref}
title="Block Explorer"
className="btn-header no-underline"
>
{address}
<FiExternalLink className="my-1 ml-2 -mr-1 h-4 w-5" />
</a>
);
}

return (
<button className="btn-header flex flex-row gap-2" onClick={connectWallet}>
<div>Connect Wallet</div>
{connectWalletIndicator && (
Expand Down
8 changes: 6 additions & 2 deletions src/components/AcceptInvitation.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,15 @@ export default function AcceptInvitation(props: Props) {
className="btn-primary rounded text-sm py-1 px-2 m-2"
title={props.sourceContract}
onClick={() => {
const offer = walletUtils?.makeOfferToAcceptInvitation(
assert(
walletUtils,
'Accept Invitation button should not be visible before wallet connection.',
);
const offer = walletUtils.makeOfferToAcceptInvitation(
props.sourceContract,
props.description,
);
void walletUtils?.sendOffer(offer);
void walletUtils.sendOffer(offer);
}}
>
Accept Invitation
Expand Down
8 changes: 6 additions & 2 deletions src/components/AuctioneerParamChange.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -102,12 +102,16 @@ export default function AuctioneerParamChange(props: Props) {
function handleSubmit(event) {
event.preventDefault();
console.debug({ event });
const offer = walletUtils?.makeVoteOnVaultAuctioneerParams(
assert(
walletUtils,
'Missing walletUtils. Button should not be enabled before wallet connection.',
);
const offer = walletUtils.makeVoteOnVaultAuctioneerParams(
props.charterOfferId,
paramPatch,
minutesUntilClose,
);
void walletUtils?.sendOffer(offer);
void walletUtils.sendOffer(offer);
}

const content =
Expand Down
10 changes: 7 additions & 3 deletions src/components/ChangeOracles.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -101,10 +101,14 @@ export default function ChangeOracles({ charterOfferId, mode }: Props) {
function handleSubmit(event) {
event.preventDefault();
console.debug({ event, addresses, minutesUntilClose });
assert(
walletUtils,
'Missing walletUtils. Button should not be enabled before wallet connection.',
);

const offerFns = {
[ChangeOraclesMode.Add]: walletUtils?.makeVoteOnAddOracles,
[ChangeOraclesMode.Remove]: walletUtils?.makeVoteOnRemoveOracles,
[ChangeOraclesMode.Add]: walletUtils.makeVoteOnAddOracles,
[ChangeOraclesMode.Remove]: walletUtils.makeVoteOnRemoveOracles,
};

const offer = offerFns[mode](
Expand All @@ -113,7 +117,7 @@ export default function ChangeOracles({ charterOfferId, mode }: Props) {
addresses,
minutesUntilClose,
);
void walletUtils?.sendOffer(offer);
void walletUtils.sendOffer(offer);
}

const addressesList = (
Expand Down
4 changes: 3 additions & 1 deletion src/components/CharterGuidance.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,9 @@ export default function CharterGuidance({
switch (status) {
case 'idle':
return (
<p>Waiting for wallet connection. Try &quot;Connect Wallet&quot;.</p>
<p>
Waiting for wallet connection. Try <b>Connect Wallet</b>.
</p>
);
case 'nodata':
return <p>Loading…</p>;
Expand Down
9 changes: 7 additions & 2 deletions src/components/DirectorParamChange.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -106,12 +106,17 @@ export default function DirectorParamChange(props: Props) {
function handleSubmit(event) {
event.preventDefault();
console.debug({ event });
const offer = walletUtils?.makeVoteOnVaultDirectorParams(
assert(
walletUtils,
'Missing walletUtils. Button should not be enabled before wallet connection.',
);

const offer = walletUtils.makeVoteOnVaultDirectorParams(
props.charterOfferId,
paramPatch,
minutesUntilClose,
);
void walletUtils?.sendOffer(offer);
void walletUtils.sendOffer(offer);
}

const content =
Expand Down
8 changes: 6 additions & 2 deletions src/components/PauseLiquidations.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -34,13 +34,17 @@ export default function PauseLiquidations(props: Props) {

function handleSubmit(event) {
event.preventDefault();
assert(
walletUtils,
'Missing walletUtils. Button should not be enabled before wallet connection.',
);
const toPause = Object.keys(checked).filter(name => checked[name]);
const offer = walletUtils?.makeVoteOnPauseLiquidationOffers(
const offer = walletUtils.makeVoteOnPauseLiquidationOffers(
props.charterOfferId,
toPause,
minutesUntilClose,
);
void walletUtils?.sendOffer(offer);
void walletUtils.sendOffer(offer);
}

const optionMessage = option => {
Expand Down
15 changes: 10 additions & 5 deletions src/components/PauseVaultDirectorOffers.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { motion } from 'framer-motion';
import { LoadStatus, usePublishedDatum, usePublishedKeys } from 'lib/rpc';
import { LoadStatus, usePublishedDatum, useVstorageChildKeys } from 'lib/rpc';
import { useEffect, useState } from 'react';
import { SubmitInput } from './SubmitButton';
import { useAtomValue } from 'jotai';
Expand Down Expand Up @@ -74,8 +74,8 @@ interface Props {

export default function PauseVaultDirectorOffers(props: Props) {
const walletUtils = useAtomValue(walletUtilsAtom);
const { data: managerIds, status: vaultKeysStatus } = usePublishedKeys(
'vaultFactory.managers',
const { data: managerIds, status: vaultKeysStatus } = useVstorageChildKeys(
'published.vaultFactory.managers',
);

const [checked, setChecked] = useState({});
Expand All @@ -87,15 +87,20 @@ export default function PauseVaultDirectorOffers(props: Props) {
function handleSubmit(event) {
event.preventDefault();
console.debug({ event, checked, minutesUntilClose });
assert(
walletUtils,
'Missing walletUtils. Button should not be enabled before wallet connection.',
);

const toPause = Object.entries(checked)
.filter(([_, check]) => check)
.map(([name]) => name);
const offer = walletUtils?.makeVoteOnPauseVaultOffers(
const offer = walletUtils.makeVoteOnPauseVaultOffers(
props.charterOfferId,
toPause,
minutesUntilClose,
);
void walletUtils?.sendOffer(offer);
void walletUtils.sendOffer(offer);
}

const managers =
Expand Down
8 changes: 6 additions & 2 deletions src/components/ProposeParamChange.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -78,13 +78,17 @@ export default function ProposeParamChange(props: Props) {
function handleSubmit(event) {
event.preventDefault();
console.debug({ event });
const offer = walletUtils?.makeVoteOnPSMParams(
assert(
walletUtils,
'Missing walletUtils. Button should not be enabled before wallet connection.',
);
const offer = walletUtils.makeVoteOnPSMParams(
props.psmCharterOfferId,
props.anchorName,
paramPatch,
minutesUntilClose,
);
void walletUtils?.sendOffer(offer);
void walletUtils.sendOffer(offer);
}

let content = (
Expand Down
8 changes: 6 additions & 2 deletions src/components/ProposePauseOffers.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -31,16 +31,20 @@ export default function ProposePauseOffers(props: Props) {
function handleSubmit(event) {
event.preventDefault();
console.debug({ event, checked, minutesUntilClose });
assert(
walletUtils,
'Missing walletUtils. Button should not be enabled before wallet connection.',
);
const toPause = Object.entries(checked)
.filter(([_, check]) => check)
.map(([name]) => name);
const offer = walletUtils?.makeVoteOnPausePSMOffers(
const offer = walletUtils.makeVoteOnPausePSMOffers(
props.psmCharterOfferId,
props.anchorName,
toPause,
minutesUntilClose,
);
void walletUtils?.sendOffer(offer);
void walletUtils.sendOffer(offer);
}

const optionMessage = option => {
Expand Down
14 changes: 9 additions & 5 deletions src/components/VaultParamChange.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { Fragment } from 'react';
import { FiChevronDown } from 'react-icons/fi';
import { Menu, Transition } from '@headlessui/react';
import { motion } from 'framer-motion';
import { LoadStatus, usePublishedDatum, usePublishedKeys } from 'lib/rpc';
import { LoadStatus, usePublishedDatum, useVstorageChildKeys } from 'lib/rpc';
import { useState } from 'react';
import { AmountInput, PercentageInput } from './inputs';
import { SubmitInput } from './SubmitButton';
Expand Down Expand Up @@ -31,8 +31,8 @@ export type ParameterValue =

export default function VaultParamChange(props: Props) {
const walletUtils = useAtomValue(walletUtilsAtom);
const { data: vaultKeys, status: vaultKeysStatus } = usePublishedKeys(
'vaultFactory.managers',
const { data: vaultKeys, status: vaultKeysStatus } = useVstorageChildKeys(
'published.vaultFactory.managers',
);

const managerIds = vaultKeys.filter(key => key.startsWith('manager'));
Expand Down Expand Up @@ -97,13 +97,17 @@ export default function VaultParamChange(props: Props) {
function handleSubmit(event) {
event.preventDefault();
console.debug({ event });
const offer = walletUtils?.makeVoteOnVaultManagerParams(
assert(
walletUtils,
'Missing walletUtils. Button should not be enabled before wallet connection.',
);
const offer = walletUtils.makeVoteOnVaultManagerParams(
props.charterOfferId,
collateralBrand,
paramPatch,
minutesUntilClose,
);
void walletUtils?.sendOffer(offer);
void walletUtils.sendOffer(offer);
}

const content =
Expand Down
4 changes: 3 additions & 1 deletion src/components/VotePanel.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,9 @@ function Eligibility({
switch (status) {
case 'idle':
return (
<p>Waiting for wallet connection. Try &quot;Connect Wallet&quot;.</p>
<p>
Waiting for wallet connection. Try <b>Connect Wallet</b>.
</p>
);
case 'nodata':
return <p>Loading…</p>;
Expand Down
8 changes: 6 additions & 2 deletions src/components/questions.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -312,12 +312,16 @@ export function VoteOnQuestion(props: {

function voteFor(position) {
console.log('voting for position', position);
const offer = walletUtils?.makeOfferToVote(
assert(
walletUtils,
'Missing walletUtils. Button should not be enabled before wallet connection.',
);
const offer = walletUtils.makeOfferToVote(
props.ecOfferId,
[position],
details.questionHandle,
);
void walletUtils?.sendOffer(offer);
void walletUtils.sendOffer(offer);
}
const {
closingRule: { deadline },
Expand Down
14 changes: 10 additions & 4 deletions src/lib/rpc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,11 @@ const fromAgoricNet = (str: string): Promise<MinimalNetworkConfig> => {
return fetch(networkConfigUrl(netName)).then(res => res.json());
};

const timeoutDurationMS = 10_000;

const makeAgoricNames = async (
follow: (path: string) => Promise<ValueFollower<unknown>>,
) => {
const timeoutDurationMS = 10_000;
const timeout = setTimeout(
() =>
notifyError(
Expand Down Expand Up @@ -133,7 +134,12 @@ export enum LoadStatus {
Received = 'received',
}

export const usePublishedKeys = (path: string) => {
/**
* Fetches the list of children keys under a given vstorage node.
*
* @param {string} path The path of the vstorage node to query
*/
export const useVstorageChildKeys = (path: string) => {
const [status, setStatus] = useState(LoadStatus.Idle);
const [data, setData] = useState([]);
const rpcUtils = useAtomValue(rpcUtilsAtom);
Expand All @@ -145,9 +151,9 @@ export const usePublishedKeys = (path: string) => {
}

const fetchKeys = async () => {
console.debug('usePublishedKeys reading', `published.${path}`);
console.debug('useVstorageChildKeys reading', path);
setStatus(LoadStatus.Waiting);
const keys = await rpcUtils.vstorage.keys(`published.${path}`);
const keys = await rpcUtils.vstorage.keys(path);
setData(keys);
setStatus(LoadStatus.Received);
};
Expand Down

0 comments on commit abe267e

Please sign in to comment.