@ledgerhq/coin-ton 0.7.0-next.0 → 0.7.0-nightly.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 (125) hide show
  1. package/CHANGELOG.md +3 -8
  2. package/lib/__tests__/fixtures/common.fixtures.d.ts +3 -4
  3. package/lib/__tests__/fixtures/common.fixtures.d.ts.map +1 -1
  4. package/lib/__tests__/fixtures/common.fixtures.js +9 -1
  5. package/lib/__tests__/fixtures/common.fixtures.js.map +1 -1
  6. package/lib/__tests__/integration/bridge.integration.test.d.ts.map +1 -1
  7. package/lib/__tests__/integration/bridge.integration.test.js +2 -2
  8. package/lib/__tests__/integration/bridge.integration.test.js.map +1 -1
  9. package/lib/__tests__/unit/api.unit.test.js +1 -0
  10. package/lib/__tests__/unit/api.unit.test.js.map +1 -1
  11. package/lib/__tests__/unit/deviceTransactionConfig.unit.test.js +12 -3
  12. package/lib/__tests__/unit/deviceTransactionConfig.unit.test.js.map +1 -1
  13. package/lib/__tests__/unit/prepareTransaction.unit.test.js +38 -0
  14. package/lib/__tests__/unit/prepareTransaction.unit.test.js.map +1 -1
  15. package/lib/__tests__/unit/utils.unit.test.js +43 -1
  16. package/lib/__tests__/unit/utils.unit.test.js.map +1 -1
  17. package/lib/bridge/js.d.ts +3 -3
  18. package/lib/bridge/js.d.ts.map +1 -1
  19. package/lib/config.d.ts +2 -0
  20. package/lib/config.d.ts.map +1 -1
  21. package/lib/config.js.map +1 -1
  22. package/lib/constants.d.ts +18 -0
  23. package/lib/constants.d.ts.map +1 -1
  24. package/lib/constants.js +20 -1
  25. package/lib/constants.js.map +1 -1
  26. package/lib/deviceTransactionConfig.d.ts.map +1 -1
  27. package/lib/deviceTransactionConfig.js +14 -3
  28. package/lib/deviceTransactionConfig.js.map +1 -1
  29. package/lib/estimateMaxSpendable.d.ts +3 -3
  30. package/lib/estimateMaxSpendable.d.ts.map +1 -1
  31. package/lib/estimateMaxSpendable.js +6 -5
  32. package/lib/estimateMaxSpendable.js.map +1 -1
  33. package/lib/getTransactionStatus.d.ts +3 -3
  34. package/lib/getTransactionStatus.d.ts.map +1 -1
  35. package/lib/getTransactionStatus.js +2 -2
  36. package/lib/getTransactionStatus.js.map +1 -1
  37. package/lib/prepareTransaction.d.ts +3 -3
  38. package/lib/prepareTransaction.d.ts.map +1 -1
  39. package/lib/prepareTransaction.js +1 -2
  40. package/lib/prepareTransaction.js.map +1 -1
  41. package/lib/signOperation.d.ts +4 -4
  42. package/lib/signOperation.d.ts.map +1 -1
  43. package/lib/signOperation.js.map +1 -1
  44. package/lib/synchronisation.d.ts +3 -3
  45. package/lib/synchronisation.d.ts.map +1 -1
  46. package/lib/synchronisation.js +4 -3
  47. package/lib/synchronisation.js.map +1 -1
  48. package/lib/types.d.ts +79 -2
  49. package/lib/types.d.ts.map +1 -1
  50. package/lib/utils.d.ts +8 -4
  51. package/lib/utils.d.ts.map +1 -1
  52. package/lib/utils.js +30 -5
  53. package/lib/utils.js.map +1 -1
  54. package/lib-es/__tests__/fixtures/common.fixtures.d.ts +3 -4
  55. package/lib-es/__tests__/fixtures/common.fixtures.d.ts.map +1 -1
  56. package/lib-es/__tests__/fixtures/common.fixtures.js +9 -1
  57. package/lib-es/__tests__/fixtures/common.fixtures.js.map +1 -1
  58. package/lib-es/__tests__/integration/bridge.integration.test.d.ts.map +1 -1
  59. package/lib-es/__tests__/integration/bridge.integration.test.js +1 -1
  60. package/lib-es/__tests__/integration/bridge.integration.test.js.map +1 -1
  61. package/lib-es/__tests__/unit/api.unit.test.js +1 -0
  62. package/lib-es/__tests__/unit/api.unit.test.js.map +1 -1
  63. package/lib-es/__tests__/unit/deviceTransactionConfig.unit.test.js +12 -3
  64. package/lib-es/__tests__/unit/deviceTransactionConfig.unit.test.js.map +1 -1
  65. package/lib-es/__tests__/unit/prepareTransaction.unit.test.js +15 -0
  66. package/lib-es/__tests__/unit/prepareTransaction.unit.test.js.map +1 -1
  67. package/lib-es/__tests__/unit/utils.unit.test.js +20 -1
  68. package/lib-es/__tests__/unit/utils.unit.test.js.map +1 -1
  69. package/lib-es/bridge/js.d.ts +3 -3
  70. package/lib-es/bridge/js.d.ts.map +1 -1
  71. package/lib-es/bridge/js.js +1 -1
  72. package/lib-es/bridge/js.js.map +1 -1
  73. package/lib-es/config.d.ts +2 -0
  74. package/lib-es/config.d.ts.map +1 -1
  75. package/lib-es/config.js.map +1 -1
  76. package/lib-es/constants.d.ts +18 -0
  77. package/lib-es/constants.d.ts.map +1 -1
  78. package/lib-es/constants.js +19 -0
  79. package/lib-es/constants.js.map +1 -1
  80. package/lib-es/deviceTransactionConfig.d.ts.map +1 -1
  81. package/lib-es/deviceTransactionConfig.js +15 -4
  82. package/lib-es/deviceTransactionConfig.js.map +1 -1
  83. package/lib-es/estimateMaxSpendable.d.ts +3 -3
  84. package/lib-es/estimateMaxSpendable.d.ts.map +1 -1
  85. package/lib-es/estimateMaxSpendable.js +7 -6
  86. package/lib-es/estimateMaxSpendable.js.map +1 -1
  87. package/lib-es/getTransactionStatus.d.ts +3 -3
  88. package/lib-es/getTransactionStatus.d.ts.map +1 -1
  89. package/lib-es/getTransactionStatus.js +2 -2
  90. package/lib-es/getTransactionStatus.js.map +1 -1
  91. package/lib-es/prepareTransaction.d.ts +3 -3
  92. package/lib-es/prepareTransaction.d.ts.map +1 -1
  93. package/lib-es/prepareTransaction.js +1 -2
  94. package/lib-es/prepareTransaction.js.map +1 -1
  95. package/lib-es/signOperation.d.ts +4 -4
  96. package/lib-es/signOperation.d.ts.map +1 -1
  97. package/lib-es/signOperation.js.map +1 -1
  98. package/lib-es/synchronisation.d.ts +3 -3
  99. package/lib-es/synchronisation.d.ts.map +1 -1
  100. package/lib-es/synchronisation.js +4 -3
  101. package/lib-es/synchronisation.js.map +1 -1
  102. package/lib-es/types.d.ts +79 -2
  103. package/lib-es/types.d.ts.map +1 -1
  104. package/lib-es/utils.d.ts +8 -4
  105. package/lib-es/utils.d.ts.map +1 -1
  106. package/lib-es/utils.js +29 -5
  107. package/lib-es/utils.js.map +1 -1
  108. package/package.json +10 -10
  109. package/src/__tests__/fixtures/common.fixtures.ts +12 -5
  110. package/src/__tests__/integration/bridge.integration.test.ts +3 -3
  111. package/src/__tests__/unit/api.unit.test.ts +1 -0
  112. package/src/__tests__/unit/deviceTransactionConfig.unit.test.ts +20 -3
  113. package/src/__tests__/unit/prepareTransaction.unit.test.ts +17 -0
  114. package/src/__tests__/unit/utils.unit.test.ts +22 -1
  115. package/src/bridge/js.ts +3 -3
  116. package/src/config.ts +2 -0
  117. package/src/constants.ts +20 -0
  118. package/src/deviceTransactionConfig.ts +23 -4
  119. package/src/estimateMaxSpendable.ts +13 -17
  120. package/src/getTransactionStatus.ts +9 -9
  121. package/src/prepareTransaction.ts +5 -6
  122. package/src/signOperation.ts +6 -4
  123. package/src/synchronisation.ts +17 -12
  124. package/src/types.ts +103 -1
  125. package/src/utils.ts +39 -8
