Skip to content

Commit

Permalink
Merge pull request #29 from OpenZeppelin/debounce-compile-calls
Browse files Browse the repository at this point in the history
debounce calls to compiler api
  • Loading branch information
MCarlomagno authored Dec 13, 2024
2 parents e6ff053 + 9a444d1 commit 95a6e4a
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 10 deletions.
8 changes: 8 additions & 0 deletions src/lib/utils/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,3 +26,11 @@ export const isUpgradeable = (sources?: ContractSources) => {
if (!sources) return false;
return Object.keys(sources).some((path) => path.includes('@openzeppelin/contracts-upgradeable'));
}

export const debouncer = (fn: (...args: any[]) => void, delay: number) => {
let timeout: NodeJS.Timeout;
return (...args: any[]) => {
clearTimeout(timeout);
timeout = setTimeout(() => fn(...args), delay);
};
}
25 changes: 16 additions & 9 deletions src/lib/wizard/components/Deploy.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,16 @@
import { addAPToDropdown, findDeploymentEnvironment, globalState } from "$lib/state/state.svelte";
import { attempt } from "$lib/utils/attempt";
import { encodeConstructorArgs, getConstructorInputsWizard, getContractBytecode } from "$lib/utils/contracts";
import { isMultisig, isUpgradeable } from "$lib/utils/helpers";
import { debouncer, isMultisig, isUpgradeable } from "$lib/utils/helpers";
import Button from "./shared/Button.svelte";
import Input from "./shared/Input.svelte";
import Message from "./shared/Message.svelte";
// debounce the compile call to avoid sending too many requests while the user is editing.
const compileDebounced = debouncer(compile, 600);
let inputsWithValue = $state<Record<string, string | number | boolean>>({});
let busy = $state(false);
let isDeploying = $state(false);
let successMessage = $state<string>("");
let errorMessage = $state<string>("");
let compilationError = $state<string>("");
Expand All @@ -24,6 +27,7 @@
let deploymentResult = $state<DeploymentResult | undefined>(undefined);
let isDeterministic = $state(false);
let salt: string = $state("");
let isCompiling = $state(false);
let contractBytecode = $derived.by(() => {
if (!globalState.contract?.target || !compilationResult) return;
Expand Down Expand Up @@ -61,12 +65,12 @@
: undefined
);
let inputs: ABIParameter[] = $state([]);
$effect(() => {
if (globalState.contract?.source?.sources) {
compile();
isCompiling = true;
compileDebounced();
}
});
Expand All @@ -75,7 +79,7 @@
inputsWithValue[target.name] = target.value;
}
async function compile() {
async function compile(): Promise<void> {
const sources = globalState.contract?.source?.sources;
if (!sources) {
return;
Expand All @@ -94,6 +98,7 @@
if (globalState.contract?.target && compilationResult) {
inputs = getConstructorInputsWizard(globalState.contract.target, compilationResult.output.contracts);
}
isCompiling = false;
}
function displayMessage(message: string, type: "success" | "error") {
Expand Down Expand Up @@ -304,9 +309,9 @@
};
async function triggerDeploy() {
busy = true;
isDeploying = true;
await deploy();
busy = false;
isDeploying = false;
}
</script>
Expand All @@ -316,7 +321,9 @@
<Message type="warn" message="Upgradable contracts are not yet fully supported. This action will only deploy the implementation contract without initializing. <br />We recommend using <u><a href='https://github.com/OpenZeppelin/openzeppelin-upgrades' target='_blank'>openzeppelin-upgrades</a></u> package instead." />
{/if}

{#if inputs.length > 0}
{#if isCompiling}
<Message type="loading" message="Compiling..." />
{:else if inputs.length > 0}
<h6 class="text-sm">Constructor Arguments</h6>
{#each inputs as input}
<Input name={input.name} placeholder={`${input.name} (${input.type})`} onchange={handleInputChange} value={''} type="text"/>
Expand Down Expand Up @@ -352,7 +359,7 @@
<Message message={compilationError} type="error" />
{/if}

<Button disabled={!globalState.authenticated || busy} loading={busy} label="Deploy" onClick={triggerDeploy} />
<Button disabled={!globalState.authenticated || isDeploying || isCompiling} loading={isDeploying} label="Deploy" onClick={triggerDeploy} />

{#if successMessage || errorMessage}
<Message message={successMessage || errorMessage} type={successMessage ? "success" : "error"} />
Expand Down
7 changes: 6 additions & 1 deletion src/lib/wizard/components/shared/Message.svelte
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<script lang="ts">
type Props = {
message: string;
type: 'success' | 'error' | 'warn' | 'info';
type: 'success' | 'error' | 'warn' | 'info' | 'loading';
};
let { message, type }: Props = $props();
Expand All @@ -27,5 +27,10 @@
<i class={`fa fa-info-circle text-blue-600`}></i>
<div class="text-xs text-blue-600">{@html message}</div>
</div>
{:else if type === 'loading'}
<div class="flex flex-row items-center gap-2">
<i class={"fa fa-circle-o-notch fa-spin text-blue-600"}></i>
<div class="text-xs text-blue-600">{@html message}</div>
</div>
{/if}

0 comments on commit 95a6e4a

Please sign in to comment.