plutosdk 0.0.8-beta.1

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.
Files changed (53) hide show
  1. package/README.md +77 -0
  2. package/dist/pluto-sdk.js +2 -0
  3. package/lib/App.d.ts +24 -0
  4. package/lib/App.js +61 -0
  5. package/lib/components/BlurMask/index.d.ts +10 -0
  6. package/lib/components/BlurMask/index.js +14 -0
  7. package/lib/components/CatAni/index.d.ts +30 -0
  8. package/lib/components/CatAni/index.js +243 -0
  9. package/lib/components/GameInfo/index.d.ts +10 -0
  10. package/lib/components/GameInfo/index.js +17 -0
  11. package/lib/components/Index/index.d.ts +10 -0
  12. package/lib/components/Index/index.js +15 -0
  13. package/lib/components/Login/index.d.ts +10 -0
  14. package/lib/components/Login/index.js +28 -0
  15. package/lib/components/NetLoading/index.d.ts +7 -0
  16. package/lib/components/NetLoading/index.js +12 -0
  17. package/lib/components/PayButton/index.d.ts +16 -0
  18. package/lib/components/PayButton/index.js +18 -0
  19. package/lib/components/PlayButton/index.d.ts +29 -0
  20. package/lib/components/PlayButton/index.js +56 -0
  21. package/lib/components/Purchase/index.d.ts +61 -0
  22. package/lib/components/Purchase/index.js +183 -0
  23. package/lib/components/ShareButton/index.d.ts +14 -0
  24. package/lib/components/ShareButton/index.js +29 -0
  25. package/lib/components/StrokeText/index.d.ts +18 -0
  26. package/lib/components/StrokeText/index.js +32 -0
  27. package/lib/components/WalletInfo/index.d.ts +36 -0
  28. package/lib/components/WalletInfo/index.js +107 -0
  29. package/lib/index.d.ts +1 -0
  30. package/lib/index.js +4 -0
  31. package/lib/interface.d.ts +42 -0
  32. package/lib/interface.js +1 -0
  33. package/lib/main.d.ts +5 -0
  34. package/lib/main.js +19 -0
  35. package/lib/sdk.d.ts +46 -0
  36. package/lib/sdk.js +306 -0
  37. package/lib/utils/Api.d.ts +32 -0
  38. package/lib/utils/Api.js +122 -0
  39. package/lib/utils/Net.d.ts +33 -0
  40. package/lib/utils/Net.js +94 -0
  41. package/lib/utils/Platform.d.ts +63 -0
  42. package/lib/utils/Platform.js +142 -0
  43. package/lib/utils/User.d.ts +62 -0
  44. package/lib/utils/User.js +78 -0
  45. package/lib/utils/Utils.d.ts +10 -0
  46. package/lib/utils/Utils.js +72 -0
  47. package/lib/utils/WalletManager.d.ts +52 -0
  48. package/lib/utils/WalletManager.js +236 -0
  49. package/lib/utils/config.d.ts +25 -0
  50. package/lib/utils/config.js +72 -0
  51. package/lib/utils/constant.d.ts +38 -0
  52. package/lib/utils/constant.js +43 -0
  53. package/package.json +51 -0
