Skip to content

Commit

Permalink
feat: separate simulation from run types
Browse files Browse the repository at this point in the history
  • Loading branch information
sohrab- committed Apr 24, 2023
1 parent d0d8143 commit a9a4e2f
Show file tree
Hide file tree
Showing 11 changed files with 3,077 additions and 1,476 deletions.
4,450 changes: 3,019 additions & 1,431 deletions package-lock.json

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@
"stream-browserify": "^3.0.0",
"uuid": "^8.3.2",
"web-vitals": "^0.2.4",
"zustand": "^4.1.5"
"zustand": "^4.3.7"
},
"devDependencies": {
"@types/bs58": "^4.0.1",
Expand All @@ -91,7 +91,7 @@
"protobufjs-cli": "^1.1.1",
"rollup-plugin-node-polyfills": "^0.2.1",
"rollup-plugin-visualizer": "^5.9.0",
"semantic-release": "^19.0.5",
"semantic-release": "^21.0.1",
"semantic-release-export-data": "^1.0.1",
"ts-json-schema-generator": "^1.2.0",
"typescript": "^5.0.4",
Expand Down
7 changes: 2 additions & 5 deletions src/components/Footer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import {
Text,
useColorModeValue,
} from "@chakra-ui/react";
import { FaGithub, FaLaptop, FaTwitter } from "react-icons/fa";
import { FaGithub, FaTwitter } from "react-icons/fa";

export const Footer: React.FC = () => (
<Grid p="3" pb="5" templateColumns="repeat(3,1fr)" alignItems="center">
Expand All @@ -28,11 +28,8 @@ export const Footer: React.FC = () => (
>
<Icon as={FaGithub} />
</Link>
<Link mr="4" href="https://twitter.com/labeleven_dev" isExternal>
<Icon as={FaTwitter} />
</Link>
<Link href="https://twitter.com/sohrabwashere" isExternal>
<Icon as={FaLaptop} />
<Icon as={FaTwitter} />
</Link>
</Center>
</Box>
Expand Down
25 changes: 18 additions & 7 deletions src/components/client/ClientHeader.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import {
AlertDescription,
AlertIcon,
Button,
Checkbox,
Collapse,
Flex,
Heading,
Expand Down Expand Up @@ -33,10 +34,9 @@ import { TRANSACTION_VERSIONS } from "utils/ui-constants";
export const ClientHeader: React.FC<{ sendButton: React.ReactNode }> = ({
sendButton,
}) => {
const [runType, setUI] = useShallowSessionStoreWithoutUndo((state) => [
state.uiState.runType,
state.set,
]);
const [runType, simulate, setUI] = useShallowSessionStoreWithoutUndo(
(state) => [state.uiState.runType, state.uiState.simulate, state.set]
);

const [
transactionVersion,
Expand Down Expand Up @@ -119,6 +119,19 @@ export const ClientHeader: React.FC<{ sendButton: React.ReactNode }> = ({
}}
/>

<Checkbox
mx="2"
colorScheme="green"
isChecked={simulate}
onChange={(event) => {
setUI((state) => {
state.uiState.simulate = event.target.checked;
});
}}
>
Simulate
</Checkbox>

{sendButton}

<Spacer />
Expand Down Expand Up @@ -181,9 +194,7 @@ export const ClientHeader: React.FC<{ sendButton: React.ReactNode }> = ({
</Alert>
</Collapse>

{(runType === "squadsSimulate" || runType === "squadsSend") && (
<SquadsConfig />
)}
{runType === "squads" && <SquadsConfig />}

<Heading mt="5" mb="2" alignItems="center" flex="1" size="lg">
<EditableName
Expand Down
42 changes: 17 additions & 25 deletions src/components/client/SendButton.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -50,23 +50,11 @@ export const RUN_TYPES: {
}[] = [
{ id: "send", name: "Send", type: "standard", icon: <Icon as={FaPlay} /> },
{
id: "simulate",
name: "Simulate",
type: "standard",
icon: <Icon as={FaFlask} />,
},
{
id: "squadsSend",
id: "squads",
name: "Send to Squads",
type: "integration",
icon: <SquadsIcon />,
},
{
id: "squadsSimulate",
name: "Simulate to Squads",
type: "integration",
icon: <SquadsIcon />,
},
];

export const SendButton: React.FC<{
Expand All @@ -76,13 +64,13 @@ export const SendButton: React.FC<{
const scrollToResults = useConfigStore(
(state) => state.appOptions.scrollToResults
);
const [runType, inProgress, setUI] = useShallowSessionStoreWithoutUndo(
(state) => [
const [runType, simulate, inProgress, setUI] =
useShallowSessionStoreWithoutUndo((state) => [
state.uiState.runType,
state.uiState.simulate,
state.transactionRun.inProgress,
state.set,
]
);
]);

const { publicKey: walletPublicKey } = useWallet();

Expand Down Expand Up @@ -151,7 +139,7 @@ export const SendButton: React.FC<{
scrollDown();
};

const { simulate, send } = useSendWeb3Transaction({
const { simulate: sendSimulate, send } = useSendWeb3Transaction({
onSimulated,
onSent,
onError,
Expand All @@ -171,13 +159,13 @@ export const SendButton: React.FC<{
};
});

if (runType === "simulate") {
simulate(transaction);
} else if (runType === "send") {
if (runType === "send" && simulate) {
sendSimulate(transaction);
} else if (runType === "send" && !simulate) {
send(transaction);
} else if (runType === "squadsSimulate") {
} else if (runType === "squads" && simulate) {
squadsSimulate(transaction);
} else if (runType === "squadsSend") {
} else if (runType === "squads" && !simulate) {
squadsSend(transaction);
}
};
Expand All @@ -203,7 +191,9 @@ export const SendButton: React.FC<{
hasArrow={!walletPublicKey}
label={
walletPublicKey
? "Send transaction"
? simulate
? "Simulate transaction"
: "Send transaction"
: "Please connect a wallet to continue"
}
>
Expand All @@ -215,7 +205,8 @@ export const SendButton: React.FC<{
w="fit-content"
colorScheme="green"
aria-label="Send transaction"
rightIcon={runIcon}
rightIcon={simulate ? <Icon as={FaFlask} /> : runIcon}
boxShadow={!simulate ? "0 0 8px 1px #9AE6B4" : undefined}
onClick={onClick}
>
{runName}
Expand All @@ -228,6 +219,7 @@ export const SendButton: React.FC<{
variant="outline"
colorScheme="green"
aria-label="More"
boxShadow={!simulate ? "0 0 8px 1px #9AE6B4" : undefined}
icon={<TriangleDownIcon w="2" />}
/>
<MenuList fontSize="md">
Expand Down
2 changes: 1 addition & 1 deletion src/components/client/SquadsConfig.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ export const SquadsConfig: React.FC = () => {
useSessionStoreWithUndo((state) => [state.squadsConfig, state.set]);

const [transactionPda, setTransactionPda] = useState("");
const { isLoading: transactionPdaIsLoading, refetch: getTransactionPda } =
const { isFetching: transactionPdaIsLoading, refetch: getTransactionPda } =
useTransactionPda(programId, multisig, (pubkey) => {
if (!!pubkey) {
setTransactionPda(pubkey.toBase58());
Expand Down
6 changes: 3 additions & 3 deletions src/hooks/squads.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import { mapITransactionToWeb3Instructions } from "mappers/internal-to-web3js";
import { useMemo } from "react";
import { useQuery } from "react-query";
import { ITransaction } from "types/internal";
import { isValidPublicKey } from "utils/web3js";
import { isValidPublicKey, toPublicKey } from "utils/web3js";

export const useTransactionPda = (
programId: string,
Expand Down Expand Up @@ -81,8 +81,8 @@ export const useSendToSquads = (
}

const anchorProvider = new AnchorProvider(connection, anchorWallet, {});
const programPubkey = new PublicKey(programId);
const multisigPda = new PublicKey(multisig);
const programPubkey = toPublicKey(programId, "Squads program");
const multisigPda = toPublicKey(multisig, "Multisig account");

const squads = new Squads({
connection: anchorProvider.connection,
Expand Down
2 changes: 1 addition & 1 deletion src/hooks/useSessionStore.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import produce from "immer";
import { SessionStateWithoutUndo, SessionStateWithUndo } from "types/state";
import {
DEFAULT_SESSION_STATE_WITHOUT_UNDO,
DEFAULT_SESSION_STATE_WITH_UNDO,
DEFAULT_SESSION_STATE_WITHOUT_UNDO,
} from "utils/state";
import create from "zustand";
import { persist, subscribeWithSelector } from "zustand/middleware";
Expand Down
3 changes: 2 additions & 1 deletion src/types/state.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ export type Explorer =
| "solscan"
| "none";

export type RunType = "send" | "simulate" | "squadsSimulate" | "squadsSend";
export type RunType = "send" | "squads";

// We mutate state using immerjs. All state fields are set to readonly
// so we don't by mistake try to mutate outside immerjs.
Expand All @@ -32,6 +32,7 @@ export interface AppOptions {

export interface UIState {
readonly runType: RunType;
readonly simulate: boolean;
readonly sidePanel: "import" | "closed";
readonly optionsOpen: boolean;
readonly shareOpen: boolean;
Expand Down
1 change: 1 addition & 0 deletions src/utils/state.ts
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,7 @@ export const DEFAULT_SESSION_STATE_WITHOUT_UNDO: SessionStateWithoutUndo = {
transactionRun: DEFAULT_TRANSACTION_RUN,
uiState: {
runType: "send",
simulate: false,
sidePanel: "closed",
optionsOpen: false,
shareOpen: false,
Expand Down
11 changes: 11 additions & 0 deletions src/utils/web3js.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { SIGHASH_GLOBAL_NAMESPACE } from "@project-serum/anchor/dist/cjs/coder/borsh/instruction";
import {
LAMPORTS_PER_SOL,
PublicKey,
TransactionVersion,
VersionedTransaction,
} from "@solana/web3.js";
Expand Down Expand Up @@ -32,6 +33,16 @@ export const isValidPublicKey = (key: string): boolean => {
return valid;
};

export const toPublicKey = (key: string, field?: string): PublicKey => {
try {
return new PublicKey(key);
} catch (_) {
throw new Error(
`Invalid public key${field ? " in " + field : ""}: ${key || "N/A"}`
);
}
};

export const toTransactionVersion = (
version: string | undefined
): TransactionVersion =>
Expand Down

0 comments on commit a9a4e2f

Please sign in to comment.