Skip to content

Commit

Permalink
feat: Retry loading bridge routes (#136)
Browse files Browse the repository at this point in the history
## Description

Added retry sdk initialization

## Related Issue Or Context

#96

Closes: #96 

## How Has This Been Tested? Testing details.

- Tested locally

## Types of changes

Changes:
    

- [x] Added state in widget component to keep track of sdk
initialization
- [x] Added event to be fired when sdk is initialized
- [x] Retry sdk init method
  • Loading branch information
saadahmsiddiqui authored Mar 8, 2024
1 parent c0fe09d commit f3244c4
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import type { ReactiveController, ReactiveElement } from 'lit';
import { MAINNET_EXPLORER_URL, TESTNET_EXPLORER_URL } from '../../constants';
import { walletContext } from '../../context';

import { SdkInitializedEvent } from '../../interfaces';
import { buildEvmFungibleTransactions, executeNextEvmTransaction } from './evm';

export enum FungibleTransferState {
Expand Down Expand Up @@ -82,10 +83,30 @@ export class FungibleTokenTransferController implements ReactiveController {
this.reset();
}

/**
* Infinite Try/catch wrapper around
* {@link Config} from `@buildwithsygma/sygma-sdk-core`
* and emits a {@link SdkInitializedEvent}
* @param {number} time to wait before retrying request in ms
* @returns {void}
*/
async retryInitSdk(retryMs = 100): Promise<void> {
try {
await this.config.init(1, this.env);
this.host.dispatchEvent(
new SdkInitializedEvent({ hasInitialized: true })
);
} catch (error) {
setTimeout(() => {
this.retryInitSdk(retryMs * 2).catch(console.error);
}, retryMs);
}
}

async init(env: Environment): Promise<void> {
this.host.requestUpdate();
this.env = env;
await this.config.init(1, env);
await this.retryInitSdk();
this.supportedSourceNetworks = this.config.getDomains();
//remove once we have proper substrate transfer support
// .filter((n) => n.type === Network.EVM);
Expand Down
8 changes: 8 additions & 0 deletions packages/widget/src/interfaces/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,3 +34,11 @@ export interface ISygmaProtocolWidget {
customLogo?: SVGElement;
theme?: Theme;
}

export class SdkInitializedEvent extends CustomEvent<{
hasInitialized: boolean;
}> {
constructor(update: { hasInitialized: boolean }) {
super('sdk-initialized', { detail: update, composed: true, bubbles: true });
}
}
8 changes: 7 additions & 1 deletion packages/widget/src/widget.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import './context/wallet';
import type {
Eip1193Provider,
ISygmaProtocolWidget,
SdkInitializedEvent,
Theme
} from './interfaces';
import { styles } from './styles';
Expand Down Expand Up @@ -59,6 +60,9 @@ class SygmaProtocolWidget
@state()
private isLoading = false;

@state()
private sdkInitialized = false;

@state()
private sourceNetwork?: Domain;

Expand All @@ -85,6 +89,8 @@ class SygmaProtocolWidget
</section>
<section class="widgetContent">
<sygma-fungible-transfer
@sdk-initialized=${(event: SdkInitializedEvent) =>
(this.sdkInitialized = event.detail.hasInitialized)}
.onSourceNetworkSelected=${(domain: Domain) =>
(this.sourceNetwork = domain)}
.whitelistedSourceResources=${this.whitelistedSourceNetworks}
Expand All @@ -94,7 +100,7 @@ class SygmaProtocolWidget
</section>
<section class="poweredBy">${sygmaLogo} Powered by Sygma</section>
${when(
this.isLoading,
this.isLoading || !this.sdkInitialized,
() => html`<sygma-overlay-component></sygma-overlay-component>`
)}
</section>
Expand Down

0 comments on commit f3244c4

Please sign in to comment.