@@ -0,0 +1,142 @@
1
+ import Config from "./Config";
2
+ import Api from "./Api";
3
+ import user from './User';
4
+ import net from "./Net";
5
+ /**
6
+ *
7
+ */
8
+ class Platform {
9
+ /**
10
+ *
11
+ */
12
+ init() {
13
+ Telegram.WebApp.expand();
14
+ Telegram.WebApp.ready();
15
+ Telegram.WebApp.disableVerticalSwipes();
16
+ }
17
+ /**
18
+ *
19
+ */
20
+ reqGameInfo() {
21
+ return Api.gameInfo().then(data => {
22
+ let game = {
23
+ name: data['gamename'],
24
+ describe: data['gamesummary'],
25
+ icon: data['gameicon']
26
+ };
27
+ user.gameInfo = game;
28
+ return game;
29
+ });
30
+ }
31
+ /**
32
+ *
33
+ */
34
+ reqLogin() {
35
+ return Api.login().then(data => {
36
+ let plutoId = user.plutoId = data['plutoid'];
37
+ let firstName = user.firstName = data['tgfirstname'];
38
+ let lastName = user.lastName = data['tglastname'];
39
+ let icon = user.icon = data['icon'];
40
+ let ticket = data['ticket'];
41
+ net.token = data['token'];
42
+ return {
43
+ plutoId,
44
+ firstName,
45
+ lastName,
46
+ icon,
47
+ ticket
48
+ };
49
+ });
50
+ }
51
+ /**
52
+ * @param productId
53
+ * @param name
54
+ * @param orderId
55
+ * @param price
56
+ * @param customData
57
+ */
58
+ reqPurchase(productId, name, orderId, amount, customData) {
59
+ return Api.priceExchange(amount).then(data => {
60
+ user.purchaseData = {
61
+ name: name,
62
+ productId: productId,
63
+ orderId: orderId,
64
+ usdt: parseFloat(data['usdt']),
65
+ ton: parseFloat(data['ton']),
66
+ not: parseFloat(data['not']),
67
+ star: parseInt(data['star']),
68
+ customData: customData
69
+ };
70
+ });
71
+ }
72
+ /**
73
+ *
74
+ * @param payType
75
+ * @param currency
76
+ */
77
+ reqOrder(payType, currency) {
78
+ const { productId, name, orderId, usdt, customData } = user.purchaseData;
79
+ return Api.order(productId, name, orderId, usdt, payType, currency, customData).then(data => {
80
+ return {
81
+ amount: data['amount'],
82
+ orderNo: data['orderNo'],
83
+ payload: data['payload'],
84
+ payLink: data['payLink'],
85
+ walletAddress: data['walletAddress']
86
+ };
87
+ });
88
+ }
89
+ /**
90
+ *
91
+ * @param url
92
+ */
93
+ openLink(url) {
94
+ Telegram.WebApp.openLink(url);
95
+ }
96
+ /**
97
+ *
98
+ * @param url
99
+ */
100
+ openTelegramLink(url) {
101
+ Telegram.WebApp.openTelegramLink(url);
102
+ }
103
+ /**
104
+ *
105
+ * @param url
106
+ */
107
+ openInvoice(url) {
108
+ return new Promise((resolve, reject) => {
109
+ Telegram.WebApp.openInvoice(url, (status) => {
110
+ //支付成功
111
+ if (status == 'paid') {
112
+ return resolve();
113
+ }
114
+ //
115
+ reject();
116
+ });
117
+ });
118
+ }
119
+ /**
120
+ *
121
+ * @param content
122
+ */
123
+ share(content) {
124
+ let link = `https://t.me/${Config.BOT_NAME}/games?startapp=g_${Config.GAME_ID}_${user.plutoId}`;
125
+ let text = encodeURIComponent(content);
126
+ let url = `https://t.me/share/url?url=${link}&text=${text}`;
127
+ this.openTelegramLink(url);
128
+ }
129
+ /**
130
+ *
131
+ */
132
+ enableClosingConfirmation() {
133
+ Telegram.WebApp.enableClosingConfirmation();
134
+ }
135
+ /**
136
+ *
137
+ */
138
+ disableClosingConfirmation() {
139
+ Telegram.WebApp.disableClosingConfirmation();
140
+ }
141
+ }
142
+ export default new Platform();
@@ -0,0 +1,62 @@
1
+ import { IGame, IPurchase } from "../interface";
2
+ /**
3
+ *
4
+ */
5
+ declare class User {
6
+ private _gameInfo;
7
+ private _plutoId;
8
+ private _firstName;
9
+ private _lastName;
10
+ private _icon;
11
+ private _purchaseData;
12
+ /**
13
+ *
14
+ */
15
+ get gameInfo(): IGame;
16
+ /**
17
+ *
18
+ */
19
+ set gameInfo(value: IGame);
20
+ /**
21
+ *
22
+ */
23
+ get plutoId(): string;
24
+ /**
25
+ *
26
+ */
27
+ set plutoId(value: string);
28
+ /**
29
+ *
30
+ */
31
+ get firstName(): string;
32
+ /**
33
+ *
34
+ */
35
+ set firstName(value: string);
36
+ /**
37
+ *
38
+ */
39
+ get lastName(): string;
40
+ /**
41
+ *
42
+ */
43
+ set lastName(value: string);
44
+ /**
45
+ *
46
+ */
47
+ get icon(): string;
48
+ /**
49
+ *
50
+ */
51
+ set icon(value: string);
52
+ /**
53
+ *
54
+ */
55
+ get purchaseData(): IPurchase;
56
+ /**
57
+ *
58
+ */
59
+ set purchaseData(value: IPurchase);
60
+ }
61
+ declare const _default: User;
62
+ export default _default;
@@ -0,0 +1,78 @@
1
+ /**
2
+ *
3
+ */
4
+ class User {
5
+ /**
6
+ *
7
+ */
8
+ get gameInfo() {
9
+ return this._gameInfo;
10
+ }
11
+ /**
12
+ *
13
+ */
14
+ set gameInfo(value) {
15
+ this._gameInfo = value;
16
+ }
17
+ /**
18
+ *
19
+ */
20
+ get plutoId() {
21
+ return this._plutoId;
22
+ }
23
+ /**
24
+ *
25
+ */
26
+ set plutoId(value) {
27
+ this._plutoId = value;
28
+ }
29
+ /**
30
+ *
31
+ */
32
+ get firstName() {
33
+ return this._firstName;
34
+ }
35
+ /**
36
+ *
37
+ */
38
+ set firstName(value) {
39
+ this._firstName = value;
40
+ }
41
+ /**
42
+ *
43
+ */
44
+ get lastName() {
45
+ return this._lastName;
46
+ }
47
+ /**
48
+ *
49
+ */
50
+ set lastName(value) {
51
+ this._lastName = value;
52
+ }
53
+ /**
54
+ *
55
+ */
56
+ get icon() {
57
+ return this._icon;
58
+ }
59
+ /**
60
+ *
61
+ */
62
+ set icon(value) {
63
+ this._icon = value;
64
+ }
65
+ /**
66
+ *
67
+ */
68
+ get purchaseData() {
69
+ return this._purchaseData || {};
70
+ }
71
+ /**
72
+ *
73
+ */
74
+ set purchaseData(value) {
75
+ this._purchaseData = value;
76
+ }
77
+ }
78
+ export default new User();
@@ -0,0 +1,10 @@
1
+ /**
2
+ *
3
+ * @param name
4
+ */
5
+ export declare const getRes: (name: string) => string;
6
+ /**
7
+ *
8
+ * @param text
9
+ */
10
+ export declare const clipboard: (text: string) => Promise<void>;
@@ -0,0 +1,72 @@
1
+ import Config from "./Config";
2
+ /**
3
+ *
4
+ * @param name
5
+ */
6
+ export const getRes = (name) => {
7
+ return `${Config.RES_URL}${name}`;
8
+ };
9
+ /**
10
+ *
11
+ * @param text
12
+ */
13
+ export const clipboard = (text) => {
14
+ return new Promise((resolve, reject) => {
15
+ if (!text) {
16
+ return reject('copy content is null.');
17
+ }
18
+ //
19
+ writeText(text).then(() => {
20
+ resolve();
21
+ }).catch(() => {
22
+ execCopy(text).then(() => {
23
+ resolve();
24
+ }).catch(() => {
25
+ reject('copy content faile.');
26
+ });
27
+ });
28
+ });
29
+ };
30
+ /**
31
+ *
32
+ * @param text
33
+ */
34
+ const writeText = (text) => {
35
+ return new Promise((resolve, reject) => {
36
+ if (!navigator || !navigator.clipboard) {
37
+ return reject();
38
+ }
39
+ //
40
+ navigator.clipboard.writeText(text).then(() => {
41
+ resolve();
42
+ }).catch(() => {
43
+ reject();
44
+ });
45
+ });
46
+ };
47
+ /**
48
+ *
49
+ * @param text
50
+ */
51
+ const execCopy = (text) => {
52
+ return new Promise((resolve, reject) => {
53
+ let textArea = document.createElement("textarea");
54
+ textArea.value = text;
55
+ // 使textarea不在viewport,同时设置不可见
56
+ textArea.style.position = "absolute";
57
+ textArea.style.opacity = "0";
58
+ textArea.style.left = "-999999px";
59
+ textArea.style.top = "-999999px";
60
+ document.body.appendChild(textArea);
61
+ textArea.focus();
62
+ textArea.select();
63
+ let success = document.execCommand('copy');
64
+ textArea.remove();
65
+ if (success) {
66
+ resolve();
67
+ }
68
+ else {
69
+ reject();
70
+ }
71
+ });
72
+ };
@@ -0,0 +1,52 @@
1
+ import { IJettonPayInfo, ITransaction } from '../interface';
2
+ /**
3
+ *
4
+ */
5
+ declare class WalletManager {
6
+ private _tonConnect;
7
+ private _tokenJettonWalletAddress;
8
+ /**
9
+ *
10
+ */
11
+ get connected(): boolean;
12
+ /**
13
+ *
14
+ */
15
+ get rawAddress(): string;
16
+ /**
17
+ *
18
+ */
19
+ get friendlyAddress(): string;
20
+ /**
21
+ *
22
+ */
23
+ init(): void;
24
+ /**
25
+ *
26
+ */
27
+ connect(): Promise<string>;
28
+ /**
29
+ *
30
+ */
31
+ disconnect(): Promise<void>;
32
+ /**
33
+ *
34
+ * @param transaction
35
+ */
36
+ sendTransaction(transaction: ITransaction): Promise<string>;
37
+ /**
38
+ *
39
+ * @param tokenName
40
+ * @param amount
41
+ * @param comment
42
+ * @param destinationWallet
43
+ */
44
+ getTokenJettonPayInfo(tokenName: string, amount: string, comment: string, destinationWallet: string): Promise<IJettonPayInfo>;
45
+ /**
46
+ *
47
+ * @param tokenName
48
+ */
49
+ private getTokenJettonWalletAddress;
50
+ }
51
+ declare const _default: WalletManager;
52
+ export default _default;
@@ -0,0 +1,236 @@
1
+ import { TonConnectUI, toUserFriendlyAddress } from '@tonconnect/ui';
2
+ import PubSub from 'pubsub-js';
3
+ import { CODE, EVENT_WALLET_CONNECT } from './constant';
4
+ import Config from './Config';
5
+ import net from './Net';
6
+ /**
7
+ *
8
+ */
9
+ class WalletManager {
10
+ constructor() {
11
+ //
12
+ this._tonConnect = null;
13
+ //
14
+ this._tokenJettonWalletAddress = {};
15
+ }
16
+ /**
17
+ *
18
+ */
19
+ get connected() {
20
+ return this._tonConnect.connected;
21
+ }
22
+ /**
23
+ *
24
+ */
25
+ get rawAddress() {
26
+ if (!this._tonConnect.connected) {
27
+ return null;
28
+ }
29
+ //
30
+ return this._tonConnect.wallet.account.address;
31
+ }
32
+ /**
33
+ *
34
+ */
35
+ get friendlyAddress() {
36
+ if (!this._tonConnect.connected) {
37
+ return null;
38
+ }
39
+ //
40
+ return toUserFriendlyAddress(this._tonConnect.wallet.account.address, false);
41
+ }
42
+ /**
43
+ *
44
+ */
45
+ init() {
46
+ let tonConnect = this._tonConnect = new TonConnectUI({
47
+ manifestUrl: Config.MANIFEST_URL
48
+ });
49
+ //
50
+ tonConnect.setConnectRequestParameters({
51
+ state: 'ready',
52
+ value: {
53
+ tonProof: 'success'
54
+ }
55
+ });
56
+ //
57
+ tonConnect.connectionRestored.then(restored => {
58
+ if (restored) {
59
+ PubSub.publish(EVENT_WALLET_CONNECT, true);
60
+ console.log('tonConnect restored.');
61
+ }
62
+ else {
63
+ console.log('tonConnect not restored.');
64
+ }
65
+ });
66
+ }
67
+ /**
68
+ *
69
+ */
70
+ connect() {
71
+ return new Promise((resolve, reject) => {
72
+ if (this.connected) {
73
+ return resolve(this._tonConnect.account.address);
74
+ }
75
+ //
76
+ const unsubscribeStatus = this._tonConnect.onStatusChange(wallet => {
77
+ console.log('onStatusChange==>' + JSON.stringify(wallet));
78
+ unsubscribeStatus();
79
+ if (!wallet) {
80
+ return reject({
81
+ code: CODE.WalletError,
82
+ message: 'wallet info is null'
83
+ });
84
+ }
85
+ //
86
+ let address = wallet.account.address;
87
+ PubSub.publish(EVENT_WALLET_CONNECT, true);
88
+ resolve(address);
89
+ });
90
+ //
91
+ const unsubscribeModal = this._tonConnect.onModalStateChange(state => {
92
+ console.log('onModalStateChange==>' + JSON.stringify(state));
93
+ if (state.status == 'closed') {
94
+ unsubscribeModal();
95
+ if (state.closeReason != 'wallet-selected') {
96
+ //钱包未连接移除订阅
97
+ unsubscribeStatus();
98
+ reject({
99
+ code: CODE.WalletError,
100
+ message: 'wallet connect failed'
101
+ });
102
+ }
103
+ }
104
+ });
105
+ //
106
+ this._tonConnect.openModal().then(() => {
107
+ console.log('openModal success');
108
+ }).catch(error => {
109
+ console.log('openModal error==>' + JSON.stringify(error));
110
+ unsubscribeStatus();
111
+ unsubscribeModal();
112
+ reject({
113
+ code: CODE.WalletError,
114
+ message: 'open modal error'
115
+ });
116
+ });
117
+ });
118
+ }
119
+ /**
120
+ *
121
+ */
122
+ disconnect() {
123
+ return new Promise((resolve, reject) => {
124
+ if (!this.connected) {
125
+ return reject({
126
+ code: CODE.WalletError,
127
+ message: 'wallet not connected'
128
+ });
129
+ }
130
+ //
131
+ this._tonConnect.disconnect().then(() => {
132
+ PubSub.publish(EVENT_WALLET_CONNECT, false);
133
+ resolve();
134
+ }).catch(error => {
135
+ reject({
136
+ code: CODE.WalletError,
137
+ message: 'wallet disconnect error'
138
+ });
139
+ });
140
+ });
141
+ }
142
+ /**
143
+ *
144
+ * @param transaction
145
+ */
146
+ sendTransaction(transaction) {
147
+ return new Promise((resolve, reject) => {
148
+ if (!this.connected) {
149
+ return reject({
150
+ code: CODE.WalletError,
151
+ message: 'wallet not connected'
152
+ });
153
+ }
154
+ //
155
+ const tx = {
156
+ validUntil: Math.floor(Date.now() / 1000) + 360,
157
+ messages: [Object.assign({}, transaction)]
158
+ };
159
+ this._tonConnect.sendTransaction(tx).then(response => {
160
+ console.log('transaction success');
161
+ resolve(response.boc);
162
+ }).catch(error => {
163
+ console.log('transaction error==>' + JSON.stringify(error));
164
+ reject({
165
+ code: CODE.WalletError,
166
+ message: 'wallet transaction error'
167
+ });
168
+ });
169
+ });
170
+ }
171
+ /**
172
+ *
173
+ * @param tokenName
174
+ * @param amount
175
+ * @param comment
176
+ * @param destinationWallet
177
+ */
178
+ getTokenJettonPayInfo(tokenName, amount, comment, destinationWallet) {
179
+ return new Promise((resolve, reject) => {
180
+ this.getTokenJettonWalletAddress(tokenName).then(address => {
181
+ let url = `${Config.PAYLOAD_URL}/${tokenName}/${this.rawAddress}/${destinationWallet}/${amount}/${comment}`;
182
+ net.get(url, null, false).then(resp => {
183
+ resolve({
184
+ address: address,
185
+ payload: resp.data.payload
186
+ });
187
+ }).catch(error => {
188
+ reject(error);
189
+ });
190
+ }).catch(error => {
191
+ reject(error);
192
+ });
193
+ });
194
+ }
195
+ /**
196
+ *
197
+ * @param tokenName
198
+ */
199
+ getTokenJettonWalletAddress(tokenName) {
200
+ return new Promise((resolve, reject) => {
201
+ if (this._tokenJettonWalletAddress[tokenName]) {
202
+ return resolve(this._tokenJettonWalletAddress[tokenName]);
203
+ }
204
+ //
205
+ let jettonAddress = Config.TOKENS_JETTON_ADDRESS[tokenName];
206
+ if (!jettonAddress) {
207
+ return reject(`token invalid: ${tokenName}`);
208
+ }
209
+ //
210
+ let params = {
211
+ 'owner_address': this.rawAddress,
212
+ 'jetton_address': jettonAddress,
213
+ 'limit': '1',
214
+ 'offset': '0'
215
+ };
216
+ net.get(Config.TON_CENTER_URL, params, false).then(resp => {
217
+ var _a;
218
+ let address = (_a = resp.jetton_wallets[0]) === null || _a === void 0 ? void 0 : _a.address;
219
+ if (!address) {
220
+ //没有代币钱包地址,说明没有此代币资产
221
+ return reject({
222
+ code: CODE.InsufficientFunds,
223
+ message: 'insufficient funds'
224
+ });
225
+ }
226
+ //
227
+ this._tokenJettonWalletAddress[tokenName] = address;
228
+ resolve(address);
229
+ }).catch(error => {
230
+ reject(error);
231
+ });
232
+ });
233
+ }
234
+ }
235
+ //
236
+ export default new WalletManager();
@@ -0,0 +1,25 @@
1
+ /**
2
+ *
3
+ */
4
+ export default class Config {
5
+ static APP_VERSION: string;
6
+ static RES_URL: string;
7
+ static API_URL: string;
8
+ static PAYLOAD_URL: string;
9
+ static TON_CENTER_URL: string;
10
+ static TOKENS_JETTON_ADDRESS: {
11
+ NOT: string;
12
+ CATI: string;
13
+ USDT: string;
14
+ };
15
+ static MANIFEST_URL: string;
16
+ static TEST_MODE: boolean;
17
+ static GAME_ID: string;
18
+ static BOT_NAME: string;
19
+ static INIT_DATA: string;
20
+ /**
21
+ *
22
+ * @param initConfig
23
+ */
24
+ static init(): void;
25
+ }