@aeternity/aepp-sdk 12.0.0 → 12.1.2

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 (283) hide show
  1. package/dist/aepp-sdk.browser-script.js +1 -1
  2. package/dist/aepp-sdk.browser-script.js.LICENSE.txt +0 -15
  3. package/dist/aepp-sdk.browser-script.js.map +1 -1
  4. package/dist/aepp-sdk.browser.js +2 -1
  5. package/dist/aepp-sdk.browser.js.LICENSE.txt +1 -0
  6. package/dist/aepp-sdk.browser.js.map +1 -1
  7. package/dist/aepp-sdk.js +20765 -1
  8. package/dist/aepp-sdk.js.map +1 -1
  9. package/es/AeSdk.d.ts +7 -7
  10. package/es/AeSdk.mjs.map +1 -1
  11. package/es/AeSdkAepp.d.ts +4 -3
  12. package/es/AeSdkAepp.mjs +2 -1
  13. package/es/AeSdkAepp.mjs.map +1 -1
  14. package/es/AeSdkBase.d.ts +22 -21
  15. package/es/AeSdkBase.mjs +25 -21
  16. package/es/AeSdkBase.mjs.map +1 -1
  17. package/es/AeSdkWallet.d.ts +4 -3
  18. package/es/AeSdkWallet.mjs +5 -1
  19. package/es/AeSdkWallet.mjs.map +1 -1
  20. package/es/Node.d.ts +7 -10
  21. package/es/Node.mjs +63 -57
  22. package/es/Node.mjs.map +1 -1
  23. package/es/account/Base.d.ts +7 -4
  24. package/es/account/Base.mjs +3 -3
  25. package/es/account/Base.mjs.map +1 -1
  26. package/es/account/Memory.d.ts +5 -4
  27. package/es/account/Memory.mjs +24 -2
  28. package/es/account/Memory.mjs.map +1 -1
  29. package/es/account/Rpc.d.ts +6 -6
  30. package/es/account/Rpc.mjs +3 -2
  31. package/es/account/Rpc.mjs.map +1 -1
  32. package/es/aens.d.ts +27 -14
  33. package/es/aens.mjs +27 -21
  34. package/es/aens.mjs.map +1 -1
  35. package/es/aepp-wallet-communication/connection/Browser.d.ts +2 -1
  36. package/es/aepp-wallet-communication/connection/Browser.mjs +5 -2
  37. package/es/aepp-wallet-communication/connection/Browser.mjs.map +1 -1
  38. package/es/aepp-wallet-communication/connection/BrowserRuntime.d.ts +1 -0
  39. package/es/aepp-wallet-communication/connection/BrowserRuntime.mjs +1 -0
  40. package/es/aepp-wallet-communication/connection/BrowserRuntime.mjs.map +1 -1
  41. package/es/aepp-wallet-communication/connection/BrowserWindowMessage.d.ts +1 -0
  42. package/es/aepp-wallet-communication/connection/BrowserWindowMessage.mjs +5 -1
  43. package/es/aepp-wallet-communication/connection/BrowserWindowMessage.mjs.map +1 -1
  44. package/es/aepp-wallet-communication/connection-proxy.d.ts +1 -0
  45. package/es/aepp-wallet-communication/connection-proxy.mjs +1 -0
  46. package/es/aepp-wallet-communication/connection-proxy.mjs.map +1 -1
  47. package/es/aepp-wallet-communication/rpc/RpcClient.d.ts +1 -0
  48. package/es/aepp-wallet-communication/rpc/RpcClient.mjs +5 -5
  49. package/es/aepp-wallet-communication/rpc/RpcClient.mjs.map +1 -1
  50. package/es/aepp-wallet-communication/rpc/types.d.ts +8 -8
  51. package/es/aepp-wallet-communication/rpc/types.mjs.map +1 -1
  52. package/es/aepp-wallet-communication/schema.d.ts +17 -2
  53. package/es/aepp-wallet-communication/schema.mjs +54 -17
  54. package/es/aepp-wallet-communication/schema.mjs.map +1 -1
  55. package/es/aepp-wallet-communication/wallet-detector.d.ts +2 -1
  56. package/es/aepp-wallet-communication/wallet-detector.mjs +2 -3
  57. package/es/aepp-wallet-communication/wallet-detector.mjs.map +1 -1
  58. package/es/apis/node/models/index.d.ts +6 -2
  59. package/es/apis/node/models/index.mjs.map +1 -1
  60. package/es/apis/node/models/mappers.mjs +4 -2
  61. package/es/apis/node/models/mappers.mjs.map +1 -1
  62. package/es/chain.d.ts +92 -64
  63. package/es/chain.mjs +190 -154
  64. package/es/chain.mjs.map +1 -1
  65. package/es/channel/handlers.d.ts +3 -3
  66. package/es/channel/handlers.mjs +42 -44
  67. package/es/channel/handlers.mjs.map +1 -1
  68. package/es/channel/index.d.ts +43 -52
  69. package/es/channel/index.mjs +46 -49
  70. package/es/channel/index.mjs.map +1 -1
  71. package/es/channel/internal.d.ts +16 -15
  72. package/es/channel/internal.mjs +67 -62
  73. package/es/channel/internal.mjs.map +1 -1
  74. package/es/contract/Compiler.d.ts +1 -0
  75. package/es/contract/Compiler.mjs +5 -4
  76. package/es/contract/Compiler.mjs.map +1 -1
  77. package/es/contract/aci.d.ts +17 -15
  78. package/es/contract/aci.mjs +58 -45
  79. package/es/contract/aci.mjs.map +1 -1
  80. package/es/contract/ga.d.ts +16 -15
  81. package/es/contract/ga.mjs +12 -7
  82. package/es/contract/ga.mjs.map +1 -1
  83. package/es/contract/methods.d.ts +9 -6
  84. package/es/contract/methods.mjs +9 -6
  85. package/es/contract/methods.mjs.map +1 -1
  86. package/es/deprecated/index.d.ts +143 -0
  87. package/es/deprecated/index.mjs +162 -0
  88. package/es/deprecated/index.mjs.map +1 -0
  89. package/es/deprecated/methods.d.ts +18 -0
  90. package/es/deprecated/methods.mjs +26 -0
  91. package/es/deprecated/methods.mjs.map +1 -0
  92. package/es/index.d.ts +4 -1
  93. package/es/index.mjs +4 -1
  94. package/es/index.mjs.map +1 -1
  95. package/es/oracle.d.ts +69 -62
  96. package/es/oracle.mjs +121 -103
  97. package/es/oracle.mjs.map +1 -1
  98. package/es/spend.d.ts +16 -15
  99. package/es/spend.mjs +19 -30
  100. package/es/spend.mjs.map +1 -1
  101. package/es/tx/builder/address.d.ts +25 -0
  102. package/es/tx/builder/address.mjs +45 -0
  103. package/es/tx/builder/address.mjs.map +1 -0
  104. package/es/tx/builder/constants.d.ts +55 -26
  105. package/es/tx/builder/constants.mjs +60 -38
  106. package/es/tx/builder/constants.mjs.map +1 -1
  107. package/es/tx/builder/field-types/coin-amount.d.ts +13 -0
  108. package/es/tx/builder/field-types/coin-amount.mjs +26 -0
  109. package/es/tx/builder/field-types/coin-amount.mjs.map +1 -0
  110. package/es/tx/builder/field-types/deposit.d.ts +15 -0
  111. package/es/tx/builder/field-types/deposit.mjs +19 -0
  112. package/es/tx/builder/field-types/deposit.mjs.map +1 -0
  113. package/es/tx/builder/field-types/fee.d.ts +25 -0
  114. package/es/tx/{min-fee.mjs → builder/field-types/fee.mjs} +62 -60
  115. package/es/tx/builder/field-types/fee.mjs.map +1 -0
  116. package/es/tx/builder/field-types/field.d.ts +6 -0
  117. package/es/tx/builder/field-types/field.mjs +15 -0
  118. package/es/tx/builder/field-types/field.mjs.map +1 -0
  119. package/es/tx/builder/field-types/gas-limit.d.ts +12 -0
  120. package/es/tx/builder/field-types/gas-limit.mjs +33 -0
  121. package/es/tx/builder/field-types/gas-limit.mjs.map +1 -0
  122. package/es/tx/builder/field-types/gas-price.d.ts +8 -0
  123. package/es/tx/builder/field-types/gas-price.mjs +18 -0
  124. package/es/tx/builder/field-types/gas-price.mjs.map +1 -0
  125. package/es/tx/builder/field-types/index.d.ts +70 -0
  126. package/es/tx/builder/field-types/index.mjs +25 -0
  127. package/es/tx/builder/field-types/index.mjs.map +1 -0
  128. package/es/tx/builder/field-types/name-fee.d.ts +20 -0
  129. package/es/tx/builder/field-types/name-fee.mjs +24 -0
  130. package/es/tx/builder/field-types/name-fee.mjs.map +1 -0
  131. package/es/tx/builder/field-types/name-id.d.ts +14 -0
  132. package/es/tx/builder/field-types/name-id.mjs +19 -0
  133. package/es/tx/builder/field-types/name-id.mjs.map +1 -0
  134. package/es/tx/builder/field-types/name.d.ts +13 -0
  135. package/es/tx/builder/field-types/name.mjs +18 -0
  136. package/es/tx/builder/field-types/name.mjs.map +1 -0
  137. package/es/tx/builder/field-types/short-u-int.d.ts +6 -0
  138. package/es/tx/builder/field-types/short-u-int.mjs +12 -0
  139. package/es/tx/builder/field-types/short-u-int.mjs.map +1 -0
  140. package/es/tx/builder/field-types/u-int.d.ts +8 -0
  141. package/es/tx/builder/field-types/u-int.mjs +14 -0
  142. package/es/tx/builder/field-types/u-int.mjs.map +1 -0
  143. package/es/tx/builder/helpers.d.ts +27 -33
  144. package/es/tx/builder/helpers.mjs +41 -47
  145. package/es/tx/builder/helpers.mjs.map +1 -1
  146. package/es/tx/builder/index.d.ts +25 -12
  147. package/es/tx/builder/index.mjs +74 -83
  148. package/es/tx/builder/index.mjs.map +1 -1
  149. package/es/tx/builder/schema.d.ts +995 -144
  150. package/es/tx/builder/schema.mjs +121 -170
  151. package/es/tx/builder/schema.mjs.map +1 -1
  152. package/es/tx/index.d.ts +28 -27
  153. package/es/tx/index.mjs +97 -117
  154. package/es/tx/index.mjs.map +1 -1
  155. package/es/tx/validator.d.ts +4 -3
  156. package/es/tx/validator.mjs +79 -92
  157. package/es/tx/validator.mjs.map +1 -1
  158. package/es/typings/@aeternity__argon2-browser/index.d.mjs.map +1 -1
  159. package/es/typings/@aeternity__uuid/index.d.mjs.map +1 -1
  160. package/es/typings/bip32-path/index.d.mjs.map +1 -1
  161. package/es/typings/blakejs/index.d.mjs.map +1 -1
  162. package/es/typings/sha-js/index.d.mjs +2 -0
  163. package/es/typings/sha-js/index.d.mjs.map +1 -0
  164. package/es/typings/tweetnacl-auth/index.d.mjs.map +1 -1
  165. package/es/utils/amount-formatter.d.ts +9 -9
  166. package/es/utils/amount-formatter.mjs +17 -17
  167. package/es/utils/amount-formatter.mjs.map +1 -1
  168. package/es/utils/autorest.mjs +7 -7
  169. package/es/utils/autorest.mjs.map +1 -1
  170. package/es/utils/bignumber.mjs +2 -1
  171. package/es/utils/bignumber.mjs.map +1 -1
  172. package/es/utils/bytes.mjs +1 -1
  173. package/es/utils/bytes.mjs.map +1 -1
  174. package/es/utils/crypto.d.ts +8 -23
  175. package/es/utils/crypto.mjs +15 -41
  176. package/es/utils/crypto.mjs.map +1 -1
  177. package/es/utils/encoder-types.d.ts +61 -0
  178. package/es/utils/encoder-types.mjs +36 -0
  179. package/es/utils/encoder-types.mjs.map +1 -0
  180. package/es/utils/encoder.d.ts +6 -7
  181. package/es/utils/encoder.mjs +42 -26
  182. package/es/utils/encoder.mjs.map +1 -1
  183. package/es/utils/errors.d.ts +1 -11
  184. package/es/utils/errors.mjs +4 -20
  185. package/es/utils/errors.mjs.map +1 -1
  186. package/es/utils/hd-wallet.d.ts +18 -2
  187. package/es/utils/hd-wallet.mjs +64 -34
  188. package/es/utils/hd-wallet.mjs.map +1 -1
  189. package/es/utils/json-big.mjs +2 -2
  190. package/es/utils/json-big.mjs.map +1 -1
  191. package/es/utils/keystore.mjs +4 -2
  192. package/es/utils/keystore.mjs.map +1 -1
  193. package/es/utils/mptree.d.ts +2 -2
  194. package/es/utils/mptree.mjs +11 -6
  195. package/es/utils/mptree.mjs.map +1 -1
  196. package/es/utils/other.d.ts +8 -2
  197. package/es/utils/other.mjs +15 -3
  198. package/es/utils/other.mjs.map +1 -1
  199. package/es/utils/semver-satisfies.d.ts +1 -1
  200. package/es/utils/semver-satisfies.mjs +2 -2
  201. package/es/utils/semver-satisfies.mjs.map +1 -1
  202. package/es/utils/string.mjs.map +1 -1
  203. package/package.json +36 -34
  204. package/src/AeSdk.ts +30 -27
  205. package/src/AeSdkAepp.ts +93 -84
  206. package/src/AeSdkBase.ts +148 -130
  207. package/src/AeSdkWallet.ts +172 -156
  208. package/src/Node.ts +108 -102
  209. package/src/account/Base.ts +42 -32
  210. package/src/account/Memory.ts +45 -30
  211. package/src/account/Rpc.ts +32 -30
  212. package/src/aens.ts +124 -108
  213. package/src/aepp-wallet-communication/connection/Browser.ts +18 -15
  214. package/src/aepp-wallet-communication/connection/BrowserRuntime.ts +22 -21
  215. package/src/aepp-wallet-communication/connection/BrowserWindowMessage.ts +63 -52
  216. package/src/aepp-wallet-communication/connection-proxy.ts +8 -7
  217. package/src/aepp-wallet-communication/rpc/RpcClient.ts +67 -61
  218. package/src/aepp-wallet-communication/rpc/types.ts +32 -32
  219. package/src/aepp-wallet-communication/schema.ts +117 -74
  220. package/src/aepp-wallet-communication/wallet-detector.ts +29 -28
  221. package/src/apis/node/code-model-v4.yaml +54 -20
  222. package/src/apis/node/models/index.ts +6 -2
  223. package/src/apis/node/models/mappers.ts +4 -2
  224. package/src/chain.ts +286 -228
  225. package/src/channel/handlers.ts +424 -372
  226. package/src/channel/index.ts +285 -251
  227. package/src/channel/internal.ts +248 -232
  228. package/src/contract/Compiler.ts +22 -21
  229. package/src/contract/aci.ts +267 -258
  230. package/src/contract/ga.ts +93 -72
  231. package/src/contract/methods.ts +32 -27
  232. package/src/deprecated/index.ts +172 -0
  233. package/src/deprecated/methods.ts +25 -0
  234. package/src/index.ts +37 -32
  235. package/src/oracle.ts +211 -189
  236. package/src/spend.ts +82 -70
  237. package/src/tx/builder/address.ts +50 -0
  238. package/src/tx/builder/constants.ts +82 -52
  239. package/src/tx/builder/field-types/coin-amount.ts +27 -0
  240. package/src/tx/builder/field-types/deposit.ts +21 -0
  241. package/src/tx/builder/field-types/fee.ts +143 -0
  242. package/src/tx/builder/field-types/field.ts +9 -0
  243. package/src/tx/builder/field-types/gas-limit.ts +41 -0
  244. package/src/tx/builder/field-types/gas-price.ts +16 -0
  245. package/src/tx/builder/field-types/index.ts +45 -0
  246. package/src/tx/builder/field-types/name-fee.ts +32 -0
  247. package/src/tx/builder/field-types/name-id.ts +20 -0
  248. package/src/tx/builder/field-types/name.ts +18 -0
  249. package/src/tx/builder/field-types/short-u-int.ts +11 -0
  250. package/src/tx/builder/field-types/u-int.ts +16 -0
  251. package/src/tx/builder/helpers.ts +112 -117
  252. package/src/tx/builder/index.ts +203 -197
  253. package/src/tx/builder/schema.ts +421 -465
  254. package/src/tx/index.ts +128 -135
  255. package/src/tx/validator.ts +165 -161
  256. package/src/typings/@aeternity__argon2-browser/index.d.ts +1 -1
  257. package/src/typings/@aeternity__uuid/index.d.ts +3 -1
  258. package/src/typings/bip32-path/index.d.ts +6 -6
  259. package/src/typings/blakejs/index.d.ts +4 -32
  260. package/src/typings/sha-js/index.d.ts +5 -0
  261. package/src/typings/tweetnacl-auth/index.d.ts +2 -2
  262. package/src/utils/amount-formatter.ts +41 -41
  263. package/src/utils/autorest.ts +57 -54
  264. package/src/utils/bignumber.ts +7 -6
  265. package/src/utils/bytes.ts +19 -19
  266. package/src/utils/crypto.ts +82 -94
  267. package/src/utils/encoder-types.ts +63 -0
  268. package/src/utils/encoder.ts +104 -56
  269. package/src/utils/errors.ts +246 -264
  270. package/src/utils/hd-wallet.ts +114 -82
  271. package/src/utils/json-big.ts +16 -15
  272. package/src/utils/keystore.ts +65 -62
  273. package/src/utils/mptree.ts +59 -56
  274. package/src/utils/other.ts +26 -18
  275. package/src/utils/semver-satisfies.ts +15 -11
  276. package/src/utils/string.ts +10 -10
  277. package/es/tx/builder/field-types.d.ts +0 -62
  278. package/es/tx/builder/field-types.mjs +0 -126
  279. package/es/tx/builder/field-types.mjs.map +0 -1
  280. package/es/tx/min-fee.d.ts +0 -20
  281. package/es/tx/min-fee.mjs.map +0 -1
  282. package/src/tx/builder/field-types.ts +0 -111
  283. package/src/tx/min-fee.ts +0 -136
