How to confirm msg received on-chain
Previously, i was checking seqno value for that but I can't seem to find a way to do that in this framework
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
but how can you be sure that the seqno is mapped to your txs? @sikvelsigma
@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 script folder?
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? what do you think about it?