Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to confirm msg received on-chain #32

Open
sikvelsigma opened this issue Mar 2, 2023 · 7 comments
Open

How to confirm msg received on-chain #32

sikvelsigma opened this issue Mar 2, 2023 · 7 comments

Comments

@sikvelsigma
Copy link

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

@sikvelsigma
Copy link
Author

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

@tuminzee
Copy link

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

@sikvelsigma
Copy link
Author

@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
Copy link
Author

sikvelsigma commented Jun 25, 2023

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`)
    }
}

@tuminzee
Copy link

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

@howardpen9
Copy link

1/ these functions is under script folder?

2/ should we add this in Blueprint in dafult?

@tuminzee
Copy link

tuminzee commented Aug 7, 2023

@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?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants