Skip to content

Commit

Permalink
callback works server side too (#450)
Browse files Browse the repository at this point in the history
  • Loading branch information
rubenmarcus authored Jan 9, 2024
1 parent fb57802 commit 47e0d35
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 30 deletions.
54 changes: 54 additions & 0 deletions packages/sdk/src/execute/checkCallback.ts
Original file line number Diff line number Diff line change
@@ -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;
};



4 changes: 0 additions & 4 deletions packages/sdk/src/execute/execute.test.ts
Original file line number Diff line number Diff line change
@@ -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';
Expand Down
29 changes: 3 additions & 26 deletions packages/sdk/src/execute/execute.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import {
genericBatchExecute,
validateSigningOptions,
} from './execute.utils';
import { callbackSideCheck } from './checkCallback';

/**
* Base method for executing contract calls.
Expand All @@ -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),
Expand Down

0 comments on commit 47e0d35

Please sign in to comment.