Skip to content

Commit

Permalink
initialise request client correctly
Browse files Browse the repository at this point in the history
  • Loading branch information
cajames committed Jun 24, 2024
1 parent 7f455b5 commit 25070bf
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 8 deletions.
2 changes: 2 additions & 0 deletions packages/internal/metrics/src/initialise.ts
Original file line number Diff line number Diff line change
Expand Up @@ -85,12 +85,14 @@ export const initialise = async () => {
try {
const runtimeDetails = flattenProperties(getRuntimeDetails());
const existingRuntimeId = getDetail(Detail.RUNTIME_ID);
const existingIdentity = getDetail(Detail.IDENTITY);

const body = {
version: 1,
data: {
runtimeDetails,
runtimeId: existingRuntimeId,
uId: existingIdentity,
},
};
const response = await post<InitialiseResponse>('/v1/sdk/initialise', body);
Expand Down
7 changes: 3 additions & 4 deletions packages/passport/sdk/src/zkEvm/sessionActivity/request.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,14 @@ const getBaseUrl = (environment?: Environment) => {
return SANDBOX_API;
case Environment.PRODUCTION:
return PROD_API;
// When in doubt, use sandbox
default:
return SANDBOX_API;
throw new Error('Environment not supported');
}
};

let client: AxiosInstance | undefined;

export const createInstanceClient = (environment?: Environment) => {
export const setupClient = (environment: Environment) => {
if (client) {
return;
}
Expand All @@ -46,7 +45,7 @@ export type CheckResponse = {

export async function get(queries: CheckParams) {
if (!client) {
createInstanceClient();
throw new Error('Client not initialised');
}
// pass queries as query string
return client!
Expand Down
14 changes: 10 additions & 4 deletions packages/passport/sdk/src/zkEvm/sessionActivity/sessionActivity.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { trackFlow, utils as metricsUtils, trackError } from '@imtbl/metrics';
import { utils } from 'ethers';
import { CheckResponse, get } from './request';
import { CheckResponse, get, setupClient } from './request';
import { errorBoundary } from './errorBoundary';
import { AccountRequestedEvent } from '../types';

Expand Down Expand Up @@ -35,6 +35,8 @@ const incrementSendCount = () => {
syncSendCount();
sendCount++;
setItem(SESSION_ACTIVITY_COUNT_KEY, sendCount);
// Reset checkCount to zero on sending
checkCount = 0;
};

// Fix no-promise-executor-return
Expand All @@ -52,10 +54,15 @@ const trackSessionActivityFn = async (args: AccountRequestedEvent) => {
}
currentSessionTrackCall = true;

const { sendTransaction } = args;
const { sendTransaction, environment } = args;
if (!sendTransaction) {
throw new Error('No sendTransaction function provided');
}
// Used to set up the request client
if (!environment) {
throw new Error('No environment provided');
}
setupClient(environment);

const clientId = args.passportClient;
if (!clientId) {
Expand Down Expand Up @@ -102,7 +109,6 @@ const trackSessionActivityFn = async (args: AccountRequestedEvent) => {
flow.addEvent('Start Sending Transaction');
const tx = await args.sendTransaction([{ to, from, data }], flow);
incrementSendCount();
checkCount = 0;
flow.addEvent('Transaction Sent', { tx });
} catch (error) {
flow.addEvent('Failed to send Transaction');
Expand All @@ -115,7 +121,7 @@ const trackSessionActivityFn = async (args: AccountRequestedEvent) => {
if (details && details.delay && details.delay > 0) {
flow.addEvent('Delaying Transaction', { delay: details.delay });
await wait(details.delay);
setTimeout(async () => {
setTimeout(() => {
flow.addEvent('Retrying after Delay');
currentSessionTrackCall = false;
// eslint-disable-next-line
Expand Down

0 comments on commit 25070bf

Please sign in to comment.