package/src/utils.ts CHANGED
@@ -1,5 +1,4 @@
1
- import { decodeAccountId, findSubAccountById } from "@ledgerhq/coin-framework/account/index";
2
- import { Account } from "@ledgerhq/types-live";
1
+ import { decodeAccountId } from "@ledgerhq/coin-framework/account/index";
3
2
  import {
4
3
  Builder,
5
4
  SendMode,
@@ -11,17 +10,22 @@ import {
11
10
  } from "@ton/ton";
12
11
  import BigNumber from "bignumber.js";
13
12
  import { estimateFee } from "./bridge/bridgeHelpers/api";
13
+ import { getCoinConfig } from "./config";
14
14
  import {
15
15
  JettonOpCode,
16
16
  MAX_COMMENT_BYTES,
17
17
  TOKEN_TRANSFER_FORWARD_AMOUNT,
18
18
  TOKEN_TRANSFER_MAX_FEE,
19
19
  TOKEN_TRANSFER_QUERY_ID,
20
+ WORKCHAIN,
20
21
  } from "./constants";
21
22
  import {
23
+ KnownJetton,
24
+ TonAccount,
22
25
  TonCell,
23
26
  TonComment,
24
27
  TonPayloadJettonTransfer,
28
+ TonSubAccount,
25
29
  TonTransaction,
26
30
  Transaction,
27
31
  } from "./types";
@@ -55,14 +59,31 @@ export const addressesAreEqual = (addr1: string, addr2: string) => {
55
59
  }
56
60
  };
