Skip to content

Commit

Permalink
Merge pull request #3 from OpenZeppelin/support-tenant-networks
Browse files Browse the repository at this point in the history
Support tenant networks
  • Loading branch information
MCarlomagno authored Nov 26, 2024
2 parents 317c5e1 + 1d15ddf commit 6e7d93c
Show file tree
Hide file tree
Showing 8 changed files with 70 additions and 31 deletions.
6 changes: 3 additions & 3 deletions src/lib/components/ApprovalProcess.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -114,12 +114,12 @@
value: globalState.form.approvalProcessSelected,
}
: undefined}
emptyLabel="No Approval Processes in selected Network"
emptyLabel="No Approval Processes Available"
/>
{/key}
</div>
<div
class="form-check"
class="form-check mt-3"
title={disableCreation ? "Deploy Environment already exists" : undefined}
>
<input
Expand Down Expand Up @@ -183,7 +183,7 @@
{/if}
{/if}
</div>
<div class="form-check">
<div class="form-check mt-3">
<input
class="form-check-input"
type="radio"
Expand Down
36 changes: 23 additions & 13 deletions src/lib/components/Depoy.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import { deployContract, switchToNetwork } from "$lib/ethereum";
import { API } from "$lib/api";
import type { APIResponse } from "$lib/models/ui";
import { getNetworkLiteral } from "$lib/models/network";
let contractName: string | undefined;
let contractBytecode: string | undefined;
Expand Down Expand Up @@ -105,7 +106,7 @@
name: `Deploy From Remix - ${ap.viaType}`,
via: ap.via,
viaType: ap.viaType,
network: globalState.form.network,
network: getNetworkLiteral(globalState.form.network),
relayerId: ap.relayerId,
component: ["deploy"],
};
Expand Down Expand Up @@ -207,7 +208,7 @@
log("[Defender Deploy] Creating contract deployment in Defender...");
const deployRequest: DeployContractRequest = {
contractName: contractName,
network: globalState.form.network,
network: getNetworkLiteral(globalState.form.network),
approvalProcessId: approvalProcess.approvalProcessId,
contractPath: contractPath,
verifySourceCode: true,
Expand Down Expand Up @@ -274,19 +275,22 @@
disabled
/>

<div class="form-check m-2">
<input
class="form-check-input"
type="checkbox"
id="isDeterministic"
checked={isDeterministic}
onchange={() => (isDeterministic = !isDeterministic)}
>
<label class="form-check-label" for="isDeterministic">
Deterministic
</label>
<div class="pt-2">
<div class="form-check">
<input
class="form-check-input"
type="checkbox"
id="isDeterministic"
checked={isDeterministic}
onchange={() => (isDeterministic = !isDeterministic)}
>
<label class="form-check-label" for="isDeterministic">
Deterministic
</label>
</div>
</div>


{#if isDeterministic}
<label for="salt">{`Salt`}</label>
<input
Expand Down Expand Up @@ -324,3 +328,9 @@
</p>
</div>
{/if}

<style>
input[type=checkbox] {
top: 2px;
}
</style>
19 changes: 12 additions & 7 deletions src/lib/components/Network.svelte
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
<script lang="ts">
import { chainDisplayNames } from "$lib/models/network";
import {
chainDisplayNames,
type TenantNetworkResponse,
} from "$lib/models/network";
import type { DropdownItem } from "$lib/models/ui";
import { globalState } from "$lib/state/state.svelte";
import Dropdown from "./shared/Dropdown.svelte";
Expand All @@ -9,10 +12,10 @@
};
const { onSelected }: Props = $props();
const networkToDropdownItem = (network: string) => ({
label: chainDisplayNames[network],
value: network,
});
const networkToDropdownItem = (network: string | TenantNetworkResponse) => {
const n = typeof network === "string" ? network : network.name;
return { label: chainDisplayNames[n] ?? n, value: network };
};
// network selection logic
let network = $state("");
Expand All @@ -23,14 +26,16 @@
// Resets Approval process state.
globalState.form.approvalProcessSelected = undefined;
globalState.form.approvalProcessToCreate = undefined;
globalState.form.approvalType = 'existing';
globalState.form.approvalType = "existing";
onSelected(network);
};
</script>

