@ton-wallet/create 14.0.2 → 14.0.3

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 (123) hide show
  1. package/dist/client/TonClient.d.ts +228 -0
  2. package/dist/client/TonClient.js +441 -0
  3. package/dist/client/TonClient.spec.d.ts +1 -0
  4. package/dist/client/TonClient.spec.js +45 -0
  5. package/dist/client/TonClient4.d.ts +1130 -0
  6. package/dist/client/TonClient4.js +687 -0
  7. package/dist/client/TonClient4.spec.d.ts +1 -0
  8. package/dist/client/TonClient4.spec.js +42 -0
  9. package/dist/client/api/HttpApi.d.ts +764 -0
  10. package/dist/client/api/HttpApi.js +306 -0
  11. package/dist/client/api/TonCache.d.ts +16 -0
  12. package/dist/client/api/TonCache.js +33 -0
  13. package/dist/config/ConfigParser.d.ts +622 -0
  14. package/dist/config/ConfigParser.js +711 -0
  15. package/dist/config/ConfigParser.spec.d.ts +8 -0
  16. package/dist/config/ConfigParser.spec.js +97 -0
  17. package/dist/elector/ElectorContract.d.ts +51 -0
  18. package/dist/elector/ElectorContract.js +192 -0
  19. package/dist/elector/ElectorContract.spec.d.ts +8 -0
  20. package/dist/elector/ElectorContract.spec.js +104 -0
  21. package/dist/index.d.ts +33 -0
  22. package/dist/index.js +129 -0
  23. package/dist/jetton/JettonMaster.d.ts +21 -0
  24. package/dist/jetton/JettonMaster.js +39 -0
  25. package/dist/jetton/JettonMaster.spec.d.ts +8 -0
  26. package/dist/jetton/JettonMaster.spec.js +27 -0
  27. package/dist/jetton/JettonWallet.d.ts +14 -0
  28. package/dist/jetton/JettonWallet.js +27 -0
  29. package/dist/multisig/MultisigOrder.d.ts +17 -0
  30. package/dist/multisig/MultisigOrder.js +73 -0
  31. package/dist/multisig/MultisigOrder.spec.d.ts +1 -0
  32. package/dist/multisig/MultisigOrder.spec.js +139 -0
  33. package/dist/multisig/MultisigOrderBuilder.d.ts +13 -0
  34. package/dist/multisig/MultisigOrderBuilder.js +37 -0
  35. package/dist/multisig/MultisigWallet.d.ts +27 -0
  36. package/dist/multisig/MultisigWallet.js +134 -0
  37. package/dist/multisig/MultisigWallet.spec.d.ts +1 -0
  38. package/dist/multisig/MultisigWallet.spec.js +248 -0
  39. package/dist/utils/createTestClient.d.ts +9 -0
  40. package/dist/utils/createTestClient.js +18 -0
  41. package/dist/utils/createTestClient4.d.ts +9 -0
  42. package/dist/utils/createTestClient4.js +15 -0
  43. package/dist/utils/fees.d.ts +25 -0
  44. package/dist/utils/fees.js +105 -0
  45. package/dist/utils/fees.spec.d.ts +1 -0
  46. package/dist/utils/fees.spec.js +83 -0
  47. package/dist/utils/maybe.d.ts +8 -0
  48. package/dist/utils/maybe.js +9 -0
  49. package/dist/utils/randomTestKey.d.ts +8 -0
  50. package/dist/utils/randomTestKey.js +24 -0
  51. package/dist/utils/time.d.ts +15 -0
  52. package/dist/utils/time.js +63 -0
  53. package/dist/utils/toUrlSafe.d.ts +8 -0
  54. package/dist/utils/toUrlSafe.js +23 -0
  55. package/dist/wallets/WalletContractV1R1.d.ts +58 -0
  56. package/dist/wallets/WalletContractV1R1.js +100 -0
  57. package/dist/wallets/WalletContractV1R1.spec.d.ts +8 -0
  58. package/dist/wallets/WalletContractV1R1.spec.js +44 -0
  59. package/dist/wallets/WalletContractV1R2.d.ts +58 -0
  60. package/dist/wallets/WalletContractV1R2.js +101 -0
  61. package/dist/wallets/WalletContractV1R2.spec.d.ts +8 -0
  62. package/dist/wallets/WalletContractV1R2.spec.js +44 -0
  63. package/dist/wallets/WalletContractV1R3.d.ts +58 -0
  64. package/dist/wallets/WalletContractV1R3.js +101 -0
  65. package/dist/wallets/WalletContractV1R3.spec.d.ts +8 -0
  66. package/dist/wallets/WalletContractV1R3.spec.js +44 -0
  67. package/dist/wallets/WalletContractV2R1.d.ts +60 -0
  68. package/dist/wallets/WalletContractV2R1.js +102 -0
  69. package/dist/wallets/WalletContractV2R1.spec.d.ts +8 -0
  70. package/dist/wallets/WalletContractV2R1.spec.js +44 -0
  71. package/dist/wallets/WalletContractV2R2.d.ts +60 -0
  72. package/dist/wallets/WalletContractV2R2.js +102 -0
  73. package/dist/wallets/WalletContractV2R2.spec.d.ts +8 -0
  74. package/dist/wallets/WalletContractV2R2.spec.js +44 -0
  75. package/dist/wallets/WalletContractV3R1.d.ts +57 -0
  76. package/dist/wallets/WalletContractV3R1.js +104 -0
  77. package/dist/wallets/WalletContractV3R1.spec.d.ts +8 -0
  78. package/dist/wallets/WalletContractV3R1.spec.js +44 -0
  79. package/dist/wallets/WalletContractV3R2.d.ts +57 -0
  80. package/dist/wallets/WalletContractV3R2.js +104 -0
  81. package/dist/wallets/WalletContractV3R2.spec.d.ts +8 -0
  82. package/dist/wallets/WalletContractV3R2.spec.js +44 -0
  83. package/dist/wallets/WalletContractV3Types.d.ts +12 -0
  84. package/dist/wallets/WalletContractV3Types.js +2 -0
  85. package/dist/wallets/WalletContractV4.d.ts +65 -0
  86. package/dist/wallets/WalletContractV4.js +105 -0
  87. package/dist/wallets/WalletContractV4.spec.d.ts +8 -0
  88. package/dist/wallets/WalletContractV4.spec.js +48 -0
  89. package/dist/wallets/WalletContractV5Beta.d.ts +3 -0
  90. package/dist/wallets/WalletContractV5Beta.js +19 -0
  91. package/dist/wallets/WalletContractV5R1.d.ts +3 -0
  92. package/dist/wallets/WalletContractV5R1.js +19 -0
  93. package/dist/wallets/signing/createWalletTransfer.d.ts +48 -0
  94. package/dist/wallets/signing/createWalletTransfer.js +190 -0
  95. package/dist/wallets/signing/singer.d.ts +9 -0
  96. package/dist/wallets/signing/singer.js +21 -0
  97. package/dist/wallets/v5beta/WalletContractV5Beta.d.ts +130 -0
  98. package/dist/wallets/v5beta/WalletContractV5Beta.js +211 -0
  99. package/dist/wallets/v5beta/WalletContractV5Beta.spec.d.ts +8 -0
  100. package/dist/wallets/v5beta/WalletContractV5Beta.spec.js +298 -0
  101. package/dist/wallets/v5beta/WalletV5BetaActions.d.ts +6 -0
  102. package/dist/wallets/v5beta/WalletV5BetaActions.js +92 -0
  103. package/dist/wallets/v5beta/WalletV5BetaActions.spec.d.ts +1 -0
  104. package/dist/wallets/v5beta/WalletV5BetaActions.spec.js +166 -0
  105. package/dist/wallets/v5beta/WalletV5BetaWalletId.d.ts +13 -0
  106. package/dist/wallets/v5beta/WalletV5BetaWalletId.js +31 -0
  107. package/dist/wallets/v5beta/WalletV5BetaWalletId.spec.d.ts +1 -0
  108. package/dist/wallets/v5beta/WalletV5BetaWalletId.spec.js +68 -0
  109. package/dist/wallets/v5beta/WalletV5OutActions.d.ts +17 -0
  110. package/dist/wallets/v5beta/WalletV5OutActions.js +11 -0
  111. package/dist/wallets/v5r1/WalletContractV5R1.d.ts +125 -0
  112. package/dist/wallets/v5r1/WalletContractV5R1.js +207 -0
  113. package/dist/wallets/v5r1/WalletContractV5R1.spec.d.ts +8 -0
  114. package/dist/wallets/v5r1/WalletContractV5R1.spec.js +321 -0
  115. package/dist/wallets/v5r1/WalletV5R1Actions.d.ts +12 -0
  116. package/dist/wallets/v5r1/WalletV5R1Actions.js +128 -0
  117. package/dist/wallets/v5r1/WalletV5R1Actions.spec.d.ts +1 -0
  118. package/dist/wallets/v5r1/WalletV5R1Actions.spec.js +262 -0
  119. package/dist/wallets/v5r1/WalletV5R1WalletId.d.ts +47 -0
  120. package/dist/wallets/v5r1/WalletV5R1WalletId.js +69 -0
  121. package/dist/wallets/v5r1/WalletV5R1WalletId.spec.d.ts +1 -0
  122. package/dist/wallets/v5r1/WalletV5R1WalletId.spec.js +72 -0
  123. package/package.json +5 -2
