@tonappchain/sdk 0.6.6-mainnet-alpha → 0.7.0-rc11

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 (97) hide show
  1. package/LICENSE +20 -20
  2. package/README.md +191 -191
  3. package/dist/adapters/contractOpener.d.ts +2 -1
  4. package/dist/adapters/contractOpener.js +6 -6
  5. package/dist/adapters/retryableContractOpener.d.ts +6 -2
  6. package/dist/adapters/retryableContractOpener.js +2 -2
  7. package/dist/assets/AssetCache.d.ts +23 -0
  8. package/dist/assets/AssetCache.js +36 -0
  9. package/dist/assets/AssetFactory.d.ts +7 -0
  10. package/dist/assets/AssetFactory.js +46 -0
  11. package/dist/assets/FT.d.ts +58 -0
  12. package/dist/assets/FT.js +231 -0
  13. package/dist/assets/NFT.d.ts +65 -0
  14. package/dist/assets/NFT.js +210 -0
  15. package/dist/assets/TON.d.ts +38 -0
  16. package/dist/assets/TON.js +91 -0
  17. package/dist/assets/index.d.ts +4 -0
  18. package/dist/assets/index.js +11 -0
  19. package/dist/errors/errors.d.ts +6 -0
  20. package/dist/errors/errors.js +15 -1
  21. package/dist/errors/index.d.ts +2 -2
  22. package/dist/errors/index.js +26 -22
  23. package/dist/errors/instances.d.ts +4 -1
  24. package/dist/errors/instances.js +11 -2
  25. package/dist/index.d.ts +7 -0
  26. package/dist/index.js +14 -1
  27. package/dist/interfaces/Asset.d.ts +73 -0
  28. package/dist/interfaces/Asset.js +2 -0
  29. package/dist/interfaces/ContractOpener.d.ts +21 -0
  30. package/dist/interfaces/ContractOpener.js +2 -0
  31. package/dist/interfaces/IConfiguration.d.ts +35 -0
  32. package/dist/interfaces/IConfiguration.js +2 -0
  33. package/dist/interfaces/IHttpClient.d.ts +16 -0
  34. package/dist/interfaces/IHttpClient.js +2 -0
  35. package/dist/interfaces/ILiteSequencerClient.d.ts +30 -0
  36. package/dist/interfaces/ILiteSequencerClient.js +2 -0
  37. package/dist/interfaces/ILiteSequencerClientFactory.d.ts +9 -0
  38. package/dist/interfaces/ILiteSequencerClientFactory.js +2 -0
  39. package/dist/interfaces/ILogger.d.ts +10 -0
  40. package/dist/interfaces/ILogger.js +2 -0
  41. package/dist/interfaces/IOperationTracker.d.ts +66 -0
  42. package/dist/interfaces/IOperationTracker.js +2 -0
  43. package/dist/interfaces/ISimulator.d.ts +47 -0
  44. package/dist/interfaces/ISimulator.js +2 -0
  45. package/dist/interfaces/ITacSDK.d.ts +147 -0
  46. package/dist/interfaces/ITacSDK.js +2 -0
  47. package/dist/interfaces/ITransactionManager.d.ts +35 -0
  48. package/dist/interfaces/ITransactionManager.js +2 -0
  49. package/dist/interfaces/SenderAbstraction.d.ts +35 -0
  50. package/dist/interfaces/SenderAbstraction.js +2 -0
  51. package/dist/interfaces/WalletInstanse.d.ts +20 -0
  52. package/dist/interfaces/WalletInstanse.js +2 -0
  53. package/dist/interfaces/index.d.ts +13 -0
  54. package/dist/interfaces/index.js +29 -0
  55. package/dist/sdk/AxiosHttpClient.d.ts +12 -0
  56. package/dist/sdk/AxiosHttpClient.js +23 -0
  57. package/dist/sdk/Configuration.d.ts +21 -0
  58. package/dist/sdk/Configuration.js +90 -0
  59. package/dist/sdk/LiteSequencerClient.d.ts +6 -2
  60. package/dist/sdk/LiteSequencerClient.js +58 -14
  61. package/dist/sdk/Logger.d.ts +13 -0
  62. package/dist/sdk/Logger.js +25 -0
  63. package/dist/sdk/OperationTracker.d.ts +10 -5
  64. package/dist/sdk/OperationTracker.js +87 -45
  65. package/dist/sdk/Simulator.d.ts +17 -0
  66. package/dist/sdk/Simulator.js +163 -0
  67. package/dist/sdk/StartTracking.d.ts +6 -0
  68. package/dist/sdk/StartTracking.js +69 -32
  69. package/dist/sdk/TacSdk.d.ts +26 -43
  70. package/dist/sdk/TacSdk.js +121 -816
  71. package/dist/sdk/TransactionManager.d.ts +22 -0
  72. package/dist/sdk/TransactionManager.js +272 -0
  73. package/dist/sdk/TxFinalizer.d.ts +10 -0
  74. package/dist/sdk/TxFinalizer.js +104 -0
  75. package/dist/sdk/Utils.d.ts +7 -2
  76. package/dist/sdk/Utils.js +43 -24
  77. package/dist/sdk/Validator.d.ts +9 -0
  78. package/dist/sdk/Validator.js +43 -0
  79. package/dist/sender/BatchSender.d.ts +6 -4
  80. package/dist/sender/BatchSender.js +18 -6
  81. package/dist/sender/RawSender.d.ts +10 -5
  82. package/dist/sender/RawSender.js +46 -18
  83. package/dist/sender/SenderFactory.d.ts +1 -1
  84. package/dist/sender/SenderFactory.js +5 -4
  85. package/dist/sender/TonConnectSender.d.ts +6 -4
  86. package/dist/sender/TonConnectSender.js +14 -10
  87. package/dist/sender/index.d.ts +2 -2
  88. package/dist/sender/index.js +2 -2
  89. package/dist/structs/InternalStruct.d.ts +51 -32
  90. package/dist/structs/Struct.d.ts +88 -90
  91. package/dist/structs/Struct.js +11 -1
  92. package/dist/wrappers/HighloadQueryId.js +0 -1
  93. package/dist/wrappers/HighloadWalletV3.d.ts +4 -3
  94. package/dist/wrappers/HighloadWalletV3.js +5 -2
  95. package/package.json +67 -67
  96. package/dist/sender/SenderAbstraction.d.ts +0 -20
  97. package/dist/sender/SenderAbstraction.js +0 -5