<Dropdown
items={globalState.networks.map(networkToDropdownItem).sort((a, b) => a.label > b.label ? 1 : -1)}
items={globalState.networks
.map(networkToDropdownItem)
.sort((a, b) => (a.label > b.label ? 1 : -1))}
placeholder="Select Network"
on:select={(e) => onNetworkSelect(e.detail)}
/>
8 changes: 7 additions & 1 deletion src/lib/components/shared/Dropdown.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@
{/each}
{#if items.length === 0}
<button type="button" class="dropdown-item" disabled>
{emptyLabel ?? "No items available"}
<small>{emptyLabel ?? "No items available"}</small>
</button>
{/if}
</div>
Expand All @@ -67,8 +67,14 @@
font-size: smaller;
}
.dropdown-menu {
background: var(--custom-select);
}
.dropdown-item {
cursor: pointer;
font-size: smaller;
background: var(--custom-select);
color: var(--text);
}
</style>
8 changes: 6 additions & 2 deletions src/lib/defender/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,12 @@ export const listApiKeyPermissions = async (credentials: Credentials) => {

export const listNetworks = async (credentials: Credentials) => {
const client = getClient(credentials);
const networks = await client.network.listSupportedNetworks();
return networks;
const [networks, forkedNetworks, privateNetworks] = await Promise.all([
client.network.listSupportedNetworks(),
client.network.listForkedNetworks(),
client.network.listPrivateNetworks(),
]);
return [...networks, ...forkedNetworks, ...privateNetworks];
}

export const listApprovalProcesses = async (credentials: Credentials) => {
Expand Down
6 changes: 3 additions & 3 deletions src/lib/ethereum/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { type Eip1193Provider, BrowserProvider, ContractFactory } from 'ethers';
import { chainIds } from "$lib/models/network";
import { chainIds, type TenantNetworkResponse } from "$lib/models/network";
import type { DeployContractResult } from '$lib/models/ethereum';

function getEthereum(): Eip1193Provider {
Expand All @@ -12,8 +12,8 @@ function getEthereum(): Eip1193Provider {
*
* @param network target network to switch to.
*/
export async function switchToNetwork(network: string) {
const chainId = chainIds[network];
export async function switchToNetwork(network: string | TenantNetworkResponse) {
const chainId = typeof network === 'string' ? chainIds[network] : network.chainId;
if (!chainId) throw new Error(`Invalid network: ${network}`);

const ethereum = getEthereum();
Expand Down
13 changes: 13 additions & 0 deletions src/lib/models/network.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,18 @@
* Network mappings for Defender
* https://github.com/OpenZeppelin/defender-sdk/blob/main/packages/base/src/utils/network.ts
*/

export interface TenantNetworkResponse {
tenantNetworkId: string;
name: string;
chainId: number;
networkType: 'private' | 'fork';
}

export function getNetworkLiteral(network: string | TenantNetworkResponse): string {
return typeof network === 'string' ? network : network.name;
}

export const chainIds: { [key in string]: number } = {
'alfajores': 44787,
'amoy': 80002,
Expand Down Expand Up @@ -88,6 +100,7 @@ export const chainDisplayNames: { [key in string]: string } = {
'matic': 'Polygon',
'matic-zkevm': 'Polygon ZK-EVM',
'matic-zkevm-testnet': 'Polygon ZK-EVM Testnet',
'matic-cardona-zkevm-testnet': 'Polygon Cardona ZK-EVM Testnet',
'meld': 'Meld',
'meld-kanazawa': 'Meld Kanazawa',
'moonbase': 'Moonbase',
Expand Down
5 changes: 3 additions & 2 deletions src/lib/models/ui.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import type { CompilationFileSources, CompilationResult, SourceWithTarget } from
import type { Relayer } from "./relayer";
import type { ApiKeyCapability, Credentials } from "./auth";
import type { ApprovalProcess } from "./approval-process";
import type { TenantNetworkResponse } from "./network";

export type DropdownItem = {
label: string;
Expand All @@ -14,7 +15,7 @@ export type GlobalState = {
successMessage?: string;
credentials: Credentials;
permissions: ApiKeyCapability[];
networks: string[];
networks: string[] | TenantNetworkResponse[];
approvalProcesses: ApprovalProcess[];
relayers: Relayer[];
contract?: {
Expand All @@ -24,7 +25,7 @@ export type GlobalState = {
data?: CompilationResult | null,
}
form: {
network?: string;
network?: string | TenantNetworkResponse;
approvalProcessSelected?: ApprovalProcess;
approvalProcessToCreate?: {
viaType: 'EOA' | 'Safe' | 'Relayer';
Expand Down

0 comments on commit 6e7d93c

Please sign in to comment.