@layerzerolabs/ton-sdk-tools 3.0.70 → 3.0.72

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/CHANGELOG.md CHANGED
@@ -1,5 +1,17 @@
1
1
  # @layerzerolabs/ton-sdk-tools
2
2
 
3
+ ## 3.0.72
4
+
5
+ ### Patch Changes
6
+
7
+ - a994069: nibiru, plumephoenix
8
+
9
+ ## 3.0.71
10
+
11
+ ### Patch Changes
12
+
13
+ - cfdef0c: nibiru testnet
14
+
3
15
  ## 3.0.70
4
16
 
5
17
  ### Patch Changes
package/dist/index.cjs CHANGED
@@ -345,8 +345,156 @@ function parseClasses(strInput) {
345
345
  }
346
346
  return results;
347
347
  }
348
- function saveTonContractWrapper(tonObjects, directory, baseWrapper) {
349
- const generatedMethods = Object.entries(tonObjects).map(([_, tonObject]) => tonObject.constructorCode ?? "").join("\n");
348
+ function saveBaseEventHandler(directory) {
349
+ const allTypes = ["ExtendedContract", "ActionEvent", "Md", "nameMap"].sort();
350
+ const savedCode = `${file_signature_header}
351
+ /* eslint-disable @typescript-eslint/no-unsafe-assignment */
352
+ /* eslint-disable @typescript-eslint/no-unnecessary-condition */
353
+
354
+ import { Cell, Transaction, TransactionComputeVm, TransactionDescriptionGeneric, beginCell } from '@ton/core'
355
+ import { BlockchainTransaction, EventMessageSent, SendMessageResult } from '@ton/sandbox'
356
+ import { flattenTransaction } from '@ton/test-utils'
357
+
358
+ import { OPCODES } from '../constants'
359
+
360
+ import { getLzDict,
361
+ ${allTypes.map((type) => " " + type + ",\n").join("")}} from './allTypes'
362
+
363
+ import { TonObjectUnwrapper } from './TonObjectUnwrapper'
364
+ import { TonContractWrapper } from './TonContractWrapper'
365
+
366
+ export class LzEvent {
367
+ private _eventPromise: Promise<ActionEvent>
368
+ private _event: ActionEvent | null
369
+ public topic: string
370
+ public body: Md
371
+ public bodyName: string
372
+ public initialStorage: Cell
373
+ public initialized: boolean
374
+
375
+ constructor(wrapper: ExtendedContract<TonContractWrapper>, cell: Cell) {
376
+ this._eventPromise = TonObjectUnwrapper.getActionEventToTS(wrapper, cell)
377
+ this._event = null
378
+ this.topic = ''
379
+ this.body = {}
380
+ this.initialStorage = beginCell().endCell()
381
+ this.initialized = false
382
+ this.bodyName = ''
383
+ }
384
+
385
+ async load(wrapper: ExtendedContract<TonContractWrapper>): Promise<boolean> {
386
+ this._event = await this._eventPromise
387
+ this.topic = TonContractWrapper.bigintToAsciiString(this._event.topic)
388
+ this.bodyName = await TonObjectUnwrapper.getTypeOf(wrapper, this._event.body)
389
+ const tsCasterName = 'get' + nameMap[this.bodyName] + 'ToTS'
390
+
391
+ const method = TonObjectUnwrapper[tsCasterName as keyof typeof TonObjectUnwrapper]
392
+ if (typeof method === 'function') {
393
+ this.body = await method(wrapper, this._event.body)
394
+ } else {
395
+ console.log('Method' + tsCasterName + 'does not exist.')
396
+ }
397
+
398
+ this.initialStorage = this._event.initialStorage
399
+ this.initialized = true
400
+ return this.initialized
401
+ }
402
+ }
403
+
404
+ export class BaseEventHandler {
405
+ public allFailures = {
406
+ txList: [] as BlockchainTransaction[],
407
+ events: [] as EventMessageSent[],
408
+ }
409
+ public allVictories = {
410
+ txList: [] as BlockchainTransaction[],
411
+ eventObjects: [] as LzEvent[],
412
+ }
413
+
414
+ async postProcessSuccess(event: LzEvent, tx: BlockchainTransaction & Transaction): Promise<void> {
415
+ // base case: do nothing
416
+ }
417
+
418
+ async postProcessFailure(event: EventMessageSent, tx: BlockchainTransaction & Transaction): Promise<void> {
419
+ // base case: do nothing
420
+ }
421
+
422
+ async addCurrentEventsToAllEvents(
423
+ results: SendMessageResult,
424
+ contract: ExtendedContract<TonContractWrapper>
425
+ ): Promise<void> {
426
+ for (let i = 0; i < results.transactions.length; i++) {
427
+ const tx: BlockchainTransaction & Transaction = results.transactions[i]
428
+ const desc = tx.description
429
+ if (desc.type !== 'generic' || desc.computePhase.type !== 'skipped') continue
430
+
431
+ const eventBody = tx.inMessage?.body
432
+ if (eventBody === undefined) continue
433
+ const eventBodySlice = eventBody.beginParse()
434
+ // the event body is empty if we are sending a payment with no corresponding payload
435
+ if (eventBodySlice.remainingBits < 32) continue
436
+ const opCode = eventBodySlice.loadUint(32)
437
+ if (opCode === Number(OPCODES.BaseInterface_OP_EVENT)) {
438
+ if (eventBody.refs.length > 0) {
439
+ const eventInnerCell = eventBody.refs[0]
440
+ const newLzEvent = new LzEvent(contract, eventInnerCell)
441
+ await newLzEvent.load(contract)
442
+ this.allVictories.txList.push(tx)
443
+ this.allVictories.eventObjects.push(newLzEvent)
444
+ await this.postProcessSuccess(newLzEvent, tx)
445
+ }
446
+ } else {
447
+ const failureEvent = results.events[i] as EventMessageSent
448
+ this.allFailures.txList.push(tx)
449
+ this.allFailures.events.push(failureEvent)
450
+ await this.postProcessFailure(failureEvent, tx)
451
+ }
452
+ }
453
+ }
454
+ }
455
+ `;
456
+ fs__namespace.writeFileSync(path__namespace.join(directory, "BaseEventHandler.ts"), savedCode);
457
+ }
458
+ function saveLzEventHandler(directory) {
459
+ const savedCode = `${file_signature_header}
460
+ /* eslint-disable @typescript-eslint/no-unsafe-assignment */
461
+ /* eslint-disable @typescript-eslint/no-unnecessary-condition */
462
+
463
+ import { Cell, Transaction } from '@ton/core'
464
+ import { BlockchainTransaction, EventMessageSent, SendMessageResult } from '@ton/sandbox'
465
+
466
+ import { Md, MdPacketSent } from './allTypes'
467
+ import { BaseEventHandler, LzEvent } from './BaseEventHandler'
468
+
469
+ export class LzEventHandler extends BaseEventHandler {
470
+ public allVictories = {
471
+ txList: [] as BlockchainTransaction[],
472
+ eventObjects: [] as LzEvent[],
473
+ packetsSent: [] as MdPacketSent[],
474
+ }
475
+
476
+ async postProcessSuccess(event: LzEvent, tx: BlockchainTransaction & Transaction): Promise<void> {
477
+ if (event.topic === 'Channel::event::PACKET_SENT' && this.isMdPacketSent(event.body)) {
478
+ this.allVictories.packetsSent.push(event.body)
479
+ }
480
+ await Promise.resolve()
481
+ }
482
+
483
+ isMdPacketSent(md: Md): md is MdPacketSent {
484
+ return (
485
+ 'nativeFee' in md &&
486
+ 'zroFee' in md &&
487
+ 'extraOptions' in md &&
488
+ 'enforcedOptions' in md &&
489
+ 'packetEncoded' in md &&
490
+ 'nonce' in md
491
+ )
492
+ }
493
+ }
494
+ `;
495
+ fs__namespace.writeFileSync(path__namespace.join(directory, "LzEventHandler.ts"), savedCode);
496
+ }
497
+ function saveClasslibWrapper(directory, baseWrapper) {
350
498
  if (baseWrapper === void 0) {
351
499
  baseWrapper = `import { BaseWrapper, SendRequestOptions } from '@layerzerolabs/ton-sdk-tools'`;
352
500
  }
@@ -377,10 +525,10 @@ export type GetIntFnNames =
377
525
  | 'cl::get<bool>'
378
526
  | 'cl::get<address>'
379
527
 
380
- export class TonContractWrapper extends BaseWrapper {
381
- static create(code: Cell, data: Cell, workchain = 0): TonContractWrapper {
528
+ export class ClasslibWrapper extends BaseWrapper {
529
+ static create(code: Cell, data: Cell, workchain = 0): ClasslibWrapper {
382
530
  const init = { code, data }
383
- return new TonContractWrapper(contractAddress(workchain, init), init)
531
+ return new ClasslibWrapper(contractAddress(workchain, init), init)
384
532
  }
385
533
 
386
534
  buildRequest(
@@ -507,8 +655,46 @@ export class TonContractWrapper extends BaseWrapper {
507
655
  const ret = await provider.get('cl::dict256::setRef', args)
508
656
  return ret.stack.readCell()
509
657
  }
658
+ }
659
+ `;
660
+ fs__namespace.writeFileSync(path__namespace.join(directory, "ClasslibWrapper.ts"), savedCode);
661
+ }
662
+ function saveTonContractWrapper(tonObjects, directory) {
663
+ const generatedMethods = Object.entries(tonObjects).map(([_, tonObject]) => tonObject.constructorCode ?? "").join("\n");
664
+ const savedCode = `${file_signature_header}
665
+
666
+ import {
667
+ Address,
668
+ Cell,
669
+ ContractProvider,
670
+ ContractState,
671
+ Sender,
672
+ SenderArguments,
673
+ TupleItem,
674
+ TupleReader,
675
+ contractAddress,
676
+ beginCell,
677
+ } from '@ton/core'
678
+
679
+ import { ClasslibWrapper } from './ClasslibWrapper'
680
+
681
+ export class TonContractWrapper extends ClasslibWrapper {
682
+ static create(code: Cell, data: Cell, workchain = 0): TonContractWrapper {
683
+ const init = { code, data }
684
+ return new TonContractWrapper(contractAddress(workchain, init), init)
685
+ }
686
+
687
+ static bigintToAsciiString(num: bigint): string {
688
+ let result = ''
689
+ while (num > 0n) {
690
+ const charCode = Number(num & 255n)
691
+ result = String.fromCharCode(charCode) + result
692
+ num = num >> 8n
693
+ }
694
+ return result
695
+ }
510
696
 
511
- ${generatedMethods}
697
+ ${generatedMethods}
512
698
  }
513
699
  `;
514
700
  fs__namespace.writeFileSync(path__namespace.join(directory, "TonContractWrapper.ts"), savedCode);
@@ -574,6 +760,11 @@ export class LzDict {
574
760
  }
575
761
  }
576
762
 
763
+ export async function getLzDict(obj: Cell, fieldName: bigint, wrapper: ExtendedContract<TonContractWrapper>): Promise<LzDict> {
764
+ const dictCell = await wrapper.getClDict(obj, fieldName)
765
+ return new LzDict(dictCell)
766
+ }
767
+
577
768
  export interface Md {}
578
769
 
579
770
  ${generatedTypes}
@@ -582,14 +773,8 @@ ${generatedNameMaps}
582
773
  `;
583
774
  fs__namespace.writeFileSync(path__namespace.join(directory, "allTypes.ts"), savedCode);
584
775
  }
585
- function saveTonObjectUnwrapper(tonObjects, directory) {
586
- const allTypes = [
587
- ...Object.values(tonObjects).map((obj) => obj.tsTypeName).filter((name) => name !== void 0),
588
- "ExtendedContract",
589
- "LzDict",
590
- "Md",
591
- "nameMap"
592
- ].sort();
776
+ function saveLzGasTracker(directory, opcodesImportPath) {
777
+ opcodesImportPath = opcodesImportPath ?? "../constants";
593
778
  const savedCode = `${file_signature_header}
594
779
  /* eslint-disable @typescript-eslint/no-unsafe-assignment */
595
780
  /* eslint-disable @typescript-eslint/no-unnecessary-condition */
@@ -598,106 +783,9 @@ import { Cell, Transaction, TransactionComputeVm, TransactionDescriptionGeneric,
598
783
  import { BlockchainTransaction, EventMessageSent, SendMessageResult } from '@ton/sandbox'
599
784
  import { flattenTransaction } from '@ton/test-utils'
600
785
 
601
- import { OPCODES } from '../constants'
602
-
603
- import {
604
- ${allTypes.map((type) => " " + type + ",\n").join("")}} from './allTypes'
605
- import { TonContractWrapper } from './TonContractWrapper'
606
-
607
- function bigintToAsciiString(num: bigint): string {
608
- let result = ''
609
- while (num > 0n) {
610
- const charCode = Number(num & 255n)
611
- result = String.fromCharCode(charCode) + result
612
- num = num >> 8n
613
- }
614
- return result
615
- }
616
-
617
- export class LzEvent {
618
- private _eventPromise: Promise<ActionEvent>
619
- private _event: ActionEvent | null
620
- public topic: string
621
- public body: Md
622
- public bodyName: string
623
- public initialStorage: Cell
624
- public initialized: boolean
625
-
626
- constructor(wrapper: ExtendedContract<TonContractWrapper>, cell: Cell) {
627
- this._eventPromise = TonObjectUnwrapper.getActionEventToTS(wrapper, cell)
628
- this._event = null
629
- this.topic = ''
630
- this.body = {}
631
- this.initialStorage = beginCell().endCell()
632
- this.initialized = false
633
- this.bodyName = ''
634
- }
635
-
636
- async load(wrapper: ExtendedContract<TonContractWrapper>): Promise<boolean> {
637
- this._event = await this._eventPromise
638
- this.topic = bigintToAsciiString(this._event.topic)
639
- this.bodyName = await TonObjectUnwrapper.getTypeOf(wrapper, this._event.body)
640
- const tsCasterName = 'get' + nameMap[this.bodyName] + 'ToTS'
641
-
642
- const method = TonObjectUnwrapper[tsCasterName as keyof typeof TonObjectUnwrapper]
643
- if (typeof method === 'function') {
644
- this.body = await method(wrapper, this._event.body)
645
- } else {
646
- console.log('Method' + tsCasterName + 'does not exist.')
647
- }
648
-
649
- this.initialStorage = this._event.initialStorage
650
- this.initialized = true
651
- return this.initialized
652
- }
653
- }
786
+ import { OPCODES } from '${opcodesImportPath}'
654
787
 
655
- export class LzEventHandler {
656
- public allFailures = {
657
- txList: [] as BlockchainTransaction[],
658
- events: [] as EventMessageSent[],
659
- }
660
- public allVictories = {
661
- txList: [] as BlockchainTransaction[],
662
- eventObjects: [] as LzEvent[],
663
- packetsSent: [] as MdPacketSent[],
664
- }
665
- async addCurrentEventsToAllEvents(results: SendMessageResult, contract: ExtendedContract<TonContractWrapper>): Promise<void> {
666
- for (let i = 0; i < results.transactions.length; i++) {
667
- const tx: BlockchainTransaction & Transaction = results.transactions[i]
668
- const desc = tx.description
669
- if (desc.type !== 'generic' || desc.computePhase.type !== 'skipped') continue
670
-
671
- const eventBody = tx.inMessage?.body
672
- if (eventBody === undefined) continue
673
- const eventBodySlice = eventBody.beginParse()
674
- // the event body is empty if we are sending a payment with no corresponding payload
675
- if (eventBodySlice.remainingBits < 32) continue
676
- const opCode = eventBodySlice.loadUint(32)
677
- if (opCode === Number(OPCODES.BaseInterface_OP_EVENT)) {
678
- if (eventBody.refs.length > 0) {
679
- const eventInnerCell = eventBody.refs[0]
680
- const newLzEvent = new LzEvent(contract, eventInnerCell)
681
- await newLzEvent.load(contract)
682
- this.allVictories.txList.push(tx)
683
- this.allVictories.eventObjects.push(newLzEvent)
684
- if (newLzEvent.topic === 'Channel::event::PACKET_SENT' && this.isMdPacketSent(newLzEvent.body)) {
685
- this.allVictories.packetsSent.push(newLzEvent.body)
686
- // getMdLzSendToTS(contract, newLzEvent.body.lzSend)
687
- }
688
- }
689
- } else {
690
- this.allFailures.txList.push(tx)
691
- this.allFailures.events.push(results.events[i] as EventMessageSent)
692
- }
693
- }
694
- }
695
- isMdPacketSent(md: Md): md is MdPacketSent {
696
- return 'nativeFee' in md && 'zroFee' in md && 'extraOptions' in md && 'enforcedOptions' in md && 'packetEncoded' in md && 'nonce' in md
697
- }
698
- }
699
-
700
- interface GasStructure {
788
+ export interface GasStructure {
701
789
  actionFee: bigint
702
790
  forwardFee: bigint
703
791
  computeFee: bigint
@@ -813,17 +901,35 @@ export class LzGasTracker {
813
901
  // Log the entire table
814
902
  return tableString
815
903
  }
904
+ }`;
905
+ fs__namespace.writeFileSync(path__namespace.join(directory, "LzGasTracker.ts"), savedCode);
816
906
  }
907
+ function saveTonObjectUnwrapper(tonObjects, directory) {
908
+ const allTypes = [
909
+ ...Object.values(tonObjects).map((obj) => obj.tsTypeName).filter((name) => name !== void 0),
910
+ "ExtendedContract",
911
+ "LzDict",
912
+ "Md",
913
+ "nameMap"
914
+ ].sort();
915
+ const savedCode = `${file_signature_header}
916
+ /* eslint-disable @typescript-eslint/no-unsafe-assignment */
917
+ /* eslint-disable @typescript-eslint/no-unnecessary-condition */
817
918
 
818
- async function getLzDict( obj: Cell, fieldName: bigint, wrapper: ExtendedContract<TonContractWrapper>): Promise<LzDict> {
819
- const dictCell = await wrapper.getClDict(obj, fieldName)
820
- return new LzDict(dictCell)
821
- }
919
+ import { Cell, Transaction, TransactionComputeVm, TransactionDescriptionGeneric, beginCell } from '@ton/core'
920
+ import { BlockchainTransaction, EventMessageSent, SendMessageResult } from '@ton/sandbox'
921
+ import { flattenTransaction } from '@ton/test-utils'
922
+
923
+ import { OPCODES } from '../constants'
924
+
925
+ import { getLzDict,
926
+ ${allTypes.map((type) => " " + type + ",\n").join("")}} from './allTypes'
927
+ import { TonContractWrapper } from './TonContractWrapper'
822
928
 
823
929
  export class TonObjectUnwrapper {
824
930
  static async getTypeOf(provider: ExtendedContract<TonContractWrapper>, obj_cell: Cell): Promise<string> {
825
931
  const ret = await provider.getViewFunction('cl::typeof', [{ type: 'cell', cell: obj_cell }])
826
- return bigintToAsciiString(ret.readBigNumber())
932
+ return TonContractWrapper.bigintToAsciiString(ret.readBigNumber())
827
933
  }
828
934
  ${Array.from(
829
935
  new Map(
@@ -6269,8 +6375,12 @@ exports.orderConfigToCell = orderConfigToCell;
6269
6375
  exports.parseDirectory = parseDirectory;
6270
6376
  exports.printTransactionTrace = printTransactionTrace;
6271
6377
  exports.saveAllTypes = saveAllTypes;
6378
+ exports.saveBaseEventHandler = saveBaseEventHandler;
6379
+ exports.saveClasslibWrapper = saveClasslibWrapper;
6272
6380
  exports.saveConstantsFile = saveConstantsFile;
6273
6381
  exports.saveEventsFile = saveEventsFile;
6382
+ exports.saveLzEventHandler = saveLzEventHandler;
6383
+ exports.saveLzGasTracker = saveLzGasTracker;
6274
6384
  exports.saveObjectsAsTS = saveObjectsAsTS;
6275
6385
  exports.saveTonContractWrapper = saveTonContractWrapper;
6276
6386
  exports.saveTonObjectUnwrapper = saveTonObjectUnwrapper;