Skip to content

Instantly share code, notes, and snippets.

@molekilla
Created March 24, 2020 19:21
Show Gist options
  • Save molekilla/ef47e221c075200277a3b903c6cb398d to your computer and use it in GitHub Desktop.
Save molekilla/ef47e221c075200277a3b903c6cb398d to your computer and use it in GitHub Desktop.
Wait for WalletConnect Transaction by looking up tx
import { from } from 'rxjs';
import { mergeMap, filter } from 'rxjs/operators';
export class WalletConnectDappUtils {
public static async waitFor(methodResponse: any) {
let result: any = {};
// CANNOT BE executed with the plugin due to next tick issues with Javascript
let exit = 5;
let n = methodResponse.beforeBlock - 2;
// eslint-disable-next-line no-constant-condition
while (true) {
n += 1;
exit -= 1;
// eslint-disable-next-line no-await-in-loop
const block = await connex.thor.block(n).get();
console.log(block);
if (block) {
// eslint-disable-next-line no-await-in-loop
const txs = await from(block.transactions)
.pipe(
// eslint-disable-next-line no-loop-func
mergeMap(i => connex.thor.transaction(i).get()),
// is origin and clauses[0].data
filter((i: Connex.Thor.Transaction) => i.clauses.length > 0
&& i.clauses[0].data === methodResponse.data)
)
.toPromise();
console.log(txs);
if (txs) {
result = {
id: txs.id,
txid: txs.id,
};
break;
}
if (exit === 0) break;
}
}
if (result.id) {
const receipt = await connex.thor.transaction(result.id).getReceipt();
if (receipt.reverted) {
throw new Error('Reverted');
}
}
return result;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment