blueprint icon indicating copy to clipboard operation
blueprint copied to clipboard

How to confirm msg received on-chain

Open sikvelsigma opened this issue 2 years ago • 7 comments

Previously, i was checking seqno value for that but I can't seem to find a way to do that in this framework

sikvelsigma avatar Mar 02 '23 20:03 sikvelsigma

solved it by getting seqno through provider.api().runMethod(address, 'seqno') and waiting until it changes, nevertheless, it would be nice to have a more straightforward approach

sikvelsigma avatar Mar 10 '23 19:03 sikvelsigma

but how can you be sure that the seqno is mapped to your txs? @sikvelsigma

tuminzee avatar Jun 25 '23 13:06 tuminzee

@tuminzee i can't, but it's enough for my purposes: to run some txs on the contracts i deployed and await their completion and since im the only one interacting with them, it is enough, tho i also await until the target contract's data changes to the expected to confirm operation

sikvelsigma avatar Jun 25 '23 13:06 sikvelsigma

Might as well post these here:

export async function getSeqNo(provider: NetworkProvider, address: Address) {
    if (await provider.isContractDeployed(address)) {
        let res = await provider.api().runMethod((await provider.api().getLastBlock()).last.seqno, address, 'seqno');
        return res.reader.readNumber();
    } else {
        return 0;
    }
}

export async function waitSeqNoChange(provider: NetworkProvider, target: Address, previousSeqno: number) {
    console.log(` - Waiting up to 45 seconds to confirm transaction`);
    let successFlag = 0;
    for (let attempt = 0; attempt < 45; attempt++) {
        await sleep(1000);
        const seqnoAfter = await getSeqNo(provider, target);
        if (seqnoAfter > previousSeqno) {
            successFlag = 1;
            break;
        };
    }
    if (successFlag) {
        console.log(` - Sent transaction done successfully`);
        return true;
    } else {
        console.log(` - Sent transaction didn't go through`);
        return false;
    }
}

export async function awaitConfirmation(fn: () => Promise<boolean>) {
    console.log(` - Waiting up to 45 seconds to confirm operation`);
    let successFlag = 0;
    for (let attempt = 0; attempt < 45; attempt++) {
        await sleep(1000);
        let res = false
        try {
            res = await fn()
        } catch {}
        
        if (res) {
            successFlag = 1
            break
        }
    }
    if (!successFlag) {
        console.log(` - Error confirming operation`);
        return false;
    }
    return true;
}

// example
const seqno = await getSeqNo(provider, provider.sender().address);
// ---------
// send tx here
// ---------
if (await waitSeqNoChange(provider, provider.sender().address, seqno)) {
    if (await awaitConfirmation(async () => {
        const data = await ctr.getData()      // target ctr data
        return data.stuff === 1                  // what we expect to see
    })) {
        console.log(` - Successfully confirmed tx`)
    } else {
        console.log(` - Error confirming tx`)
    }
}

sikvelsigma avatar Jun 25 '23 21:06 sikvelsigma

@sikvelsigma thank you for sharing the code, very helpful 👍🏼

tuminzee avatar Jun 26 '23 00:06 tuminzee

1/ these functions is under script folder?

2/ should we add this in Blueprint in dafult?

howardpen9 avatar Aug 04 '23 13:08 howardpen9

@howardpen9 having it by default would be a good option, also it makes sense to add it in the interactive menu? so user can select if they want it or not? what do you think about it?

tuminzee avatar Aug 07 '23 18:08 tuminzee