Skip to content

Commit

Permalink
Add network checks to stake, withdraw, and register operator
Browse files Browse the repository at this point in the history
  • Loading branch information
ccali11 committed Oct 26, 2023
1 parent fff422b commit 52ffbb5
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 19 deletions.
7 changes: 7 additions & 0 deletions apps/web/src/composables/operators.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import useEthers from '@/composables/ethers'
import useLedger from '@/composables/ledger'
import useTrezor from '@/composables/trezor'
import useUser from '@/composables/user'
import useWallets from '@/composables/wallets'
import useWalletConnectV2 from '@/composables/walletConnectV2'
import { CasimirManager, CasimirRegistry, CasimirViews } from '@casimir/ethereum/build/@types'

Expand All @@ -25,6 +26,7 @@ const { browserProvidersList, getEthersBrowserSigner } = useEthers()
const { getEthersLedgerSigner } = useLedger()
const { getEthersTrezorSigner } = useTrezor()
const { user } = useUser()
const { detectActiveNetwork, switchEthersNetwork } = useWallets()
const { getWalletConnectSignerV2 } = useWalletConnectV2()
const loadingInitializeOperators = ref(false)
const loadingInitializeOperatorsError = ref(false)
Expand Down Expand Up @@ -193,6 +195,11 @@ export default function useOperators() {

// TODO: Move this to operators.ts to combine with AddOperator method
async function registerOperatorWithCasimir({ walletProvider, address, operatorId, collateral, nodeUrl }: RegisterOperatorWithCasimirParams) {
const activeNetwork = await detectActiveNetwork(walletProvider)
if (activeNetwork !== 5) {
await switchEthersNetwork(walletProvider, '0x5')
return window.location.reload()
}
loadingRegisteredOperators.value = true
try {
let signer
Expand Down
14 changes: 14 additions & 0 deletions apps/web/src/composables/staking.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import useEnvironment from '@/composables/environment'
import useEthers from '@/composables/ethers'
import useLedger from '@/composables/ledger'
import useTrezor from '@/composables/trezor'
import useWallets from '@/composables/wallets'
import useWalletConnectV2 from './walletConnectV2'
import { CasimirManager } from '@casimir/ethereum/build/@types'

Expand All @@ -14,6 +15,7 @@ const { provider } = useEnvironment()
const { browserProvidersList, getEthersBrowserSigner } = useEthers()
const { getEthersLedgerSigner } = useLedger()
const { getEthersTrezorSigner } = useTrezor()
const { detectActiveNetwork, switchEthersNetwork } = useWallets()
const { getWalletConnectSignerV2 } = useWalletConnectV2()

const stakingComposableInitialized = ref(false)
Expand All @@ -38,6 +40,12 @@ export default function useStaking() {

async function deposit({ amount, walletProvider, type }: { amount: string, walletProvider: ProviderString, type: 'default' | 'eigen' }) {
try {
const activeNetwork = await detectActiveNetwork(walletProvider)
if (activeNetwork !== 5) {
await switchEthersNetwork(walletProvider, '0x5')
return window.location.reload()
}

let signer
if (browserProvidersList.includes(walletProvider)) {
signer = getEthersBrowserSigner(walletProvider)
Expand Down Expand Up @@ -111,6 +119,12 @@ export default function useStaking() {
}

async function withdraw({ amount, walletProvider, type }: { amount: string, walletProvider: ProviderString, type: 'default' | 'eigen' }) {
const activeNetwork = await detectActiveNetwork(walletProvider)
if (activeNetwork !== 5) {
await switchEthersNetwork(walletProvider, '0x5')
return window.location.reload()
}

let signer
if (browserProvidersList.includes(walletProvider)) {
signer = getEthersBrowserSigner(walletProvider)
Expand Down
38 changes: 19 additions & 19 deletions apps/web/src/composables/wallets.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,17 +67,17 @@ export default function useWallets() {
try {
await provider.request({
method:'wallet_switchEthereumChain',
params: [{chainId: chainId}]
params: [{ chainId }]
})
} catch (err: any) {
console.log(`Error occurred while switching chain to chainId ${chainId}, err: ${err.message} code: ${err.code}`)
if (err.code === 4902){
if (chainId === '5') {
addEthersNetwork(providerString, goerliNetwork)
} else if (chainId === '0x1252') {
addEthersNetwork(providerString, iotexNetwork)
}
}
// if (err.code === 4902){
// if (chainId === '5') {
// addEthersNetwork(providerString, goerliNetwork)
// } else if (chainId === '0x1252') {
// addEthersNetwork(providerString, iotexNetwork)
// }
// }
}
}

Expand All @@ -90,14 +90,14 @@ export default function useWallets() {
}
}

async function addEthersNetwork (providerString: ProviderString, network: any) {
const provider = getBrowserProvider(providerString)
try {
await provider.request({
method: 'wallet_addEthereumChain',
params: [network]
})
} catch(error: any) {
console.log(`Error occurred while adding network ${network.chainName}, Message: ${error.message} Code: ${error.code}`)
}
}
// async function addEthersNetwork (providerString: ProviderString, network: any) {
// const provider = getBrowserProvider(providerString)
// try {
// await provider.request({
// method: 'wallet_addEthereumChain',
// params: [network]
// })
// } catch(error: any) {
// console.log(`Error occurred while adding network ${network.chainName}, Message: ${error.message} Code: ${error.code}`)
// }
// }

0 comments on commit 52ffbb5

Please sign in to comment.