@evaafi/sdk 0.2.0

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 (50) hide show
  1. package/README.md +7 -0
  2. package/dist/api/helpers.d.ts +8 -0
  3. package/dist/api/helpers.js +33 -0
  4. package/dist/api/math.d.ts +20 -0
  5. package/dist/api/math.js +214 -0
  6. package/dist/api/parser.d.ts +8 -0
  7. package/dist/api/parser.js +227 -0
  8. package/dist/api/prices.d.ts +9 -0
  9. package/dist/api/prices.js +104 -0
  10. package/dist/constants.d.ts +38 -0
  11. package/dist/constants.js +42 -0
  12. package/dist/contracts/JettonWallet.d.ts +7 -0
  13. package/dist/contracts/JettonWallet.js +20 -0
  14. package/dist/contracts/MasterContract.d.ts +166 -0
  15. package/dist/contracts/MasterContract.js +217 -0
  16. package/dist/contracts/UserContract.d.ts +49 -0
  17. package/dist/contracts/UserContract.js +99 -0
  18. package/dist/index.d.ts +12 -0
  19. package/dist/index.js +53 -0
  20. package/dist/types/Common.d.ts +5 -0
  21. package/dist/types/Common.js +2 -0
  22. package/dist/types/Master.d.ts +63 -0
  23. package/dist/types/Master.js +2 -0
  24. package/dist/types/Redstone.d.ts +15 -0
  25. package/dist/types/Redstone.js +2 -0
  26. package/dist/types/User.d.ts +55 -0
  27. package/dist/types/User.js +8 -0
  28. package/dist/utils/sha256BigInt.d.ts +1 -0
  29. package/dist/utils/sha256BigInt.js +13 -0
  30. package/dist/utils/tonConnectSender.d.ts +4 -0
  31. package/dist/utils/tonConnectSender.js +37 -0
  32. package/dist/utils/userJettonWallet.d.ts +2 -0
  33. package/dist/utils/userJettonWallet.js +37 -0
  34. package/package.json +38 -0
  35. package/src/api/helpers.ts +32 -0
  36. package/src/api/math.ts +252 -0
  37. package/src/api/parser.ts +277 -0
  38. package/src/api/prices.ts +127 -0
  39. package/src/constants.ts +53 -0
  40. package/src/contracts/JettonWallet.ts +20 -0
  41. package/src/contracts/MasterContract.ts +387 -0
  42. package/src/contracts/UserContract.ts +125 -0
  43. package/src/index.ts +78 -0
  44. package/src/types/Common.ts +6 -0
  45. package/src/types/Master.ts +70 -0
  46. package/src/types/Redstone.ts +15 -0
  47. package/src/types/User.ts +66 -0
  48. package/src/utils/sha256BigInt.ts +7 -0
  49. package/src/utils/tonConnectSender.ts +37 -0
  50. package/src/utils/userJettonWallet.ts +34 -0
