Giter Club home page Giter Club logo

Comments (7)

sikvelsigma avatar sikvelsigma commented on July 30, 2024 2

@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

from blueprint.

sikvelsigma avatar sikvelsigma commented on July 30, 2024 2

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

from blueprint.

sikvelsigma avatar sikvelsigma commented on July 30, 2024 1

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

from blueprint.

howardpen9 avatar howardpen9 commented on July 30, 2024 1

1/ these functions is under script folder?

2/ should we add this in Blueprint in dafult?

from blueprint.

tuminzee avatar tuminzee commented on July 30, 2024

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

from blueprint.

tuminzee avatar tuminzee commented on July 30, 2024

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

from blueprint.

tuminzee avatar tuminzee commented on July 30, 2024

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

from blueprint.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.