0

I've seen similar questions here with explanations, but can't quite make this work.

Using ethers.js, I have a function that makes a transaction on the smart contract. I am listening to an event below to extract a parameter, but then outside of the Promise it is undefined:

let orderHash;
myContract.on("MakeOrder", (order, hash) => {
  orderHash = hash;
});

console.log(orderHash) // logs undefined

How to properly extract the value?

0

Ended up with this working code:

const makeOrderEvent = new Promise((resolve, reject) => {
  myContract.on("MakeOrder", (order, hash, event) => {
    event.removeListener();

    resolve({
      order: order,
      hash: hash,
    });
  });

  setTimeout(() => {
    reject(new Error("timeout"));
  }, 60000);
});

const event = await makeOrderEvent;
console.log(event);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.