sygmaprotocol / sygma-sdk

A Typescript SDK to add cross-chain capabilities such as bridging tokens and sending messages across dApp projects.
https://docs.buildwithsygma.com/
GNU Lesser General Public License v3.0
31 stars 19 forks source link

SDK examples improvements for toNumber and FailToBridge status updated from Indexer #381

Open LyonSsS opened 3 months ago

LyonSsS commented 3 months ago

Context of toNumber: We are currently using this logic

if (!contractValueAfter.eq(valueBefore)) { console.log("Transaction successfully bridged."); console.log( Value after update: ${new Date( contractValueAfter.toNumber() ).toString()} ); break;

But the Sotrage Contract can generate a value like "161145517021206282627239951666939041182785141645" which may generate the following error when converted toNumber:

_reason: 'overflow', code: 'NUMERICFAULT', fault: 'overflow', operation: 'toNumber', value: '161145517021206282627239951666939041182785141645'

Context of FailToBridge: We are currently using this logic to display status of a transaction

const id = setInterval(() => { getStatus(response.hash) .then((data) => { if (data[0]) { console.log("Status of the transfer", data[0].status); if(data[0].status == "executed") { clearInterval(id); process.exit(0); } } else { console.log("Waiting for the TX to be indexed"); } }) .catch((e) => { console.log("error:", e); }); }, 5000); }`

The problem of this logic is that if the Indexer does not report a problem ( aka there is an issue on relayer side and we do not execute), the example logs are going to execute continuously with "Waiting for the TX to be indexed" .

Also, this logic is not linked with the following method:

const waitUntilBridged = async ( valueBefore: BigNumber, intervalDuration: number = 15000, attempts: number = 8 ): Promise => { let i = 0; let contractValueAfter: BigNumber; for (;;) { await sleep(intervalDuration); contractValueAfter = await fetchAfterValue(); if (!contractValueAfter.eq(valueBefore)) { console.log("Transaction successfully bridged."); console.log( Value after update: ${new Date( contractValueAfter.toNumber() ).toString()} ); break; } i++; if (i > attempts) { // transaction should have been bridged already console.log("transaction is taking too much time to bridge!"); break; } } };

We should stop providing status ( probably with a bool flag) if this method goes on the "transaction is taking too much time to bridge!" logic ( so after attempts x duration)