-
Notifications
You must be signed in to change notification settings - Fork 2
/
widget.ts
149 lines (124 loc) · 4.8 KB
/
widget.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
import type { Domain } from '@buildwithsygma/sygma-sdk-core';
import { Environment } from '@buildwithsygma/sygma-sdk-core';
import type { ApiPromise } from '@polkadot/api';
import type { Signer } from '@polkadot/api/types';
import type { HTMLTemplateResult } from 'lit';
import { html } from 'lit';
import { customElement, property, state } from 'lit/decorators.js';
import { when } from 'lit/directives/when.js';
import type { WalletConnectOptions } from '@web3-onboard/walletconnect/dist/types';
import type { WalletInit, AppMetadata } from '@web3-onboard/common';
import { sygmaLogo } from './assets';
import './components';
import './components/address-input';
import './components/resource-amount-selector';
import { BaseComponent } from './components/common/base-component';
import './components/transfer/fungible/fungible-token-transfer';
import './components/network-selector';
import './context/wallet';
import type {
Eip1193Provider,
ISygmaProtocolWidget,
SdkInitializedEvent,
Theme
} from './interfaces';
import { styles } from './styles';
@customElement('sygmaprotocol-widget')
class SygmaProtocolWidget
extends BaseComponent
implements ISygmaProtocolWidget
{
static styles = styles;
@property({ type: Array }) walletModules?: WalletInit[];
@property({ type: String }) environment?: Environment;
@property({ type: Array }) whitelistedSourceNetworks?: string[];
@property({ type: Array }) whitelistedDestinationNetworks?: string[];
@property({ type: Array }) whitelistedSourceResources?: string[];
@property({ type: Object }) evmProvider?: Eip1193Provider;
@property({ type: Array }) substrateProviders?: Array<ApiPromise>;
@property({ type: Object }) substrateSigner?: Signer;
@property({ type: Boolean }) show?: boolean;
@property({ type: Boolean }) expandable?: boolean;
@property({ type: Boolean }) darkTheme?: boolean;
@property({ type: Object }) customLogo?: SVGElement;
@property({ type: Object }) theme?: Theme;
@property({ type: Object }) walletConnectOptions?: WalletConnectOptions;
@property({ type: Object }) appMetadata?: AppMetadata;
@state()
private isLoading = false;
@state()
private sdkInitialized = false;
@state()
private sourceNetwork?: Domain;
private renderConnect(): HTMLTemplateResult {
if (this.sourceNetwork) {
return html`
<sygma-connect-wallet-btn
.sourceNetwork=${this.sourceNetwork}
></sygma-connect-wallet-btn>
`;
}
return html``;
}
connectedCallback(): void {
super.connectedCallback();
const env = this.environment ?? Environment.MAINNET;
if (Object.values(Environment).includes(env as Environment)) {
this.environment = env as Environment;
} else {
throw new Error(
`Invalid environment value, please choose following: ${Object.values(Environment).join(', ')}`
);
}
}
render(): HTMLTemplateResult {
return html`
<sygma-config-context-provider
.appMetadata=${this.appMetadata}
.theme=${this.theme}
.walletConnectOptions=${this.walletConnectOptions}
>
<sygma-wallet-context-provider
.walletModules=${this.walletModules}
.substrateProviders=${this.substrateProviders}
.environment=${this.environment}
>
<section
class="widgetContainer ${this.isLoading ? 'noPointerEvents' : ''}"
>
<section class="widgetHeader">
<div class="brandLogoContainer title">[Brand] Transfer</div>
${this.renderConnect()}
</section>
<section class="widgetContent">
<sygma-fungible-transfer
@sdk-initialized=${(event: SdkInitializedEvent) =>
(this.sdkInitialized = event.detail.hasInitialized)}
.environment=${this.environment as Environment}
.onSourceNetworkSelected=${(domain: Domain) =>
(this.sourceNetwork = domain)}
environment=${this.environment ?? Environment.MAINNET}
.whitelistedSourceNetworks=${this.whitelistedSourceNetworks}
.whitelistedDestinationNetworks=${this
.whitelistedDestinationNetworks}
.whitelistedSourceResources=${this.whitelistedSourceResources}
>
</sygma-fungible-transfer>
</section>
<section class="poweredBy">${sygmaLogo} Powered by Sygma</section>
${when(
this.isLoading || !this.sdkInitialized,
() => html`<sygma-overlay-component></sygma-overlay-component>`
)}
</section>
</sygma-wallet-context-provider>
</sygma-config-context-provider>
`;
}
}
export { SygmaProtocolWidget };
declare global {
interface HTMLElementTagNameMap {
'sygmaprotocol-widget': ISygmaProtocolWidget;
}
}