Skip to content

Commit

Permalink
Add showSwap
Browse files Browse the repository at this point in the history
  • Loading branch information
chaitanyapotti committed Oct 22, 2024
1 parent cf9892e commit c46886b
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 7 deletions.
1 change: 1 addition & 0 deletions packages/base/src/hooks/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ export interface IBaseWeb3AuthHookContext {
export interface IBaseWalletServicesHookContext {
isPluginConnected: boolean;
showWalletConnectScanner(showWalletConnectParams?: BaseEmbedControllerState["showWalletConnect"]): Promise<void>;
showSwap(showSwapParams?: BaseEmbedControllerState["showSwap"]): Promise<void>;
showCheckout(showCheckoutParams?: BaseEmbedControllerState["showCheckout"]): Promise<void>;
showWalletUI(showWalletUiParams?: BaseEmbedControllerState["showWalletUi"]): Promise<void>;
}
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { BaseEmbedControllerState } from "@toruslabs/base-controllers";
import { EVM_PLUGINS, IPlugin, PLUGIN_EVENTS, WalletServicesPluginError, Web3AuthContextKey } from "@web3auth/base";
import { WalletServicesPlugin } from "@web3auth/wallet-services-plugin";
import { defineComponent, h, inject, provide, Ref, ref, watch } from "vue";
Expand Down Expand Up @@ -57,25 +58,32 @@ export const WalletServicesProvider = defineComponent({
}
});

const showWalletConnectScanner = async () => {
const showWalletConnectScanner = async (showWalletConnectScannerParams?: BaseEmbedControllerState["showWalletConnect"]) => {
if (!walletServicesPlugin.value) throw WalletServicesPluginError.notInitialized();
if (!isPluginConnected.value) throw WalletServicesPluginError.walletPluginNotConnected();

return walletServicesPlugin.value.showWalletConnectScanner();
return walletServicesPlugin.value.showWalletConnectScanner(showWalletConnectScannerParams);
};

const showWalletUI = async () => {
const showWalletUI = async (showWalletUiParams?: BaseEmbedControllerState["showWalletUi"]) => {
if (!walletServicesPlugin.value) throw WalletServicesPluginError.notInitialized();
if (!isPluginConnected.value) throw WalletServicesPluginError.walletPluginNotConnected();

return walletServicesPlugin.value.showWalletUi();
return walletServicesPlugin.value.showWalletUi(showWalletUiParams);
};

const showCheckout = async () => {
const showCheckout = async (showCheckoutParams?: BaseEmbedControllerState["showCheckout"]) => {
if (!walletServicesPlugin.value) throw WalletServicesPluginError.notInitialized();
if (!isPluginConnected.value) throw WalletServicesPluginError.walletPluginNotConnected();

return walletServicesPlugin.value.showCheckout();
return walletServicesPlugin.value.showCheckout(showCheckoutParams);
};

const showSwap = async (showSwapParams?: BaseEmbedControllerState["showSwap"]) => {
if (!walletServicesPlugin.value) throw WalletServicesPluginError.notInitialized();
if (!isPluginConnected.value) throw WalletServicesPluginError.walletPluginNotConnected();

return walletServicesPlugin.value.showSwap(showSwapParams);
};

provide<IWalletServicesContext>(WalletServicesContextKey, {
Expand All @@ -84,6 +92,7 @@ export const WalletServicesProvider = defineComponent({
showWalletConnectScanner,
showCheckout,
showWalletUI,
showSwap,
});
},
render() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ export interface IBaseWalletServicesComposableContext {
showWalletConnectScanner(showWalletConnectParams?: BaseEmbedControllerState["showWalletConnect"]): Promise<void>;
showCheckout(showCheckoutParams?: BaseEmbedControllerState["showCheckout"]): Promise<void>;
showWalletUI(showWalletUiParams?: BaseEmbedControllerState["showWalletUi"]): Promise<void>;
showSwap(showSwapParams?: BaseEmbedControllerState["showSwap"]): Promise<void>;
}

export interface IWalletServicesContext extends IBaseWalletServicesComposableContext {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,15 +79,26 @@ export function WalletServicesContextProvider<T extends IBaseWeb3AuthHookContext
[walletServicesPlugin, isPluginConnected]
);

const showSwap = useCallback(
async (showSwapParams?: BaseEmbedControllerState["showSwap"]) => {
if (!walletServicesPlugin) throw WalletServicesPluginError.notInitialized();
if (!isPluginConnected) throw WalletServicesPluginError.walletPluginNotConnected();

return walletServicesPlugin.showSwap(showSwapParams);
},
[walletServicesPlugin, isPluginConnected]
);

const value = useMemo(() => {
return {
plugin: walletServicesPlugin,
isPluginConnected,
showWalletConnectScanner,
showCheckout,
showWalletUI,
showSwap,
};
}, [walletServicesPlugin, isPluginConnected, showWalletConnectScanner, showCheckout, showWalletUI]);
}, [walletServicesPlugin, isPluginConnected, showWalletConnectScanner, showCheckout, showWalletUI, showSwap]);

return createElement(WalletServicesContext.Provider, { value }, children);
}
5 changes: 5 additions & 0 deletions packages/plugins/wallet-services-plugin/src/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,11 @@ export class WalletServicesPlugin extends SafeEventEmitter implements IPlugin {
return this.wsEmbedInstance.showWalletUi(showWalletUiParams);
}

async showSwap(showSwapParams?: BaseEmbedControllerState["showSwap"]): Promise<void> {
if (!this.wsEmbedInstance.isLoggedIn) throw WalletServicesPluginError.walletPluginNotConnected();
return this.wsEmbedInstance.showSwap(showSwapParams);
}

async cleanup(): Promise<void> {
return this.wsEmbedInstance.cleanUp();
}
Expand Down

0 comments on commit c46886b

Please sign in to comment.