diff --git a/packages/sdk/src/execute/checkCallback.ts b/packages/sdk/src/execute/checkCallback.ts new file mode 100644 index 00000000..3ca1d569 --- /dev/null +++ b/packages/sdk/src/execute/checkCallback.ts @@ -0,0 +1,54 @@ +import { Wallet } from '@near-wallet-selector/core'; + +type Environment = 'client' | 'server'; + +const getEnvironment = (): Environment => { + if (typeof window !== 'undefined') { + return 'client'; + } else { + return 'server'; + } +}; + +const currentEnv: Environment = getEnvironment(); + + +export const callbackSideCheck = ( + callbackUrl: string | undefined, + wallet: Wallet, +): string => { + let callbackFinal = callbackUrl; + + if (currentEnv === 'client') { + const shouldGetFromMbjs = callbackUrl?.length < 1 || callbackUrl === undefined && + window?.['mbjs']?.callbackUrl && window?.['mbjs']?.callbackUrl.length > 0; + + if (wallet?.id == 'mintbase-wallet') { + if (callbackUrl?.length < 1 || callbackUrl === undefined) { + let mbjsCallbackUrl = ''; + + if ( + window?.['mbjs']?.callbackUrl && + window?.['mbjs']?.callbackUrl.length > 0 + ) { + mbjsCallbackUrl = window?.['mbjs']?.callbackUrl; + } + + const globalCBUrl = + localStorage?.getItem('mintbase-wallet:callback_url') || mbjsCallbackUrl; + + callbackFinal = globalCBUrl; + } + } + + if (shouldGetFromMbjs) { + callbackFinal = window?.['mbjs']?.callbackUrl || ''; + } + } + + // Return the final callback URL. + return callbackFinal; +}; + + + diff --git a/packages/sdk/src/execute/execute.test.ts b/packages/sdk/src/execute/execute.test.ts index e6fb56a9..db3957ac 100644 --- a/packages/sdk/src/execute/execute.test.ts +++ b/packages/sdk/src/execute/execute.test.ts @@ -1,7 +1,3 @@ -/** - * @jest-environment jsdom - */ - /* eslint-disable @typescript-eslint/no-explicit-any */ import { execute } from './execute'; import { MAX_GAS, ONE_YOCTO } from '../constants'; diff --git a/packages/sdk/src/execute/execute.ts b/packages/sdk/src/execute/execute.ts index 5caa7c0c..4f7eb3fb 100644 --- a/packages/sdk/src/execute/execute.ts +++ b/packages/sdk/src/execute/execute.ts @@ -6,6 +6,7 @@ import { genericBatchExecute, validateSigningOptions, } from './execute.utils'; +import { callbackSideCheck } from './checkCallback'; /** * Base method for executing contract calls. @@ -22,32 +23,8 @@ export const execute = async ( > => { validateSigningOptions({ wallet, account }); - let callbackFinal = callbackUrl; - - const shouldGetFromMbjs = callbackUrl?.length < 1 || callbackUrl === undefined && - window?.['mbjs']?.callbackUrl && window?.['mbjs']?.callbackUrl.length > 0; - - if (wallet?.id == 'mintbase-wallet') { - if (callbackUrl?.length < 1 || callbackUrl === undefined) { - let mbjsCallbackUrl = ''; - - if ( - window?.['mbjs']?.callbackUrl && - window?.['mbjs']?.callbackUrl.length > 0 - ) { - mbjsCallbackUrl = window?.['mbjs']?.callbackUrl; - } - - const globalCBUrl = - localStorage?.getItem('mintbase-wallet:callback_url') || mbjsCallbackUrl; - - callbackFinal = globalCBUrl; - } - } - - if (shouldGetFromMbjs) { - callbackFinal = window?.['mbjs']?.callbackUrl; - } + // check if execute is client side and return callback from global object, if not return callback from param + const callbackFinal = callbackSideCheck(callbackUrl, wallet); const outcomes = await genericBatchExecute( flattenArgs(calls),