@@ -0,0 +1,42 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.FEES = exports.OPCODES = exports.JETTON_WALLET_CODE = exports.LENDING_CODE = exports.MASTER_CONSTANTS = exports.JETTON_MASTER_ADDRESSES = exports.ASSET_ID = exports.TESTNET_VERSION = exports.EVAA_MASTER_TESTNET = exports.MAINNET_VERSION = exports.EVAA_MASTER_MAINNET = void 0;
4
+ const core_1 = require("@ton/core");
5
+ const sha256BigInt_1 = require("./utils/sha256BigInt");
6
+ exports.EVAA_MASTER_MAINNET = core_1.Address.parse('EQC8rUZqR_pWV1BylWUlPNBzyiTYVoBEmQkMIQDZXICfnuRr');
7
+ exports.MAINNET_VERSION = 2;
8
+ exports.EVAA_MASTER_TESTNET = core_1.Address.parse('kQBi2jDaq_-Oi62V7u_j_Vr1We8EqC3h0jkhB4Gf4JW741vX');
9
+ exports.TESTNET_VERSION = 1;
10
+ exports.ASSET_ID = {
11
+ TON: (0, sha256BigInt_1.sha256Hash)('TON'),
12
+ jUSDT: (0, sha256BigInt_1.sha256Hash)('jUSDT'),
13
+ jUSDC: (0, sha256BigInt_1.sha256Hash)('jUSDC'),
14
+ };
15
+ exports.JETTON_MASTER_ADDRESSES = {
16
+ jUSDT_MAINNET: core_1.Address.parse('EQBwbF0otxLyA3VrRjjC1q7i3G7LtoEpdyBjZEuNtrhC4drm'),
17
+ jUSDT_TESTNET: core_1.Address.parse('kQAGvWIETv29VRXLBL3lugxdLeCZ3r7jcuUvqKeZpbcwiQo0'),
18
+ jUSDC_MAINNET: core_1.Address.parse('EQCEHZi-CLX2ghdsNbY35umR1OFODG5ySHrtK6GItMdWv7dS'),
19
+ jUSDC_TESTNET: core_1.Address.parse('kQCwj7zrRnBIYUhM7BShqupouNgubrdJUmmA3AAuPH9CK0Kj'),
20
+ };
21
+ exports.MASTER_CONSTANTS = {
22
+ FACTOR_SCALE: BigInt(1e12),
23
+ ASSET_COEFFICIENT_SCALE: 10000n,
24
+ ASSET_PRICE_SCALE: BigInt(1e8),
25
+ };
26
+ exports.LENDING_CODE = core_1.Cell.fromBoc(Buffer.from('b5ee9c72c1010e0100fd000d12182a555a6065717691969efd0114ff00f4a413f4bcf2c80b010202c8050202039f740403001ff2f8276a2687d2018fd201800f883b840051d38642c678b64e4400780e58fc10802faf07f80e59fa801e78b096664c02078067c07c100627a7978402014807060007a0ddb0c60201c709080013a0fd007a026900aa90400201200b0a0031b8e1002191960aa00b9e2ca007f4042796d225e8019203f6010201200d0c000bf7c147d2218400b9d10e86981fd201840b07f8138d809797976a2687d2029116382f970fd9178089910374daf81b619fd20182c7883b8701981684100627910eba56001797a6a6ba610fd8200e8768f76a9f6aa00cc2a32a8292878809bef2f1889f883bbcdeb86f01', 'hex'))[0];
27
+ exports.JETTON_WALLET_CODE = core_1.Cell.fromBase64('te6cckECEQEAAyMAART/APSkE/S88sgLAQIBYgIDAgLMBAUAG6D2BdqJofQB9IH0gahhAgHUBgcCASAICQDDCDHAJJfBOAB0NMDAXGwlRNfA/AM4PpA+kAx+gAxcdch+gAx+gAwc6m0AALTH4IQD4p+pVIgupUxNFnwCeCCEBeNRRlSILqWMUREA/AK4DWCEFlfB7y6k1nwC+BfBIQP8vCAAET6RDBwuvLhTYAIBIAoLAIPUAQa5D2omh9AH0gfSBqGAJpj8EIC8aijKkQXUEIPe7L7wndCVj5cWLpn5j9ABgJ0CgR5CgCfQEsZ4sA54tmZPaqQB8VA9M/+gD6QCHwAe1E0PoA+kD6QNQwUTahUirHBfLiwSjC//LiwlQ0QnBUIBNUFAPIUAT6AljPFgHPFszJIsjLARL0APQAywDJIPkAcHTIywLKB8v/ydAE+kD0BDH6ACDXScIA8uLEd4AYyMsFUAjPFnD6AhfLaxPMgMAgEgDQ4AnoIQF41FGcjLHxnLP1AH+gIizxZQBs8WJfoCUAPPFslQBcwjkXKRceJQCKgToIIJycOAoBS88uLFBMmAQPsAECPIUAT6AljPFgHPFszJ7VQC9ztRND6APpA+kDUMAjTP/oAUVGgBfpA+kBTW8cFVHNtcFQgE1QUA8hQBPoCWM8WAc8WzMkiyMsBEvQA9ADLAMn5AHB0yMsCygfL/8nQUA3HBRyx8uLDCvoAUaihggiYloBmtgihggiYloCgGKEnlxBJEDg3XwTjDSXXCwGAPEADXO1E0PoA+kD6QNQwB9M/+gD6QDBRUaFSSccF8uLBJ8L/8uLCBYIJMS0AoBa88uLDghB73ZfeyMsfFcs/UAP6AiLPFgHPFslxgBjIywUkzxZw+gLLaszJgED7AEATyFAE+gJYzxYBzxbMye1UgAHBSeaAYoYIQc2LQnMjLH1Iwyz9Y+gJQB88WUAfPFslxgBDIywUkzxZQBvoCFctqFMzJcfsAECQQIwB8wwAjwgCwjiGCENUydttwgBDIywVQCM8WUAT6AhbLahLLHxLLP8ly+wCTNWwh4gPIUAT6AljPFgHPFszJ7VSV6u3X');
28
+ exports.OPCODES = {
29
+ SUPPLY: 0x1,
30
+ WITHDRAW: 0x2,
31
+ LIQUIDATE: 0x3,
32
+ JETTON_TRANSFER: 0xf8a7ea5,
33
+ };
34
+ exports.FEES = {
35
+ SUPPLY: (0, core_1.toNano)('0.3'),
36
+ WITHDRAW: (0, core_1.toNano)('0.5'),
37
+ SUPPLY_JETTON: (0, core_1.toNano)('0.8'),
38
+ SUPPLY_JETTON_FWD: (0, core_1.toNano)('0.5'),
39
+ LIQUIDATION: (0, core_1.toNano)('0.8'),
40
+ LIQUIDATION_JETTON: (0, core_1.toNano)('1'),
41
+ LIQUIDATION_JETTON_FWD: (0, core_1.toNano)('0.8'),
42
+ };
@@ -0,0 +1,7 @@
1
+ import { Address, Cell, Contract, ContractProvider, Sender } from '@ton/core';
2
+ export declare class JettonWallet implements Contract {
3
+ readonly address: Address;
4
+ static createFromAddress(address: Address): JettonWallet;
5
+ private constructor();
6
+ sendTransfer(provider: ContractProvider, via: Sender, value: bigint, payload: Cell): Promise<void>;
7
+ }
@@ -0,0 +1,20 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.JettonWallet = void 0;
4
+ const core_1 = require("@ton/core");
5
+ class JettonWallet {
6
+ static createFromAddress(address) {
7
+ return new JettonWallet(address);
8
+ }
9
+ constructor(address) {
10
+ this.address = address;
11
+ }
12
+ async sendTransfer(provider, via, value, payload) {
13
+ await provider.internal(via, {
14
+ value,
15
+ sendMode: core_1.SendMode.PAY_GAS_SEPARATELY + core_1.SendMode.IGNORE_ERRORS,
16
+ body: payload,
17
+ });
18
+ }
19
+ }
20
+ exports.JettonWallet = JettonWallet;
@@ -0,0 +1,166 @@
1
+ import { Address, Cell, Contract, ContractProvider, OpenedContract, Sender } from '@ton/core';
2
+ import { Maybe } from '@ton/core/dist/utils/maybe';
3
+ import { EvaaUser } from './UserContract';
4
+ import { MasterData } from '../types/Master';
5
+ /**
6
+ * Parameters for the Evaa contract
7
+ * @property testnet - true for testnet, false for mainnet
8
+ * @property debug - true to enable debug mode (optional)
9
+ */
10
+ export type EvaaParameters = {
11
+ testnet: boolean;
12
+ debug?: boolean;
13
+ };
14
+ /**
15
+ * Parameters for the Jetton message
16
+ * @property responseAddress - address to send excesses
17
+ * @property forwardAmount - amount to forward to the destination address
18
+ */
19
+ export type JettonMessageParameters = {
20
+ responseAddress?: Address;
21
+ forwardAmount?: bigint;
22
+ };
23
+ /**
24
+ * Base parameters for supply
25
+ * @property queryID - unique query ID
26
+ * @property includeUserCode - true to include user code for update (needed when user contract code version is outdated)
27
+ * @property amount - amount to supply
28
+ * @property userAddress - user address
29
+ * @property assetID - asset ID
30
+ */
31
+ export type SupplyBaseParameters = {
32
+ queryID: bigint;
33
+ includeUserCode: boolean;
34
+ amount: bigint;
35
+ userAddress: Address;
36
+ assetID: bigint;
37
+ };
38
+ /**
39
+ * Parameters for the TON supply message
40
+ * @property type - 'ton'
41
+ */
42
+ export type TonSupplyParameters = SupplyBaseParameters & {
43
+ type: 'ton';
44
+ };
45
+ /**
46
+ * Parameters for the jetton supply message
47
+ * @property type - 'jetton'
48
+ */
49
+ export type JettonSupplyParameters = SupplyBaseParameters & JettonMessageParameters & {
50
+ type: 'jetton';
51
+ };
52
+ /**
53
+ * Parameters for the withdraw message
54
+ * @property queryID - unique query ID
55
+ * @property assetID - asset ID
56
+ * @property amount - amount to withdraw
57
+ * @property userAddress - user address
58
+ * @property includeUserCode - true to include user code for update (needed when user contract code version is outdated)
59
+ * @property priceData - price data cell. Can be obtained from the getPrices function
60
+ */
61
+ export type WithdrawParameters = {
62
+ queryID: bigint;
63
+ assetID: bigint;
64
+ amount: bigint;
65
+ userAddress: Address;
66
+ includeUserCode: boolean;
67
+ priceData: Cell;
68
+ };
69
+ /**
70
+ * Base data for liquidation. Can be obtained from the user contract liquidationParameters getter
71
+ * @property borrowerAddress - borrower address (user address that is being liquidated)
72
+ * @property loanAsset - loan asset ID
73
+ * @property collateralAsset - collateral asset ID
74
+ * @property minCollateralAmount - minimal amount to receive from the liquidation
75
+ * @property liquidationAmount - amount to liquidate
76
+ * @property tonLiquidation - true if the loan asset is TON
77
+ */
78
+ export type LiquidationBaseData = {
79
+ borrowerAddress: Address;
80
+ loanAsset: bigint;
81
+ collateralAsset: bigint;
82
+ minCollateralAmount: bigint;
83
+ liquidationAmount: bigint;
84
+ tonLiquidation: boolean;
85
+ };
86
+ /**
87
+ * Base parameters for liquidation
88
+ * @property queryID - unique query ID
89
+ * @property liquidatorAddress - liquidator address, where and collateral will be sent
90
+ * @property includeUserCode - true to include user code for update (needed when user contract code version is outdated)
91
+ * @property priceData - price data cell. Can be obtained from the getPrices function
92
+ */
93
+ export type LiquidationBaseParameters = LiquidationBaseData & {
94
+ queryID: bigint;
95
+ liquidatorAddress: Address;
96
+ includeUserCode: boolean;
97
+ priceData: Cell;
98
+ };
99
+ /**
100
+ * Parameters for the TON liquidation message
101
+ * @property type - 'ton'
102
+ */
103
+ export type TonLiquidationParameters = LiquidationBaseParameters & {
104
+ type: 'ton';
105
+ };
106
+ /**
107
+ * Parameters for the jetton liquidation message
108
+ * @property type - 'jetton'
109
+ */
110
+ export type JettonLiquidationParameters = LiquidationBaseParameters & JettonMessageParameters & {
111
+ type: 'jetton';
112
+ };
113
+ /**
114
+ * Evaa master contract wrapper
115
+ */
116
+ export declare class Evaa implements Contract {
117
+ readonly address: Address;
118
+ readonly network: 'mainnet' | 'testnet';
119
+ private readonly debug?;
120
+ private _data?;
121
+ private lastSync;
122
+ /**
123
+ * Create Evaa contract wrapper
124
+ * @param parameters Evaa contract parameters
125
+ */
126
+ constructor(parameters?: EvaaParameters);
127
+ /**
128
+ * Create supply message
129
+ * @returns supply message as a cell
130
+ */
131
+ createSupplyMessage(parameters: TonSupplyParameters | JettonSupplyParameters): Cell;
132
+ /**
133
+ * Create withdraw message
134
+ * @returns withdraw message as a cell
135
+ */
136
+ createWithdrawMessage(parameters: WithdrawParameters): Cell;
137
+ /**
138
+ * Create liquidation message
139
+ * @returns liquidation message as a cell
140
+ */
141
+ createLiquidationMessage(parameters: TonLiquidationParameters | JettonLiquidationParameters): Cell;
142
+ /**
143
+ * Calculate user contract address
144
+ * @param userAddress
145
+ * @returns user contract address
146
+ */
147
+ calculateUserSCAddr(userAddress: Address): Address;
148
+ /**
149
+ * Open user contract wrapper
150
+ * @param userAddress
151
+ * @returns user contract
152
+ */
153
+ openUserContract(userAddress: Address): EvaaUser;
154
+ getOpenedUserContract(provider: ContractProvider, userAddress: Address): OpenedContract<EvaaUser>;
155
+ /**
156
+ * Get master contract data
157
+ */
158
+ get data(): Maybe<MasterData>;
159
+ sendSupply(provider: ContractProvider, via: Sender, value: bigint, parameters: TonSupplyParameters | JettonSupplyParameters): Promise<void>;
160
+ sendWithdraw(provider: ContractProvider, via: Sender, value: bigint, parameters: WithdrawParameters): Promise<void>;
161
+ sendLiquidation(provider: ContractProvider, via: Sender, value: bigint, parameters: TonLiquidationParameters | JettonLiquidationParameters): Promise<void>;
162
+ /**
163
+ * Sync master contract data
164
+ */
165
+ getSync(provider: ContractProvider): Promise<void>;
166
+ }
@@ -0,0 +1,217 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.Evaa = void 0;
4
+ const core_1 = require("@ton/core");
5
+ const constants_1 = require("../constants");
6
+ const UserContract_1 = require("./UserContract");
7
+ const parser_1 = require("../api/parser");
8
+ const JettonWallet_1 = require("./JettonWallet");
9
+ const userJettonWallet_1 = require("../utils/userJettonWallet");
10
+ /**
11
+ * Evaa master contract wrapper
12
+ */
13
+ class Evaa {
14
+ /**
15
+ * Create Evaa contract wrapper
16
+ * @param parameters Evaa contract parameters
17
+ */
18
+ constructor(parameters) {
19
+ this.address = constants_1.EVAA_MASTER_MAINNET;
20
+ this.network = 'mainnet';
21
+ this.lastSync = 0;
22
+ if (parameters?.testnet) {
23
+ this.network = 'testnet';
24
+ this.address = constants_1.EVAA_MASTER_TESTNET;
25
+ }
26
+ this.debug = parameters?.debug;
27
+ }
28
+ /**
29
+ * Create supply message
30
+ * @returns supply message as a cell
31
+ */
32
+ createSupplyMessage(parameters) {
33
+ if (parameters.type === 'jetton') {
34
+ return (0, core_1.beginCell)()
35
+ .storeUint(constants_1.OPCODES.JETTON_TRANSFER, 32)
36
+ .storeUint(parameters.queryID, 64)
37
+ .storeCoins(parameters.amount)
38
+ .storeAddress(this.address)
39
+ .storeAddress(parameters.responseAddress ?? parameters.userAddress)
40
+ .storeBit(0)
41
+ .storeCoins(parameters.forwardAmount ?? constants_1.FEES.SUPPLY_JETTON_FWD)
42
+ .storeBit(1)
43
+ .storeRef((0, core_1.beginCell)()
44
+ .storeUint(constants_1.OPCODES.SUPPLY, 32)
45
+ .storeInt(parameters.includeUserCode ? -1 : 0, 2)
46
+ .storeAddress(parameters.userAddress)
47
+ .endCell())
48
+ .endCell();
49
+ }
50
+ else {
51
+ return (0, core_1.beginCell)()
52
+ .storeUint(constants_1.OPCODES.SUPPLY, 32)
53
+ .storeUint(parameters.queryID, 64)
54
+ .storeInt(parameters.includeUserCode ? -1 : 0, 2)
55
+ .storeUint(parameters.amount, 64)
56
+ .storeAddress(parameters.userAddress)
57
+ .endCell();
58
+ }
59
+ }
60
+ /**
61
+ * Create withdraw message
62
+ * @returns withdraw message as a cell
63
+ */
64
+ createWithdrawMessage(parameters) {
65
+ return (0, core_1.beginCell)()
66
+ .storeUint(constants_1.OPCODES.WITHDRAW, 32)
67
+ .storeUint(parameters.queryID, 64)
68
+ .storeUint(parameters.assetID, 256)
69
+ .storeUint(parameters.amount, 64)
70
+ .storeAddress(parameters.userAddress)
71
+ .storeInt(parameters.includeUserCode ? -1 : 0, 2)
72
+ .storeRef(parameters.priceData)
73
+ .endCell();
74
+ }
75
+ /**
76
+ * Create liquidation message
77
+ * @returns liquidation message as a cell
78
+ */
79
+ createLiquidationMessage(parameters) {
80
+ if (parameters.type === 'jetton') {
81
+ return (0, core_1.beginCell)()
82
+ .storeUint(constants_1.OPCODES.JETTON_TRANSFER, 32)
83
+ .storeUint(parameters.queryID, 64)
84
+ .storeCoins(parameters.liquidationAmount)
85
+ .storeAddress(this.address)
86
+ .storeAddress(parameters.responseAddress ?? parameters.liquidatorAddress)
87
+ .storeBit(0)
88
+ .storeCoins(parameters.forwardAmount ?? constants_1.FEES.LIQUIDATION_JETTON_FWD)
89
+ .storeBit(1)
90
+ .storeRef((0, core_1.beginCell)()
91
+ .storeUint(constants_1.OPCODES.LIQUIDATE, 32)
92
+ .storeAddress(parameters.borrowerAddress)
93
+ .storeAddress(parameters.liquidatorAddress)
94
+ .storeUint(parameters.collateralAsset, 256)
95
+ .storeUint(parameters.minCollateralAmount, 64)
96
+ .storeInt(parameters.includeUserCode ? -1 : 0, 2)
97
+ // do not need liquidationAmount in case of jetton liquidation because
98
+ // the exact amount of transferred jettons for liquidation is known
99
+ .storeUint(0, 64)
100
+ .storeRef(parameters.priceData)
101
+ .endCell())
102
+ .endCell();
103
+ }
104
+ else {
105
+ return (0, core_1.beginCell)()
106
+ .storeUint(constants_1.OPCODES.LIQUIDATE, 32)
107
+ .storeUint(parameters.queryID, 64)
108
+ .storeAddress(parameters.borrowerAddress)
109
+ .storeAddress(parameters.liquidatorAddress)
110
+ .storeUint(parameters.collateralAsset, 256)
111
+ .storeUint(parameters.minCollateralAmount, 64)
112
+ .storeInt(parameters.includeUserCode ? -1 : 0, 2)
113
+ .storeUint(parameters.liquidationAmount, 64)
114
+ .storeRef(parameters.priceData)
115
+ .endCell();
116
+ }
117
+ }
118
+ /**
119
+ * Calculate user contract address
120
+ * @param userAddress
121
+ * @returns user contract address
122
+ */
123
+ calculateUserSCAddr(userAddress) {
124
+ const lendingData = (0, core_1.beginCell)()
125
+ .storeAddress(this.address)
126
+ .storeAddress(userAddress)
127
+ .storeUint(0, 8)
128
+ .storeBit(0)
129
+ .endCell();
130
+ const stateInit = (0, core_1.beginCell)()
131
+ .store((0, core_1.storeStateInit)({
132
+ code: constants_1.LENDING_CODE,
133
+ data: lendingData,
134
+ }))
135
+ .endCell();
136
+ return new core_1.Address(0, stateInit.hash());
137
+ }
138
+ /**
139
+ * Open user contract wrapper
140
+ * @param userAddress
141
+ * @returns user contract
142
+ */
143
+ openUserContract(userAddress) {
144
+ return UserContract_1.EvaaUser.createFromAddress(this.calculateUserSCAddr(userAddress));
145
+ }
146
+ getOpenedUserContract(provider, userAddress) {
147
+ return provider.open(this.openUserContract(userAddress));
148
+ }
149
+ /**
150
+ * Get master contract data
151
+ */
152
+ get data() {
153
+ return this._data;
154
+ }
155
+ async sendSupply(provider, via, value, parameters) {
156
+ const message = this.createSupplyMessage(parameters);
157
+ if (parameters.type === 'jetton') {
158
+ if (!via.address) {
159
+ throw Error('Via address is required for jetton supply');
160
+ }
161
+ const jettonWallet = provider.open(JettonWallet_1.JettonWallet.createFromAddress((0, userJettonWallet_1.getUserJettonWallet)(via.address, parameters.assetID, this.network)));
162
+ await jettonWallet.sendTransfer(via, value, message);
163
+ }
164
+ else {
165
+ await provider.internal(via, {
166
+ value,
167
+ sendMode: core_1.SendMode.PAY_GAS_SEPARATELY + core_1.SendMode.IGNORE_ERRORS,
168
+ body: message,
169
+ });
170
+ }
171
+ }
172
+ async sendWithdraw(provider, via, value, parameters) {
173
+ const message = this.createWithdrawMessage(parameters);
174
+ await provider.internal(via, {
175
+ value,
176
+ sendMode: core_1.SendMode.PAY_GAS_SEPARATELY + core_1.SendMode.IGNORE_ERRORS,
177
+ body: message,
178
+ });
179
+ }
180
+ async sendLiquidation(provider, via, value, parameters) {
181
+ const message = this.createLiquidationMessage(parameters);
182
+ if (parameters.type === 'jetton') {
183
+ if (!via.address) {
184
+ throw Error('Via address is required for jetton liquidation');
185
+ }
186
+ const jettonWallet = provider.open(JettonWallet_1.JettonWallet.createFromAddress((0, userJettonWallet_1.getUserJettonWallet)(via.address, parameters.loanAsset, this.network)));
187
+ await jettonWallet.sendTransfer(via, value, message);
188
+ }
189
+ else {
190
+ await provider.internal(via, {
191
+ value,
192
+ sendMode: core_1.SendMode.PAY_GAS_SEPARATELY + core_1.SendMode.IGNORE_ERRORS,
193
+ body: message,
194
+ });
195
+ }
196
+ }
197
+ /**
198
+ * Sync master contract data
199
+ */
200
+ async getSync(provider) {
201
+ const state = (await provider.getState()).state;
202
+ if (state.type === 'active') {
203
+ this._data = (0, parser_1.parseMasterData)(state.data.toString('base64url'));
204
+ if (this.network === 'testnet' && this._data.upgradeConfig.masterCodeVersion !== constants_1.TESTNET_VERSION) {
205
+ throw Error(`Outdated SDK version. It supports only master code version ${constants_1.TESTNET_VERSION} on testnet, but the current master code version is ${this._data.upgradeConfig.masterCodeVersion}`);
206
+ }
207
+ if (this.network === 'mainnet' && this._data.upgradeConfig.masterCodeVersion !== constants_1.MAINNET_VERSION) {
208
+ throw Error(`Outdated SDK version. It supports only master code version ${constants_1.MAINNET_VERSION} on mainnet, but the current master code version is ${this._data.upgradeConfig.masterCodeVersion}`);
209
+ }
210
+ this.lastSync = Math.floor(Date.now() / 1000);
211
+ }
212
+ else {
213
+ throw Error('Master contract is not active');
214
+ }
215
+ }
216
+ }
217
+ exports.Evaa = Evaa;
@@ -0,0 +1,49 @@
1
+ import { Address, Contract, ContractProvider, Dictionary } from '@ton/core';
2
+ import { UserData, UserLiteData } from '../types/User';
3
+ import { AssetConfig, ExtendedAssetData } from '../types/Master';
4
+ import { LiquidationBaseData } from './MasterContract';
5
+ /**
6
+ * User contract wrapper
7
+ */
8
+ export declare class EvaaUser implements Contract {
9
+ readonly address: Address;
10
+ private lastSync;
11
+ private _liteData?;
12
+ private _data?;
13
+ /**
14
+ * Create user contract wrapper from address
15
+ * @param address user contract address
16
+ */
17
+ static createFromAddress(address: Address): EvaaUser;
18
+ private constructor();
19
+ getSyncLite(provider: ContractProvider, assetsData: Dictionary<bigint, ExtendedAssetData>, assetsConfig: Dictionary<bigint, AssetConfig>): Promise<void>;
20
+ /**
21
+ * Calculate full user data from lite data and prices
22
+ * @param assetsData assets data
23
+ * @param assetsConfig assets config
24
+ * @param prices prices
25
+ * @returns true if user data was calculated
26
+ */
27
+ calculateUserData(assetsData: Dictionary<bigint, ExtendedAssetData>, assetsConfig: Dictionary<bigint, AssetConfig>, prices: Dictionary<bigint, bigint>): boolean;
28
+ getSync(provider: ContractProvider, assetsData: Dictionary<bigint, ExtendedAssetData>, assetsConfig: Dictionary<bigint, AssetConfig>, prices: Dictionary<bigint, bigint>): Promise<void>;
29
+ /**
30
+ * Get user contract lite data
31
+ * @returns user lite data if available, otherwise undefined
32
+ */
33
+ get liteData(): UserLiteData | undefined;
34
+ /**
35
+ * Get user contract full data
36
+ * @returns user data if available , otherwise undefined
37
+ */
38
+ get data(): UserData | undefined;
39
+ /**
40
+ * Get if user is liquidable
41
+ * @returns true if user is liquidable
42
+ */
43
+ get isLiquidable(): boolean;
44
+ /**
45
+ * Get liquidation parameters for passing to liquidation message
46
+ * @returns liquidation parameters if user is liquidable, otherwise undefined
47
+ */
48
+ get liquidationParameters(): LiquidationBaseData | undefined;
49
+ }
@@ -0,0 +1,99 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.EvaaUser = void 0;
4
+ const parser_1 = require("../api/parser");
5
+ const constants_1 = require("../constants");
6
+ /**
7
+ * User contract wrapper
8
+ */
9
+ class EvaaUser {
10
+ /**
11
+ * Create user contract wrapper from address
12
+ * @param address user contract address
13
+ */
14
+ static createFromAddress(address) {
15
+ return new EvaaUser(address);
16
+ }
17
+ constructor(address) {
18
+ this.lastSync = 0;
19
+ this.address = address;
20
+ }
21
+ async getSyncLite(provider, assetsData, assetsConfig) {
22
+ const state = (await provider.getState()).state;
23
+ if (state.type === 'active') {
24
+ this._liteData = (0, parser_1.parseUserLiteData)(state.data.toString('base64url'), assetsData, assetsConfig);
25
+ this.lastSync = Math.floor(Date.now() / 1000);
26
+ }
27
+ else {
28
+ this._liteData = undefined;
29
+ this._data = { type: 'inactive' };
30
+ }
31
+ }
32
+ /**
33
+ * Calculate full user data from lite data and prices
34
+ * @param assetsData assets data
35
+ * @param assetsConfig assets config
36
+ * @param prices prices
37
+ * @returns true if user data was calculated
38
+ */
39
+ calculateUserData(assetsData, assetsConfig, prices) {
40
+ if (this._liteData) {
41
+ this._data = (0, parser_1.parseUserData)(this._liteData, assetsData, assetsConfig, prices);
42
+ return true;
43
+ }
44
+ return false;
45
+ }
46
+ async getSync(provider, assetsData, assetsConfig, prices) {
47
+ const state = (await provider.getState()).state;
48
+ if (state.type === 'active') {
49
+ this._liteData = (0, parser_1.parseUserLiteData)(state.data.toString('base64url'), assetsData, assetsConfig);
50
+ this._data = (0, parser_1.parseUserData)(this._liteData, assetsData, assetsConfig, prices);
51
+ this.lastSync = Math.floor(Date.now() / 1000);
52
+ }
53
+ else {
54
+ this._data = { type: 'inactive' };
55
+ }
56
+ }
57
+ /**
58
+ * Get user contract lite data
59
+ * @returns user lite data if available, otherwise undefined
60
+ */
61
+ get liteData() {
62
+ return this._liteData;
63
+ }
64
+ /**
65
+ * Get user contract full data
66
+ * @returns user data if available , otherwise undefined
67
+ */
68
+ get data() {
69
+ return this._data;
70
+ }
71
+ /**
72
+ * Get if user is liquidable
73
+ * @returns true if user is liquidable
74
+ */
75
+ get isLiquidable() {
76
+ if (!this._data || this._data.type === 'inactive') {
77
+ return false;
78
+ }
79
+ return this._data.liquidationData.liquidable;
80
+ }
81
+ /**
82
+ * Get liquidation parameters for passing to liquidation message
83
+ * @returns liquidation parameters if user is liquidable, otherwise undefined
84
+ */
85
+ get liquidationParameters() {
86
+ if (!this._data || this._data.type === 'inactive' || !this._data.liquidationData.liquidable) {
87
+ return undefined;
88
+ }
89
+ return {
90
+ borrowerAddress: this._data.ownerAddress,
91
+ loanAsset: this._data.liquidationData.greatestLoanAsset,
92
+ collateralAsset: this._data.liquidationData.greatestCollateralAsset,
93
+ minCollateralAmount: this._data.liquidationData.minCollateralAmount,
94
+ liquidationAmount: this._data.liquidationData.liquidationAmount,
95
+ tonLiquidation: this._data.liquidationData.greatestLoanAsset === constants_1.ASSET_ID.TON,
96
+ };
97
+ }
98
+ }
99
+ exports.EvaaUser = EvaaUser;
@@ -0,0 +1,12 @@
1
+ export { mulFactor, mulDiv, bigIntMin, bigIntMax, calculatePresentValue, calculateCurrentRates, calculateAssetData, calculateAssetInterest, getAvailableToBorrow, presentValue, calculateLiquidationData, } from './api/math';
2
+ export { createAssetData, createAssetConfig, parseMasterData, parseUserData } from './api/parser';
3
+ export { getPrices } from './api/prices';
4
+ export { JettonWallet } from './contracts/JettonWallet';
5
+ export { EvaaParameters, JettonMessageParameters, TonSupplyParameters, JettonSupplyParameters, WithdrawParameters, LiquidationBaseData, TonLiquidationParameters, JettonLiquidationParameters, Evaa, } from './contracts/MasterContract';
6
+ export { EvaaUser } from './contracts/UserContract';
7
+ export { PriceData } from './types/Common';
8
+ export { UpgradeConfig, AssetConfig, MasterConfig, AssetData, AssetInterest, AssetApy, ExtendedAssetData, MasterData, } from './types/Master';
9
+ export { BalanceType, UserBalance, UserLiqudationData, LiquidableData, NonLiquidableData, LiquidationData, UserDataInactive, UserDataActive, UserData, } from './types/User';
10
+ export { EVAA_MASTER_MAINNET, MAINNET_VERSION, EVAA_MASTER_TESTNET, TESTNET_VERSION, ASSET_ID, JETTON_MASTER_ADDRESSES, MASTER_CONSTANTS, LENDING_CODE, JETTON_WALLET_CODE, OPCODES, FEES, } from './constants';
11
+ export { getLastSentBoc, getTonConnectSender } from './utils/tonConnectSender';
12
+ export { getUserJettonWallet } from './utils/userJettonWallet';
package/dist/index.js ADDED
@@ -0,0 +1,53 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.getUserJettonWallet = exports.getTonConnectSender = exports.getLastSentBoc = exports.FEES = exports.OPCODES = exports.JETTON_WALLET_CODE = exports.LENDING_CODE = exports.MASTER_CONSTANTS = exports.JETTON_MASTER_ADDRESSES = exports.ASSET_ID = exports.TESTNET_VERSION = exports.EVAA_MASTER_TESTNET = exports.MAINNET_VERSION = exports.EVAA_MASTER_MAINNET = exports.BalanceType = exports.EvaaUser = exports.Evaa = exports.JettonWallet = exports.getPrices = exports.parseUserData = exports.parseMasterData = exports.createAssetConfig = exports.createAssetData = exports.calculateLiquidationData = exports.presentValue = exports.getAvailableToBorrow = exports.calculateAssetInterest = exports.calculateAssetData = exports.calculateCurrentRates = exports.calculatePresentValue = exports.bigIntMax = exports.bigIntMin = exports.mulDiv = exports.mulFactor = void 0;
4
+ // Math
5
+ var math_1 = require("./api/math");
6
+ Object.defineProperty(exports, "mulFactor", { enumerable: true, get: function () { return math_1.mulFactor; } });
7
+ Object.defineProperty(exports, "mulDiv", { enumerable: true, get: function () { return math_1.mulDiv; } });
8
+ Object.defineProperty(exports, "bigIntMin", { enumerable: true, get: function () { return math_1.bigIntMin; } });
9
+ Object.defineProperty(exports, "bigIntMax", { enumerable: true, get: function () { return math_1.bigIntMax; } });
10
+ Object.defineProperty(exports, "calculatePresentValue", { enumerable: true, get: function () { return math_1.calculatePresentValue; } });
11
+ Object.defineProperty(exports, "calculateCurrentRates", { enumerable: true, get: function () { return math_1.calculateCurrentRates; } });
12
+ Object.defineProperty(exports, "calculateAssetData", { enumerable: true, get: function () { return math_1.calculateAssetData; } });
13
+ Object.defineProperty(exports, "calculateAssetInterest", { enumerable: true, get: function () { return math_1.calculateAssetInterest; } });
14
+ Object.defineProperty(exports, "getAvailableToBorrow", { enumerable: true, get: function () { return math_1.getAvailableToBorrow; } });
15
+ Object.defineProperty(exports, "presentValue", { enumerable: true, get: function () { return math_1.presentValue; } });
16
+ Object.defineProperty(exports, "calculateLiquidationData", { enumerable: true, get: function () { return math_1.calculateLiquidationData; } });
17
+ // Parser
18
+ var parser_1 = require("./api/parser");
19
+ Object.defineProperty(exports, "createAssetData", { enumerable: true, get: function () { return parser_1.createAssetData; } });
20
+ Object.defineProperty(exports, "createAssetConfig", { enumerable: true, get: function () { return parser_1.createAssetConfig; } });
21
+ Object.defineProperty(exports, "parseMasterData", { enumerable: true, get: function () { return parser_1.parseMasterData; } });
22
+ Object.defineProperty(exports, "parseUserData", { enumerable: true, get: function () { return parser_1.parseUserData; } });
23
+ // Prices
24
+ var prices_1 = require("./api/prices");
25
+ Object.defineProperty(exports, "getPrices", { enumerable: true, get: function () { return prices_1.getPrices; } });
26
+ // Contracts' wrappers
27
+ var JettonWallet_1 = require("./contracts/JettonWallet");
28
+ Object.defineProperty(exports, "JettonWallet", { enumerable: true, get: function () { return JettonWallet_1.JettonWallet; } });
29
+ var MasterContract_1 = require("./contracts/MasterContract");
30
+ Object.defineProperty(exports, "Evaa", { enumerable: true, get: function () { return MasterContract_1.Evaa; } });
31
+ var UserContract_1 = require("./contracts/UserContract");
32
+ Object.defineProperty(exports, "EvaaUser", { enumerable: true, get: function () { return UserContract_1.EvaaUser; } });
33
+ var User_1 = require("./types/User");
34
+ Object.defineProperty(exports, "BalanceType", { enumerable: true, get: function () { return User_1.BalanceType; } });
35
+ // Constants
36
+ var constants_1 = require("./constants");
37
+ Object.defineProperty(exports, "EVAA_MASTER_MAINNET", { enumerable: true, get: function () { return constants_1.EVAA_MASTER_MAINNET; } });
38
+ Object.defineProperty(exports, "MAINNET_VERSION", { enumerable: true, get: function () { return constants_1.MAINNET_VERSION; } });
39
+ Object.defineProperty(exports, "EVAA_MASTER_TESTNET", { enumerable: true, get: function () { return constants_1.EVAA_MASTER_TESTNET; } });
40
+ Object.defineProperty(exports, "TESTNET_VERSION", { enumerable: true, get: function () { return constants_1.TESTNET_VERSION; } });
41
+ Object.defineProperty(exports, "ASSET_ID", { enumerable: true, get: function () { return constants_1.ASSET_ID; } });
42
+ Object.defineProperty(exports, "JETTON_MASTER_ADDRESSES", { enumerable: true, get: function () { return constants_1.JETTON_MASTER_ADDRESSES; } });
43
+ Object.defineProperty(exports, "MASTER_CONSTANTS", { enumerable: true, get: function () { return constants_1.MASTER_CONSTANTS; } });
44
+ Object.defineProperty(exports, "LENDING_CODE", { enumerable: true, get: function () { return constants_1.LENDING_CODE; } });
45
+ Object.defineProperty(exports, "JETTON_WALLET_CODE", { enumerable: true, get: function () { return constants_1.JETTON_WALLET_CODE; } });
46
+ Object.defineProperty(exports, "OPCODES", { enumerable: true, get: function () { return constants_1.OPCODES; } });
47
+ Object.defineProperty(exports, "FEES", { enumerable: true, get: function () { return constants_1.FEES; } });
48
+ // Utils
49
+ var tonConnectSender_1 = require("./utils/tonConnectSender");
50
+ Object.defineProperty(exports, "getLastSentBoc", { enumerable: true, get: function () { return tonConnectSender_1.getLastSentBoc; } });
51
+ Object.defineProperty(exports, "getTonConnectSender", { enumerable: true, get: function () { return tonConnectSender_1.getTonConnectSender; } });
52
+ var userJettonWallet_1 = require("./utils/userJettonWallet");
53
+ Object.defineProperty(exports, "getUserJettonWallet", { enumerable: true, get: function () { return userJettonWallet_1.getUserJettonWallet; } });