Skip to content

Commit

Permalink
feat: redemption widget without connecting wallet (#627)
Browse files Browse the repository at this point in the history
* feat: redemption without connecting wallet works

* feat: both signer and web3libadapter
  • Loading branch information
albertfolch-redeemeum authored Dec 12, 2023
1 parent 9779933 commit 241fbcb
Show file tree
Hide file tree
Showing 15 changed files with 324 additions and 75 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { useEnvContext } from "../../environment/EnvironmentContext";
import { Context } from "./ChatContext";
import { getChatEnvName } from "./const";
import { useSigner } from "../../../hooks/connection/connection";
import { useExternalSigner } from "../../signer/useExternalSigner";

interface Props {
children: ReactNode;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import Confirmation, { ConfirmationProps } from "./Confirmation";
import { NonModalProps, useNonModalContext } from "../../../nonModal/NonModal";
import Typography from "../../../../ui/Typography";
import { theme } from "../../../../../theme";
import { useAccount } from "wagmi";
import { useAccount } from "../../../../../hooks/connection/connection";

const colors = theme.colors.light;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { EvaluationMethod, TokenType, GatingType } from "@bosonprotocol/common";
import { EvaluationMethod, TokenType } from "@bosonprotocol/common";
import { CoreSDK } from "@bosonprotocol/core-sdk";
import { Check, X } from "phosphor-react";
import React, { CSSProperties, useEffect, useMemo, useState } from "react";
Expand Down Expand Up @@ -61,14 +61,11 @@ const buildMessage = (
tokenType,
minTokenId,
maxTokenId,
gatingType,
// gatingType,
tokenAddress,
threshold
} = condition;

const perWalletOrPerToken =
gatingType === GatingType.PerAddress ? " (per wallet)" : " (per token)";

const TokenLink = (
<a
href={getTxExplorerUrl?.(tokenAddress, true)}
Expand All @@ -91,22 +88,21 @@ const buildMessage = (
if (method === EvaluationMethod.Threshold) {
return (
<>
{threshold} tokens {perWalletOrPerToken} from {TokenLink}
{threshold} tokens from {TokenLink}
</>
);
}
if (method === EvaluationMethod.TokenRange) {
if (minTokenId === maxTokenId) {
return (
<>
Token ID {perWalletOrPerToken}: {minTokenId} from {TokenLink}
Token ID: {minTokenId} from {TokenLink}
</>
);
}
return (
<>
From token ID {minTokenId} to token ID {maxTokenId}{" "}
{perWalletOrPerToken} from {TokenLink}
From token ID {minTokenId} to token ID {maxTokenId} from {TokenLink}
</>
);
}
Expand All @@ -115,15 +111,14 @@ const buildMessage = (
if (minTokenId === maxTokenId) {
return (
<>
{threshold} x token(s) with id: {minTokenId} {perWalletOrPerToken}{" "}
from {TokenLink}
{threshold} x token(s) with id: {minTokenId} from {TokenLink}
</>
);
}
return (
<>
{threshold} x token(s) from token ID {minTokenId} to token ID{" "}
{maxTokenId} {perWalletOrPerToken} from {TokenLink}
{maxTokenId} from {TokenLink}
</>
);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ export type RedeemNonModalProps = {
cancellationViewOnSuccess?: CancellationViewProps["onSuccess"];
confirmationViewOnSuccess?: ConfirmationViewProps["onSuccess"];
forcedAccount?: string;
parentOrigin?: string | null;
};

export default function RedeemWrapper({
Expand All @@ -105,7 +106,8 @@ export default function RedeemWrapper({
footerComponent: <BosonFooter />,
contentStyle: {
background: colors.white
}
},
showConnectButton: !props.parentOrigin
}}
>
<RedeemNonModal hideModal={hideModal} {...props} />
Expand Down
6 changes: 4 additions & 2 deletions packages/react-kit/src/components/modal/nonModal/Header.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -30,20 +30,22 @@ type HeaderProps = {
HeaderComponent: ReactNode;
closable: boolean;
handleOnClose: () => void;
showConnectButton: boolean;
};

const Header: React.FC<HeaderProps> = ({
HeaderComponent,
closable,
handleOnClose
handleOnClose,
showConnectButton
}) => {
return (
<>
{HeaderComponent && (
<Wrapper tag="div" margin="0">
{HeaderComponent}
<Grid justifyContent="flex-end">
<ConnectButton showChangeWallet />
{showConnectButton && <ConnectButton showChangeWallet />}
{closable && (
<ThemedButton data-close theme="blank" onClick={handleOnClose}>
<Close size={32} />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,7 @@ export interface NonModalProps {
maxWidths?: Store["modalMaxWidth"];
theme?: NonNullable<Store["theme"]>;
closable?: boolean;
showConnectButton: boolean;
}

export default function NonModal({
Expand All @@ -219,7 +220,8 @@ export default function NonModal({
maxWidths = null,
theme = "light",
contentStyle: _contentStyle,
closable = true
closable = true,
showConnectButton
}
}: {
children: React.ReactNode;
Expand All @@ -243,6 +245,7 @@ export default function NonModal({
HeaderComponent={HeaderComponent}
closable={closable}
handleOnClose={handleOnClose}
showConnectButton={showConnectButton}
/>

<Content $size={size} style={contentStyle}>
Expand Down
13 changes: 7 additions & 6 deletions packages/react-kit/src/components/signer/SignerContext.tsx
Original file line number Diff line number Diff line change
@@ -1,24 +1,25 @@
import React, { createContext, ReactNode } from "react";
import { Web3LibAdapter } from "@bosonprotocol/common";
import { useProvideExternalSigner } from "../../lib/signer/externalSigner";
import { Signer } from "ethers";

export const SignerContext = createContext<Web3LibAdapter | undefined>(
undefined
);
export const SignerContext = createContext<
{ externalWeb3LibAdapter: Web3LibAdapter; externalSigner: Signer } | undefined
>(undefined);

type SignerProviderProps = {
children: ReactNode;
parentOrigin: `http${string}` | null | undefined;
parentOrigin: string | null | undefined;
};
export const SignerProvider = ({
children,
parentOrigin
}: SignerProviderProps) => {
const externalSigner = useProvideExternalSigner({
const externalSignerListenerObject = useProvideExternalSigner({
parentOrigin
});
return (
<SignerContext.Provider value={externalSigner}>
<SignerContext.Provider value={externalSignerListenerObject}>
{children}
</SignerContext.Provider>
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ type FinanceWidgetProps = {
} & Omit<ConfigProviderProps, "magicLinkKey" | "infuraKey"> &
EnvironmentProviderProps &
ConvertionRateProviderProps & {
parentOrigin: `http${string}` | undefined | null;
parentOrigin: string | undefined | null;
sellerId: string | null | undefined;
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import { RedeemNonModalProps } from "../../modal/components/Redeem/RedeemNonModa
import { RedeemModalWithExchange } from "./RedeemModalWithExchange";
import { MagicProvider } from "../../magicLink/MagicContext";
import { CONFIG } from "../../../lib/config/config";
import { SignerProvider } from "../../signer/SignerContext";

type RedemptionProps = {
buttonProps?: Omit<ButtonProps, "onClick">;
Expand All @@ -41,7 +42,9 @@ type WidgetProps = RedemptionProps &
Omit<RedemptionContextProps, "setWidgetAction"> &
EnvironmentProviderProps &
ConvertionRateProviderProps &
Omit<WalletConnectionProviderProps, "children" | "envName">;
Omit<WalletConnectionProviderProps, "children" | "envName"> & {
parentOrigin?: string | undefined | null;
};
const queryClient = new QueryClient({
defaultOptions: {
queries: {
Expand Down Expand Up @@ -69,28 +72,30 @@ export function RedemptionWidget(props: WidgetProps) {
infuraKey={infuraKey}
{...props}
>
<MagicProvider>
<QueryClientProvider client={queryClient}>
<WalletConnectionProvider
walletConnectProjectId={props.walletConnectProjectId}
>
<ChatProvider>
<IpfsProvider {...props}>
<ConvertionRateProvider>
<ModalProvider>
<RedemptionProvider {...props}>
<RedeemModalWithExchange
{...props}
hideModal={props.closeWidgetClick}
/>
</RedemptionProvider>
</ModalProvider>
</ConvertionRateProvider>
</IpfsProvider>
</ChatProvider>
</WalletConnectionProvider>
</QueryClientProvider>
</MagicProvider>
<SignerProvider parentOrigin={props.parentOrigin}>
<MagicProvider>
<QueryClientProvider client={queryClient}>
<WalletConnectionProvider
walletConnectProjectId={props.walletConnectProjectId}
>
<ChatProvider>
<IpfsProvider {...props}>
<ConvertionRateProvider>
<ModalProvider>
<RedemptionProvider {...props}>
<RedeemModalWithExchange
{...props}
hideModal={props.closeWidgetClick}
/>
</RedemptionProvider>
</ModalProvider>
</ConvertionRateProvider>
</IpfsProvider>
</ChatProvider>
</WalletConnectionProvider>
</QueryClientProvider>
</MagicProvider>
</SignerProvider>
</ConfigProvider>
</EnvironmentProvider>
</div>
Expand Down
27 changes: 20 additions & 7 deletions packages/react-kit/src/hooks/connection/connection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,23 +13,31 @@ import { useEthersSigner } from "../ethers/useEthersSigner";
import { useConfigContext } from "../../components/config/ConfigContext";
import { useExternalSigner } from "../../components/signer/useExternalSigner";
import { useSignerAddress } from "../useSignerAddress";
import { useExternalSignerChainId } from "../../lib/signer/externalSigner";
import { Signer } from "ethers";

export function useAccount() {
const { address: account } = useWagmiAccount();
const { user } = useUser();
const externalSigner = useExternalSigner();
const externalSignerAddress = useSignerAddress(externalSigner);
const { externalWeb3LibAdapter } = useExternalSigner() ?? {};
const externalSignerAddress = useSignerAddress(externalWeb3LibAdapter);
return useMemo(
() => ({ address: externalSignerAddress ?? account ?? user }),
[account, user, externalSignerAddress]
);
}

export function useChainId() {
export function useChainId(): number | undefined {
const externalSigner = useExternalSigner();
const { data: externalSignerChainId } = useExternalSignerChainId();
const { chain } = useNetwork();
const magicChainId = useMagicChainId();
const isMagicLoggedIn = useIsMagicLoggedIn();
const chainIdToReturn = isMagicLoggedIn ? magicChainId : chain?.id;
const chainIdToReturn = externalSigner
? externalSignerChainId
: isMagicLoggedIn
? magicChainId
: chain?.id;
return chainIdToReturn;
}

Expand All @@ -40,13 +48,18 @@ export function useIsConnectedToWrongChain(): boolean {
return connectedToWrongChain;
}

export function useSigner() {
export function useSigner(): Signer | undefined {
const wagmiSigner = useEthersSigner();
const { externalSigner } = useExternalSigner() ?? {};
const magicProvider = useMagicProvider();
const isMagicLoggedIn = useIsMagicLoggedIn();

const signer = useMemo(() => {
return isMagicLoggedIn ? magicProvider?.getSigner() : wagmiSigner;
}, [wagmiSigner, magicProvider, isMagicLoggedIn]);
return externalSigner
? externalSigner
: isMagicLoggedIn
? magicProvider?.getSigner()
: wagmiSigner;
}, [externalSigner, wagmiSigner, magicProvider, isMagicLoggedIn]);
return signer;
}
2 changes: 1 addition & 1 deletion packages/react-kit/src/hooks/useCoreSdkOverrides.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ export const useCoreSdkOverrides = ({
}) => {
const externalSigner = useExternalSigner();
const overrides = useMemo(() => {
return externalSigner ? { web3Lib: externalSigner } : undefined;
return externalSigner ? { web3Lib: externalSigner.externalWeb3LibAdapter } : undefined;
}, [externalSigner]);
const coreSdk = useCoreSdk(coreSdkConfig, overrides);
return coreSdk;
Expand Down
2 changes: 1 addition & 1 deletion packages/react-kit/src/hooks/useCoreSdkWithContext.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ export function useCoreSDKWithContext() {
const signer = useSigner();
const defaultConfig = getEnvConfigById(envName, configId);
const overrides = useMemo(() => {
return externalSigner ? { web3Lib: externalSigner } : undefined;
return externalSigner ? { web3Lib: externalSigner.externalWeb3LibAdapter } : undefined;
}, [externalSigner]);
return hooks.useCoreSdk(
{
Expand Down
3 changes: 1 addition & 2 deletions packages/react-kit/src/lib/errors/transactions.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { Provider } from "@bosonprotocol/ethers-sdk";
import * as Sentry from "@sentry/browser";
import { getRevertReason } from "./eth-revert-reason";
import { providers } from "ethers";
Expand All @@ -12,7 +11,7 @@ export async function extractUserFriendlyError(
}: {
defaultError?: string;
txResponse?: providers.TransactionResponse;
provider?: Provider;
provider?: providers.Provider;
} = {}
): Promise<string> {
try {
Expand Down
Loading

0 comments on commit 241fbcb

Please sign in to comment.