-
Notifications
You must be signed in to change notification settings - Fork 94
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
Comments
solved it by getting |
but how can you be sure that the seqno is mapped to your txs? |
@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 |
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 thank you for sharing the code, very helpful 👍🏼 |
1/ these functions is under 2/ should we add this in Blueprint in dafult? |
@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? |
Previously, i was checking
seqno
value for that but I can't seem to find a way to do that in this frameworkThe text was updated successfully, but these errors were encountered: