forked from aragon/gov-app-template
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(addresses): fix address structure for three chains
- Loading branch information
1 parent
ea79003
commit 23211b4
Showing
21 changed files
with
358 additions
and
61 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
"use client"; | ||
|
||
import { FallbackProvider, JsonRpcProvider, BrowserProvider, JsonRpcSigner } from "ethers"; | ||
import { useMemo } from "react"; | ||
import type { Chain, Client, Transport, Account } from "viem"; | ||
import { type Config, useClient, useConnectorClient } from "wagmi"; | ||
|
||
export function clientToProvider(client: Client<Transport, Chain>) { | ||
const { chain, transport } = client; | ||
const network = { | ||
chainId: chain.id, | ||
name: chain.name, | ||
ensAddress: chain.contracts?.ensRegistry?.address, | ||
}; | ||
if (transport.type === "fallback") { | ||
const providers = (transport.transports as ReturnType<Transport>[]).map( | ||
({ value }) => new JsonRpcProvider(value?.url, network) | ||
); | ||
if (providers.length === 1) return providers[0]; | ||
return new FallbackProvider(providers); | ||
} | ||
return new JsonRpcProvider(transport.url, network); | ||
} | ||
|
||
/** Action to convert a viem Client to an ethers.js Provider. */ | ||
export function useEthersProvider({ chainId }: { chainId?: number } = {}) { | ||
const client = useClient<Config>({ chainId }); | ||
return useMemo(() => (client ? clientToProvider(client) : undefined), [client]); | ||
} | ||
|
||
export function clientToSigner(client: Client<Transport, Chain, Account>) { | ||
const { account, chain, transport } = client; | ||
const network = { | ||
chainId: chain.id, | ||
name: chain.name, | ||
ensAddress: chain.contracts?.ensRegistry?.address, | ||
}; | ||
const provider = new BrowserProvider(transport, network); | ||
const signer = new JsonRpcSigner(provider, account.address); | ||
return signer; | ||
} | ||
|
||
/** Hook to convert a viem Wallet Client to an ethers.js Signer. */ | ||
export function useEthersSigner({ chainId }: { chainId?: number } = {}) { | ||
const { data: client } = useConnectorClient<Config>({ chainId }); | ||
return useMemo(() => (client ? clientToSigner(client) : undefined), [client]); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
import { useEffect, useState } from "react"; | ||
import { crab, darwinia } from "viem/chains"; | ||
import { useChainId } from "wagmi"; | ||
|
||
export default function useConstant() { | ||
const [publicDaoAddress, setPublicDaoAddress] = useState<string>(""); | ||
const [publicTokenAddress, setPublicTokenAddress] = useState<string>(""); | ||
const [publicTokenVotingPluginAddress, setPublicTokenVotingPluginAddress] = useState<string>(""); | ||
const [publicDelegationContractAddress, setPublicDelegationContractAddress] = useState<string>(""); | ||
const [publicDelegationAnnouncementsStartBlock, setPublicDelegationAnnouncementsStartBlock] = useState<string>(""); | ||
|
||
const chainId = useChainId(); | ||
|
||
useEffect(() => { | ||
if (chainId === darwinia.id) { | ||
console.log("switched to darwinia"); | ||
setPublicDaoAddress(process.env.NEXT_PUBLIC_DAO_ADDRESS ? process.env.NEXT_PUBLIC_DAO_ADDRESS : ""); | ||
setPublicTokenAddress(process.env.NEXT_PUBLIC_TOKEN_ADDRESS ? process.env.NEXT_PUBLIC_TOKEN_ADDRESS : ""); | ||
setPublicTokenVotingPluginAddress( | ||
process.env.NEXT_PUBLIC_TOKEN_VOTING_PLUGIN_ADDRESS ? process.env.NEXT_PUBLIC_TOKEN_VOTING_PLUGIN_ADDRESS : "" | ||
); | ||
setPublicDelegationContractAddress( | ||
process.env.NEXT_PUBLIC_DELEGATION_CONTRACT_ADDRESS ? process.env.NEXT_PUBLIC_DELEGATION_CONTRACT_ADDRESS : "" | ||
); | ||
setPublicDelegationAnnouncementsStartBlock( | ||
process.env.NEXT_PUBLIC_DELEGATION_ANNOUNCEMENTS_START_BLOCK | ||
? process.env.NEXT_PUBLIC_DELEGATION_ANNOUNCEMENTS_START_BLOCK | ||
: "" | ||
); | ||
} else if (chainId === crab.id) { | ||
console.log("switched to crab"); | ||
setPublicDaoAddress(process.env.NEXT_PUBLIC_DAO_ADDRESS_CRAD ? process.env.NEXT_PUBLIC_DAO_ADDRESS_CRAD : ""); | ||
setPublicTokenAddress( | ||
process.env.NEXT_PUBLIC_TOKEN_ADDRESS_CRAD ? process.env.NEXT_PUBLIC_TOKEN_ADDRESS_CRAD : "" | ||
); | ||
setPublicTokenVotingPluginAddress( | ||
process.env.NEXT_PUBLIC_TOKEN_VOTING_PLUGIN_ADDRESS_CRAD | ||
? process.env.NEXT_PUBLIC_TOKEN_VOTING_PLUGIN_ADDRESS_CRAD | ||
: "" | ||
); | ||
setPublicDelegationContractAddress( | ||
process.env.NEXT_PUBLIC_DELEGATION_CONTRACT_ADDRESS_CRAD | ||
? process.env.NEXT_PUBLIC_DELEGATION_CONTRACT_ADDRESS_CRAD | ||
: "" | ||
); | ||
setPublicDelegationAnnouncementsStartBlock( | ||
process.env.NEXT_PUBLIC_DELEGATION_ANNOUNCEMENTS_START_BLOCK_CRAD | ||
? process.env.NEXT_PUBLIC_DELEGATION_ANNOUNCEMENTS_START_BLOCK_CRAD | ||
: "" | ||
); | ||
} else if (chainId === 701) { | ||
console.log("switched to koi"); | ||
setPublicDaoAddress(process.env.NEXT_PUBLIC_DAO_ADDRESS_KOI ? process.env.NEXT_PUBLIC_DAO_ADDRESS_KOI : ""); | ||
setPublicTokenAddress(process.env.NEXT_PUBLIC_TOKEN_ADDRESS_KOI ? process.env.NEXT_PUBLIC_TOKEN_ADDRESS_KOI : ""); | ||
setPublicTokenVotingPluginAddress( | ||
process.env.NEXT_PUBLIC_TOKEN_VOTING_PLUGIN_ADDRESS_KOI | ||
? process.env.NEXT_PUBLIC_TOKEN_VOTING_PLUGIN_ADDRESS_KOI | ||
: "" | ||
); | ||
setPublicDelegationContractAddress( | ||
process.env.NEXT_PUBLIC_DELEGATION_CONTRACT_ADDRESS_KOI | ||
? process.env.NEXT_PUBLIC_DELEGATION_CONTRACT_ADDRESS_KOI | ||
: "" | ||
); | ||
setPublicDelegationAnnouncementsStartBlock( | ||
process.env.NEXT_PUBLIC_DELEGATION_ANNOUNCEMENTS_START_BLOCK_KOI | ||
? process.env.NEXT_PUBLIC_DELEGATION_ANNOUNCEMENTS_START_BLOCK_KOI | ||
: "" | ||
); | ||
} | ||
}, [chainId]); | ||
|
||
return { | ||
publicDaoAddress, | ||
publicTokenAddress, | ||
publicTokenVotingPluginAddress, | ||
publicDelegationContractAddress, | ||
publicDelegationAnnouncementsStartBlock, | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
import { useEffect, useState } from "react"; | ||
import { useChainId } from "wagmi"; | ||
import { IconType } from "@aragon/ods"; | ||
import { Address } from "viem"; | ||
import { crab, darwinia } from "viem/chains"; | ||
|
||
type PluginItem = { | ||
/** The URL fragment after /plugins */ | ||
id: string; | ||
/** The name of the folder within `/plugins` */ | ||
folderName: string; | ||
/** Title on menu */ | ||
title: string; | ||
icon: IconType; | ||
pluginAddress: string; | ||
}; | ||
|
||
const darwiniaPlugins: PluginItem[] = [ | ||
{ | ||
id: "community-proposals", | ||
folderName: "tokenVoting", | ||
title: "Community proposals", | ||
icon: IconType.BLOCKCHAIN_BLOCKCHAIN, | ||
pluginAddress: (process.env.NEXT_PUBLIC_TOKEN_VOTING_PLUGIN_ADDRESS ?? "") as Address, | ||
}, | ||
{ | ||
id: "delegate-wall", | ||
folderName: "delegateAnnouncer", | ||
title: "Delegation", | ||
icon: IconType.FEEDBACK, | ||
pluginAddress: (process.env.NEXT_PUBLIC_DELEGATION_CONTRACT_ADDRESS ?? "") as Address, | ||
}, | ||
]; | ||
|
||
const carbPlugins: PluginItem[] = [ | ||
{ | ||
id: "community-proposals", | ||
folderName: "tokenVoting", | ||
title: "Community proposals", | ||
icon: IconType.BLOCKCHAIN_BLOCKCHAIN, | ||
pluginAddress: (process.env.NEXT_PUBLIC_TOKEN_VOTING_PLUGIN_ADDRESS_CRAB ?? "") as Address, | ||
}, | ||
{ | ||
id: "delegate-wall", | ||
folderName: "delegateAnnouncer", | ||
title: "Delegation", | ||
icon: IconType.FEEDBACK, | ||
pluginAddress: (process.env.NEXT_PUBLIC_DELEGATION_CONTRACT_ADDRESS_CRAB ?? "") as Address, | ||
}, | ||
]; | ||
|
||
const koiPlugins: PluginItem[] = [ | ||
{ | ||
id: "community-proposals", | ||
folderName: "tokenVoting", | ||
title: "Community proposals", | ||
icon: IconType.BLOCKCHAIN_BLOCKCHAIN, | ||
pluginAddress: (process.env.NEXT_PUBLIC_TOKEN_VOTING_PLUGIN_ADDRESS_KOI ?? "") as Address, | ||
}, | ||
{ | ||
id: "delegate-wall", | ||
folderName: "delegateAnnouncer", | ||
title: "Delegation", | ||
icon: IconType.FEEDBACK, | ||
pluginAddress: (process.env.NEXT_PUBLIC_DELEGATION_CONTRACT_ADDRESS_KOI ?? "") as Address, | ||
}, | ||
]; | ||
|
||
export default function usePlugins() { | ||
const chainId = useChainId(); | ||
const [plugins, setPlugins] = useState<PluginItem[]>(darwiniaPlugins); | ||
|
||
useEffect(() => { | ||
if (chainId === darwinia.id) { | ||
setPlugins([...darwiniaPlugins]); | ||
} else if (chainId === crab.id) { | ||
setPlugins([...carbPlugins]); | ||
} else if (chainId === 701) { | ||
setPlugins([...koiPlugins]); | ||
} | ||
}, [chainId]); | ||
|
||
return plugins; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.