Skip to content

Commit

Permalink
feat: [CM-632][Sale Widget] Add configuration flat to enable/disable …
Browse files Browse the repository at this point in the history
…transaction finality confirmation (#1753)
  • Loading branch information
mimi-imtbl authored May 10, 2024
1 parent 5d5f89f commit fe1269a
Show file tree
Hide file tree
Showing 7 changed files with 23 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,5 @@ import { WidgetConfiguration } from './widget';
*/
export type SaleWidgetConfiguration = {
multicurrency?: boolean;
waitFulfillmentSettlements?: boolean;
} & WidgetConfiguration;
3 changes: 3 additions & 0 deletions packages/checkout/widgets-lib/src/widgets/sale/SaleWidget.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ Omit<SaleWidgetParams, 'walletProviderName'>
type WidgetParams = RequiredWidgetParams &
OptionalWidgetParams & {
multicurrency: boolean;
waitFulfillmentSettlements: boolean;
};
export interface SaleWidgetProps extends WidgetParams {
config: StrongCheckoutWidgetsConfig;
Expand All @@ -63,6 +64,7 @@ export default function SaleWidget(props: SaleWidgetProps) {
collectionName,
excludePaymentTypes,
multicurrency = false,
waitFulfillmentSettlements = true,
} = props;
const { connectLoaderState } = useContext(ConnectLoaderContext);
const { checkout, provider } = connectLoaderState;
Expand Down Expand Up @@ -128,6 +130,7 @@ export default function SaleWidget(props: SaleWidgetProps) {
collectionName,
excludePaymentTypes,
multicurrency,
waitFulfillmentSettlements,
}}
>
<CryptoFiatProvider environment={config.environment}>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,9 @@ export class Sale extends Base<WidgetType.SALE> {
validatedParams.collectionName = '';
}

if (params.excludePaymentTypes !== undefined && !Array.isArray(params.excludePaymentTypes)) {
if (
params.excludePaymentTypes !== undefined && !Array.isArray(params.excludePaymentTypes)
) {
// eslint-disable-next-line no-console
console.warn('[IMTBL]: invalid "excludePaymentTypes" widget input');
validatedParams.excludePaymentTypes = [];
Expand Down Expand Up @@ -122,7 +124,11 @@ export class Sale extends Base<WidgetType.SALE> {
sendSaleWidgetCloseEvent(window);
}}
>
<Suspense fallback={<LoadingView loadingText={t('views.LOADING_VIEW.text')} />}>
<Suspense
fallback={
<LoadingView loadingText={t('views.LOADING_VIEW.text')} />
}
>
<SaleWidget
config={config}
amount={this.parameters.amount!}
Expand All @@ -132,6 +138,7 @@ export class Sale extends Base<WidgetType.SALE> {
excludePaymentTypes={this.parameters.excludePaymentTypes!}
language="en"
multicurrency={!!this.properties?.config?.multicurrency}
waitFulfillmentSettlements={this.properties?.config?.waitFulfillmentSettlements ?? true}
/>
</Suspense>
</ConnectLoader>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ type SaleContextProps = {
passport?: Passport;
excludePaymentTypes: SalePaymentTypes[];
multicurrency: boolean;
waitFulfillmentSettlements: boolean;
};

type SaleContextValues = SaleContextProps & {
Expand All @@ -64,7 +65,6 @@ type SaleContextValues = SaleContextProps & {
) => Promise<SignResponse | undefined>;
execute: (
signResponse: SignResponse | undefined,
waitForTrnsactionSettlement: boolean,
onTxnSuccess: (txn: ExecutedTransaction) => void,
onTxnError: (error: any, txns: ExecutedTransaction[]) => void
) => Promise<ExecutedTransaction[]>;
Expand Down Expand Up @@ -136,6 +136,7 @@ const SaleContext = createContext<SaleContextValues>({
excludePaymentTypes: [],
multicurrency: false,
selectedCurrency: undefined,
waitFulfillmentSettlements: true,
});

SaleContext.displayName = 'SaleSaleContext';
Expand All @@ -161,6 +162,7 @@ export function SaleContextProvider(props: {
collectionName,
excludePaymentTypes,
multicurrency,
waitFulfillmentSettlements,
},
} = props;

Expand Down Expand Up @@ -252,6 +254,7 @@ export function SaleContextProvider(props: {
recipientAddress,
environmentId,
environment,
waitFulfillmentSettlements,
});

const sign = useCallback(
Expand Down Expand Up @@ -428,6 +431,7 @@ export function SaleContextProvider(props: {
excludePaymentTypes,
multicurrency,
selectedCurrency,
waitFulfillmentSettlements,
}),
[
config,
Expand Down Expand Up @@ -462,6 +466,7 @@ export function SaleContextProvider(props: {
excludePaymentTypes,
multicurrency,
selectedCurrency,
waitFulfillmentSettlements,
],
);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ const toSignResponse = (

export const useSignOrder = (input: SignOrderInput) => {
const {
provider, items, environment, environmentId,
provider, items, environment, environmentId, waitFulfillmentSettlements,
} = input;
const [signError, setSignError] = useState<SignOrderError | undefined>(
undefined,
Expand Down Expand Up @@ -186,7 +186,6 @@ export const useSignOrder = (input: SignOrderInput) => {
data: string,
gasLimit: number,
method: string,
waitForTrnsactionSettlement: boolean,
): Promise<[hash: string | undefined, error: any]> => {
let transactionHash: string | undefined;

Expand All @@ -202,7 +201,7 @@ export const useSignOrder = (input: SignOrderInput) => {

setExecuteTransactions({ method, hash: txnResponse?.hash });

if (waitForTrnsactionSettlement) {
if (waitFulfillmentSettlements) {
await txnResponse?.wait();
}

Expand Down Expand Up @@ -326,7 +325,6 @@ export const useSignOrder = (input: SignOrderInput) => {

const execute = async (
signData: SignResponse | undefined,
waitForTrnsactionSettlement: boolean,
onTxnSuccess: (txn: ExecutedTransaction) => void,
onTxnError: (error: any, txns: ExecutedTransaction[]) => void,
): Promise<ExecutedTransaction[]> => {
Expand Down Expand Up @@ -360,7 +358,6 @@ export const useSignOrder = (input: SignOrderInput) => {
data,
gasEstimate,
method,
waitForTrnsactionSettlement,
);

if (txnError || !hash) {
Expand Down
1 change: 1 addition & 0 deletions packages/checkout/widgets-lib/src/widgets/sale/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ export type SignOrderInput = {
recipientAddress: string;
environment: string;
environmentId: string;
waitFulfillmentSettlements: boolean;
};

export type SignOrderError = {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ export function PayWithCoins() {
sendSuccessEvent,
} = useSaleEvent();
const {
execute, signResponse, executeResponse, signTokenIds, provider,
execute, signResponse, executeResponse, signTokenIds,
} = useSaleContext();
const executedTxns = executeResponse?.transactions.length || 0;

Expand All @@ -35,10 +35,8 @@ export function PayWithCoins() {
}

const sendTransaction = async () => {
const waitForTrnsactionSettlement = !('isMetaMask' in (provider?.provider as any) && provider?.provider.isMetaMask);
execute(
signResponse,
waitForTrnsactionSettlement,
(txn) => {
sendTransactionSuccessEvent(txn); // not an analytics event
},
Expand Down

0 comments on commit fe1269a

Please sign in to comment.