@@ -1,111 +0,0 @@
1
- import {
2
- writeId, readId, produceNameId, getMinimumNameFee, readInt, writeInt, isNameValid
3
- } from './helpers'
4
- import { InsufficientNameFeeError, IllegalArgumentError } from '../../utils/errors'
5
- import BigNumber from 'bignumber.js'
6
- import { MIN_GAS_PRICE, AensName } from './constants'
7
- import { EncodedData } from '../../utils/encoder'
8
-
9
- type Int = number | string | BigNumber
10
-
11
- // todo: update me
12
- // ? This is a TS workaround because this has to be a class with only static methods
13
- export class Field {
14
- public prototype: any
15
-
16
- static serialize (value: any, _txFields: object = {}): Buffer {
17
- return Buffer.from(value)
18
- }
19
-
20
- static deserialize (value: Buffer): any {
21
- return value.toString()
22
- }
23
- }
24
-
25
- export class Name extends Field {
26
- /**
27
- * @param value - AENS name
28
- */
29
- static serialize (value: AensName): Buffer {
30
- return Buffer.from(value)
31
- }
32
-
33
- /**
34
- * @param value - AENS name
35
- */
36
- static deserialize (value: Buffer): string {
37
- return value.toString()
38
- }
39
- }
40
-
41
- export class NameId extends Field {
42
- /**
43
- * @param value - AENS name ID
44
- */
45
- static serialize (value: AensName | EncodedData<'nm'>): Buffer {
46
- return writeId(isNameValid(value) ? produceNameId(value) : value)
47
- }
48
-
49
- /**
50
- * @param value - AENS name ID Buffer
51
- */
52
- static deserialize (value: Buffer): string {
53
- return readId(value)
54
- }
55
- }
56
-
57
- export class NameFee extends Field {
58
- /**
59
- * @param value - AENS name fee Buffer
60
- * @param txFields - Transaction fields
61
- * @param txFields.name - AENS Name in transaction
62
- */
63
- static serialize (value: Int | undefined, { name }: { name: AensName }): Buffer {
64
- const minNameFee = getMinimumNameFee(name)
65
- value ??= minNameFee
66
- if (minNameFee.gt(value)) {
67
- throw new InsufficientNameFeeError(new BigNumber(value), minNameFee)
68
- }
69
- return writeInt(value)
70
- }
71
-
72
- /**
73
- * @param value - AENS name fee Buffer
74
- * @returns AENS name fee
75
- */
76
- static deserialize (value: Buffer): string {
77
- return readInt(value)
78
- }
79
- }
80
-
81
- export class Deposit extends Field {
82
- /**
83
- * @param value - Deposit value in string format. Should be equal to '0'.
84
- * @returns Deposit value Buffer.
85
- */
86
- static serialize (value: string = '0'): Buffer {
87
- if (parseInt(value) !== 0) throw new IllegalArgumentError(`Contract deposit is not refundable, so it should be equal 0, got ${value.toString()} instead`)
88
- return writeInt(0)
89
- }
90
-
91
- /**
92
- * @param value - Deposit value Buffer.
93
- * @returns Deposit value.
94
- */
95
- static deserialize (value: Buffer): string {
96
- return readInt(value)
97
- }
98
- }
99
-
100
- export class GasPrice extends Field {
101
- static serialize (value: Int = MIN_GAS_PRICE): Buffer {
102
- if (+value < MIN_GAS_PRICE) {
103
- throw new IllegalArgumentError(`Gas price ${value.toString()} must be bigger then ${MIN_GAS_PRICE}`)
104
- }
105
- return writeInt(value)
106
- }
107
-
108
- static deserialize (value: Buffer): string {
109
- return readInt(value)
110
- }
111
- }
package/src/tx/min-fee.ts DELETED
@@ -1,136 +0,0 @@
1
- import BigNumber from 'bignumber.js'
2
- import { MIN_GAS_PRICE, TX_TYPE, TxParamsCommon } from './builder/schema'
3
- import { AE_AMOUNT_FORMATS } from '../utils/amount-formatter'
4
- import { buildTx } from './builder'
5
- import { isKeyOfObject } from '../utils/other'
6
-
7
- const BASE_GAS = 15000
8
- const GAS_PER_BYTE = 20
9
- const KEY_BLOCK_INTERVAL = 3
10
-
11
- /**
12
- * Calculate the Base fee gas
13
- * @see {@link https://github.com/aeternity/protocol/blob/master/consensus/README.md#gas}
14
- * @param txType - The transaction type
15
- * @returns The base fee
16
- * @example
17
- * ```js
18
- * TX_FEE_BASE('channelForceProgress') => new BigNumber(30 * 15000)
19
- * ```
20
- */
21
- const TX_FEE_BASE_GAS = (txType: TX_TYPE): BigNumber => {
22
- const feeFactors = {
23
- [TX_TYPE.channelForceProgress]: 30,
24
- [TX_TYPE.channelOffChain]: 0,
25
- [TX_TYPE.channelOffChainCallContract]: 0,
26
- [TX_TYPE.channelOffChainCreateContract]: 0,
27
- [TX_TYPE.channelOffChainUpdateDeposit]: 0,
28
- [TX_TYPE.channelOffChainUpdateWithdrawal]: 0,
29
- [TX_TYPE.channelOffChainUpdateTransfer]: 0,
30
- [TX_TYPE.contractCreate]: 5,
31
- [TX_TYPE.contractCall]: 12,
32
- [TX_TYPE.gaAttach]: 5,
33
- [TX_TYPE.gaMeta]: 5,
34
- [TX_TYPE.payingFor]: 1 / 5
35
- } as const
36
- const factor = feeFactors[txType as keyof typeof feeFactors] ?? 1
37
- return new BigNumber(factor * BASE_GAS)
38
- }
39
-
40
- /**
41
- * Calculate fee for Other types of transactions
42
- * @see {@link https://github.com/aeternity/protocol/blob/master/consensus/README.md#gas}
43
- * @param txType - The transaction type
44
- * @param txSize - The transaction size
45
- * @returns parameters - The transaction parameters
46
- * @returns parameters.relativeTtl - The relative ttl
47
- * @returns parameters.innerTxSize - The size of the inner transaction
48
- * @returns The Other fee
49
- * @example
50
- * ```js
51
- * TX_FEE_OTHER_GAS('oracleResponse',10, { relativeTtl: 10, innerTxSize: 10 })
52
- * => new BigNumber(10).times(20).plus(Math.ceil(32000 * 10 / Math.floor(60 * 24 * 365 / 2)))
53
- * ```
54
- */
55
- const TX_FEE_OTHER_GAS = (
56
- txType: TX_TYPE,
57
- txSize: number,
58
- { relativeTtl, innerTxSize }: { relativeTtl: number, innerTxSize: number }
59
- ): BigNumber => {
60
- switch (txType) {
61
- case TX_TYPE.oracleRegister:
62
- case TX_TYPE.oracleExtend:
63
- case TX_TYPE.oracleQuery:
64
- case TX_TYPE.oracleResponse:
65
- return new BigNumber(txSize)
66
- .times(GAS_PER_BYTE)
67
- .plus(
68
- Math.ceil(32000 * relativeTtl / Math.floor(60 * 24 * 365 / KEY_BLOCK_INTERVAL))
69
- )
70
- case TX_TYPE.gaMeta:
71
- case TX_TYPE.payingFor:
72
- return new BigNumber(txSize).minus(innerTxSize).times(GAS_PER_BYTE)
73
- default:
74
- return new BigNumber(txSize).times(GAS_PER_BYTE)
75
- }
76
- }
77
-
78
- function getOracleRelativeTtl (params: any, txType: TX_TYPE): number {
79
- const ttlKeys = {
80
- [TX_TYPE.oracleRegister]: 'oracleTtlValue',
81
- [TX_TYPE.oracleExtend]: 'oracleTtlValue',
82
- [TX_TYPE.oracleQuery]: 'queryTtlValue',
83
- [TX_TYPE.oracleResponse]: 'responseTtlValue'
84
- } as const
85
-
86
- if (!isKeyOfObject(txType, ttlKeys)) return 1
87
- else return params[ttlKeys[txType]]
88
- }
89
-
90
- /**
91
- * Calculate fee based on tx type and params
92
- */
93
- function buildFee (
94
- txType: TX_TYPE,
95
- { params, vsn, denomination }:
96
- { params: TxParamsCommon, vsn?: number, denomination?: AE_AMOUNT_FORMATS }
97
- ): BigNumber {
98
- const { rlpEncoded: { length } } = buildTx(params, txType, { vsn, denomination })
99
-
100
- return TX_FEE_BASE_GAS(txType)
101
- .plus(TX_FEE_OTHER_GAS(txType, length, {
102
- relativeTtl: getOracleRelativeTtl(params, txType),
103
- innerTxSize: [TX_TYPE.gaMeta, TX_TYPE.payingFor].includes(txType)
104
- ? params.tx.tx.encodedTx.rlpEncoded.length
105
- : 0
106
- }))
107
- .times(MIN_GAS_PRICE)
108
- }
109
-
110
- /**
111
- * Calculate min fee
112
- * @param txType - Transaction type
113
- * @param options - Options object
114
- * @param options.params - Tx params
115
- * @example
116
- * ```js
117
- * calculateMinFee('spendTx', { gasLimit, params })
118
- * ```
119
- */
120
- export default function calculateMinFee (
121
- txType: TX_TYPE, { params, vsn, denomination }: CalculateMinFeeOptions
122
- ): BigNumber {
123
- let fee = new BigNumber(0)
124
- let previousFee
125
- do {
126
- previousFee = fee
127
- fee = buildFee(txType, { params: { ...params, fee }, vsn, denomination })
128
- } while (!fee.eq(previousFee))
129
- return fee
130
- }
131
-
132
- interface CalculateMinFeeOptions {
133
- params: TxParamsCommon
134
- vsn?: number
135
- denomination?: AE_AMOUNT_FORMATS
136
- }