@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,66 @@
1
+ import { Address, Cell, Dictionary } from '@ton/core';
2
+
3
+ export enum BalanceType {
4
+ supply = 'supply',
5
+ borrow = 'borrow',
6
+ }
7
+
8
+ export type UserBalance = {
9
+ amount: bigint;
10
+ type?: BalanceType;
11
+ };
12
+
13
+ export type UserLiqudationData = {
14
+ greatestCollateralValue: bigint;
15
+ greatestCollateralAsset: bigint;
16
+ greatestLoanValue: bigint;
17
+ greatestLoanAsset: bigint;
18
+ totalDebt: bigint;
19
+ totalLimit: bigint;
20
+ };
21
+
22
+ export type LiquidableData = UserLiqudationData & {
23
+ liquidable: true;
24
+ liquidationAmount: bigint;
25
+ minCollateralAmount: bigint;
26
+ };
27
+
28
+ export type NonLiquidableData = UserLiqudationData & {
29
+ liquidable: false;
30
+ };
31
+
32
+ export type LiquidationData = LiquidableData | NonLiquidableData;
33
+
34
+ export type UserLiteData = {
35
+ type: 'active';
36
+ codeVersion: number;
37
+ masterAddress: Address;
38
+ ownerAddress: Address;
39
+ principals: Dictionary<bigint, bigint>;
40
+ state: number;
41
+ balances: Dictionary<bigint, UserBalance>;
42
+ trackingSupplyIndex: bigint;
43
+ trackingBorrowIndex: bigint;
44
+ dutchAuctionStart: number;
45
+ backupCell: Cell;
46
+ };
47
+
48
+ export type UserDataActive = UserLiteData & {
49
+ withdrawalLimits: Dictionary<bigint, bigint>;
50
+ borrowLimits: Dictionary<bigint, bigint>;
51
+ repayLimits?: Dictionary<bigint, bigint>;
52
+ supplyBalance: bigint;
53
+ borrowBalance: bigint;
54
+ availableToBorrow: bigint;
55
+ limitUsedPercent: number;
56
+ limitUsed: bigint;
57
+ healthFactor: number;
58
+
59
+ liquidationData: LiquidationData;
60
+ };
61
+
62
+ export type UserDataInactive = {
63
+ type: 'inactive';
64
+ };
65
+
66
+ export type UserData = UserDataActive | UserDataInactive;
@@ -0,0 +1,7 @@
1
+ import sha256 from 'crypto-js/sha256';
2
+
3
+ export function sha256Hash(input: string): bigint {
4
+ const hash = sha256(input);
5
+ const hashHex = hash.toString();
6
+ return BigInt('0x' + hashHex);
7
+ }
@@ -0,0 +1,37 @@
1
+ import { ITonConnect, SendTransactionResponse } from '@tonconnect/sdk';
2
+ import { Address, beginCell, Sender, SenderArguments, storeStateInit } from '@ton/core';
3
+
4
+ /*
5
+ This is not the best solution to get the BOC of the sent external message, however the Sender
6
+ interface does not support returning any value from send(), so at the moment you can get it from
7
+ this global variable.
8
+ */
9
+ let lastSentBoc: SendTransactionResponse | undefined;
10
+
11
+ export function getLastSentBoc() {
12
+ return lastSentBoc;
13
+ }
14
+
15
+ export function getTonConnectSender(connector: ITonConnect): Sender {
16
+ return {
17
+ get address(): Address | undefined {
18
+ return connector.account ? Address.parse(connector.account.address) : undefined;
19
+ },
20
+
21
+ async send(args: SenderArguments): Promise<void> {
22
+ lastSentBoc = await connector.sendTransaction({
23
+ validUntil: Date.now() + 2 * 60 * 1000, // 1 minutes
24
+ messages: [
25
+ {
26
+ address: args.to.toString(),
27
+ amount: args.value.toString(),
28
+ payload: args.body?.toBoc().toString('base64'),
29
+ stateInit: args.init
30
+ ? beginCell().store(storeStateInit(args.init)).endCell().toBoc().toString('base64')
31
+ : undefined,
32
+ },
33
+ ],
34
+ });
35
+ },
36
+ };
37
+ }
@@ -0,0 +1,34 @@
1
+ import { Address, beginCell, storeStateInit } from '@ton/core';
2
+ import { ASSET_ID, JETTON_MASTER_ADDRESSES, JETTON_WALLET_CODE } from '../constants';
3
+
4
+ export function getUserJettonWallet(ownerAddress: Address, assetID: bigint, network: 'mainnet' | 'testnet'): Address {
5
+ const builder = beginCell().storeCoins(0).storeAddress(ownerAddress);
6
+ switch (assetID) {
7
+ case ASSET_ID.jUSDT:
8
+ if (network === 'mainnet') {
9
+ builder.storeAddress(JETTON_MASTER_ADDRESSES.jUSDT_MAINNET);
10
+ } else {
11
+ builder.storeAddress(JETTON_MASTER_ADDRESSES.jUSDT_TESTNET);
12
+ }
13
+ break;
14
+ case ASSET_ID.jUSDC:
15
+ if (network === 'mainnet') {
16
+ builder.storeAddress(JETTON_MASTER_ADDRESSES.jUSDC_MAINNET);
17
+ } else {
18
+ builder.storeAddress(JETTON_MASTER_ADDRESSES.jUSDC_TESTNET);
19
+ }
20
+ break;
21
+ default:
22
+ throw new Error('Unsupported asset');
23
+ }
24
+ const data = builder.storeRef(JETTON_WALLET_CODE).endCell();
25
+ const stateInit = beginCell()
26
+ .store(
27
+ storeStateInit({
28
+ code: JETTON_WALLET_CODE,
29
+ data: data,
30
+ }),
31
+ )
32
+ .endCell();
33
+ return new Address(0, stateInit.hash());
34
+ }