57
61
 
62
+ /**
63
+ * Returns the known jetton ID and workchain for a given token address.
64
+ * Returns null if the token is not found in the known jettons list.
65
+ */
66
+ function getKnownJettonId(tokenAddress: string, knownJettons: KnownJetton[]) {
67
+ const index = knownJettons.findIndex(jetton => jetton.masterAddress.toString() === tokenAddress);
68
+ return index > -1 ? { jettonId: index, workchain: WORKCHAIN } : null;
69
+ }
70
+
71
+ /**
72
+ * Finds a sub-account by its ID in a TON account.
73
+ * Returns undefined if no matching sub-account is found.
74
+ */
75
+ export function findSubAccountById(account: TonAccount, id: string): TonSubAccount | undefined {
76
+ return account.subAccounts?.find(a => a.id === id) as TonSubAccount | undefined;
77
+ }
78
+
58
79
  /**
59
80
  * Builds a TonTransaction object based on the given transaction details.
60
81
  */
61
- export const buildTonTransaction = (
82
+ export function buildTonTransaction(
62
83
  transaction: Transaction,
63
84
  seqno: number,
64
- account: Account,
65
- ): TonTransaction => {
85
+ account: TonAccount,
86
+ ): TonTransaction {
66
87
  const { subAccountId, useAllAmount, amount, comment: commentTx, recipient } = transaction;
67
88
  let recipientParsed = recipient;
68
89
  // if recipient is not valid calculate fees with empty address
@@ -76,12 +97,16 @@ export const buildTonTransaction = (
76
97
  // if there is a sub account, the transaction is a token transfer
77
98
  const subAccount = findSubAccountById(account, subAccountId ?? "");
78
99
 
100
+ if (subAccount && !subAccount.jettonWallet) {
101
+ throw new Error("[ton] jetton wallet not found");
102
+ }
103
+
79
104
  const finalAmount = subAccount
80
105
  ? toNano(TOKEN_TRANSFER_MAX_FEE) // for commission fees, excess will be returned
81
106
  : useAllAmount
82
107
  ? BigInt(0)
83
108
  : BigInt(amount.toFixed());
84
- const to = subAccount ? subAccount.token.contractAddress : recipientParsed;
109
+ const to = subAccount?.jettonWallet ?? recipientParsed;
85
110
 
86
111
  const tonTransaction: TonTransaction = {
87
112
  to: TonAddress.parse(to),
@@ -102,6 +127,9 @@ export const buildTonTransaction = (
102
127
  if (subAccount) {
103
128
  const forwardPayload = commentTx.text.length ? comment(commentTx.text) : null;
104
129
 
130
+ const currencyConfig = getCoinConfig();
131
+ const knownJettons = currencyConfig.infra.KNOWN_JETTONS;
132
+
105
133
  tonTransaction.payload = {
106
134
  type: "jetton-transfer",
107
135
  queryId: BigInt(TOKEN_TRANSFER_QUERY_ID),
@@ -111,11 +139,14 @@ export const buildTonTransaction = (
111
139
  customPayload: null,
112
140
  forwardAmount: BigInt(TOKEN_TRANSFER_FORWARD_AMOUNT),
113
141
  forwardPayload,
142
+ knownJetton: knownJettons
143
+ ? getKnownJettonId(subAccount?.token.contractAddress, knownJettons)
144
+ : null,
114
145
  };
115
146
  }
116
147
 
117
148
  return tonTransaction;
118
- };
149
+ }
119
150
 
120
151
  /**
121
152
  * Validates if the given comment is valid.
@@ -132,7 +163,7 @@ export const getTransferExpirationTime = () => Math.floor(Date.now() / 1000 + 60
132
163
  * Estimates the fees for a Ton transaction.
133
164
  */
134
165
  export const getTonEstimatedFees = async (
135
- account: Account,
166
+ account: TonAccount,
136
167
  needsInit: boolean,
137
168
  tx: TonTransaction,
138
169
  ) => {