How to apply time delay function in an action tigger javascript to delay the tx by a random time period between 7-9 minutes

So I am following this example code in setting up an actions https://docs.openzeppelin.com/defender/v2/tutorial/actions using this template code

const { Defender } = require('@openzeppelin/defender-sdk');

exports.handler = async function(credentials) {
  const client = new Defender(credentials);

  const txRes = await client.relaySigner.sendTransaction({
    to: '0x7A99C479775C945C2Ea4CF6986425de1d816DaE4',
    speed: 'fast',
    data: '0x62029d2a',
    gasLimit: '80000',
  });

  console.log(txRes);
  return txRes.hash;
}

I am facing issue with when I use this template code as trigger to one of the monitoring event on a smart contract I want the code to execute but only after a certain time delay which needs to be randomly choosen btween 6-8 minutes. So I use a one lineer code from ai generated like this one

 setTimeout(() => console.log("Waited for a random duration"), Math.floor(Math.random() * 240000) + 360000);

but when i put this one liner code at the start of the action javascript code and run it the code executes without considering this time delay right away, can you help me how can i implement this random time delay before the sending of the transaction in action code. Is there any alternative to asyn function or another workaround so that i can have this time delay before actually sending the transaction. Thanks team

Global function setTimeout takes two input arguments:

  1. A function to be executed after the timer expires
  2. The time in milliseconds to wait before the specified function is executed

In your code, the value of the 1st input argument is:

() => console.log("Waited for a random duration")

This is a function which takes no input, prints a message, and returns no output.

The code in THIS function is THE ONLY part in your program which is subjected to the requested timeout; anything appearing after your call to the setTimeout function, will continue running IMMEDIATELY after that call, without being subjected to any timeout.

1 Like

Thanks for a quick reply but then I also tried to implement it like this but I get program error in the code saying "userHandler is not a function"

function mainFunction() {
  const waitTime = Math.floor(Math.random() * 3) + 5; // generates a random number between 5 and 7
  console.log(`Waiting ${waitTime} minutes...`);
  
  setTimeout(() => {
    const { Defender } = require('@openzeppelin/defender-sdk');
    exports.handler = async function(credentials) {
        const client = new Defender(credentials);
        const txRes = await client.relaySigner.sendTransaction({
          to: 'the contract i am monitoring',
          data: 'function method id i am calling',
          speed: 'fast',
        });
      
        console.log(txRes);
        return txRes.hash;
      }
    console.log('Execution resumed!');
  }, waitTime * 60 * 1000);
}

mainFunction();

can you please update it so that the contract is called only after the random time delay is taken into account, thanks

Above is the function (1st input argument) that you are passing to function setTimeout.

And here is what this function does:

  1. It imports Defender
  2. It sets the value of exports.handler
  3. It prints a message via console.log

The only observable action at the end of the timeout is that message being printed.

Thanks Barakman but I am bit novice to js and openzeppelin all i know is that this function is not doing what i intend it to do can you please correct it so that It does function as per my requirment or if not update it with a better code which can perform the function as per what i need, Thanks.

I cannot help you to that extent because:

  1. I don't have a subscription to OpenZeppelin's Defender (nor do I wish to pay for it)
  2. I obviously cannot execute your transaction (nor do I wish to pay for it)
  3. I have no idea what the credentials are
  4. I have no idea what the contract i am monitoring is
  5. I have no idea what the function method id i am calling is
  6. I don't see userHandler anywhere in your code, so I cannot say where that error stems from

Finally, regarding:

I think that you need to learn some basic concepts, BOTH in JavaScript AND in OpenZeppelin, before moving forward with this project (i.e., instead of just "throwing in a piece of code" and hoping that it would miraculously "do what you intend it to do" somehow).

I can help with a specific technical problem, but I'd require all the relevant technical details, and most of them are completely absent in your question.

1 Like

well then thanks for you inputs and feedback i will wait for someone from the team to reply to this @spalladino