@@ -0,0 +1,228 @@
1
+ /**
2
+ * Copyright (c) Whales Corp.
3
+ * All Rights Reserved.
4
+ *
5
+ * This source code is licensed under the MIT license found in the
6
+ * LICENSE file in the root directory of this source tree.
7
+ */
8
+ /// <reference types="node" />
9
+ import { HttpApi } from "./api/HttpApi";
10
+ import { AxiosAdapter } from 'axios';
11
+ import { Address, Cell, Contract, ContractProvider, Message, Transaction, TupleItem, TupleReader, StateInit, OpenedContract } from '@ton/core';
12
+ export type TonClientParameters = {
13
+ /**
14
+ * API Endpoint
15
+ */
16
+ endpoint: string;
17
+ /**
18
+ * HTTP request timeout in milliseconds.
19
+ */
20
+ timeout?: number;
21
+ /**
22
+ * API Key
23
+ */
24
+ apiKey?: string;
25
+ /**
26
+ * HTTP Adapter for axios
27
+ */
28
+ httpAdapter?: AxiosAdapter;
29
+ };
30
+ export declare class TonClient {
31
+ readonly parameters: TonClientParameters;
32
+ protected api: HttpApi;
33
+ constructor(parameters: TonClientParameters);
34
+ /**
35
+ * Get Address Balance
36
+ * @param address address for balance check
37
+ * @returns balance
38
+ */
39
+ getBalance(address: Address): Promise<bigint>;
40
+ /**
41
+ * Invoke get method
42
+ * @param address contract address
43
+ * @param name name of method
44
+ * @param params optional parameters
45
+ * @returns stack and gas_used field
46
+ */
47
+ runMethod(address: Address, name: string, stack?: TupleItem[]): Promise<{
48
+ gas_used: number;
49
+ stack: TupleReader;
50
+ }>;
51
+ /**
52
+ * Invoke get method
53
+ * @param address contract address
54
+ * @param name name of method
55
+ * @param params optional parameters
56
+ * @returns stack and gas_used field
57
+ * @deprecated use runMethod instead
58
+ */
59
+ callGetMethod(address: Address, name: string, stack?: TupleItem[]): Promise<{
60
+ gas_used: number;
61
+ stack: TupleReader;
62
+ }>;
63
+ /**
64
+ * Invoke get method that returns error code instead of throwing error
65
+ * @param address contract address
66
+ * @param name name of method
67
+ * @param params optional parameters
68
+ * @returns stack and gas_used field
69
+ */
70
+ runMethodWithError(address: Address, name: string, params?: any[]): Promise<{
71
+ gas_used: number;
72
+ stack: TupleReader;
73
+ exit_code: number;
74
+ }>;
75
+ /**
76
+ * Invoke get method that returns error code instead of throwing error
77
+ * @param address contract address
78
+ * @param name name of method
79
+ * @param params optional parameters
80
+ * @returns stack and gas_used field
81
+ * @deprecated use runMethodWithError instead
82
+ */
83
+ callGetMethodWithError(address: Address, name: string, stack?: TupleItem[]): Promise<{
84
+ gas_used: number;
85
+ stack: TupleReader;
86
+ }>;
87
+ /**
88
+ * Get transactions
89
+ * @param address address
90
+ */
91
+ getTransactions(address: Address, opts: {
92
+ limit: number;
93
+ lt?: string;
94
+ hash?: string;
95
+ to_lt?: string;
96
+ inclusive?: boolean;
97
+ archival?: boolean;
98
+ }): Promise<Transaction[]>;
99
+ /**
100
+ * Get transaction by it's id
101
+ * @param address address
102
+ * @param lt logical time
103
+ * @param hash transaction hash
104
+ * @returns transaction or null if not exist
105
+ */
106
+ getTransaction(address: Address, lt: string, hash: string): Promise<Transaction | null>;
107
+ /**
108
+ * Locate outcoming transaction of destination address by incoming message
109
+ * @param source message source address
110
+ * @param destination message destination address
111
+ * @param created_lt message's created lt
112
+ * @returns transaction
113
+ */
114
+ tryLocateResultTx(source: Address, destination: Address, created_lt: string): Promise<Transaction>;
115
+ /**
116
+ * Locate incoming transaction of source address by outcoming message
117
+ * @param source message source address
118
+ * @param destination message destination address
119
+ * @param created_lt message's created lt
120
+ * @returns transaction
121
+ */
122
+ tryLocateSourceTx(source: Address, destination: Address, created_lt: string): Promise<Transaction>;
123
+ /**
124
+ * Fetch latest masterchain info
125
+ * @returns masterchain info
126
+ */
127
+ getMasterchainInfo(): Promise<{
128
+ workchain: number;
129
+ shard: string;
130
+ initSeqno: number;
131
+ latestSeqno: number;
132
+ }>;
133
+ /**
134
+ * Fetch latest workchain shards
135
+ * @param seqno masterchain seqno
136
+ */
137
+ getWorkchainShards(seqno: number): Promise<{
138
+ workchain: number;
139
+ shard: string;
140
+ seqno: number;
141
+ }[]>;
142
+ /**
143
+ * Fetch transactions inf shards
144
+ * @param workchain
145
+ * @param seqno
146
+ * @param shard
147
+ */
148
+ getShardTransactions(workchain: number, seqno: number, shard: string): Promise<{
149
+ account: Address;
150
+ lt: string;
151
+ hash: string;
152
+ }[]>;
153
+ /**
154
+ * Send message to a network
155
+ * @param src source message
156
+ */
157
+ sendMessage(src: Message): Promise<void>;
158
+ /**
159
+ * Send file to a network
160
+ * @param src source file
161
+ */
162
+ sendFile(src: Buffer): Promise<void>;
163
+ /**
164
+ * Estimate fees for external message
165
+ * @param address target address
166
+ * @returns
167
+ */
168
+ estimateExternalMessageFee(address: Address, args: {
169
+ body: Cell;
170
+ initCode: Cell | null;
171
+ initData: Cell | null;
172
+ ignoreSignature: boolean;
173
+ }): Promise<{
174
+ '@type': "query.fees";
175
+ source_fees: {
176
+ '@type': "fees";
177
+ in_fwd_fee: number;
178
+ storage_fee: number;
179
+ gas_fee: number;
180
+ fwd_fee: number;
181
+ };
182
+ }>;
183
+ /**
184
+ * Send external message to contract
185
+ * @param contract contract to send message
186
+ * @param src message body
187
+ */
188
+ sendExternalMessage(contract: Contract, src: Cell): Promise<void>;
189
+ /**
190
+ * Check if contract is deployed
191
+ * @param address addres to check
192
+ * @returns true if contract is in active state
193
+ */
194
+ isContractDeployed(address: Address): Promise<boolean>;
195
+ /**
196
+ * Resolves contract state
197
+ * @param address contract address
198
+ */
199
+ getContractState(address: Address): Promise<{
200
+ balance: bigint;
201
+ state: "active" | "uninitialized" | "frozen";
202
+ code: Buffer | null;
203
+ data: Buffer | null;
204
+ lastTransaction: {
205
+ lt: string;
206
+ hash: string;
207
+ } | null;
208
+ blockId: {
209
+ workchain: number;
210
+ shard: string;
211
+ seqno: number;
212
+ };
213
+ timestampt: number;
214
+ }>;
215
+ /**
216
+ * Open contract
217
+ * @param src source contract
218
+ * @returns contract
219
+ */
220
+ open<T extends Contract>(src: T): OpenedContract<T>;
221
+ /**
222
+ * Create a provider
223
+ * @param address address
224
+ * @param init optional init
225
+ * @returns provider
226
+ */
227
+ provider(address: Address, init?: StateInit | null): ContractProvider;
228
+ }
@@ -0,0 +1,441 @@
1
+ "use strict";
2
+ /**
3
+ * Copyright (c) Whales Corp.
4
+ * All Rights Reserved.
5
+ *
6
+ * This source code is licensed under the MIT license found in the
7
+ * LICENSE file in the root directory of this source tree.
8
+ */
9
+ Object.defineProperty(exports, "__esModule", { value: true });
10
+ exports.TonClient = void 0;
11
+ const HttpApi_1 = require("./api/HttpApi");
12
+ const core_1 = require("@ton/core");
13
+ class TonClient {
14
+ constructor(parameters) {
15
+ this.parameters = {
16
+ endpoint: parameters.endpoint
17
+ };
18
+ this.api = new HttpApi_1.HttpApi(this.parameters.endpoint, {
19
+ timeout: parameters.timeout,
20
+ apiKey: parameters.apiKey,
21
+ adapter: parameters.httpAdapter
22
+ });
23
+ }
24
+ /**
25
+ * Get Address Balance
26
+ * @param address address for balance check
27
+ * @returns balance
28
+ */
29
+ async getBalance(address) {
30
+ return (await this.getContractState(address)).balance;
31
+ }
32
+ /**
33
+ * Invoke get method
34
+ * @param address contract address
35
+ * @param name name of method
36
+ * @param params optional parameters
37
+ * @returns stack and gas_used field
38
+ */
39
+ async runMethod(address, name, stack = []) {
40
+ let res = await this.api.callGetMethod(address, name, stack);
41
+ if (res.exit_code !== 0) {
42
+ throw Error('Unable to execute get method. Got exit_code: ' + res.exit_code);
43
+ }
44
+ return { gas_used: res.gas_used, stack: parseStack(res.stack) };
45
+ }
46
+ /**
47
+ * Invoke get method
48
+ * @param address contract address
49
+ * @param name name of method
50
+ * @param params optional parameters
51
+ * @returns stack and gas_used field
52
+ * @deprecated use runMethod instead
53
+ */
54
+ async callGetMethod(address, name, stack = []) {
55
+ return this.runMethod(address, name, stack);
56
+ }
57
+ /**
58
+ * Invoke get method that returns error code instead of throwing error
59
+ * @param address contract address
60
+ * @param name name of method
61
+ * @param params optional parameters
62
+ * @returns stack and gas_used field
63
+ */
64
+ async runMethodWithError(address, name, params = []) {
65
+ let res = await this.api.callGetMethod(address, name, params);
66
+ return { gas_used: res.gas_used, stack: parseStack(res.stack), exit_code: res.exit_code };
67
+ }
68
+ /**
69
+ * Invoke get method that returns error code instead of throwing error
70
+ * @param address contract address
71
+ * @param name name of method
72
+ * @param params optional parameters
73
+ * @returns stack and gas_used field
74
+ * @deprecated use runMethodWithError instead
75
+ */
76
+ async callGetMethodWithError(address, name, stack = []) {
77
+ return this.runMethodWithError(address, name, stack);
78
+ }
79
+ /**
80
+ * Get transactions
81
+ * @param address address
82
+ */
83
+ async getTransactions(address, opts) {
84
+ // Fetch transactions
85
+ let tx = await this.api.getTransactions(address, opts);
86
+ let res = [];
87
+ for (let r of tx) {
88
+ res.push((0, core_1.loadTransaction)(core_1.Cell.fromBoc(Buffer.from(r.data, 'base64'))[0].beginParse()));
89
+ }
90
+ return res;
91
+ }
92
+ /**
93
+ * Get transaction by it's id
94
+ * @param address address
95
+ * @param lt logical time
96
+ * @param hash transaction hash
97
+ * @returns transaction or null if not exist
98
+ */
99
+ async getTransaction(address, lt, hash) {
100
+ let res = await this.api.getTransaction(address, lt, hash);
101
+ if (res) {
102
+ return (0, core_1.loadTransaction)(core_1.Cell.fromBoc(Buffer.from(res.data, 'base64'))[0].beginParse());
103
+ }
104
+ else {
105
+ return null;
106
+ }
107
+ }
108
+ /**
109
+ * Locate outcoming transaction of destination address by incoming message
110
+ * @param source message source address
111
+ * @param destination message destination address
112
+ * @param created_lt message's created lt
113
+ * @returns transaction
114
+ */
115
+ async tryLocateResultTx(source, destination, created_lt) {
116
+ let res = await this.api.tryLocateResultTx(source, destination, created_lt);
117
+ return (0, core_1.loadTransaction)(core_1.Cell.fromBase64(res.data).beginParse());
118
+ }
119
+ /**
120
+ * Locate incoming transaction of source address by outcoming message
121
+ * @param source message source address
122
+ * @param destination message destination address
123
+ * @param created_lt message's created lt
124
+ * @returns transaction
125
+ */
126
+ async tryLocateSourceTx(source, destination, created_lt) {
127
+ let res = await this.api.tryLocateSourceTx(source, destination, created_lt);
128
+ return (0, core_1.loadTransaction)(core_1.Cell.fromBase64(res.data).beginParse());
129
+ }
130
+ /**
131
+ * Fetch latest masterchain info
132
+ * @returns masterchain info
133
+ */
134
+ async getMasterchainInfo() {
135
+ let r = await this.api.getMasterchainInfo();
136
+ return {
137
+ workchain: r.init.workchain,
138
+ shard: r.last.shard,
139
+ initSeqno: r.init.seqno,
140
+ latestSeqno: r.last.seqno
141
+ };
142
+ }
143
+ /**
144
+ * Fetch latest workchain shards
145
+ * @param seqno masterchain seqno
146
+ */
147
+ async getWorkchainShards(seqno) {
148
+ let r = await this.api.getShards(seqno);
149
+ return r.map((m) => ({
150
+ workchain: m.workchain,
151
+ shard: m.shard,
152
+ seqno: m.seqno
153
+ }));
154
+ }
155
+ /**
156
+ * Fetch transactions inf shards
157
+ * @param workchain
158
+ * @param seqno
159
+ * @param shard
160
+ */
161
+ async getShardTransactions(workchain, seqno, shard) {
162
+ let tx = await this.api.getBlockTransactions(workchain, seqno, shard);
163
+ if (tx.incomplete) {
164
+ throw Error('Unsupported');
165
+ }
166
+ return tx.transactions.map((v) => ({
167
+ account: core_1.Address.parseRaw(v.account),
168
+ lt: v.lt,
169
+ hash: v.hash
170
+ }));
171
+ }
172
+ /**
173
+ * Send message to a network
174
+ * @param src source message
175
+ */
176
+ async sendMessage(src) {
177
+ const boc = (0, core_1.beginCell)()
178
+ .store((0, core_1.storeMessage)(src))
179
+ .endCell()
180
+ .toBoc();
181
+ await this.api.sendBoc(boc);
182
+ }
183
+ /**
184
+ * Send file to a network
185
+ * @param src source file
186
+ */
187
+ async sendFile(src) {
188
+ await this.api.sendBoc(src);
189
+ }
190
+ /**
191
+ * Estimate fees for external message
192
+ * @param address target address
193
+ * @returns
194
+ */
195
+ async estimateExternalMessageFee(address, args) {
196
+ return await this.api.estimateFee(address, { body: args.body, initCode: args.initCode, initData: args.initData, ignoreSignature: args.ignoreSignature });
197
+ }
198
+ /**
199
+ * Send external message to contract
200
+ * @param contract contract to send message
201
+ * @param src message body
202
+ */
203
+ async sendExternalMessage(contract, src) {
204
+ if (await this.isContractDeployed(contract.address) || !contract.init) {
205
+ const message = (0, core_1.external)({
206
+ to: contract.address,
207
+ body: src
208
+ });
209
+ await this.sendMessage(message);
210
+ }
211
+ else {
212
+ const message = (0, core_1.external)({
213
+ to: contract.address,
214
+ init: contract.init,
215
+ body: src
216
+ });
217
+ await this.sendMessage(message);
218
+ }
219
+ }
220
+ /**
221
+ * Check if contract is deployed
222
+ * @param address addres to check
223
+ * @returns true if contract is in active state
224
+ */
225
+ async isContractDeployed(address) {
226
+ return (await this.getContractState(address)).state === 'active';
227
+ }
228
+ /**
229
+ * Resolves contract state
230
+ * @param address contract address
231
+ */
232
+ async getContractState(address) {
233
+ let info = await this.api.getAddressInformation(address);
234
+ let balance = BigInt(info.balance);
235
+ let state = info.state;
236
+ return {
237
+ balance,
238
+ state,
239
+ code: info.code !== '' ? Buffer.from(info.code, 'base64') : null,
240
+ data: info.data !== '' ? Buffer.from(info.data, 'base64') : null,
241
+ lastTransaction: info.last_transaction_id.lt !== '0' ? {
242
+ lt: info.last_transaction_id.lt,
243
+ hash: info.last_transaction_id.hash,
244
+ } : null,
245
+ blockId: {
246
+ workchain: info.block_id.workchain,
247
+ shard: info.block_id.shard,
248
+ seqno: info.block_id.seqno
249
+ },
250
+ timestampt: info.sync_utime
251
+ };
252
+ }
253
+ /**
254
+ * Open contract
255
+ * @param src source contract
256
+ * @returns contract
257
+ */
258
+ open(src) {
259
+ return (0, core_1.openContract)(src, (args) => createProvider(this, args.address, args.init));
260
+ }
261
+ /**
262
+ * Create a provider
263
+ * @param address address
264
+ * @param init optional init
265
+ * @returns provider
266
+ */
267
+ provider(address, init) {
268
+ return createProvider(this, address, init ?? null);
269
+ }
270
+ }
271
+ exports.TonClient = TonClient;
272
+ function parseStackEntry(x) {
273
+ const typeName = x['@type'];
274
+ switch (typeName) {
275
+ case 'tvm.list':
276
+ case 'tvm.tuple':
277
+ return x.elements.map(parseStackEntry);
278
+ case 'tvm.cell':
279
+ return core_1.Cell.fromBoc(Buffer.from(x.bytes, 'base64'))[0];
280
+ case 'tvm.slice':
281
+ return core_1.Cell.fromBoc(Buffer.from(x.bytes, 'base64'))[0];
282
+ case 'tvm.stackEntryCell':
283
+ return parseStackEntry(x.cell);
284
+ case 'tvm.stackEntrySlice':
285
+ return parseStackEntry(x.slice);
286
+ case 'tvm.stackEntryTuple':
287
+ return parseStackEntry(x.tuple);
288
+ case 'tvm.stackEntryList':
289
+ return parseStackEntry(x.list);
290
+ case 'tvm.stackEntryNumber':
291
+ return parseStackEntry(x.number);
292
+ case 'tvm.numberDecimal':
293
+ return BigInt(x.number);
294
+ default:
295
+ throw Error('Unsupported item type: ' + typeName);
296
+ }
297
+ }
298
+ function parseStackItem(s) {
299
+ if (s[0] === 'num') {
300
+ let val = s[1];
301
+ if (val.startsWith('-')) {
302
+ return { type: 'int', value: -BigInt(val.slice(1)) };
303
+ }
304
+ else {
305
+ return { type: 'int', value: BigInt(val) };
306
+ }
307
+ }
308
+ else if (s[0] === 'null') {
309
+ return { type: 'null' };
310
+ }
311
+ else if (s[0] === 'cell') {
312
+ return { type: 'cell', cell: core_1.Cell.fromBoc(Buffer.from(s[1].bytes, 'base64'))[0] };
313
+ }
314
+ else if (s[0] === 'slice') {
315
+ return { type: 'slice', cell: core_1.Cell.fromBoc(Buffer.from(s[1].bytes, 'base64'))[0] };
316
+ }
317
+ else if (s[0] === 'builder') {
318
+ return { type: 'builder', cell: core_1.Cell.fromBoc(Buffer.from(s[1].bytes, 'base64'))[0] };
319
+ }
320
+ else if (s[0] === 'tuple' || s[0] === 'list') {
321
+ if (s[1].elements.length === 0) {
322
+ return { type: 'null' };
323
+ }
324
+ return { type: 'tuple', items: s[1].elements.map(parseStackEntry) };
325
+ }
326
+ else {
327
+ throw Error('Unsupported stack item type: ' + s[0]);
328
+ }
329
+ }
330
+ function parseStack(src) {
331
+ let stack = [];
332
+ for (let s of src) {
333
+ stack.push(parseStackItem(s));
334
+ }
335
+ return new core_1.TupleReader(stack);
336
+ }
337
+ function createProvider(client, address, init) {
338
+ return {
339
+ async getState() {
340
+ let state = await client.getContractState(address);
341
+ let balance = state.balance;
342
+ let last = state.lastTransaction ? { lt: BigInt(state.lastTransaction.lt), hash: Buffer.from(state.lastTransaction.hash, 'base64') } : null;
343
+ let storage;
344
+ if (state.state === 'active') {
345
+ storage = {
346
+ type: 'active',
347
+ code: state.code ? state.code : null,
348
+ data: state.data ? state.data : null,
349
+ };
350
+ }
351
+ else if (state.state === 'uninitialized') {
352
+ storage = {
353
+ type: 'uninit',
354
+ };
355
+ }
356
+ else if (state.state === 'frozen') {
357
+ storage = {
358
+ type: 'frozen',
359
+ stateHash: Buffer.alloc(0),
360
+ };
361
+ }
362
+ else {
363
+ throw Error('Unsupported state');
364
+ }
365
+ return {
366
+ balance,
367
+ last,
368
+ state: storage,
369
+ };
370
+ },
371
+ async get(name, args) {
372
+ let method = await client.callGetMethod(address, name, args);
373
+ return { stack: method.stack };
374
+ },
375
+ async external(message) {
376
+ //
377
+ // Resolve init
378
+ //
379
+ let neededInit = null;
380
+ if (init && !await client.isContractDeployed(address)) {
381
+ neededInit = init;
382
+ }
383
+ //
384
+ // Send package
385
+ //
386
+ const ext = (0, core_1.external)({
387
+ to: address,
388
+ init: neededInit,
389
+ body: message
390
+ });
391
+ let boc = (0, core_1.beginCell)()
392
+ .store((0, core_1.storeMessage)(ext))
393
+ .endCell()
394
+ .toBoc();
395
+ await client.sendFile(boc);
396
+ },
397
+ async internal(via, message) {
398
+ // Resolve init
399
+ let neededInit = null;
400
+ if (init && (!await client.isContractDeployed(address))) {
401
+ neededInit = init;
402
+ }
403
+ // Resolve bounce
404
+ let bounce = true;
405
+ if (message.bounce !== null && message.bounce !== undefined) {
406
+ bounce = message.bounce;
407
+ }
408
+ // Resolve value
409
+ let value;
410
+ if (typeof message.value === 'string') {
411
+ value = (0, core_1.toNano)(message.value);
412
+ }
413
+ else {
414
+ value = message.value;
415
+ }
416
+ // Resolve body
417
+ let body = null;
418
+ if (typeof message.body === 'string') {
419
+ body = (0, core_1.comment)(message.body);
420
+ }
421
+ else if (message.body) {
422
+ body = message.body;
423
+ }
424
+ // Send internal message
425
+ await via.send({
426
+ to: address,
427
+ value,
428
+ bounce,
429
+ sendMode: message.sendMode,
430
+ init: neededInit,
431
+ body
432
+ });
433
+ },
434
+ open(contract) {
435
+ return (0, core_1.openContract)(contract, (args) => createProvider(client, args.address, args.init ?? null));
436
+ },
437
+ getTransactions(address, lt, hash, limit) {
438
+ return client.getTransactions(address, { limit: limit ?? 100, lt: lt.toString(), hash: hash.toString('base64'), inclusive: true });
439
+ }
440
+ };
441
+ }
@@ -0,0 +1 @@
1
+ export {};