Skip to content

Commit

Permalink
fix: [GPR-415][Sale Widget] Fix transak integration after breaking ch…
Browse files Browse the repository at this point in the history
…anges (#1522)
  • Loading branch information
jhesgodi authored Feb 23, 2024
1 parent dabd6f2 commit 33b0cc3
Show file tree
Hide file tree
Showing 10 changed files with 158 additions and 100 deletions.
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { useRef } from 'react';
import { Environment } from '@imtbl/config';
import { TransakEventHandlers, useTransakEvents } from './useTransakEvents';

import {
Expand All @@ -10,17 +11,19 @@ import { UserJourney } from '../../context/analytics-provider/SegmentAnalyticsPr
import { CenteredBoxContent } from '../CenteredBoxContent/CenteredBoxContent';
import { LoadingBox } from '../../views/loading/LoadingBox';

export type TransactionIframeProps = {
export type TransakIframeProps = {
id: string;
type: TransakWidgetType;
email: string;
contractId: string;
environment: Environment;
walletAddress: string;
isPassportWallet: boolean;
loadingText: string;
} & TransakEventHandlers &
TransakNFTCheckoutParams;

export function TransakIframe(props: TransactionIframeProps) {
export function TransakIframe(props: TransakIframeProps) {
const {
id,
type,
Expand All @@ -32,7 +35,6 @@ export function TransakIframe(props: TransactionIframeProps) {
cryptoCurrencyCode,
estimatedGasLimit,
exchangeScreenTitle,
smartContractAddress,
partnerOrderId,
onOpen,
onInit,
Expand All @@ -43,6 +45,8 @@ export function TransakIframe(props: TransactionIframeProps) {
onFailedToLoad,
failedToLoadTimeoutInMs,
loadingText,
environment,
contractId,
} = props;
const iframeRef = useRef<HTMLIFrameElement>(null);

Expand All @@ -64,15 +68,18 @@ export function TransakIframe(props: TransactionIframeProps) {

const { iframeSrc } = useTransakIframe({
type,
nftData,
calldata,
cryptoCurrencyCode,
estimatedGasLimit,
exchangeScreenTitle,
smartContractAddress,
email,
walletAddress,
partnerOrderId,
contractId,
environment,
transakParams: {
nftData,
calldata,
cryptoCurrencyCode,
estimatedGasLimit,
exchangeScreenTitle,
email,
walletAddress,
partnerOrderId,
},
});

return (
Expand All @@ -83,8 +90,8 @@ export function TransakIframe(props: TransactionIframeProps) {
</CenteredBoxContent>
)}
<iframe
ref={iframeRef}
id={id}
ref={iframeRef}
src={iframeSrc}
title="Transak-Iframe"
allow="camera;microphone;fullscreen;payment"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ export type TransakNFTCheckoutParams = {
calldata: string;
cryptoCurrencyCode: string;
estimatedGasLimit: number;
smartContractAddress: string;
exchangeScreenTitle: string;
partnerOrderId?: string;
walletAddress?: string;
Expand All @@ -20,28 +19,32 @@ export type TransakNFTCheckoutParams = {

type UseTransakIframeProps = {
type: TransakWidgetType;
} & TransakNFTCheckoutParams;
contractId: string;
environment: Environment;
transakParams: TransakNFTCheckoutParams;
};

const MAX_GAS_LIMIT = '30000000';

// TODO: Move to common config file inside Checkout SDK while refactoring onRamp
// const { baseUrl, apiKey } = checkout.fiatExchangeConfig('transak')
export const TRANSAK_API_BASE_URL = {
[Environment.SANDBOX]: 'https://global-stg.transak.com',
[Environment.PRODUCTION]: 'https://global.transak.com/',
};

export const TRANSAK_ENVIRONMENT = {
[Environment.SANDBOX]: 'STAGING',
[Environment.PRODUCTION]: 'PRODUCTION',
};

export const useTransakIframe = (props: UseTransakIframeProps) => {
const { type, ...transakParams } = props;
const { contractId, environment, transakParams } = props;
const [iframeSrc, setIframeSrc] = useState<string>('');

// TODO: Get transak config from checkout SDK
// const { checkout, provider } = connectLoaderState;
// const { config } = checkout.fiatExchangeConfig('transak');
// const { environment, apiKey, network } = config
const environment = 'sandbox';
// const { baseUrl, apiKey, environment } = checkout.fiatExchangeConfig('transak')
const apiKey = 'd14b44fb-0f84-4db5-affb-e044040d724b';
const network = 'immutablezkevm';

const getNFTCheckoutURL = useCallback(() => {
const {
Expand All @@ -59,10 +62,10 @@ export const useTransakIframe = (props: UseTransakIframeProps) => {

const params = {
apiKey,
network,
isNFT: 'true',
disableWalletAddressForm: 'true',
environment: 'STAGING',
contractId,
environment: TRANSAK_ENVIRONMENT[environment],
calldata: btoa(String.fromCharCode.apply(null, pako.deflate(calldata))),
nftData: btoa(JSON.stringify(nftData)),
estimatedGasLimit: gasLimit.toString(),
Expand Down
11 changes: 5 additions & 6 deletions packages/checkout/widgets-lib/src/widgets/sale/SaleWidget.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,7 @@ import { TopUpView } from '../../views/top-up/TopUpView';
import { UserJourney } from '../../context/analytics-provider/SegmentAnalyticsProvider';
import { sendSaleWidgetCloseEvent } from './SaleWidgetEvents';
import { EventTargetContext } from '../../context/event-target-context/EventTargetContext';
import { useCurrency } from './hooks/useCurrency';
import { CURRENCY_NAME } from './utils/config';
import { useClientConfig } from './hooks/useClientConfig';

export interface SaleWidgetProps
extends Required<Omit<SaleWidgetParams, 'walletProviderName'>> {
Expand Down Expand Up @@ -67,10 +66,9 @@ export default function SaleWidget(props: SaleWidgetProps) {
() => ({ viewState, viewDispatch }),
[viewState, viewDispatch],
);
const { currencyResponse: currency } = useCurrency({
env: config.environment,
const { currency, clientConfig } = useClientConfig({
environmentId,
currencyName: CURRENCY_NAME,
environment: config.environment,
});

const fromTokenAddress = currency?.erc20Address || '';
Expand Down Expand Up @@ -117,12 +115,13 @@ export default function SaleWidget(props: SaleWidgetProps) {
items,
amount,
fromTokenAddress,
env: config.environment,
environment: config.environment,
environmentId,
provider,
checkout,
passport: checkout?.passport,
collectionName,
clientConfig,
}}
>
<CryptoFiatProvider environment={config.environment}>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,13 @@ export function WithCard(props: WithCardProps) {
onOrderFailed,
} = props;
const {
recipientEmail, recipientAddress, isPassportWallet, signResponse, goToErrorView,
recipientEmail,
recipientAddress,
isPassportWallet,
signResponse,
goToErrorView,
environment,
clientConfig,
} = useSaleContext();
const executeTxn = signResponse?.transactions.find((txn) => txn.methodCall.startsWith('execute'));

Expand Down Expand Up @@ -63,7 +69,6 @@ export function WithCard(props: WithCardProps) {
calldata={executeTxn.rawData}
cryptoCurrencyCode={signResponse.order.currency.name}
estimatedGasLimit={executeTxn.gasEstimate}
smartContractAddress={executeTxn.tokenAddress}
partnerOrderId={executeTxn.params.reference}
onInit={onInit}
onOpen={onOpen}
Expand All @@ -72,6 +77,8 @@ export function WithCard(props: WithCardProps) {
onOrderCompleted={onOrderCompleted}
onOrderFailed={onOrderFailed}
onFailedToLoad={onFailedToLoad}
environment={environment}
contractId={clientConfig.contractId}
/>
);
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import {
useRef,
useState,
} from 'react';
import { Environment } from '@imtbl/config';
import { ConnectLoaderState } from '../../../context/connect-loader-context/ConnectLoaderContext';
import {
FundWithSmartCheckoutSubViews,
Expand All @@ -28,6 +29,7 @@ import {
import { StrongCheckoutWidgetsConfig } from '../../../lib/withDefaultWidgetConfig';
import { useSignOrder } from '../hooks/useSignOrder';
import {
ClientConfig,
ExecuteOrderResponse,
ExecutedTransaction,
SaleErrorTypes,
Expand All @@ -37,10 +39,11 @@ import {
} from '../types';

import { useSmartCheckout } from '../hooks/useSmartCheckout';
import { defaultClientConfig } from '../hooks/useClientConfig';

type SaleContextProps = {
config: StrongCheckoutWidgetsConfig;
env: string;
environment: Environment;
environmentId: string;
items: SaleItem[];
amount: string;
Expand All @@ -49,6 +52,7 @@ type SaleContextProps = {
provider: ConnectLoaderState['provider'];
checkout: ConnectLoaderState['checkout'];
passport?: Passport;
clientConfig: ClientConfig;
};

type SaleContextValues = SaleContextProps & {
Expand Down Expand Up @@ -90,7 +94,7 @@ const SaleContext = createContext<SaleContextValues>({
provider: undefined,
checkout: undefined,
environmentId: '',
env: '',
environment: Environment.SANDBOX,
recipientAddress: '',
recipientEmail: '',
sign: () => Promise.resolve(undefined),
Expand All @@ -111,6 +115,7 @@ const SaleContext = createContext<SaleContextValues>({
fundingRoutes: [],
disabledPaymentTypes: [],
invalidParameters: false,
clientConfig: defaultClientConfig,
});

SaleContext.displayName = 'SaleSaleContext';
Expand All @@ -126,7 +131,7 @@ export function SaleContextProvider(props: {
children,
value: {
config,
env,
environment,
environmentId,
items,
amount,
Expand All @@ -135,6 +140,7 @@ export function SaleContextProvider(props: {
checkout,
passport,
collectionName,
clientConfig,
},
} = props;

Expand Down Expand Up @@ -199,7 +205,7 @@ export function SaleContextProvider(props: {
fromTokenAddress,
recipientAddress,
environmentId,
env,
environment,
});

const sign = useCallback(
Expand Down Expand Up @@ -363,7 +369,7 @@ export function SaleContextProvider(props: {
executeResponse,
environmentId,
collectionName,
env,
environment,
provider,
checkout,
recipientAddress,
Expand All @@ -379,10 +385,11 @@ export function SaleContextProvider(props: {
fundingRoutes,
disabledPaymentTypes,
invalidParameters,
clientConfig,
}),
[
config,
env,
environment,
environmentId,
items,
amount,
Expand All @@ -405,6 +412,7 @@ export function SaleContextProvider(props: {
fundingRoutes,
disabledPaymentTypes,
invalidParameters,
clientConfig,
],
);

Expand Down
Loading

0 comments on commit 33b0cc3

Please sign in to comment.