@@ -0,0 +1,58 @@
1
+ import { SandboxContract } from '@ton/sandbox';
2
+ import { Cell, OpenedContract } from '@ton/ton';
3
+ import { AssetOpType } from '../structs/InternalStruct';
4
+ import { Asset, IConfiguration, ContractOpener } from '../interfaces';
5
+ import { AssetType, EVMAddress, FeeParams, TVMAddress, UserWalletBalanceExtended } from '../structs/Struct';
6
+ import { Origin } from '../structs/Struct';
7
+ import { JettonMasterData } from '../wrappers/JettonMaster';
8
+ import { JettonWallet } from '../wrappers/JettonWallet';
9
+ export declare class FT implements Asset {
10
+ private _tvmAddress;
11
+ readonly type: AssetType;
12
+ readonly origin: Origin;
13
+ private _configuration;
14
+ private _jettonMinter;
15
+ private _decimals?;
16
+ private _transferAmount;
17
+ private _evmAddress?;
18
+ get address(): string;
19
+ static getJettonData(contractOpener: ContractOpener, address: TVMAddress): Promise<JettonMasterData>;
20
+ getJettonData(): Promise<JettonMasterData>;
21
+ static getOrigin(configuration: IConfiguration, address: TVMAddress): Promise<Origin>;
22
+ static getTVMAddress(configuration: IConfiguration, address: EVMAddress): Promise<string>;
23
+ static getEVMAddress(configuration: IConfiguration, address: TVMAddress): Promise<string>;
24
+ private constructor();
25
+ static fromAddress(configuration: IConfiguration, address: TVMAddress | EVMAddress): Promise<FT>;
26
+ get rawAmount(): bigint;
27
+ get clone(): FT;
28
+ withAmount(amount: {
29
+ rawAmount: bigint;
30
+ } | {
31
+ amount: number;
32
+ }): Promise<FT>;
33
+ addAmount(amount: {
34
+ rawAmount: bigint;
35
+ } | {
36
+ amount: number;
37
+ }): Promise<FT>;
38
+ getDecimals(): Promise<number>;
39
+ getEVMAddress(): Promise<string>;
40
+ getTVMAddress(): Promise<string>;
41
+ generatePayload(params: {
42
+ excessReceiver: string;
43
+ evmData: Cell;
44
+ crossChainTonAmount?: bigint;
45
+ forwardFeeTonAmount?: bigint;
46
+ feeParams?: FeeParams;
47
+ }): Promise<Cell>;
48
+ get opType(): AssetOpType.JETTON_BURN | AssetOpType.JETTON_TRANSFER;
49
+ getWallet(userAddress: string): Promise<OpenedContract<JettonWallet> | SandboxContract<JettonWallet>>;
50
+ getUserWalletAddress(userAddress: string): Promise<string>;
51
+ getUserBalance(userAddress: string): Promise<bigint>;
52
+ getUserBalanceExtended(userAddress: string): Promise<UserWalletBalanceExtended>;
53
+ checkBalance(userAddress: string): Promise<void>;
54
+ checkCanBeTransferredBy(userAddress: string): Promise<void>;
55
+ getBalanceOf(userAddress: string): Promise<bigint>;
56
+ private getTransferPayload;
57
+ private getBurnPayload;
58
+ }
@@ -0,0 +1,231 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.FT = void 0;
4
+ const ton_1 = require("@ton/ton");
5
+ const ethers_1 = require("ethers");
6
+ const errors_1 = require("../errors");
7
+ const Consts_1 = require("../sdk/Consts");
8
+ const Utils_1 = require("../sdk/Utils");
9
+ const Validator_1 = require("../sdk/Validator");
10
+ const InternalStruct_1 = require("../structs/InternalStruct");
11
+ const Struct_1 = require("../structs/Struct");
12
+ const Struct_2 = require("../structs/Struct");
13
+ const JettonMaster_1 = require("../wrappers/JettonMaster");
14
+ const JettonWallet_1 = require("../wrappers/JettonWallet");
15
+ class FT {
16
+ get address() {
17
+ return this._tvmAddress.toString({ bounceable: true });
18
+ }
19
+ static async getJettonData(contractOpener, address) {
20
+ Validator_1.Validator.validateTVMAddress(address);
21
+ const jetton = contractOpener.open(JettonMaster_1.JettonMaster.createFromAddress(ton_1.Address.parse(address)));
22
+ return jetton.getJettonData();
23
+ }
24
+ async getJettonData() {
25
+ return FT.getJettonData(this._configuration.TONParams.contractOpener, this._tvmAddress.toString());
26
+ }
27
+ static async getOrigin(configuration, address) {
28
+ const { jettonMinterCode, crossChainLayerAddress, jettonWalletCode } = configuration.TONParams;
29
+ const { code: thisCodeBOC } = await configuration.TONParams.contractOpener.getContractState(ton_1.Address.parse(address));
30
+ if (!thisCodeBOC) {
31
+ throw errors_1.emptyContractError;
32
+ }
33
+ const thisCode = ton_1.Cell.fromBoc(thisCodeBOC)[0];
34
+ if (!jettonMinterCode.equals(thisCode)) {
35
+ return Struct_2.Origin.TON;
36
+ }
37
+ const jettonMinter = configuration.TONParams.contractOpener.open(JettonMaster_1.JettonMaster.createFromAddress(ton_1.Address.parse(address)));
38
+ const evmAddress = await jettonMinter.getEVMAddress();
39
+ const expectedMinterAddress = await (0, Utils_1.calculateContractAddress)(jettonMinterCode, (0, ton_1.beginCell)()
40
+ .storeCoins(0)
41
+ .storeAddress(ton_1.Address.parse(crossChainLayerAddress))
42
+ .storeAddress(null)
43
+ .storeRef((0, ton_1.beginCell)().endCell())
44
+ .storeRef(jettonWalletCode)
45
+ .storeStringTail(evmAddress)
46
+ .endCell());
47
+ if (!expectedMinterAddress.equals(ton_1.Address.parse(address))) {
48
+ return Struct_2.Origin.TON;
49
+ }
50
+ return Struct_2.Origin.TAC;
51
+ }
52
+ static async getTVMAddress(configuration, address) {
53
+ Validator_1.Validator.validateEVMAddress(address);
54
+ // If token is TON native
55
+ const fromTVM = await configuration.TACParams.tokenUtils['exists(address)'](address);
56
+ if (fromTVM) {
57
+ const erc20Token = configuration.artifacts.tac.wrappers.CrossChainLayerERC20FactoryTAC.connect(address, configuration.TACParams.provider);
58
+ const info = await erc20Token.getInfo();
59
+ return info.tvmAddress;
60
+ }
61
+ // If token is TAC native
62
+ const jettonMaster = JettonMaster_1.JettonMaster.createFromConfig({
63
+ evmTokenAddress: address,
64
+ crossChainLayerAddress: ton_1.Address.parse(configuration.TONParams.crossChainLayerAddress),
65
+ code: configuration.TONParams.jettonMinterCode,
66
+ walletCode: configuration.TONParams.jettonWalletCode,
67
+ });
68
+ return jettonMaster.address.toString();
69
+ }
70
+ static async getEVMAddress(configuration, address) {
71
+ const tokenAddressString = ton_1.Address.parse(address).toString({ bounceable: true });
72
+ const origin = await FT.getOrigin(configuration, address);
73
+ if (origin === Struct_2.Origin.TON) {
74
+ return configuration.TACParams.tokenUtils.computeAddress(tokenAddressString);
75
+ }
76
+ else {
77
+ const givenMinter = configuration.TONParams.contractOpener.open(new JettonMaster_1.JettonMaster(ton_1.Address.parse(tokenAddressString)));
78
+ return givenMinter.getEVMAddress();
79
+ }
80
+ }
81
+ constructor(address, origin, configuration) {
82
+ this.type = Struct_1.AssetType.FT;
83
+ this._tvmAddress = ton_1.Address.parse(address);
84
+ this._configuration = configuration;
85
+ this._jettonMinter = this._configuration.TONParams.contractOpener.open(new JettonMaster_1.JettonMaster(this._tvmAddress));
86
+ this.origin = origin;
87
+ this._transferAmount = 0n;
88
+ }
89
+ static async fromAddress(configuration, address) {
90
+ const tvmAddress = (0, ethers_1.isAddress)(address) ? await this.getTVMAddress(configuration, address) : address;
91
+ const origin = await FT.getOrigin(configuration, tvmAddress).catch((e) => {
92
+ if (e instanceof errors_1.ContractError) {
93
+ return Struct_2.Origin.TAC;
94
+ }
95
+ throw e;
96
+ });
97
+ const token = new FT(tvmAddress, origin, configuration);
98
+ if ((0, ethers_1.isAddress)(address)) {
99
+ token._evmAddress = address;
100
+ }
101
+ return token;
102
+ }
103
+ get rawAmount() {
104
+ return this._transferAmount;
105
+ }
106
+ get clone() {
107
+ const ft = new FT(this._tvmAddress.toString(), this.origin, this._configuration);
108
+ ft._transferAmount = this._transferAmount;
109
+ ft._evmAddress = this._evmAddress;
110
+ ft._decimals = this._decimals;
111
+ return ft;
112
+ }
113
+ async withAmount(amount) {
114
+ if (this._transferAmount > 0n) {
115
+ // clone token if withAmount set before to avoid changing the original token
116
+ const newToken = this.clone;
117
+ newToken._transferAmount =
118
+ 'rawAmount' in amount ? amount.rawAmount : (0, Utils_1.calculateRawAmount)(amount.amount, await this.getDecimals());
119
+ return newToken;
120
+ }
121
+ if ('rawAmount' in amount) {
122
+ this._transferAmount = amount.rawAmount;
123
+ }
124
+ else {
125
+ const decimals = await this.getDecimals();
126
+ this._transferAmount = (0, Utils_1.calculateRawAmount)(amount.amount, decimals);
127
+ }
128
+ return this;
129
+ }
130
+ async addAmount(amount) {
131
+ if ('rawAmount' in amount) {
132
+ this._transferAmount = this._transferAmount + amount.rawAmount;
133
+ }
134
+ else {
135
+ const decimals = await this.getDecimals();
136
+ this._transferAmount = this._transferAmount + (0, Utils_1.calculateRawAmount)(amount.amount, decimals);
137
+ }
138
+ return this;
139
+ }
140
+ async getDecimals() {
141
+ if (!this._decimals) {
142
+ const decimalsRaw = (await this._jettonMinter.getJettonData()).content.metadata.decimals;
143
+ this._decimals = decimalsRaw ? Number(decimalsRaw) : 9;
144
+ }
145
+ return this._decimals;
146
+ }
147
+ async getEVMAddress() {
148
+ if (this._evmAddress) {
149
+ return this._evmAddress;
150
+ }
151
+ const tokenAddressString = this._tvmAddress.toString({ bounceable: true });
152
+ if (this.origin === Struct_2.Origin.TON) {
153
+ this._evmAddress = await this._configuration.TACParams.tokenUtils.computeAddress(tokenAddressString);
154
+ }
155
+ else if (this.origin === Struct_2.Origin.TAC) {
156
+ const givenMinter = this._configuration.TONParams.contractOpener.open(new JettonMaster_1.JettonMaster(ton_1.Address.parse(tokenAddressString)));
157
+ this._evmAddress = await givenMinter.getEVMAddress();
158
+ }
159
+ else {
160
+ throw (0, errors_1.unknownTokenTypeError)(tokenAddressString, 'Token origin is neither TON nor TAC');
161
+ }
162
+ return this._evmAddress;
163
+ }
164
+ async getTVMAddress() {
165
+ return this._tvmAddress.toString({ bounceable: true });
166
+ }
167
+ async generatePayload(params) {
168
+ const { excessReceiver, evmData, crossChainTonAmount = 0n, forwardFeeTonAmount = 0n, feeParams } = params;
169
+ const feeData = (0, Utils_1.generateFeeData)(feeParams);
170
+ let payload;
171
+ switch (this.opType) {
172
+ case InternalStruct_1.AssetOpType.JETTON_BURN:
173
+ payload = this.getBurnPayload(this._transferAmount, this._configuration.TONParams.crossChainLayerAddress, evmData, crossChainTonAmount, feeData);
174
+ break;
175
+ case InternalStruct_1.AssetOpType.JETTON_TRANSFER:
176
+ payload = this.getTransferPayload(this._transferAmount, this._configuration.TONParams.jettonProxyAddress, excessReceiver, evmData, crossChainTonAmount, forwardFeeTonAmount, feeData);
177
+ break;
178
+ }
179
+ return payload;
180
+ }
181
+ get opType() {
182
+ return this.origin === Struct_2.Origin.TAC ? InternalStruct_1.AssetOpType.JETTON_BURN : InternalStruct_1.AssetOpType.JETTON_TRANSFER;
183
+ }
184
+ async getWallet(userAddress) {
185
+ const walletAddress = await this.getUserWalletAddress(userAddress);
186
+ return this._configuration.TONParams.contractOpener.open(JettonWallet_1.JettonWallet.createFromAddress(ton_1.Address.parse(walletAddress)));
187
+ }
188
+ async getUserWalletAddress(userAddress) {
189
+ return this._jettonMinter.getWalletAddress(userAddress);
190
+ }
191
+ async getUserBalance(userAddress) {
192
+ const wallet = await this.getWallet(userAddress);
193
+ return BigInt(await wallet.getJettonBalance());
194
+ }
195
+ async getUserBalanceExtended(userAddress) {
196
+ const masterState = await this._configuration.TONParams.contractOpener.getContractState(this._tvmAddress);
197
+ if (masterState.state !== 'active') {
198
+ return { exists: false };
199
+ }
200
+ const wallet = await this.getWallet(userAddress);
201
+ const rawAmount = await wallet.getJettonBalance();
202
+ const decimals = await this.getDecimals();
203
+ return {
204
+ rawAmount,
205
+ decimals,
206
+ amount: (0, Utils_1.calculateAmount)(rawAmount, decimals),
207
+ exists: true,
208
+ };
209
+ }
210
+ async checkBalance(userAddress) {
211
+ const balance = await this.getUserBalance(userAddress);
212
+ if (balance < this._transferAmount) {
213
+ throw (0, errors_1.insufficientBalanceError)(this._tvmAddress.toString());
214
+ }
215
+ }
216
+ async checkCanBeTransferredBy(userAddress) {
217
+ await this.checkBalance(userAddress);
218
+ }
219
+ async getBalanceOf(userAddress) {
220
+ return this.getUserBalance(userAddress);
221
+ }
222
+ getTransferPayload(rawAmount, notificationReceiverAddress, responseAddress, evmData, crossChainTonAmount, forwardFeeAmount, feeData) {
223
+ const queryId = (0, Utils_1.generateRandomNumberByTimestamp)().randomNumber;
224
+ return JettonWallet_1.JettonWallet.transferMessage(rawAmount, notificationReceiverAddress, responseAddress, Consts_1.JETTON_TRANSFER_FORWARD_TON_AMOUNT + forwardFeeAmount + crossChainTonAmount, crossChainTonAmount, feeData, evmData, queryId);
225
+ }
226
+ getBurnPayload(rawAmount, notificationReceiverAddress, evmData, crossChainTonAmount, feeData) {
227
+ const queryId = (0, Utils_1.generateRandomNumberByTimestamp)().randomNumber;
228
+ return JettonWallet_1.JettonWallet.burnMessage(rawAmount, notificationReceiverAddress, crossChainTonAmount, feeData, evmData, queryId);
229
+ }
230
+ }
231
+ exports.FT = FT;
@@ -0,0 +1,65 @@
1
+ import { Cell } from '@ton/ton';
2
+ import { Asset, IConfiguration, ContractOpener } from '../interfaces';
3
+ import { AssetType, EVMAddress, FeeParams, NFTCollectionData, NFTItemData, Origin, TVMAddress } from '../structs/Struct';
4
+ export declare class NFT implements Asset {
5
+ private _addresses;
6
+ readonly origin: Origin;
7
+ readonly type: AssetType;
8
+ private _nftCollection;
9
+ private _configuration;
10
+ /**
11
+ * @description Create NFT from item address. Item MUST BE deployed on TON.
12
+ * @param configuration - Configuration
13
+ * @param item - Item address (TVM address)
14
+ * @returns NFT
15
+ */
16
+ static fromItem(configuration: IConfiguration, item: TVMAddress): Promise<NFT>;
17
+ /**
18
+ * @description Create NFT from collection address. TON-native assets MUST BE deployed on TON.
19
+ * @param configuration - Configuration
20
+ * @param item - Item address (TVM address)
21
+ * @returns NFT
22
+ */
23
+ static fromCollection(configuration: IConfiguration, item: {
24
+ collection: TVMAddress | EVMAddress;
25
+ index: bigint;
26
+ }): Promise<NFT>;
27
+ static getItemData(contractOpener: ContractOpener, itemAddress: TVMAddress): Promise<NFTItemData>;
28
+ getItemData(): Promise<NFTItemData>;
29
+ static getCollectionData(contractOpener: ContractOpener, collectionAddress: TVMAddress): Promise<NFTCollectionData>;
30
+ getCollectionData(): Promise<NFTCollectionData>;
31
+ static getOrigin(configuration: IConfiguration, itemOrCollection: TVMAddress): Promise<Origin>;
32
+ static getItemAddress(contractOpener: ContractOpener, collectionAddress: TVMAddress, index: bigint): Promise<string>;
33
+ static getTVMAddress(configuration: IConfiguration, collectionAddress: EVMAddress, tokenId?: bigint): Promise<string>;
34
+ constructor(nftAddress: {
35
+ item: TVMAddress;
36
+ collection: TVMAddress;
37
+ index: bigint;
38
+ evmAddress?: EVMAddress;
39
+ }, origin: Origin, configuration: IConfiguration);
40
+ get addresses(): {
41
+ item: string;
42
+ collection: string;
43
+ index: bigint;
44
+ evmAddress?: string;
45
+ };
46
+ get address(): string;
47
+ get rawAmount(): bigint;
48
+ get clone(): NFT;
49
+ withAmount(): Promise<NFT>;
50
+ addAmount(): Promise<NFT>;
51
+ getEVMAddress(): Promise<string>;
52
+ getTVMAddress(): Promise<string>;
53
+ generatePayload(params: {
54
+ excessReceiver: string;
55
+ evmData: Cell;
56
+ crossChainTonAmount?: bigint;
57
+ forwardFeeTonAmount?: bigint;
58
+ feeParams?: FeeParams;
59
+ }): Promise<Cell>;
60
+ isOwnedBy(userAddress: string): Promise<boolean>;
61
+ checkCanBeTransferredBy(userAddress: string): Promise<void>;
62
+ getBalanceOf(userAddress: string): Promise<bigint>;
63
+ private getBurnPayload;
64
+ private getTransferPayload;
65
+ }
@@ -0,0 +1,210 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.NFT = void 0;
4
+ const ton_1 = require("@ton/ton");
5
+ const wrappers_1 = require("@tonappchain/artifacts/dist/src/ton/wrappers");
6
+ const ethers_1 = require("ethers");
7
+ const errors_1 = require("../errors");
8
+ const Consts_1 = require("../sdk/Consts");
9
+ const Utils_1 = require("../sdk/Utils");
10
+ const Validator_1 = require("../sdk/Validator");
11
+ const InternalStruct_1 = require("../structs/InternalStruct");
12
+ const Struct_1 = require("../structs/Struct");
13
+ class NFT {
14
+ /**
15
+ * @description Create NFT from item address. Item MUST BE deployed on TON.
16
+ * @param configuration - Configuration
17
+ * @param item - Item address (TVM address)
18
+ * @returns NFT
19
+ */
20
+ static async fromItem(configuration, item) {
21
+ Validator_1.Validator.validateTVMAddress(item);
22
+ const nftItem = configuration.TONParams.contractOpener.open(wrappers_1.NFTItem.createFromAddress(ton_1.Address.parse(item)));
23
+ const { collectionAddress, index } = await nftItem.getNFTData();
24
+ const origin = await NFT.getOrigin(configuration, item);
25
+ return new NFT({ item, collection: collectionAddress.toString(), index: BigInt(index) }, origin, configuration);
26
+ }
27
+ /**
28
+ * @description Create NFT from collection address. TON-native assets MUST BE deployed on TON.
29
+ * @param configuration - Configuration
30
+ * @param item - Item address (TVM address)
31
+ * @returns NFT
32
+ */
33
+ static async fromCollection(configuration, item) {
34
+ const tvmCollectionAddress = (0, ethers_1.isAddress)(item.collection)
35
+ ? await this.getTVMAddress(configuration, item.collection)
36
+ : item.collection;
37
+ const origin = await NFT.getOrigin(configuration, tvmCollectionAddress).catch((e) => {
38
+ if (e instanceof errors_1.ContractError) {
39
+ return Struct_1.Origin.TAC;
40
+ }
41
+ throw e;
42
+ });
43
+ const nftCollection = configuration.TONParams.contractOpener.open(wrappers_1.NFTCollection.createFromAddress(ton_1.Address.parse(tvmCollectionAddress)));
44
+ const itemAddress = origin === Struct_1.Origin.TAC
45
+ ? wrappers_1.NFTItem.createFromConfig({
46
+ collectionAddress: nftCollection.address,
47
+ cclAddress: ton_1.Address.parse(configuration.TONParams.crossChainLayerAddress),
48
+ // @ts-expect-error // bigint can be used, wrapper is not typed properly
49
+ index: item.index,
50
+ }, configuration.TONParams.nftItemCode).address
51
+ : await nftCollection.getNFTAddressByIndex(item.index);
52
+ return new NFT({
53
+ item: itemAddress.toString(),
54
+ collection: tvmCollectionAddress,
55
+ index: item.index,
56
+ evmAddress: (0, ethers_1.isAddress)(item.collection) ? item.collection : undefined,
57
+ }, origin, configuration);
58
+ }
59
+ static async getItemData(contractOpener, itemAddress) {
60
+ Validator_1.Validator.validateTVMAddress(itemAddress);
61
+ const nftItem = contractOpener.open(wrappers_1.NFTItem.createFromAddress(ton_1.Address.parse(itemAddress)));
62
+ return nftItem.getNFTData();
63
+ }
64
+ async getItemData() {
65
+ return NFT.getItemData(this._configuration.TONParams.contractOpener, this._addresses.item);
66
+ }
67
+ static async getCollectionData(contractOpener, collectionAddress) {
68
+ Validator_1.Validator.validateTVMAddress(collectionAddress);
69
+ const nftCollection = contractOpener.open(wrappers_1.NFTCollection.createFromAddress(ton_1.Address.parse(collectionAddress)));
70
+ return nftCollection.getCollectionData();
71
+ }
72
+ async getCollectionData() {
73
+ return NFT.getCollectionData(this._configuration.TONParams.contractOpener, this._addresses.collection);
74
+ }
75
+ static async getOrigin(configuration, itemOrCollection) {
76
+ const { nftItemCode, nftCollectionCode } = configuration.TONParams;
77
+ const { code: givenCodeBOC } = await configuration.TONParams.contractOpener.getContractState(ton_1.Address.parse(itemOrCollection));
78
+ if (!givenCodeBOC) {
79
+ throw errors_1.emptyContractError;
80
+ }
81
+ const givenNFTCode = ton_1.Cell.fromBoc(givenCodeBOC)[0];
82
+ if (nftItemCode.equals(givenNFTCode) || nftCollectionCode.equals(givenNFTCode)) {
83
+ return Struct_1.Origin.TAC;
84
+ }
85
+ return Struct_1.Origin.TON;
86
+ }
87
+ static async getItemAddress(contractOpener, collectionAddress, index) {
88
+ Validator_1.Validator.validateTVMAddress(collectionAddress);
89
+ const nftCollection = contractOpener.open(wrappers_1.NFTCollection.createFromAddress(ton_1.Address.parse(collectionAddress)));
90
+ const address = await nftCollection.getNFTAddressByIndex(index);
91
+ return address.toString();
92
+ }
93
+ static async getTVMAddress(configuration, collectionAddress, tokenId) {
94
+ Validator_1.Validator.validateEVMAddress(collectionAddress);
95
+ const exists = await configuration.TACParams.tokenUtils['exists(address)'](collectionAddress);
96
+ if (exists) {
97
+ const erc721Token = configuration.artifacts.tac.wrappers.CrossChainLayerERC721FactoryTAC.connect(collectionAddress, configuration.TACParams.provider);
98
+ const info = await erc721Token.getInfo();
99
+ const nftCollection = configuration.TONParams.contractOpener.open(wrappers_1.NFTCollection.createFromAddress((0, ton_1.address)(info.tvmAddress)));
100
+ return tokenId == undefined
101
+ ? nftCollection.address.toString()
102
+ : (await nftCollection.getNFTAddressByIndex(tokenId)).toString();
103
+ }
104
+ else {
105
+ const nftCollection = configuration.TONParams.contractOpener.open(wrappers_1.NFTCollection.createFromConfig({
106
+ adminAddress: (0, ton_1.address)(configuration.TONParams.crossChainLayerAddress),
107
+ newAdminAddress: null,
108
+ collectionContent: (0, ton_1.beginCell)().endCell(),
109
+ commonContent: (0, ton_1.beginCell)().endCell(),
110
+ nftItemCode: configuration.TONParams.nftItemCode,
111
+ originalAddress: collectionAddress,
112
+ }, configuration.TONParams.nftCollectionCode));
113
+ return tokenId == undefined
114
+ ? nftCollection.address.toString()
115
+ : wrappers_1.NFTItem.createFromConfig({
116
+ collectionAddress: nftCollection.address,
117
+ cclAddress: ton_1.Address.parse(configuration.TONParams.crossChainLayerAddress),
118
+ // @ts-expect-error // bigint can be used, wrapper is not typed properly
119
+ index: tokenId,
120
+ }, configuration.TONParams.nftItemCode).address.toString();
121
+ }
122
+ }
123
+ constructor(nftAddress, origin, configuration) {
124
+ this.type = Struct_1.AssetType.NFT;
125
+ this._addresses = nftAddress;
126
+ this._configuration = configuration;
127
+ this.origin = origin;
128
+ this._nftCollection = configuration.TONParams.contractOpener.open(wrappers_1.NFTCollection.createFromAddress(ton_1.Address.parse(this._addresses.collection)));
129
+ }
130
+ get addresses() {
131
+ return this._addresses;
132
+ }
133
+ get address() {
134
+ return this._addresses.item;
135
+ }
136
+ get rawAmount() {
137
+ return 1n;
138
+ }
139
+ get clone() {
140
+ return new NFT(this._addresses, this.origin, this._configuration);
141
+ }
142
+ async withAmount() {
143
+ return this;
144
+ }
145
+ async addAmount() {
146
+ return this;
147
+ }
148
+ async getEVMAddress() {
149
+ if (this._addresses.evmAddress) {
150
+ return this._addresses.evmAddress;
151
+ }
152
+ const tvmNFTAddress = ton_1.Address.parse(this._addresses.collection).toString({ bounceable: true });
153
+ const { code: givenNFTCollection } = await this._configuration.TONParams.contractOpener.getContractState(ton_1.Address.parse(tvmNFTAddress));
154
+ if (givenNFTCollection &&
155
+ this._configuration.TONParams.nftCollectionCode.equals(ton_1.Cell.fromBoc(givenNFTCollection)[0])) {
156
+ const nftCollection = this._configuration.TONParams.contractOpener.open(wrappers_1.NFTCollection.createFromAddress((0, ton_1.address)(tvmNFTAddress)));
157
+ const evmAddress = await nftCollection.getOriginalAddress();
158
+ this._addresses.evmAddress = evmAddress.toString();
159
+ }
160
+ else {
161
+ this._addresses.evmAddress =
162
+ await this._configuration.TACParams.tokenUtils.computeAddressERC721(tvmNFTAddress);
163
+ }
164
+ return this._addresses.evmAddress;
165
+ }
166
+ async getTVMAddress() {
167
+ return this.address.toString();
168
+ }
169
+ async generatePayload(params) {
170
+ const { excessReceiver, evmData, crossChainTonAmount = 0n, forwardFeeTonAmount = 0n, feeParams } = params;
171
+ const opType = this.origin === Struct_1.Origin.TAC ? InternalStruct_1.AssetOpType.NFT_BURN : InternalStruct_1.AssetOpType.NFT_TRANSFER;
172
+ const feeData = (0, Utils_1.generateFeeData)(feeParams);
173
+ let payload;
174
+ switch (opType) {
175
+ case InternalStruct_1.AssetOpType.NFT_BURN:
176
+ payload = this.getBurnPayload(this._configuration.TONParams.crossChainLayerAddress, evmData, crossChainTonAmount, feeData);
177
+ break;
178
+ case InternalStruct_1.AssetOpType.NFT_TRANSFER:
179
+ payload = this.getTransferPayload(this._configuration.TONParams.nftProxyAddress, excessReceiver, forwardFeeTonAmount, evmData, crossChainTonAmount, feeData);
180
+ break;
181
+ }
182
+ return payload;
183
+ }
184
+ async isOwnedBy(userAddress) {
185
+ const nftData = await NFT.getItemData(this._configuration.TONParams.contractOpener, this.address.toString());
186
+ return !!nftData.ownerAddress?.equals(ton_1.Address.parse(userAddress));
187
+ }
188
+ async checkCanBeTransferredBy(userAddress) {
189
+ if (!(await this.isOwnedBy(userAddress))) {
190
+ throw (0, errors_1.insufficientBalanceError)(this.address.toString());
191
+ }
192
+ }
193
+ async getBalanceOf(userAddress) {
194
+ return (await this.isOwnedBy(userAddress)) ? 1n : 0n;
195
+ }
196
+ getBurnPayload(crossChainLayerAddress, evmData, crossChainTonAmount, feeData) {
197
+ const queryId = (0, Utils_1.generateRandomNumberByTimestamp)().randomNumber;
198
+ return wrappers_1.NFTItem.burnMessage(queryId, (0, ton_1.address)(crossChainLayerAddress), crossChainTonAmount, evmData, feeData);
199
+ }
200
+ getTransferPayload(to, responseAddress, forwardFeeAmount, evmData, crossChainTonAmount, feeData) {
201
+ const queryId = (0, Utils_1.generateRandomNumberByTimestamp)().randomNumber;
202
+ const forwardPayload = (0, ton_1.beginCell)()
203
+ .storeCoins(crossChainTonAmount)
204
+ .storeMaybeRef(feeData)
205
+ .storeMaybeRef(evmData)
206
+ .endCell();
207
+ return wrappers_1.NFTItem.transferMessage(queryId, (0, ton_1.address)(to), (0, ton_1.address)(responseAddress), Number((0, ton_1.fromNano)(Consts_1.NFT_TRANSFER_FORWARD_TON_AMOUNT + forwardFeeAmount + crossChainTonAmount)), forwardPayload);
208
+ }
209
+ }
210
+ exports.NFT = NFT;
@@ -0,0 +1,38 @@
1
+ import { Cell } from '@ton/ton';
2
+ import type { SenderAbstraction } from '../sender';
3
+ import type { ShardTransaction } from '../structs/InternalStruct';
4
+ import { Asset, IConfiguration } from '../interfaces';
5
+ import { AssetType, FeeParams } from '../structs/Struct';
6
+ export declare class TON implements Asset {
7
+ readonly address: string;
8
+ readonly type: AssetType;
9
+ private _rawAmount;
10
+ private _config;
11
+ constructor(config: IConfiguration);
12
+ static create(config: IConfiguration): TON;
13
+ get rawAmount(): bigint;
14
+ get clone(): TON;
15
+ withAmount(amount: {
16
+ rawAmount: bigint;
17
+ } | {
18
+ amount: number;
19
+ }): Promise<TON>;
20
+ addAmount(amount: {
21
+ rawAmount: bigint;
22
+ } | {
23
+ amount: number;
24
+ }): Promise<TON>;
25
+ getEVMAddress(): Promise<string>;
26
+ getTVMAddress(): Promise<string>;
27
+ generatePayload(params: {
28
+ excessReceiver: string;
29
+ evmData: Cell;
30
+ crossChainTonAmount?: bigint;
31
+ forwardFeeTonAmount?: bigint;
32
+ feeParams?: FeeParams;
33
+ }): Promise<Cell>;
34
+ getUserBalance(userAddress: string): Promise<bigint>;
35
+ static checkBalance(sender: SenderAbstraction, config: IConfiguration, transactions: ShardTransaction[]): Promise<void>;
36
+ checkCanBeTransferredBy(userAddress: string): Promise<void>;
37
+ getBalanceOf(userAddress: string): Promise<bigint>;
38
+ }