-
Notifications
You must be signed in to change notification settings - Fork 31
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add switch network and get current network (#174)
- add switch network and get current network methods - fix bug when compare chain id - enable add network
- Loading branch information
1 parent
a857eed
commit 560751f
Showing
18 changed files
with
398 additions
and
63 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
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,16 @@ | ||
import { toJson } from './utils/serializer'; | ||
import { ApiParams } from './types/snapApi'; | ||
import { getCurrentNetwork as getCurrentNetworkUtil } from './utils/snapUtils'; | ||
import { logger } from './utils/logger'; | ||
|
||
export async function getCurrentNetwork(params: ApiParams) { | ||
try { | ||
const { state } = params; | ||
const networks = getCurrentNetworkUtil(state); | ||
logger.log(`getCurrentNetwork: networks:\n${toJson(networks, 2)}`); | ||
return networks; | ||
} catch (err) { | ||
logger.error(`Problem found: ${err}`); | ||
throw err; | ||
} | ||
} |
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,35 @@ | ||
import { toJson } from './utils/serializer'; | ||
import { ApiParams, SwitchNetworkRequestParams } from './types/snapApi'; | ||
import { getNetwork, setCurrentNetwork, getNetworkTxt } from './utils/snapUtils'; | ||
import { DialogType } from '@metamask/rpc-methods'; | ||
import { panel, heading } from '@metamask/snaps-ui'; | ||
import { logger } from './utils/logger'; | ||
|
||
export async function switchNetwork(params: ApiParams) { | ||
try { | ||
const { state, wallet, saveMutex, requestParams } = params; | ||
const requestParamsObj = requestParams as SwitchNetworkRequestParams; | ||
const network = getNetwork(state, requestParamsObj.chainId); | ||
if (!network) { | ||
throw new Error(`The given chainId is invalid: ${requestParamsObj.chainId}`); | ||
} | ||
const components = getNetworkTxt(network); | ||
|
||
const response = await wallet.request({ | ||
method: 'snap_dialog', | ||
params: { | ||
type: DialogType.Confirmation, | ||
content: panel([heading('Do you want to switch to this network?'), ...components]), | ||
}, | ||
}); | ||
if (!response) return false; | ||
|
||
logger.log(`switchNetwork: network:\n${toJson(network, 2)}`); | ||
await setCurrentNetwork(network, wallet, saveMutex, state); | ||
|
||
return true; | ||
} catch (err) { | ||
logger.error(`Problem found: ${err}`); | ||
throw err; | ||
} | ||
} |
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.