forked from ton-core/ton
-
Notifications
You must be signed in to change notification settings - Fork 74
Expand file tree
/
Copy pathTonClient4.ts
More file actions
988 lines (916 loc) · 27.7 KB
/
TonClient4.ts
File metadata and controls
988 lines (916 loc) · 27.7 KB
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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
/**
* Copyright (c) Whales Corp.
* All Rights Reserved.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
import axios, {
AxiosAdapter,
InternalAxiosRequestConfig,
AxiosInstance,
} from "axios";
import {
Address,
beginCell,
Cell,
comment,
Contract,
ContractProvider,
ContractState,
external,
ExtraCurrency,
loadTransaction,
openContract,
OpenedContract,
parseTuple,
serializeTuple,
StateInit,
storeMessage,
toNano,
Transaction,
TupleItem,
TupleReader,
} from "@ton/core";
import { Maybe } from "../utils/maybe";
import { toUrlSafe } from "../utils/toUrlSafe";
import { z } from "zod";
export type TonClient4Parameters = {
/**
* API endpoint
*/
endpoint: string;
/**
* HTTP request timeout in milliseconds.
*/
timeout?: number;
/**
* HTTP Adapter for axios
*/
httpAdapter?: AxiosAdapter;
/**
* HTTP request interceptor for axios
*/
requestInterceptor?: (
config: InternalAxiosRequestConfig,
) => InternalAxiosRequestConfig;
};
export class TonClient4 {
#endpoint: string;
#timeout: number;
#adapter?: AxiosAdapter;
#axios: AxiosInstance;
constructor(args: TonClient4Parameters) {
this.#axios = axios.create();
this.#endpoint = args.endpoint;
this.#timeout = args.timeout || 5000;
this.#adapter = args.httpAdapter;
if (args.requestInterceptor) {
this.#axios.interceptors.request.use(args.requestInterceptor);
}
}
/**
* Get Last Block
* @returns last block info
*/
async getLastBlock() {
let res = await this.#axios.get(this.#endpoint + "/block/latest", {
adapter: this.#adapter,
timeout: this.#timeout,
});
let lastBlock = lastBlockCodec.safeParse(res.data);
if (!lastBlock.success) {
throw Error(
"Mailformed response: " +
lastBlock.error.format()._errors.join(", "),
);
}
return lastBlock.data;
}
/**
* Get block info
* @param seqno block sequence number
* @returns block info
*/
async getBlock(seqno: number) {
let res = await this.#axios.get(this.#endpoint + "/block/" + seqno, {
adapter: this.#adapter,
timeout: this.#timeout,
});
let block = blockCodec.safeParse(res.data);
if (!block.success) {
throw Error("Mailformed response");
}
if (!block.data.exist) {
throw Error("Block is out of scope");
}
return block.data.block;
}
/**
* Get block info by unix timestamp
* @param ts unix timestamp
* @returns block info
*/
async getBlockByUtime(ts: number) {
let res = await this.#axios.get(this.#endpoint + "/block/utime/" + ts, {
adapter: this.#adapter,
timeout: this.#timeout,
});
let block = blockCodec.safeParse(res.data);
if (!block.success) {
throw Error("Mailformed response");
}
if (!block.data.exist) {
throw Error("Block is out of scope");
}
return block.data.block;
}
/**
* Get block info by unix timestamp
* @param seqno block sequence number
* @param address account address
* @returns account info
*/
async getAccount(seqno: number, address: Address) {
let res = await this.#axios.get(
this.#endpoint +
"/block/" +
seqno +
"/" +
address.toString({ urlSafe: true }),
{ adapter: this.#adapter, timeout: this.#timeout },
);
let account = accountCodec.safeParse(res.data);
if (!account.success) {
throw Error("Mailformed response");
}
return account.data;
}
/**
* Get account lite info (without code and data)
* @param seqno block sequence number
* @param address account address
* @returns account lite info
*/
async getAccountLite(seqno: number, address: Address) {
let res = await this.#axios.get(
this.#endpoint +
"/block/" +
seqno +
"/" +
address.toString({ urlSafe: true }) +
"/lite",
{ adapter: this.#adapter, timeout: this.#timeout },
);
let account = accountLiteCodec.safeParse(res.data);
if (!account.success) {
throw Error("Mailformed response");
}
return account.data;
}
/**
* Check if contract is deployed
* @param address addres to check
* @returns true if contract is in active state
*/
async isContractDeployed(seqno: number, address: Address) {
let account = await this.getAccountLite(seqno, address);
return account.account.state.type === "active";
}
/**
* Check if account was updated since
* @param seqno block sequence number
* @param address account address
* @param lt account last transaction lt
* @returns account change info
*/
async isAccountChanged(seqno: number, address: Address, lt: bigint) {
let res = await this.#axios.get(
this.#endpoint +
"/block/" +
seqno +
"/" +
address.toString({ urlSafe: true }) +
"/changed/" +
lt.toString(10),
{ adapter: this.#adapter, timeout: this.#timeout },
);
let changed = changedCodec.safeParse(res.data);
if (!changed.success) {
throw Error("Mailformed response");
}
return changed.data;
}
/**
* Load unparsed account transactions
* @param address address
* @param lt last transaction lt
* @param hash last transaction hash
* @returns unparsed transactions
*/
async getAccountTransactions(address: Address, lt: bigint, hash: Buffer) {
let res = await this.#axios.get(
this.#endpoint +
"/account/" +
address.toString({ urlSafe: true }) +
"/tx/" +
lt.toString(10) +
"/" +
toUrlSafe(hash.toString("base64")),
{ adapter: this.#adapter, timeout: this.#timeout },
);
let transactions = transactionsCodec.safeParse(res.data);
if (!transactions.success) {
throw Error("Mailformed response");
}
let data = transactions.data;
let tx: {
block: {
workchain: number;
seqno: number;
shard: string;
rootHash: string;
fileHash: string;
};
tx: Transaction;
}[] = [];
let cells = Cell.fromBoc(Buffer.from(data.boc, "base64"));
for (let i = 0; i < data.blocks.length; i++) {
tx.push({
block: data.blocks[i],
tx: loadTransaction(cells[i].beginParse()),
});
}
return tx;
}
/**
* Load parsed account transactions
* @param address address
* @param lt last transaction lt
* @param hash last transaction hash
* @param count number of transactions to load
* @returns parsed transactions
*/
async getAccountTransactionsParsed(
address: Address,
lt: bigint,
hash: Buffer,
count: number = 20,
) {
let res = await this.#axios.get(
this.#endpoint +
"/account/" +
address.toString({ urlSafe: true }) +
"/tx/parsed/" +
lt.toString(10) +
"/" +
toUrlSafe(hash.toString("base64")),
{
adapter: this.#adapter,
timeout: this.#timeout,
params: {
count,
},
},
);
let parsedTransactionsRes = parsedTransactionsCodec.safeParse(res.data);
if (!parsedTransactionsRes.success) {
throw Error("Mailformed response");
}
return parsedTransactionsRes.data as ParsedTransactions;
}
/**
* Get network config
* @param seqno block sequence number
* @param ids optional config ids
* @returns network config
*/
async getConfig(seqno: number, ids?: number[]) {
let tail = "";
if (ids && ids.length > 0) {
tail = "/" + [...ids].sort().join(",");
}
let res = await this.#axios.get(
this.#endpoint + "/block/" + seqno + "/config" + tail,
{ adapter: this.#adapter, timeout: this.#timeout },
);
let config = configCodec.safeParse(res.data);
if (!config.success) {
throw Error("Mailformed response");
}
return config.data;
}
/**
* Execute run method
* @param seqno block sequence number
* @param address account address
* @param name method name
* @param args method arguments
* @returns method result
*/
async runMethod(
seqno: number,
address: Address,
name: string,
args?: TupleItem[],
) {
let tail =
args && args.length > 0
? "/" +
toUrlSafe(
serializeTuple(args)
.toBoc({ idx: false, crc32: false })
.toString("base64"),
)
: "";
let url =
this.#endpoint +
"/block/" +
seqno +
"/" +
address.toString({ urlSafe: true }) +
"/run/" +
encodeURIComponent(name) +
tail;
let res = await this.#axios.get(url, {
adapter: this.#adapter,
timeout: this.#timeout,
});
let runMethod = runMethodCodec.safeParse(res.data);
if (!runMethod.success) {
throw Error("Mailformed response");
}
let resultTuple = runMethod.data.resultRaw
? parseTuple(
Cell.fromBoc(
Buffer.from(runMethod.data.resultRaw, "base64"),
)[0],
)
: [];
return {
exitCode: runMethod.data.exitCode,
result: resultTuple,
resultRaw: runMethod.data.resultRaw,
block: runMethod.data.block,
shardBlock: runMethod.data.shardBlock,
reader: new TupleReader(resultTuple),
};
}
/**
* Send external message
* @param message message boc
* @returns message status
*/
async sendMessage(message: Buffer) {
let res = await this.#axios.post(
this.#endpoint + "/send",
{ boc: message.toString("base64") },
{ adapter: this.#adapter, timeout: this.#timeout },
);
let send = sendCodec.safeParse(res.data);
if (!send.success) {
throw Error("Mailformed response");
}
return { status: res.data.status };
}
/**
* Open smart contract
* @param contract contract
* @returns opened contract
*/
open<T extends Contract>(contract: T) {
return openContract<T>(contract, (args) =>
createProvider(this, null, args.address, args.init),
);
}
/**
* Open smart contract
* @param block block number
* @param contract contract
* @returns opened contract
*/
openAt<T extends Contract>(block: number, contract: T) {
return openContract<T>(contract, (args) =>
createProvider(this, block, args.address, args.init),
);
}
/**
* Create provider
* @param address address
* @param init optional init data
* @returns provider
*/
provider(address: Address, init?: StateInit | null) {
return createProvider(this, null, address, init ?? null);
}
/**
* Create provider at specified block number
* @param block block number
* @param address address
* @param init optional init data
* @returns provider
*/
providerAt(block: number, address: Address, init?: StateInit | null) {
return createProvider(this, block, address, init ?? null);
}
}
function createProvider(
client: TonClient4,
block: number | null,
address: Address,
init: StateInit | null,
): ContractProvider {
return {
async getState(): Promise<ContractState> {
// Resolve block
let sq = block;
if (sq === null) {
let res = await client.getLastBlock();
sq = res.last.seqno;
}
// Load state
let state = await client.getAccount(sq, address);
// Convert state
let last = state.account.last
? {
lt: BigInt(state.account.last.lt),
hash: Buffer.from(state.account.last.hash, "base64"),
}
: null;
let storage:
| {
type: "uninit";
}
| {
type: "active";
code: Maybe<Buffer>;
data: Maybe<Buffer>;
}
| {
type: "frozen";
stateHash: Buffer;
};
if (state.account.state.type === "active") {
storage = {
type: "active",
code: state.account.state.code
? Buffer.from(state.account.state.code, "base64")
: null,
data: state.account.state.data
? Buffer.from(state.account.state.data, "base64")
: null,
};
} else if (state.account.state.type === "uninit") {
storage = {
type: "uninit",
};
} else if (state.account.state.type === "frozen") {
storage = {
type: "frozen",
stateHash: Buffer.from(
state.account.state.stateHash,
"base64",
),
};
} else {
throw Error("Unsupported state");
}
let ecMap: ExtraCurrency | null = null;
if (state.account.balance.currencies) {
ecMap = {};
let currencies = state.account.balance.currencies;
for (let [k, v] of Object.entries(currencies)) {
ecMap[Number(k)] = BigInt(v);
}
}
return {
balance: BigInt(state.account.balance.coins),
extracurrency: ecMap,
last: last,
state: storage,
};
},
async get(name, args) {
if (typeof name !== "string") {
throw new Error(
"Method name must be a string for TonClient4 provider",
);
}
let sq = block;
if (sq === null) {
let res = await client.getLastBlock();
sq = res.last.seqno;
}
let method = await client.runMethod(sq, address, name, args);
if (method.exitCode !== 0 && method.exitCode !== 1) {
throw Error("Exit code: " + method.exitCode);
}
return {
stack: new TupleReader(method.result),
};
},
async external(message) {
// Resolve last
let last = await client.getLastBlock();
// Resolve init
let neededInit: StateInit | null = null;
if (
init &&
(await client.getAccountLite(last.last.seqno, address)).account
.state.type !== "active"
) {
neededInit = init;
}
// Send with state init
const ext = external({
to: address,
init: neededInit,
body: message,
});
let pkg = beginCell().store(storeMessage(ext)).endCell().toBoc();
await client.sendMessage(pkg);
},
async internal(via, message) {
// Resolve last
let last = await client.getLastBlock();
// Resolve init
let neededInit: StateInit | null = null;
if (
init &&
(await client.getAccountLite(last.last.seqno, address)).account
.state.type !== "active"
) {
neededInit = init;
}
// Resolve bounce
let bounce = true;
if (message.bounce !== null && message.bounce !== undefined) {
bounce = message.bounce;
}
// Resolve value
let value: bigint;
if (typeof message.value === "string") {
value = toNano(message.value);
} else {
value = message.value;
}
// Resolve body
let body: Cell | null = null;
if (typeof message.body === "string") {
body = comment(message.body);
} else if (message.body) {
body = message.body;
}
// Send internal message
await via.send({
to: address,
value,
extracurrency: message.extracurrency,
bounce,
sendMode: message.sendMode,
init: neededInit,
body,
});
},
open<T extends Contract>(contract: T): OpenedContract<T> {
return openContract<T>(contract, (args) =>
createProvider(client, block, args.address, args.init ?? null),
);
},
async getTransactions(
address: Address,
lt: bigint,
hash: Buffer,
limit?: number,
): Promise<Transaction[]> {
// Resolve last
const useLimit = typeof limit === "number";
if (useLimit && limit <= 0) {
return [];
}
// Load transactions
let transactions: Transaction[] = [];
do {
const txs = await client.getAccountTransactions(
address,
lt,
hash,
);
const firstTx = txs[0].tx;
const [firstLt, firstHash] = [firstTx.lt, firstTx.hash()];
const needSkipFirst =
transactions.length > 0 &&
firstLt === lt &&
firstHash.equals(hash);
if (needSkipFirst) {
txs.shift();
}
if (txs.length === 0) {
break;
}
const lastTx = txs[txs.length - 1].tx;
const [lastLt, lastHash] = [lastTx.lt, lastTx.hash()];
if (lastLt === lt && lastHash.equals(hash)) {
break;
}
transactions.push(...txs.map((tx) => tx.tx));
lt = lastLt;
hash = lastHash;
} while (useLimit && transactions.length < limit);
// Apply limit
if (useLimit) {
transactions = transactions.slice(0, limit);
}
// Return transactions
return transactions;
},
};
}
//
// Codecs
//
const lastBlockCodec = z.object({
last: z.object({
seqno: z.number(),
shard: z.string(),
workchain: z.number(),
fileHash: z.string(),
rootHash: z.string(),
}),
init: z.object({
fileHash: z.string(),
rootHash: z.string(),
}),
stateRootHash: z.string(),
now: z.number(),
});
const blockCodec = z.union([
z.object({
exist: z.literal(false),
}),
z.object({
exist: z.literal(true),
block: z.object({
shards: z.array(
z.object({
workchain: z.number(),
seqno: z.number(),
shard: z.string(),
rootHash: z.string(),
fileHash: z.string(),
transactions: z.array(
z.object({
account: z.string(),
hash: z.string(),
lt: z.string(),
}),
),
}),
),
}),
}),
]);
// {"lastPaid":1653099243,"duePayment":null,"used":{"bits":119,"cells":1,"publicCells":0}}
const storageStatCodec = z.object({
lastPaid: z.number(),
duePayment: z.union([z.null(), z.string()]),
used: z.object({
bits: z.number(),
cells: z.number(),
publicCells: z.number().optional(),
}),
});
const accountCodec = z.object({
account: z.object({
state: z.union([
z.object({ type: z.literal("uninit") }),
z.object({
type: z.literal("active"),
code: z.union([z.string(), z.null()]),
data: z.union([z.string(), z.null()]),
}),
z.object({ type: z.literal("frozen"), stateHash: z.string() }),
]),
balance: z.object({
coins: z.string(),
currencies: z.record(z.string(), z.string()),
}),
last: z.union([
z.null(),
z.object({
lt: z.string(),
hash: z.string(),
}),
]),
storageStat: z.union([z.null(), storageStatCodec]),
}),
block: z.object({
workchain: z.number(),
seqno: z.number(),
shard: z.string(),
rootHash: z.string(),
fileHash: z.string(),
}),
});
const accountLiteCodec = z.object({
account: z.object({
state: z.union([
z.object({ type: z.literal("uninit") }),
z.object({
type: z.literal("active"),
codeHash: z.string(),
dataHash: z.string(),
}),
z.object({ type: z.literal("frozen"), stateHash: z.string() }),
]),
balance: z.object({
coins: z.string(),
currencies: z.record(z.string(), z.string()),
}),
last: z.union([
z.null(),
z.object({
lt: z.string(),
hash: z.string(),
}),
]),
storageStat: z.union([z.null(), storageStatCodec]),
}),
});
const changedCodec = z.object({
changed: z.boolean(),
block: z.object({
workchain: z.number(),
seqno: z.number(),
shard: z.string(),
rootHash: z.string(),
fileHash: z.string(),
}),
});
const runMethodCodec = z.object({
exitCode: z.number(),
resultRaw: z.union([z.string(), z.null()]),
block: z.object({
workchain: z.number(),
seqno: z.number(),
shard: z.string(),
rootHash: z.string(),
fileHash: z.string(),
}),
shardBlock: z.object({
workchain: z.number(),
seqno: z.number(),
shard: z.string(),
rootHash: z.string(),
fileHash: z.string(),
}),
});
const configCodec = z.object({
config: z.object({
cell: z.string(),
address: z.string(),
globalBalance: z.object({
coins: z.string(),
}),
}),
});
const sendCodec = z.object({
status: z.number(),
});
const blocksCodec = z.array(
z.object({
workchain: z.number(),
seqno: z.number(),
shard: z.string(),
rootHash: z.string(),
fileHash: z.string(),
}),
);
const transactionsCodec = z.object({
blocks: blocksCodec,
boc: z.string(),
});
const parsedAddressExternalCodec = z.object({
bits: z.number(),
data: z.string(),
});
const parsedMessageInfoCodec = z.union([
z.object({
type: z.literal("internal"),
value: z.string(),
dest: z.string(),
src: z.string(),
bounced: z.boolean(),
bounce: z.boolean(),
ihrDisabled: z.boolean(),
createdAt: z.number(),
createdLt: z.string(),
fwdFee: z.string(),
ihrFee: z.string(),
}),
z.object({
type: z.literal("external-in"),
dest: z.string(),
src: z.union([parsedAddressExternalCodec, z.null()]),
importFee: z.string(),
}),
z.object({
type: z.literal("external-out"),
dest: z.union([parsedAddressExternalCodec, z.null()]),
}),
]);
const parsedStateInitCodec = z.object({
splitDepth: z.union([z.number(), z.null()]),
code: z.union([z.string(), z.null()]),
data: z.union([z.string(), z.null()]),
special: z.union([
z.object({ tick: z.boolean(), tock: z.boolean() }),
z.null(),
]),
});
const parsedMessageCodec = z.object({
body: z.string(),
info: parsedMessageInfoCodec,
init: z.union([parsedStateInitCodec, z.null()]),
});
const accountStatusCodec = z.union([
z.literal("uninitialized"),
z.literal("frozen"),
z.literal("active"),
z.literal("non-existing"),
]);
const txBodyCodec = z.union([
z.object({ type: z.literal("comment"), comment: z.string() }),
z.object({ type: z.literal("payload"), cell: z.string() }),
]);
const parsedOperationItemCodec = z.union([
z.object({ kind: z.literal("ton"), amount: z.string() }),
z.object({ kind: z.literal("token"), amount: z.string() }),
]);
const supportedMessageTypeCodec = z.union([
z.literal("jetton::excesses"),
z.literal("jetton::transfer"),
z.literal("jetton::transfer_notification"),
z.literal("deposit"),
z.literal("deposit::ok"),
z.literal("withdraw"),
z.literal("withdraw::all"),
z.literal("withdraw::delayed"),
z.literal("withdraw::ok"),
z.literal("airdrop"),
]);
const opCodec = z.object({
type: supportedMessageTypeCodec,
options: z.optional(z.record(z.string())),
});
const parsedOperationCodec = z.object({
address: z.string(),
comment: z.optional(z.string()),
items: z.array(parsedOperationItemCodec),
op: z.optional(opCodec),
});
const parsedTransactionCodec = z.object({
address: z.string(),
lt: z.string(),
hash: z.string(),
prevTransaction: z.object({
lt: z.string(),
hash: z.string(),
}),
time: z.number(),
outMessagesCount: z.number(),
oldStatus: accountStatusCodec,
newStatus: accountStatusCodec,
fees: z.string(),
update: z.object({
oldHash: z.string(),
newHash: z.string(),
}),
inMessage: z.union([parsedMessageCodec, z.null()]),
outMessages: z.array(parsedMessageCodec),
parsed: z.object({
seqno: z.union([z.number(), z.null()]),
body: z.union([txBodyCodec, z.null()]),
status: z.union([
z.literal("success"),
z.literal("failed"),
z.literal("pending"),
]),
dest: z.union([z.string(), z.null()]),
kind: z.union([z.literal("out"), z.literal("in")]),
amount: z.string(),
resolvedAddress: z.string(),
bounced: z.boolean(),
mentioned: z.array(z.string()),
}),
operation: parsedOperationCodec,
});
const parsedTransactionsCodec = z.object({
blocks: blocksCodec,
transactions: z.array(parsedTransactionCodec),
});
export type ParsedTransaction = z.infer<typeof parsedTransactionCodec>;
export type ParsedTransactions = {
blocks: z.infer<typeof blocksCodec>;
transactions: ParsedTransaction[];
};