@tonconnect/ui 0.0.3 → 0.0.6
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +167 -2
- package/lib/index.d.ts +58 -17
- package/lib/index.js +7917 -4078
- package/lib/index.js.map +1 -0
- package/lib/index.umd.js +10361 -95
- package/lib/index.umd.js.map +1 -0
- package/package.json +7 -7
package/README.md
CHANGED
|
@@ -1,3 +1,168 @@
|
|
|
1
|
-
# TON Connect
|
|
1
|
+
# TON Connect UI
|
|
2
2
|
|
|
3
|
-
⚠️ API is work in progress right now.
|
|
3
|
+
⚠️ **API is work in progress right now. Don't use this package in production.**
|
|
4
|
+
|
|
5
|
+
TonConnect UI is a UI kit for TonConnect SDK. Use it to connect your app to TON wallets via TonConnect protocol.
|
|
6
|
+
|
|
7
|
+
If you use React for your dapp, take a look at [TonConnect UI React kit](https://github.com/ton-connect/sdk/tree/main/packages/ui-react).
|
|
8
|
+
|
|
9
|
+
If you want to use TonConnect on the server side, you should use the [TonConnect SDK](https://github.com/ton-connect/sdk/tree/main/packages/sdk).
|
|
10
|
+
|
|
11
|
+
You can find more details and the protocol specification in the [docs](https://github.com/ton-connect/docs).
|
|
12
|
+
|
|
13
|
+
---
|
|
14
|
+
|
|
15
|
+
[Latest API documentation](https://ton-connect.github.io/sdk/modules/_tonconnect_ui.html)
|
|
16
|
+
|
|
17
|
+
# Getting started
|
|
18
|
+
|
|
19
|
+
## Installation with npm
|
|
20
|
+
`npm i @tonconnect/ui`
|
|
21
|
+
|
|
22
|
+
# Usage
|
|
23
|
+
|
|
24
|
+
## Create TonConnectUI instance
|
|
25
|
+
```ts
|
|
26
|
+
import TonConnectUI from '@tonconnect/ui'
|
|
27
|
+
|
|
28
|
+
const tonConnectUI = new TonConnectUI({
|
|
29
|
+
manifestUrl: 'https://<YOUR_APP_URL>/tonconnect-manifest.json',
|
|
30
|
+
buttonRootId: '<YOUR_CONNECT_BUTTON_ANCHOR_ID>'
|
|
31
|
+
});
|
|
32
|
+
```
|
|
33
|
+
|
|
34
|
+
See all available options:
|
|
35
|
+
[TonConnectUiOptionsWithManifest](https://ton-connect.github.io/sdk/docs/interfaces/_tonconnect_ui.TonConnectUiOptionsWithManifest.html)
|
|
36
|
+
[TonConnectUiOptionsWithConnector](https://ton-connect.github.io/sdk/docs/interfaces/_tonconnect_ui.TonConnectUiOptionsWithConnector.html)
|
|
37
|
+
|
|
38
|
+
## Change options if needed
|
|
39
|
+
```ts
|
|
40
|
+
tonConnectUI.uiOptions = {
|
|
41
|
+
language: 'ru',
|
|
42
|
+
theme: THEME.DARK
|
|
43
|
+
};
|
|
44
|
+
```
|
|
45
|
+
|
|
46
|
+
UI element will be rerendered after assignation. You should pass only options that you want to change.
|
|
47
|
+
Passed options will be merged with current UI options. Note, that you have to pass object to `tonConnectUI.uiOptions` to keep reactivity.
|
|
48
|
+
|
|
49
|
+
DON'T do this:
|
|
50
|
+
```ts
|
|
51
|
+
tonConnectUI.uiOptions.language = 'ru'; // WRONG, WILL NOT WORK
|
|
52
|
+
```
|
|
53
|
+
|
|
54
|
+
[See all available options](https://ton-connect.github.io/sdk/docs/interfaces/_tonconnect_ui.TonConnectUiOptions.html)
|
|
55
|
+
|
|
56
|
+
## Fetch wallets list
|
|
57
|
+
```ts
|
|
58
|
+
const walletsList = await tonConnectUI.getWallets();
|
|
59
|
+
|
|
60
|
+
/* walletsList is
|
|
61
|
+
{
|
|
62
|
+
name: string;
|
|
63
|
+
imageUrl: string;
|
|
64
|
+
tondns?: string;
|
|
65
|
+
aboutUrl: string;
|
|
66
|
+
universalLink?: string;
|
|
67
|
+
deepLink?: string;
|
|
68
|
+
bridgeUrl?: string;
|
|
69
|
+
jsBridgeKey?: string;
|
|
70
|
+
injected?: boolean; // true if this wallet is injected to the webpage
|
|
71
|
+
embedded?: boolean; // true if the dapp is opened inside this wallet's browser
|
|
72
|
+
}[]
|
|
73
|
+
*/
|
|
74
|
+
```
|
|
75
|
+
|
|
76
|
+
or
|
|
77
|
+
|
|
78
|
+
```ts
|
|
79
|
+
const walletsList = await TonConnectUI.getWallets();
|
|
80
|
+
```
|
|
81
|
+
|
|
82
|
+
## Call connect
|
|
83
|
+
"TonConnect UI connect button" (which is added at `buttonRootId`) automatically handles clicks and calls connect.
|
|
84
|
+
But you are still able to open "connect modal" programmatically, e.g. after click on your custom connect button.
|
|
85
|
+
|
|
86
|
+
```ts
|
|
87
|
+
const connectedWallet = await tonConnectUI.connectWallet();
|
|
88
|
+
```
|
|
89
|
+
|
|
90
|
+
If there is an error while wallet connecting, `TonConnectUIError` or `TonConnectError` will be thrown depends on situation.
|
|
91
|
+
|
|
92
|
+
## Get current connected Wallet and WalletInfo
|
|
93
|
+
You can use special getters to read current connection state. Note that this getter only represents current value, so they are not reactive.
|
|
94
|
+
To react and handle wallet changes use `onStatusChange` mathod.
|
|
95
|
+
|
|
96
|
+
```ts
|
|
97
|
+
const currentWallet = tonConnectUI.wallet;
|
|
98
|
+
const currentWalletInfo = tonConnectUI.walletInfo;
|
|
99
|
+
const currentAccount = tonConnectUI.account;
|
|
100
|
+
const currentIsConnectedStatus = tonConnectUI.connected;
|
|
101
|
+
```
|
|
102
|
+
|
|
103
|
+
## Subscribe to the connection status changes
|
|
104
|
+
```js
|
|
105
|
+
const unsubscribe = tonConnectUI.onStatusChange(
|
|
106
|
+
walletAndwalletInfo => {
|
|
107
|
+
// update state/reactive variables to show updates in the ui
|
|
108
|
+
}
|
|
109
|
+
);
|
|
110
|
+
|
|
111
|
+
// call `unsubscribe()` later to save resources when you don't need to listen for updates anymore.
|
|
112
|
+
```
|
|
113
|
+
|
|
114
|
+
## Disconnect wallet
|
|
115
|
+
Call to disconnect the wallet.
|
|
116
|
+
|
|
117
|
+
```ts
|
|
118
|
+
await tonConnectUI.disconnect();
|
|
119
|
+
```
|
|
120
|
+
|
|
121
|
+
## Send transaction
|
|
122
|
+
Wallet must be connected when you call `sendTransaction`. Otherwise, an error will be thrown.
|
|
123
|
+
|
|
124
|
+
|
|
125
|
+
```ts
|
|
126
|
+
const transaction = {
|
|
127
|
+
validUntil: Date.now() + 1000000,
|
|
128
|
+
messages: [
|
|
129
|
+
{
|
|
130
|
+
address: "0:412410771DA82CBA306A55FA9E0D43C9D245E38133CB58F1457DFB8D5CD8892F",
|
|
131
|
+
amount: "20000000",
|
|
132
|
+
stateInit: "base64bocblahblahblah==" // just for instance. Replace with your transaction initState or remove
|
|
133
|
+
},
|
|
134
|
+
{
|
|
135
|
+
address: "0:E69F10CC84877ABF539F83F879291E5CA169451BA7BCE91A37A5CED3AB8080D3",
|
|
136
|
+
amount: "60000000",
|
|
137
|
+
payload: "base64bocblahblahblah==" // just for instance. Replace with your transaction payload or remove
|
|
138
|
+
}
|
|
139
|
+
]
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
try {
|
|
143
|
+
const result = await tonConnectUI.sendTransaction(transaction);
|
|
144
|
+
|
|
145
|
+
// you can use signed boc to find the transaction
|
|
146
|
+
const someTxData = await myAppExplorerService.getTransaction(result.boc);
|
|
147
|
+
alert('Transaction was sent successfully', someTxData);
|
|
148
|
+
} catch (e) {
|
|
149
|
+
console.error(e);
|
|
150
|
+
}
|
|
151
|
+
```
|
|
152
|
+
|
|
153
|
+
`sendTransaction` will automatically render informational modals and notifications. You can change its behaviour:
|
|
154
|
+
|
|
155
|
+
```ts
|
|
156
|
+
const result = await tonConnectUI.sendTransaction(defaultTx, {
|
|
157
|
+
modals: ['before', 'success', 'error'],
|
|
158
|
+
notifications: ['before', 'success', 'error']
|
|
159
|
+
});
|
|
160
|
+
```
|
|
161
|
+
|
|
162
|
+
Default configuration is:
|
|
163
|
+
```ts
|
|
164
|
+
const defaultBehaviour = {
|
|
165
|
+
modals: ['before'],
|
|
166
|
+
notifications: ['before', 'success', 'error']
|
|
167
|
+
}
|
|
168
|
+
```
|
package/lib/index.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { WalletConnectionSource, ITonConnect, Account, Wallet,
|
|
1
|
+
import { WalletConnectionSource, ITonConnect, WalletInfo, Account, Wallet, TonConnectError, SendTransactionRequest, SendTransactionResponse } from '@tonconnect/sdk';
|
|
2
2
|
import { Property } from 'csstype';
|
|
3
3
|
|
|
4
4
|
declare enum THEME {
|
|
@@ -47,47 +47,89 @@ declare type Locales = 'en' | 'ru';
|
|
|
47
47
|
declare type Color = Property.Color;
|
|
48
48
|
|
|
49
49
|
interface TonConnectUiOptions {
|
|
50
|
+
/**
|
|
51
|
+
* Color theme for the UI elements.
|
|
52
|
+
* @default SYSTEM theme.
|
|
53
|
+
*/
|
|
50
54
|
theme?: Theme;
|
|
55
|
+
/**
|
|
56
|
+
* Accent color for the UI elements.
|
|
57
|
+
* @default #31A6F5 (blue).
|
|
58
|
+
*/
|
|
51
59
|
accentColor?: Color;
|
|
60
|
+
/**
|
|
61
|
+
* HTML element id to attach the wallet connect button. If not passed button won't appear.
|
|
62
|
+
* @default null.
|
|
63
|
+
*/
|
|
52
64
|
buttonRootId?: string | null;
|
|
65
|
+
/**
|
|
66
|
+
* Language for the phrases it the UI elements.
|
|
67
|
+
* @default system
|
|
68
|
+
*/
|
|
53
69
|
language?: Locales;
|
|
70
|
+
/**
|
|
71
|
+
* Configuration for the wallet connect button.
|
|
72
|
+
*/
|
|
54
73
|
buttonConfiguration?: Partial<ButtonConfiguration>;
|
|
74
|
+
/**
|
|
75
|
+
* Configuration for the wallet connect modal and action modals.
|
|
76
|
+
*/
|
|
55
77
|
widgetConfiguration?: Partial<WidgetConfiguration>;
|
|
56
78
|
}
|
|
57
79
|
|
|
58
80
|
declare type TonConnectUiCreateOptions = TonConnectUiOptionsWithConnector | TonConnectUiOptionsWithManifest;
|
|
59
81
|
interface TonConnectUiOptionsWithManifest extends TonConnectUiCreateOptionsBase {
|
|
82
|
+
/**
|
|
83
|
+
* Url to the [manifest]{@link https://github.com/ton-connect/docs/blob/main/requests-responses.md#app-manifest} with the Dapp metadata that will be displayed in the user's wallet.
|
|
84
|
+
* If not passed, manifest from `${window.location.origin}/tonconnect-manifest.json` will be taken.
|
|
85
|
+
*/
|
|
60
86
|
manifestUrl?: string;
|
|
61
87
|
}
|
|
62
88
|
interface TonConnectUiOptionsWithConnector extends TonConnectUiCreateOptionsBase {
|
|
89
|
+
/**
|
|
90
|
+
* TonConnect instance. Can be helpful if you use custom ITonConnect implementation, or use both of @tonconnect/sdk and @tonconnect/ui in your app.
|
|
91
|
+
*/
|
|
63
92
|
connector?: ITonConnect;
|
|
64
93
|
}
|
|
65
94
|
interface TonConnectUiCreateOptionsBase extends TonConnectUiOptions {
|
|
95
|
+
/**
|
|
96
|
+
* Try to restore existing session and reconnect to the corresponding wallet.
|
|
97
|
+
* @default true.
|
|
98
|
+
*/
|
|
66
99
|
restoreConnection?: boolean;
|
|
100
|
+
/**
|
|
101
|
+
* HTML element id to attach the modal window element. If not passed, `div#tc-widget-root` in the end of the <body> will be added and used.
|
|
102
|
+
* @default `div#tc-widget-root`.
|
|
103
|
+
*/
|
|
67
104
|
widgetRootId?: string;
|
|
68
105
|
}
|
|
69
106
|
|
|
70
|
-
declare class
|
|
107
|
+
declare class TonConnectUI {
|
|
108
|
+
static getWallets(): Promise<WalletInfo[]>;
|
|
71
109
|
private readonly walletInfoStorage;
|
|
72
110
|
private readonly connector;
|
|
73
111
|
private _walletInfo;
|
|
74
112
|
private systemThemeChangeUnsubscribe;
|
|
75
113
|
/**
|
|
76
|
-
* Current connection status
|
|
114
|
+
* Current connection status.
|
|
77
115
|
*/
|
|
78
116
|
get connected(): boolean;
|
|
79
117
|
/**
|
|
80
|
-
* Current connected account or null
|
|
118
|
+
* Current connected account or null.
|
|
81
119
|
*/
|
|
82
120
|
get account(): Account | null;
|
|
83
121
|
/**
|
|
84
|
-
* Curren connected wallet app or null
|
|
122
|
+
* Curren connected wallet app or null.
|
|
85
123
|
*/
|
|
86
124
|
get wallet(): Wallet | null;
|
|
87
125
|
/**
|
|
88
|
-
* Curren connected wallet's info or null
|
|
126
|
+
* Curren connected wallet's info or null.
|
|
89
127
|
*/
|
|
90
128
|
get walletInfo(): WalletInfo | null;
|
|
129
|
+
/**
|
|
130
|
+
* Set and apply new UI options. Object with partial options should be passed. Passed options will be merged with current options.
|
|
131
|
+
* @param options
|
|
132
|
+
*/
|
|
91
133
|
set uiOptions(options: TonConnectUiOptions);
|
|
92
134
|
constructor(options?: TonConnectUiCreateOptions);
|
|
93
135
|
/**
|
|
@@ -95,27 +137,26 @@ declare class TonConnectUi {
|
|
|
95
137
|
*/
|
|
96
138
|
getWallets(): Promise<WalletInfo[]>;
|
|
97
139
|
/**
|
|
98
|
-
* Subscribe to connection status change
|
|
99
|
-
* @return function which has to be called to unsubscribe
|
|
140
|
+
* Subscribe to connection status change.
|
|
141
|
+
* @return function which has to be called to unsubscribe.
|
|
100
142
|
*/
|
|
101
|
-
onStatusChange(
|
|
143
|
+
onStatusChange(callback: (wallet: (Wallet & WalletInfo) | null) => void, errorsHandler?: (err: TonConnectError) => void): ReturnType<ITonConnect['onStatusChange']>;
|
|
102
144
|
/**
|
|
103
145
|
* Opens the modal window and handles a wallet connection.
|
|
104
146
|
*/
|
|
105
|
-
connectWallet(): Promise<Wallet>;
|
|
147
|
+
connectWallet(): Promise<Wallet & WalletInfo>;
|
|
106
148
|
/**
|
|
107
149
|
* Disconnect wallet and clean localstorage.
|
|
108
150
|
*/
|
|
109
151
|
disconnect(): Promise<void>;
|
|
110
152
|
/**
|
|
111
|
-
* Opens the modal window and handles the
|
|
112
|
-
* @param tx
|
|
113
|
-
* @param options
|
|
153
|
+
* Opens the modal window and handles the transaction sending.
|
|
154
|
+
* @param tx transaction to send.
|
|
155
|
+
* @param options modal and notifications behaviour settings. Default is show only 'before' modal and all notifications.
|
|
114
156
|
*/
|
|
115
157
|
sendTransaction(tx: SendTransactionRequest, options?: {
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
showErrorModalAfter: boolean;
|
|
158
|
+
modals?: ('before' | 'success' | 'error')[];
|
|
159
|
+
notifications?: ('before' | 'success' | 'error')[];
|
|
119
160
|
}): Promise<SendTransactionResponse>;
|
|
120
161
|
private subscribeToWalletChange;
|
|
121
162
|
private updateWalletInfo;
|
|
@@ -127,4 +168,4 @@ declare class TonConnectUIError extends TonConnectError {
|
|
|
127
168
|
constructor(...args: ConstructorParameters<typeof Error>);
|
|
128
169
|
}
|
|
129
170
|
|
|
130
|
-
export { ButtonAppearance, ButtonConfiguration, ButtonSize, Locales, THEME, Theme, Themed,
|
|
171
|
+
export { ButtonAppearance, ButtonConfiguration, ButtonSize, Locales, THEME, Theme, Themed, TonConnectUI, TonConnectUIError, TonConnectUiCreateOptions, TonConnectUiCreateOptionsBase, TonConnectUiOptions, TonConnectUiOptionsWithConnector, TonConnectUiOptionsWithManifest, WalletsListConfiguration, WalletsListConfigurationExplicit, WalletsListConfigurationImplicit, WidgetConfiguration, TonConnectUI as default };
|