1

I'm running substrate-contracts-node locally with --dev (instant seals). I created a custom react hook to wrap events, but it never gets finalized. Am I doing something wrong, is there a caveat in using instant seals?

type Status = 'pending' | 'finalized' | 'none';

type Response = {
  send: () => void;
  events: EventRecord[];
  status: Status;
};

export const useExecuteFunc = (): Response => {
  const game = useGameContract();
  const { activeAccount, activeSigner } = useInk();
  const [pixels, setPixels] = useState<EventRecord[]>([]);
  const [status, setStatus] = useState<Status>('none');

  const send = useMemo(() => {
    return async () => {
      if (!activeAccount || !game || !activeSigner) return () => null;

      const unsub = await game.tx
        .executeTurns({
          gasLimit: -1,
        })
        .signAndSend(activeAccount?.address, { signer: activeSigner.signer }, async (result) => {
          console.log('result', result);

          if (result.status.isInBlock) {
            console.log('status is pending');
            setStatus('pending');
          } else if (result.status.isFinalized) {
            console.log('status is finalized');
            setStatus('finalized');
            unsub();
          }
        });
    };
  }, [activeAccount, activeSigner, game]);

  return {
    send,
    events: pixels,
    status,
  };
};

I then use this like this:

const executeGameFunc = useExecuteGameFunc()

// ...

<button onClick={() => executeGameFunc.send()}>Play Game</button>

polkadotjs explorer shows that the transaction was successful.

1 Answer 1

5

You are doing nothing wrong. Instant seal does not finalize the blocks for some reason. However, a change was recently merged to allow just that. We are yet to adapt it in substrate-contracts-node.

That said, you should really reconsider if you need to wait for finalization anyways. Only if a later reversion of the state would be really bad you should wait for it. Like acting on some payment you received. Displaying the current state of a game should not require finalization.

1
  • Finalization is very important for displaying state in the UI. e.g. If I'm showing a spinner after a button was clicked (and transaction sent) how do I know when to stop showing it? Things like "is in block", "pending signature", "is finalized" are very useful states to know about for creating robust UIs. This is out of scope for this thread, so I'll share my ideas another time elsewhere to get your take. Thanks for the help Oct 6, 2022 at 20:57

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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