-
Notifications
You must be signed in to change notification settings - Fork 159
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
68 additions
and
1 deletion.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,11 @@ | ||
import kdaAccountsGet from "./kda_accounts_get"; | ||
import kdaBalanceGet from "./kda_balance_get"; | ||
import kdaSignerSignPayload from "./kda_signer_signPayload"; | ||
import kdaSwitchNetwork from "./kda_switchNetwork"; | ||
|
||
export default [kdaAccountsGet, kdaBalanceGet, kdaSignerSignPayload]; | ||
export default [ | ||
kdaAccountsGet, | ||
kdaBalanceGet, | ||
kdaSignerSignPayload, | ||
kdaSwitchNetwork, | ||
]; |
59 changes: 59 additions & 0 deletions
59
packages/extension/src/providers/kadena/methods/kda_switchNetwork.ts
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,59 @@ | ||
import { getCustomError } from "@/libs/error"; | ||
import { sendToBackgroundFromBackground } from "@/libs/messenger/extension"; | ||
import { InternalMethods } from "@/types/messenger"; | ||
import { ProviderRPCRequest } from "@/types/provider"; | ||
import { MiddlewareFunction } from "@enkryptcom/types"; | ||
import DomainState from "@/libs/domain-state"; | ||
|
||
import KadenaProvider from ".."; | ||
import KDANetworks from "../networks"; | ||
import { KadenaNetworks } from "../types"; | ||
|
||
const method: MiddlewareFunction = function ( | ||
this: KadenaProvider, | ||
payload: ProviderRPCRequest, | ||
res, | ||
next | ||
): void { | ||
if (payload.method !== "kda_switchNetwork") return next(); | ||
else { | ||
if ( | ||
!payload.params || | ||
payload.params.length < 1 || | ||
!Object.values(KadenaNetworks).includes(payload.params[0]) | ||
) { | ||
return res(getCustomError("kda_switchNetwork: invalid params")); | ||
} | ||
|
||
const allNetworks = Object.values(KDANetworks); | ||
const validNetwork = allNetworks.find( | ||
(net) => net.name === payload.params![0] | ||
); | ||
|
||
if (validNetwork) { | ||
sendToBackgroundFromBackground({ | ||
message: JSON.stringify({ | ||
method: InternalMethods.changeNetwork, | ||
params: [validNetwork.name], | ||
}), | ||
provider: validNetwork.provider, | ||
tabId: payload.options?.tabId, | ||
}).then(() => { | ||
const domainState = new DomainState(); | ||
domainState | ||
.setSelectedNetwork(validNetwork.name) | ||
.then(() => res(null, true)); | ||
}); | ||
} else { | ||
return res( | ||
getCustomError( | ||
`kda_switchNetwork: provided network ${ | ||
payload.params![0] | ||
} not supported` | ||
) | ||
); | ||
} | ||
} | ||
}; | ||
|
||
export default method; |
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