Skip to content

Commit

Permalink
feat: add interface to trigger session activity for a different clien…
Browse files Browse the repository at this point in the history
…t id (#2461)
  • Loading branch information
shineli1984 authored Dec 11, 2024
1 parent 6e11807 commit 17e01d7
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 25 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,12 @@ const EthereumMethods: EthereumMethod[] = [
{ name: 'blockNumber/tag', default: 'latest' },
],
},
{
name: 'im_addSessionActivity',
params: [
{ name: 'clientId' },
],
},
];

function Request({ showModal, setShowModal }: ModalProps) {
Expand Down
45 changes: 22 additions & 23 deletions packages/passport/sdk/src/zkEvm/sessionActivity/sessionActivity.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,19 +10,19 @@ const SESSION_ACTIVITY_COUNT_KEY = 'sessionActivitySendCount';
const SESSION_ACTIVITY_DAY_KEY = 'sessionActivityDate';

// Maintain a few local counters for session activity
let checkCount = 0;
let sendCount = 0;
let currentSessionTrackCall = false;
const checkCount: { [k: string]: number } = {};
let sendCount: { [k: string]: number } = {};
const currentSessionTrackCall: { [k: string]: boolean } = {};

// Sync sendCount to localStorage
const syncSendCount = () => {
sendCount = getItem(SESSION_ACTIVITY_COUNT_KEY) || 0;
sendCount = getItem(SESSION_ACTIVITY_COUNT_KEY) || {};
const sendDay = getItem(SESSION_ACTIVITY_DAY_KEY);

// If no day, set count to zero. If not today, reset sendCount to 0
const today = new Date().toISOString().split('T')[0];
if (!sendDay || sendDay !== today) {
sendCount = 0;
sendCount = {};
}

setItem(SESSION_ACTIVITY_DAY_KEY, today);
Expand All @@ -31,12 +31,12 @@ const syncSendCount = () => {
// Run as soon as module initialised.
syncSendCount();

const incrementSendCount = () => {
const incrementSendCount = (clientId: string) => {
syncSendCount();
sendCount++;
sendCount[clientId]++;
setItem(SESSION_ACTIVITY_COUNT_KEY, sendCount);
// Reset checkCount to zero on sending
checkCount = 0;
checkCount[clientId] = 0;
};

// Fix no-promise-executor-return
Expand All @@ -47,12 +47,17 @@ const wait = async (seconds: number) => new Promise((resolve) => {
const trackSessionActivityFn = async (args: AccountsRequestedEvent) => {
// Use an existing flow if one is provided, or create a new one
const flow = args.flow || trackFlow('passport', 'sendSessionActivity');
const clientId = args.passportClient;
if (!clientId) {
flow.addEvent('No Passport Client ID');
throw new Error('No Passport Client ID provided');
}
// If there is already a tracking call in progress, do nothing
if (currentSessionTrackCall) {
if (currentSessionTrackCall[clientId]) {
flow.addEvent('Existing Delay Early Exit');
return;
}
currentSessionTrackCall = true;
currentSessionTrackCall[clientId] = true;

const { sendTransaction, environment } = args;
if (!sendTransaction) {
Expand All @@ -64,12 +69,6 @@ const trackSessionActivityFn = async (args: AccountsRequestedEvent) => {
}
setupClient(environment);

const clientId = args.passportClient;
if (!clientId) {
flow.addEvent('No Passport Client ID');
throw new Error('No Passport Client ID provided');
}

const from = args.walletAddress;
if (!from) {
flow.addEvent('No Passport Wallet Address');
Expand All @@ -84,11 +83,11 @@ const trackSessionActivityFn = async (args: AccountsRequestedEvent) => {
details = await get({
clientId,
wallet: from,
checkCount,
sendCount,
checkCount: checkCount[clientId] || 0,
sendCount: sendCount[clientId] || 0,
});
checkCount++;
flow.addEvent('Fetched details', { checkCount });
checkCount[clientId]++;
flow.addEvent('Fetched details', { checkCount: checkCount[clientId] });

if (!details) {
flow.addEvent('No details found');
Expand All @@ -108,7 +107,7 @@ const trackSessionActivityFn = async (args: AccountsRequestedEvent) => {
try {
flow.addEvent('Start Sending Transaction');
const tx = await args.sendTransaction([{ to, from, data }], flow);
incrementSendCount();
incrementSendCount(clientId);
flow.addEvent('Transaction Sent', { tx });
} catch (error) {
flow.addEvent('Failed to send Transaction');
Expand All @@ -123,7 +122,7 @@ const trackSessionActivityFn = async (args: AccountsRequestedEvent) => {
await wait(details.delay);
setTimeout(() => {
flow.addEvent('Retrying after Delay');
currentSessionTrackCall = false;
currentSessionTrackCall[clientId] = false;
// eslint-disable-next-line
trackSessionWrapper({ ...args, flow });
}, 0);
Expand All @@ -132,7 +131,7 @@ const trackSessionActivityFn = async (args: AccountsRequestedEvent) => {

// Wrapper design to ensure that after track function is called, current session Track call is false.
const trackSessionWrapper = (args: AccountsRequestedEvent) => errorBoundary(trackSessionActivityFn)(args).then(() => {
currentSessionTrackCall = false;
currentSessionTrackCall[args.passportClient] = false;
});

export const trackSessionActivity = trackSessionWrapper;
12 changes: 10 additions & 2 deletions packages/passport/sdk/src/zkEvm/zkEvmProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,7 @@ export class ZkEvmProvider implements Provider {
return ethSigner;
}

async #callSessionActivity(zkEvmAddress: string) {
async #callSessionActivity(zkEvmAddress: string, clientId?: string) {
const sendTransactionClosure = async (params: Array<any>, flow: Flow) => {
const ethSigner = await this.#getSigner();
return await sendTransaction({
Expand All @@ -209,7 +209,7 @@ export class ZkEvmProvider implements Provider {
environment: this.#config.baseConfig.environment,
sendTransaction: sendTransactionClosure,
walletAddress: zkEvmAddress,
passportClient: this.#config.oidcConfiguration.clientId,
passportClient: clientId || this.#config.oidcConfiguration.clientId,
});
}

Expand Down Expand Up @@ -498,6 +498,14 @@ export class ZkEvmProvider implements Provider {
flow.addEvent('End');
}
}
case 'im_addSessionActivity': {
const [clientId] = request.params || [];
const zkEvmAddress = await this.#getZkEvmAddress();
if (zkEvmAddress) {
this.#callSessionActivity(zkEvmAddress, clientId);
}
return null;
}
default: {
throw new JsonRpcError(
ProviderErrorCode.UNSUPPORTED_METHOD,
Expand Down

0 comments on commit 17e01d7

Please sign in to comment.