@aeternity/aepp-sdk 10.0.0-beta.1 → 11.0.1

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 (250) hide show
  1. package/README.md +15 -14
  2. package/dist/aepp-sdk.browser-script.js +1 -1
  3. package/dist/aepp-sdk.browser-script.js.LICENSE.txt +7 -27
  4. package/dist/aepp-sdk.browser-script.js.map +1 -1
  5. package/dist/aepp-sdk.browser.js +1 -1
  6. package/dist/aepp-sdk.browser.js.map +1 -1
  7. package/dist/aepp-sdk.js +1 -1
  8. package/dist/aepp-sdk.js.map +1 -1
  9. package/es/account/{base.js → base.mjs} +23 -11
  10. package/es/account/base.mjs.map +1 -0
  11. package/es/account/{memory.js → memory.mjs} +29 -14
  12. package/es/account/memory.mjs.map +1 -0
  13. package/es/account/{multiple.js → multiple.mjs} +14 -11
  14. package/es/account/multiple.mjs.map +1 -0
  15. package/es/ae/{aens.js → aens.mjs} +69 -59
  16. package/es/ae/aens.mjs.map +1 -0
  17. package/es/ae/{aepp.js → aepp.mjs} +9 -9
  18. package/es/ae/aepp.mjs.map +1 -0
  19. package/es/ae/contract.mjs +159 -0
  20. package/es/ae/contract.mjs.map +1 -0
  21. package/es/ae/{index.js → index.mjs} +12 -10
  22. package/es/ae/index.mjs.map +1 -0
  23. package/es/ae/{oracle.js → oracle.mjs} +37 -20
  24. package/es/ae/oracle.mjs.map +1 -0
  25. package/es/ae/{universal.js → universal.mjs} +10 -10
  26. package/es/ae/universal.mjs.map +1 -0
  27. package/es/ae/{wallet.js → wallet.mjs} +10 -10
  28. package/es/ae/wallet.mjs.map +1 -0
  29. package/es/aepp-wallet-communication/schema.d.ts +74 -0
  30. package/es/bignumber.d.ts +1 -7
  31. package/es/chain/{index.js → index.mjs} +28 -8
  32. package/es/chain/index.mjs.map +1 -0
  33. package/es/chain/{node.js → node.mjs} +133 -90
  34. package/es/chain/node.mjs.map +1 -0
  35. package/es/channel/{handlers.js → handlers.mjs} +18 -20
  36. package/es/channel/handlers.mjs.map +1 -0
  37. package/es/channel/{index.js → index.mjs} +43 -22
  38. package/es/channel/index.mjs.map +1 -0
  39. package/es/channel/{internal.js → internal.mjs} +51 -35
  40. package/es/channel/internal.mjs.map +1 -0
  41. package/es/contract/aci.mjs +477 -0
  42. package/es/contract/aci.mjs.map +1 -0
  43. package/es/contract/compiler.mjs +115 -0
  44. package/es/contract/compiler.mjs.map +1 -0
  45. package/es/contract/ga/helpers.mjs +30 -0
  46. package/es/contract/ga/helpers.mjs.map +1 -0
  47. package/es/contract/ga/{index.js → index.mjs} +40 -28
  48. package/es/contract/ga/index.mjs.map +1 -0
  49. package/es/encoder.d.ts +26 -0
  50. package/es/errors.d.ts +284 -0
  51. package/es/index.mjs +57 -0
  52. package/es/index.mjs.map +1 -0
  53. package/es/node-pool/{index.js → index.mjs} +15 -12
  54. package/es/node-pool/index.mjs.map +1 -0
  55. package/es/{node.js → node.mjs} +17 -11
  56. package/es/node.mjs.map +1 -0
  57. package/es/tx/builder/constants.mjs +108 -0
  58. package/es/tx/builder/constants.mjs.map +1 -0
  59. package/es/tx/builder/field-types.mjs +74 -0
  60. package/es/tx/builder/field-types.mjs.map +1 -0
  61. package/es/tx/builder/helpers.mjs +321 -0
  62. package/es/tx/builder/helpers.mjs.map +1 -0
  63. package/es/tx/builder/{index.js → index.mjs} +92 -74
  64. package/es/tx/builder/index.mjs.map +1 -0
  65. package/es/tx/builder/{schema.js → schema.mjs} +16 -172
  66. package/es/tx/builder/schema.mjs.map +1 -0
  67. package/es/tx/{index.js → index.mjs} +106 -14
  68. package/es/tx/index.mjs.map +1 -0
  69. package/es/tx/{tx-object.js → tx-object.mjs} +31 -18
  70. package/es/tx/tx-object.mjs.map +1 -0
  71. package/es/tx/{tx.js → tx.mjs} +15 -14
  72. package/es/tx/tx.mjs.map +1 -0
  73. package/es/tx/{validator.js → validator.mjs} +72 -33
  74. package/es/tx/validator.mjs.map +1 -0
  75. package/es/typings/blakejs/index.d.mjs +2 -0
  76. package/es/typings/blakejs/{index.d.js.map → index.d.mjs.map} +1 -1
  77. package/es/utils/aepp-wallet-communication/connection/{browser-runtime.js → browser-runtime.mjs} +10 -8
  78. package/es/utils/aepp-wallet-communication/connection/browser-runtime.mjs.map +1 -0
  79. package/es/utils/aepp-wallet-communication/connection/{browser-window-message.js → browser-window-message.mjs} +25 -17
  80. package/es/utils/aepp-wallet-communication/connection/browser-window-message.mjs.map +1 -0
  81. package/es/utils/aepp-wallet-communication/connection/{index.js → index.mjs} +7 -3
  82. package/es/utils/aepp-wallet-communication/connection/index.mjs.map +1 -0
  83. package/es/utils/aepp-wallet-communication/{content-script-bridge.js → content-script-bridge.mjs} +15 -8
  84. package/es/utils/aepp-wallet-communication/content-script-bridge.mjs.map +1 -0
  85. package/es/utils/aepp-wallet-communication/{helpers.js → helpers.mjs} +3 -14
  86. package/es/utils/aepp-wallet-communication/helpers.mjs.map +1 -0
  87. package/es/utils/aepp-wallet-communication/rpc/{aepp-rpc.js → aepp-rpc.mjs} +126 -72
  88. package/es/utils/aepp-wallet-communication/rpc/aepp-rpc.mjs.map +1 -0
  89. package/es/utils/aepp-wallet-communication/rpc/{rpc-client.js → rpc-client.mjs} +23 -14
  90. package/es/utils/aepp-wallet-communication/rpc/rpc-client.mjs.map +1 -0
  91. package/es/utils/aepp-wallet-communication/rpc/{wallet-rpc.js → wallet-rpc.mjs} +116 -91
  92. package/es/utils/aepp-wallet-communication/rpc/wallet-rpc.mjs.map +1 -0
  93. package/es/utils/aepp-wallet-communication/schema.mjs +102 -0
  94. package/es/utils/aepp-wallet-communication/schema.mjs.map +1 -0
  95. package/es/utils/aepp-wallet-communication/{wallet-detector.js → wallet-detector.mjs} +6 -5
  96. package/es/utils/aepp-wallet-communication/wallet-detector.mjs.map +1 -0
  97. package/es/utils/{amount-formatter.js → amount-formatter.mjs} +14 -7
  98. package/es/utils/amount-formatter.mjs.map +1 -0
  99. package/es/utils/{async-init.js → async-init.mjs} +8 -11
  100. package/es/utils/async-init.mjs.map +1 -0
  101. package/es/utils/{bignumber.js → bignumber.mjs} +2 -9
  102. package/es/utils/bignumber.mjs.map +1 -0
  103. package/es/utils/{bytes.js → bytes.mjs} +16 -8
  104. package/es/utils/bytes.mjs.map +1 -0
  105. package/es/utils/{crypto-ts.js → crypto-ts.mjs} +11 -3
  106. package/es/utils/crypto-ts.mjs.map +1 -0
  107. package/es/utils/{crypto.js → crypto.mjs} +42 -156
  108. package/es/utils/crypto.mjs.map +1 -0
  109. package/es/utils/encoder.mjs +106 -0
  110. package/es/utils/encoder.mjs.map +1 -0
  111. package/es/utils/errors.mjs +689 -0
  112. package/es/utils/errors.mjs.map +1 -0
  113. package/es/utils/{hd-wallet.js → hd-wallet.mjs} +33 -34
  114. package/es/utils/hd-wallet.mjs.map +1 -0
  115. package/es/utils/{json-big.js → json-big.mjs} +1 -1
  116. package/es/utils/{json-big.js.map → json-big.mjs.map} +1 -1
  117. package/es/utils/{keystore.js → keystore.mjs} +63 -51
  118. package/es/utils/keystore.mjs.map +1 -0
  119. package/es/utils/{mptree.js → mptree.mjs} +21 -12
  120. package/es/utils/mptree.mjs.map +1 -0
  121. package/es/utils/{other.js → other.mjs} +2 -2
  122. package/es/utils/other.mjs.map +1 -0
  123. package/es/utils/{semver-satisfies.js → semver-satisfies.mjs} +1 -1
  124. package/es/utils/{semver-satisfies.js.map → semver-satisfies.mjs.map} +1 -1
  125. package/es/utils/{string.js → string.mjs} +1 -1
  126. package/es/utils/{string.js.map → string.mjs.map} +1 -1
  127. package/es/utils/{swagger.js → swagger.mjs} +49 -15
  128. package/es/utils/swagger.mjs.map +1 -0
  129. package/package.json +49 -58
  130. package/src/account/base.js +3 -1
  131. package/src/account/memory.js +13 -8
  132. package/src/account/multiple.js +16 -6
  133. package/src/ae/aens.js +52 -58
  134. package/src/ae/aepp.js +2 -2
  135. package/src/ae/contract.js +21 -154
  136. package/src/ae/index.js +6 -4
  137. package/src/ae/oracle.js +33 -14
  138. package/src/ae/universal.js +4 -2
  139. package/src/ae/wallet.js +2 -2
  140. package/src/chain/index.js +29 -6
  141. package/src/chain/node.js +95 -65
  142. package/src/channel/handlers.js +30 -20
  143. package/src/channel/index.js +46 -16
  144. package/src/channel/internal.js +35 -30
  145. package/src/contract/aci.js +376 -0
  146. package/src/contract/compiler.js +22 -113
  147. package/src/contract/ga/helpers.js +10 -6
  148. package/src/contract/ga/index.js +31 -24
  149. package/src/index.js +9 -8
  150. package/src/node-pool/index.js +13 -10
  151. package/src/node.js +11 -10
  152. package/src/tx/builder/constants.js +100 -0
  153. package/src/tx/builder/field-types.js +61 -0
  154. package/src/tx/builder/helpers.js +99 -103
  155. package/src/tx/builder/index.js +81 -40
  156. package/src/tx/builder/schema.js +44 -132
  157. package/src/tx/index.js +101 -12
  158. package/src/tx/tx-object.js +35 -15
  159. package/src/tx/tx.js +110 -35
  160. package/src/tx/validator.js +25 -5
  161. package/src/utils/aepp-wallet-communication/connection/browser-runtime.js +11 -5
  162. package/src/utils/aepp-wallet-communication/connection/browser-window-message.js +35 -13
  163. package/src/utils/aepp-wallet-communication/connection/index.js +2 -1
  164. package/src/utils/aepp-wallet-communication/content-script-bridge.js +14 -7
  165. package/src/utils/aepp-wallet-communication/helpers.js +6 -14
  166. package/src/utils/aepp-wallet-communication/rpc/aepp-rpc.js +106 -57
  167. package/src/utils/aepp-wallet-communication/rpc/rpc-client.js +24 -11
  168. package/src/utils/aepp-wallet-communication/rpc/wallet-rpc.js +95 -53
  169. package/src/utils/aepp-wallet-communication/schema.ts +80 -0
  170. package/src/utils/aepp-wallet-communication/wallet-detector.js +6 -2
  171. package/src/utils/amount-formatter.ts +4 -3
  172. package/src/utils/async-init.js +10 -6
  173. package/src/utils/bignumber.ts +1 -9
  174. package/src/utils/bytes.ts +3 -2
  175. package/src/utils/crypto-ts.ts +2 -2
  176. package/src/utils/crypto.js +11 -155
  177. package/src/utils/encoder.ts +101 -0
  178. package/src/utils/errors.ts +683 -0
  179. package/src/utils/hd-wallet.js +11 -26
  180. package/src/utils/keystore.js +57 -53
  181. package/src/utils/mptree.ts +12 -6
  182. package/src/utils/swagger.js +68 -45
  183. package/es/account/base.js.map +0 -1
  184. package/es/account/memory.js.map +0 -1
  185. package/es/account/multiple.js.map +0 -1
  186. package/es/ae/aens.js.map +0 -1
  187. package/es/ae/aepp.js.map +0 -1
  188. package/es/ae/contract.js +0 -308
  189. package/es/ae/contract.js.map +0 -1
  190. package/es/ae/index.js.map +0 -1
  191. package/es/ae/oracle.js.map +0 -1
  192. package/es/ae/universal.js.map +0 -1
  193. package/es/ae/wallet.js.map +0 -1
  194. package/es/chain/index.js.map +0 -1
  195. package/es/chain/node.js.map +0 -1
  196. package/es/channel/handlers.js.map +0 -1
  197. package/es/channel/index.js.map +0 -1
  198. package/es/channel/internal.js.map +0 -1
  199. package/es/contract/aci/helpers.js +0 -298
  200. package/es/contract/aci/helpers.js.map +0 -1
  201. package/es/contract/aci/index.js +0 -356
  202. package/es/contract/aci/index.js.map +0 -1
  203. package/es/contract/aci/transformation.js +0 -66
  204. package/es/contract/aci/transformation.js.map +0 -1
  205. package/es/contract/compiler.js +0 -242
  206. package/es/contract/compiler.js.map +0 -1
  207. package/es/contract/ga/helpers.js +0 -22
  208. package/es/contract/ga/helpers.js.map +0 -1
  209. package/es/contract/ga/index.js.map +0 -1
  210. package/es/contract/index.js +0 -158
  211. package/es/contract/index.js.map +0 -1
  212. package/es/index.js +0 -56
  213. package/es/index.js.map +0 -1
  214. package/es/node-pool/index.js.map +0 -1
  215. package/es/node.js.map +0 -1
  216. package/es/tx/builder/helpers.js +0 -318
  217. package/es/tx/builder/helpers.js.map +0 -1
  218. package/es/tx/builder/index.js.map +0 -1
  219. package/es/tx/builder/schema.js.map +0 -1
  220. package/es/tx/index.js.map +0 -1
  221. package/es/tx/tx-object.js.map +0 -1
  222. package/es/tx/tx.js.map +0 -1
  223. package/es/tx/validator.js.map +0 -1
  224. package/es/typings/blakejs/index.d.js +0 -2
  225. package/es/utils/aepp-wallet-communication/connection/browser-runtime.js.map +0 -1
  226. package/es/utils/aepp-wallet-communication/connection/browser-window-message.js.map +0 -1
  227. package/es/utils/aepp-wallet-communication/connection/index.js.map +0 -1
  228. package/es/utils/aepp-wallet-communication/content-script-bridge.js.map +0 -1
  229. package/es/utils/aepp-wallet-communication/helpers.js.map +0 -1
  230. package/es/utils/aepp-wallet-communication/rpc/aepp-rpc.js.map +0 -1
  231. package/es/utils/aepp-wallet-communication/rpc/rpc-client.js.map +0 -1
  232. package/es/utils/aepp-wallet-communication/rpc/wallet-rpc.js.map +0 -1
  233. package/es/utils/aepp-wallet-communication/schema.js +0 -107
  234. package/es/utils/aepp-wallet-communication/schema.js.map +0 -1
  235. package/es/utils/aepp-wallet-communication/wallet-detector.js.map +0 -1
  236. package/es/utils/amount-formatter.js.map +0 -1
  237. package/es/utils/async-init.js.map +0 -1
  238. package/es/utils/bignumber.js.map +0 -1
  239. package/es/utils/bytes.js.map +0 -1
  240. package/es/utils/crypto-ts.js.map +0 -1
  241. package/es/utils/crypto.js.map +0 -1
  242. package/es/utils/hd-wallet.js.map +0 -1
  243. package/es/utils/keystore.js.map +0 -1
  244. package/es/utils/mptree.js.map +0 -1
  245. package/es/utils/other.js.map +0 -1
  246. package/es/utils/swagger.js.map +0 -1
  247. package/src/contract/aci/index.js +0 -310
  248. package/src/contract/aci/transformation.js +0 -72
  249. package/src/contract/index.js +0 -159
  250. package/src/utils/aepp-wallet-communication/schema.js +0 -108
package/src/ae/wallet.js CHANGED
@@ -26,7 +26,7 @@ import Ae from './'
26
26
  import Chain from '../chain/node'
27
27
  import Tx from '../tx/tx'
28
28
  import Contract from './contract'
29
- import GeneralizeAccount from '../contract/ga'
29
+ import GeneralizedAccount from '../contract/ga'
30
30
  import WalletRpc from '../utils/aepp-wallet-communication/rpc/wallet-rpc'
31
31
  import Oracle from './oracle'
32
32
  import Aens from './aens'
@@ -47,4 +47,4 @@ import Aens from './aens'
47
47
  address: keypair.publicKey,
48
48
  })
49
49
  */
50
- export default Ae.compose(WalletRpc, Tx, Contract, Oracle, Aens, GeneralizeAccount, Chain)
50
+ export default Ae.compose(WalletRpc, Tx, Contract, Oracle, Aens, GeneralizedAccount, Chain)
@@ -37,13 +37,32 @@ import { required } from '@stamp/required'
37
37
  * @return {Object} Chain instance
38
38
  */
39
39
  const Chain = stampit({
40
- deepProps: { Ae: { defaults: { waitMined: true, verify: true } } }
40
+ deepProps: {
41
+ Ae: {
42
+ defaults: {
43
+ waitMined: true,
44
+ verify: true,
45
+ _expectedMineRate: 180000,
46
+ _microBlockCycle: 3000,
47
+ _maxPollInterval: 5000
48
+ }
49
+ }
50
+ },
51
+ methods: {
52
+ _getPollInterval (type) {
53
+ const base = {
54
+ block: this.Ae.defaults._expectedMineRate,
55
+ microblock: this.Ae.defaults._microBlockCycle
56
+ }[type]
57
+ if (!base) throw new Error(`Unknown poll type: ${type}`)
58
+ return Math.min(base / 3, this.Ae.defaults._maxPollInterval)
59
+ }
60
+ }
41
61
  }, required({
42
62
  methods: {
43
63
  sendTransaction: required,
44
64
  height: required,
45
65
  awaitHeight: required,
46
- topBlock: required,
47
66
  poll: required,
48
67
  balance: required,
49
68
  getBalance: required,
@@ -113,8 +132,10 @@ const Chain = stampit({
113
132
  * @rtype (address: String, options?: Object) => balance: Number
114
133
  * @param {String} address - The public account address to obtain the balance for
115
134
  * @param {Object} [options={}] - Options
116
- * @param {Number} options.height - The chain height at which to obtain the balance for (default: top of chain)
117
- * @param {String} options.hash - The block hash on which to obtain the balance for (default: top of chain)
135
+ * @param {Number} options.height - The chain height at which to obtain the balance for (default:
136
+ * top of chain)
137
+ * @param {String} options.hash - The block hash on which to obtain the balance for (default: top
138
+ * of chain)
118
139
  * @return {Object} The transaction as it was mined
119
140
  */
120
141
 
@@ -126,7 +147,8 @@ const Chain = stampit({
126
147
  * @category async
127
148
  * @rtype (hash: String, info = false) => tx: Object
128
149
  * @param {String} hash - Transaction hash
129
- * @param {Boolean} info - Retrieve additional transaction date. Works only for (ContractCreate and ContractCall transaction's)
150
+ * @param {Boolean} info - Retrieve additional transaction date. Works only for (ContractCreate and
151
+ * ContractCall transaction's)
130
152
  * @return {Object} Transaction
131
153
  */
132
154
 
@@ -239,7 +261,8 @@ const Chain = stampit({
239
261
  * @param {String} tx - transaction to execute
240
262
  * @param {String} accountAddress - address that will be used to execute transaction
241
263
  * @param {String|Number} [options.top] - hash of block on which to make dry-run
242
- * @param {Boolean} [options.txEvents] - collect and return on-chain tx events that would result from the call
264
+ * @param {Boolean} [options.txEvents] - collect and return on-chain tx events that would result
265
+ * from the call
243
266
  * @return {Object} Result
244
267
  */
245
268
 
package/src/chain/node.js CHANGED
@@ -20,7 +20,17 @@ import verifyTransaction from '../tx/validator'
20
20
  import NodePool from '../node-pool'
21
21
  import { pause } from '../utils/other'
22
22
  import { isNameValid, produceNameId, decode } from '../tx/builder/helpers'
23
- import { DRY_RUN_ACCOUNT, NAME_ID_KEY } from '../tx/builder/schema'
23
+ import { DRY_RUN_ACCOUNT } from '../tx/builder/schema'
24
+ import {
25
+ AensPointerContextError,
26
+ DryRunError,
27
+ InvalidAensNameError,
28
+ InvalidTxError,
29
+ RequestTimedOutError,
30
+ TxTimedOutError,
31
+ TxNotInChainError,
32
+ ArgumentError
33
+ } from '../utils/errors'
24
34
 
25
35
  /**
26
36
  * ChainNode module
@@ -32,13 +42,13 @@ import { DRY_RUN_ACCOUNT, NAME_ID_KEY } from '../tx/builder/schema'
32
42
  */
33
43
 
34
44
  async function sendTransaction (tx, options = {}) {
35
- const { waitMined, verify } = { ...this.Ae.defaults, ...options }
36
- if (verify) {
45
+ const opt = { ...this.Ae.defaults, ...options }
46
+ if (opt.verify) {
37
47
  const validation = await verifyTransaction(tx, this.selectedNode.instance)
38
48
  if (validation.length) {
39
49
  const message = 'Transaction verification errors: ' +
40
50
  validation.map(v => v.message).join(', ')
41
- throw Object.assign(new Error(message), {
51
+ throw Object.assign(new InvalidTxError(message), {
42
52
  code: 'TX_VERIFICATION_ERROR',
43
53
  validation,
44
54
  transaction: tx
@@ -47,9 +57,11 @@ async function sendTransaction (tx, options = {}) {
47
57
  }
48
58
 
49
59
  try {
50
- const { txHash } = await this.api.postTransaction({ tx })
60
+ const { txHash } = await this.api.postTransaction({ tx }, {
61
+ __queue: `tx-${await this.address?.(options).catch(() => '')}`
62
+ })
51
63
 
52
- if (waitMined) {
64
+ if (opt.waitMined) {
53
65
  const txData = { ...await this.poll(txHash, options), rawTx: tx }
54
66
  // wait for transaction confirmation
55
67
  if (options.confirm) {
@@ -59,18 +71,21 @@ async function sendTransaction (tx, options = {}) {
59
71
  }
60
72
  return { hash: txHash, rawTx: tx }
61
73
  } catch (error) {
62
- throw Object.assign(error, { rawTx: tx, verifyTx: () => verifyTransaction(tx, this.selectedNode.instance) })
74
+ throw Object.assign(error, {
75
+ rawTx: tx,
76
+ verifyTx: () => verifyTransaction(tx, this.selectedNode.instance)
77
+ })
63
78
  }
64
79
  }
65
80
 
66
81
  async function waitForTxConfirm (txHash, options = { confirm: 3 }) {
67
82
  options.confirm = options.confirm === true ? 3 : options.confirm
68
- const { blockHeight } = await this.tx(txHash)
83
+ const { blockHeight } = await this.api.getTransactionByHash(txHash)
69
84
  const height = await this.awaitHeight(blockHeight + options.confirm, options)
70
- const { blockHeight: newBlockHeight } = await this.tx(txHash)
85
+ const { blockHeight: newBlockHeight } = await this.api.getTransactionByHash(txHash)
71
86
  switch (newBlockHeight) {
72
87
  case -1:
73
- throw new Error(`Transaction ${txHash} is removed from chain`)
88
+ throw new TxNotInChainError(txHash)
74
89
  case blockHeight:
75
90
  return height
76
91
  default:
@@ -100,6 +115,9 @@ async function getBalance (address, { height, hash, format = AE_AMOUNT_FORMATS.A
100
115
  return formatAmount(balance, { targetDenomination: format }).toString()
101
116
  }
102
117
 
118
+ /**
119
+ * @deprecated use `sdk.api.getTransactionByHash/getTransactionInfoByHash` instead
120
+ */
103
121
  async function tx (hash, info = true) {
104
122
  const tx = await this.api.getTransactionByHash(hash)
105
123
  if (['ContractCreateTx', 'ContractCallTx', 'ChannelForceProgressTx'].includes(tx.tx.type) && info && tx.blockHeight !== -1) {
@@ -111,45 +129,36 @@ async function tx (hash, info = true) {
111
129
  }
112
130
 
113
131
  async function height () {
114
- try {
115
- if (!this._heightPromise) {
116
- this._heightPromise = this.api.getCurrentKeyBlockHeight()
117
- }
118
- return (await this._heightPromise).height
119
- } finally {
120
- delete this._heightPromise
121
- }
132
+ return (await this.api.getCurrentKeyBlockHeight()).height
122
133
  }
123
134
 
124
- async function awaitHeight (height, { interval = 5000, attempts = 20 } = {}) {
135
+ async function awaitHeight (
136
+ height,
137
+ { interval = this._getPollInterval('block'), attempts = 20 } = {}
138
+ ) {
125
139
  let currentHeight
126
140
  for (let i = 0; i < attempts; i++) {
127
141
  if (i) await pause(interval)
128
142
  currentHeight = await this.height()
129
143
  if (currentHeight >= height) return currentHeight
130
144
  }
131
- throw new Error(`Giving up after ${(attempts - 1) * interval}ms, current height: ${currentHeight}, desired height: ${height}`)
132
- }
133
-
134
- /**
135
- * @deprecated
136
- */
137
- async function topBlock () {
138
- return this.api.getTopHeader()
145
+ throw new RequestTimedOutError((attempts - 1) * interval, currentHeight, height)
139
146
  }
140
147
 
141
- async function poll (th, { blocks = 10, interval = 500, allowUnsynced = false } = {}) {
148
+ async function poll (th, { blocks = 10, interval = this._getPollInterval('microblock') } = {}) {
142
149
  const max = await this.height() + blocks
143
150
  do {
144
- const tx = await this.tx(th).catch(_ => null)
145
- if (tx && (tx.blockHeight !== -1 || (allowUnsynced && tx.height))) {
146
- return tx
147
- }
151
+ const tx = await this.api.getTransactionByHash(th)
152
+ if (tx.blockHeight !== -1) return tx
148
153
  await pause(interval)
149
154
  } while (await this.height() < max)
150
- throw new Error(`Giving up after ${blocks} blocks mined, transaction hash: ${th}`)
155
+ const status = this.api.getCheckTxInPool && (await this.api.getCheckTxInPool(th)).status
156
+ throw new TxTimedOutError(blocks, th, status)
151
157
  }
152
158
 
159
+ /**
160
+ * @deprecated use `sdk.api.getTransactionInfoByHash` instead
161
+ */
153
162
  async function getTxInfo (hash) {
154
163
  const result = await this.api.getTransactionInfoByHash(hash)
155
164
  return result.callInfo || result
@@ -166,7 +175,7 @@ async function getCurrentGeneration () {
166
175
  async function getGeneration (hashOrHeight) {
167
176
  if (typeof hashOrHeight === 'string') return this.api.getGenerationByHash(hashOrHeight)
168
177
  if (typeof hashOrHeight === 'number') return this.api.getGenerationByHeight(hashOrHeight)
169
- throw new Error('Invalid param, param must be hash or height')
178
+ throw new ArgumentError('hashOrHeight', 'a string or number', hashOrHeight)
170
179
  }
171
180
 
172
181
  async function getMicroBlockTransactions (hash) {
@@ -176,30 +185,53 @@ async function getMicroBlockTransactions (hash) {
176
185
  async function getKeyBlock (hashOrHeight) {
177
186
  if (typeof hashOrHeight === 'string') return this.api.getKeyBlockByHash(hashOrHeight)
178
187
  if (typeof hashOrHeight === 'number') return this.api.getKeyBlockByHeight(hashOrHeight)
179
- throw new Error('Invalid param, param must be hash or height')
188
+ throw new ArgumentError('hashOrHeight', 'a string or number', hashOrHeight)
180
189
  }
181
190
 
182
191
  async function getMicroBlockHeader (hash) {
183
192
  return this.api.getMicroBlockHeaderByHash(hash)
184
193
  }
185
194
 
186
- async function txDryRun (tx, accountAddress, options) {
187
- const { results: [{ result, reason, ...resultPayload }], ...other } =
188
- await this.api.protectedDryRunTxs({
189
- ...options,
190
- txs: [{ tx }],
191
- accounts: [{
192
- pubKey: accountAddress,
193
- amount: DRY_RUN_ACCOUNT.amount
194
- }]
195
+ async function txDryRunHandler (key) {
196
+ const rs = this._txDryRun[key]
197
+ delete this._txDryRun[key]
198
+
199
+ let dryRunRes
200
+ try {
201
+ dryRunRes = await this.api.protectedDryRunTxs({
202
+ top: rs[0].top,
203
+ txEvents: rs[0].txEvents,
204
+ txs: rs.map(req => ({ tx: req.tx })),
205
+ accounts: Array.from(new Set(rs.map(req => req.accountAddress)))
206
+ .map(pubKey => ({ pubKey, amount: DRY_RUN_ACCOUNT.amount }))
195
207
  })
208
+ } catch (error) {
209
+ rs.forEach(({ reject }) => reject(error))
210
+ return
211
+ }
196
212
 
197
- if (result === 'ok') return { ...resultPayload, ...other }
213
+ const { results, txEvents } = dryRunRes
214
+ results.forEach(({ result, reason, ...resultPayload }, idx) => {
215
+ const { resolve, reject, tx, options, accountAddress } = rs[idx]
216
+ if (result === 'ok') return resolve({ ...resultPayload, txEvents })
217
+ reject(Object.assign(
218
+ new DryRunError(reason), { tx, accountAddress, options }
219
+ ))
220
+ })
221
+ }
198
222
 
199
- throw Object.assign(
200
- new Error('Dry run error, ' + reason),
201
- { tx, accountAddress, options }
202
- )
223
+ async function txDryRun (tx, accountAddress, { top, txEvents, combine }) {
224
+ const key = combine ? [top, txEvents].join() : 'immediate'
225
+ this._txDryRun ??= {}
226
+ this._txDryRun[key] ??= []
227
+ return new Promise((resolve, reject) => {
228
+ this._txDryRun[key].push({ tx, accountAddress, top, txEvents, resolve, reject })
229
+ if (!combine) {
230
+ txDryRunHandler.call(this, key)
231
+ return
232
+ }
233
+ this._txDryRun[key].timeout ??= setTimeout(txDryRunHandler.bind(this, key))
234
+ })
203
235
  }
204
236
 
205
237
  async function getContractByteCode (contractId) {
@@ -217,42 +249,41 @@ async function getName (name) {
217
249
  /**
218
250
  * Resolve AENS name and return name hash
219
251
  * @param {String} nameOrId
220
- * @param {String} prefix
252
+ * @param {String} key in AENS pointers record
221
253
  * @param {Object} [options]
222
254
  * @param {Boolean} [options.verify] To ensure that name exist and have a corresponding pointer
223
- * @param {Boolean} [options.resolveByNode] Enables pointer resolving using node // TODO: avoid that to don't trust to current api gateway
255
+ * // TODO: avoid that to don't trust to current api gateway
256
+ * @param {Boolean} [options.resolveByNode] Enables pointer resolving using node
224
257
  * @return {String} Address or AENS name hash
225
258
  */
226
- async function resolveName (nameOrId, prefix, { verify, resolveByNode } = {}) {
259
+ async function resolveName (nameOrId, key, { verify, resolveByNode } = {}) {
227
260
  if (!nameOrId || typeof nameOrId !== 'string') {
228
- throw new Error(`Name or address should be a string: ${nameOrId}`)
229
- }
230
- const prefixes = Object.keys(NAME_ID_KEY)
231
- if (!prefixes.includes(prefix)) {
232
- throw new Error(`Invalid prefix ${prefix}, should be one of [${prefixes}]`)
261
+ throw new InvalidAensNameError(`Name or address should be a string: ${nameOrId}`)
233
262
  }
234
263
  try {
235
- decode(nameOrId, prefix)
264
+ decode(nameOrId)
236
265
  return nameOrId
237
266
  } catch (error) {}
238
267
  if (isNameValid(nameOrId)) {
239
268
  if (verify || resolveByNode) {
240
- const name = await this.getName(nameOrId).catch(_ => null)
241
- if (!name) throw new Error(`Name not found: ${nameOrId}`)
242
- const pointer = name.pointers.find(({ id }) => id.split('_')[0] === prefix)
243
- if (!pointer) throw new Error(`Name ${nameOrId} don't have pointers for ${prefix}`)
269
+ const name = await this.api.getNameEntryByName(nameOrId)
270
+ const pointer = name.pointers.find(pointer => pointer.key === key)
271
+ if (!pointer) {
272
+ throw new AensPointerContextError(`Name ${nameOrId} don't have pointers for ${key}`)
273
+ }
244
274
  if (resolveByNode) return pointer.id
245
275
  }
246
276
  return produceNameId(nameOrId)
247
277
  }
248
- throw new Error(`Invalid name or address: ${nameOrId}`)
278
+ throw new InvalidAensNameError(`Invalid name or address: ${nameOrId}`)
249
279
  }
250
280
 
251
281
  /**
252
282
  * ChainNode Stamp
253
283
  *
254
284
  * This is implementation of {@link module:@aeternity/aepp-sdk/es/chain--Chain}
255
- * composed with {@link module:@aeternity/aepp-sdk/es/contract/node--ContractNodeAPI} and {@link module:@aeternity/aepp-sdk/es/oracle/node--OracleNodeAPI}
285
+ * composed with {@link module:@aeternity/aepp-sdk/es/contract/node--ContractNodeAPI} and
286
+ * {@link module:@aeternity/aepp-sdk/es/oracle/node--OracleNodeAPI}
256
287
  * @function
257
288
  * @alias module:@aeternity/aepp-sdk/es/chain/node
258
289
  * @rtype Stamp
@@ -266,7 +297,6 @@ const ChainNode = Chain.compose(NodePool, {
266
297
  balance,
267
298
  getBalance,
268
299
  getAccount,
269
- topBlock,
270
300
  tx,
271
301
  height,
272
302
  awaitHeight,
@@ -15,7 +15,7 @@
15
15
  * PERFORMANCE OF THIS SOFTWARE.
16
16
  */
17
17
 
18
- import { generateKeyPair, encodeContractAddress, encodeBase64Check } from '../utils/crypto'
18
+ import { generateKeyPair, encodeContractAddress } from '../utils/crypto'
19
19
  import {
20
20
  options,
21
21
  changeStatus,
@@ -28,20 +28,23 @@ import {
28
28
  fsmId
29
29
  } from './internal'
30
30
  import { unpackTx, buildTx } from '../tx/builder'
31
-
32
- function encodeRlpTx (rlpBinary) {
33
- return `tx_${encodeBase64Check(rlpBinary)}`
34
- }
31
+ import { encode } from '../tx/builder/helpers'
32
+ import {
33
+ IllegalArgumentError,
34
+ InsufficientBalanceError,
35
+ ChannelConnectionError,
36
+ UnexpectedChannelMessageError
37
+ } from '../utils/errors'
35
38
 
36
39
  async function appendSignature (tx, signFn) {
37
40
  const { signatures, encodedTx } = unpackTx(tx).tx
38
- const result = await signFn(encodeRlpTx(encodedTx.rlpEncoded))
41
+ const result = await signFn(encode(encodedTx.rlpEncoded, 'tx'))
39
42
  if (typeof result === 'string') {
40
43
  const { tx: signedTx, txType } = unpackTx(result)
41
- return encodeRlpTx(buildTx({
44
+ return buildTx({
42
45
  signatures: signatures.concat(signedTx.signatures),
43
46
  encodedTx: signedTx.encodedTx.rlpEncoded
44
- }, txType).rlpEncoded)
47
+ }, txType).tx
45
48
  }
46
49
  return result
47
50
  }
@@ -49,7 +52,7 @@ async function appendSignature (tx, signFn) {
49
52
  function handleUnexpectedMessage (channel, message, state) {
50
53
  if (state && state.reject) {
51
54
  state.reject(Object.assign(
52
- new Error(`Unexpected message received:\n\n${JSON.stringify(message)}`),
55
+ new UnexpectedChannelMessageError(`Unexpected message received:\n\n${JSON.stringify(message)}`),
53
56
  { wsMessage: message }
54
57
  ))
55
58
  }
@@ -75,7 +78,7 @@ export function awaitingConnection (channel, message, state) {
75
78
  return { handler: awaitingConnection }
76
79
  }
77
80
  if (message.method === 'channels.error') {
78
- emit(channel, 'error', new Error(message.payload.message))
81
+ emit(channel, 'error', new ChannelConnectionError(message.payload.message))
79
82
  return { handler: channelClosed }
80
83
  }
81
84
  }
@@ -102,7 +105,9 @@ export async function awaitingChannelCreateTx (channel, message, state) {
102
105
  send(channel, { jsonrpc: '2.0', method: `channels.${tag}`, params: { tx: signedTx } })
103
106
  return { handler: awaitingOnChainTx }
104
107
  }
105
- const signedTx = await appendSignature(message.params.data.signed_tx, tx => options.get(channel).sign(tag, tx))
108
+ const signedTx = await appendSignature(
109
+ message.params.data.signed_tx, tx => options.get(channel).sign(tag, tx)
110
+ )
106
111
  send(channel, { jsonrpc: '2.0', method: `channels.${tag}`, params: { signed_tx: signedTx } })
107
112
  return { handler: awaitingOnChainTx }
108
113
  }
@@ -249,17 +254,17 @@ export async function awaitingOffChainTx (channel, message, state) {
249
254
  }
250
255
  }
251
256
  if (message.method === 'channels.error') {
252
- state.reject(new Error(message.data.message))
257
+ state.reject(new ChannelConnectionError(message.data.message))
253
258
  return { handler: channelOpen }
254
259
  }
255
260
  if (message.error) {
256
261
  const { data = [] } = message.error
257
262
  if (data.find(i => i.code === 1001)) {
258
- state.reject(new Error('Insufficient balance'))
263
+ state.reject(new InsufficientBalanceError('Insufficient balance'))
259
264
  } else if (data.find(i => i.code === 1002)) {
260
- state.reject(new Error('Amount cannot be negative'))
265
+ state.reject(new IllegalArgumentError('Amount cannot be negative'))
261
266
  } else {
262
- state.reject(new Error(message.error.message))
267
+ state.reject(new ChannelConnectionError(message.error.message))
263
268
  }
264
269
  return { handler: channelOpen }
265
270
  }
@@ -301,7 +306,7 @@ export function awaitingOffChainUpdate (channel, message, state) {
301
306
  }
302
307
  }
303
308
  if (message.error) {
304
- state.reject(new Error(message.error.message))
309
+ state.reject(new ChannelConnectionError(message.error.message))
305
310
  return { handler: channelOpen }
306
311
  }
307
312
  return handleUnexpectedMessage(channel, message, state)
@@ -385,7 +390,7 @@ export function awaitingLeave (channel, message, state) {
385
390
  return { handler: channelClosed }
386
391
  }
387
392
  if (message.method === 'channels.error') {
388
- state.reject(new Error(message.data.message))
393
+ state.reject(new ChannelConnectionError(message.data.message))
389
394
  return { handler: channelOpen }
390
395
  }
391
396
  return handleUnexpectedMessage(channel, message, state)
@@ -574,11 +579,16 @@ export function awaitingNewContractCompletion (channel, message, state) {
574
579
  export async function awaitingCallContractUpdateTx (channel, message, state) {
575
580
  if (message.method === 'channels.sign.update') {
576
581
  if (message.params.data.tx) {
577
- const signedTx = await state.sign(message.params.data.tx, { updates: message.params.data.updates })
582
+ const signedTx = await state.sign(
583
+ message.params.data.tx, { updates: message.params.data.updates }
584
+ )
578
585
  send(channel, { jsonrpc: '2.0', method: 'channels.update', params: { tx: signedTx } })
579
586
  return { handler: awaitingCallContractCompletion, state }
580
587
  }
581
- const signedTx = await appendSignature(message.params.data.signed_tx, tx => state.sign(tx, { updates: message.params.data.updates }))
588
+ const signedTx = await appendSignature(
589
+ message.params.data.signed_tx,
590
+ tx => state.sign(tx, { updates: message.params.data.updates })
591
+ )
582
592
  if (typeof signedTx === 'string') {
583
593
  send(channel, { jsonrpc: '2.0', method: 'channels.update', params: { signed_tx: signedTx } })
584
594
  return { handler: awaitingCallContractCompletion, state }
@@ -644,7 +654,7 @@ export function awaitingCallsPruned (channels, message, state) {
644
654
  state.resolve()
645
655
  return { handler: channelOpen }
646
656
  }
647
- state.reject(new Error('Unexpected message received'))
657
+ state.reject(new UnexpectedChannelMessageError('Unexpected message received'))
648
658
  return { handler: channelClosed }
649
659
  }
650
660
 
@@ -338,7 +338,8 @@ function shutdown (sign) {
338
338
  * @param {Number} amount - Amount of tokens to withdraw
339
339
  * @param {Function} sign - Function which verifies and signs withdraw transaction
340
340
  * @param {Object} [callbacks]
341
- * @param {Function} [callbacks.onOnChainTx] - Called when withdraw transaction has been posted on chain
341
+ * @param {Function} [callbacks.onOnChainTx] - Called when withdraw transaction has been posted
342
+ * on chain
342
343
  * @param {Function} [callbacks.onOwnWithdrawLocked]
343
344
  * @param {Function} [callbacks.onWithdrawLocked]
344
345
  * @return {Promise<Object>}
@@ -407,7 +408,8 @@ function withdraw (amount, sign, { onOnChainTx, onOwnWithdrawLocked, onWithdrawL
407
408
  * @param {Number} amount - Amount of tokens to deposit
408
409
  * @param {Function} sign - Function which verifies and signs deposit transaction
409
410
  * @param {Object} [callbacks]
410
- * @param {Function} [callbacks.onOnChainTx] - Called when deposit transaction has been posted on chain
411
+ * @param {Function} [callbacks.onOnChainTx] - Called when deposit transaction has been posted
412
+ * on chain
411
413
  * @param {Function} [callbacks.onOwnDepositLocked]
412
414
  * @param {Function} [callbacks.onDepositLocked]
413
415
  * @return {Promise<Object>}
@@ -597,7 +599,11 @@ function callContract ({ amount, callData, contract, abiVersion }, sign) {
597
599
  * }
598
600
  * })
599
601
  */
600
- function forceProgress ({ amount, callData, contract, abiVersion, gas = 1000000, gasPrice = 1000000000, nonce }, sign, { onOnChainTx } = {}) {
602
+ function forceProgress (
603
+ { amount, callData, contract, abiVersion, gas = 1000000, gasPrice = 1000000000, nonce },
604
+ sign,
605
+ { onOnChainTx } = {}
606
+ ) {
601
607
  return new Promise((resolve, reject) => {
602
608
  enqueueAction(
603
609
  this,
@@ -800,23 +806,42 @@ async function reconnect (options, txParams) {
800
806
  * @param {String} options.initiatorId - Initiator's public key
801
807
  * @param {String} options.responderId - Responder's public key
802
808
  * @param {Number} options.pushAmount - Initial deposit in favour of the responder by the initiator
803
- * @param {Number} options.initiatorAmount - Amount of tokens the initiator has committed to the channel
804
- * @param {Number} options.responderAmount - Amount of tokens the responder has committed to the channel
809
+ * @param {Number} options.initiatorAmount - Amount of tokens the initiator has committed to
810
+ * the channel
811
+ * @param {Number} options.responderAmount - Amount of tokens the responder has committed to
812
+ * the channel
805
813
  * @param {Number} options.channelReserve - The minimum amount both peers need to maintain
806
814
  * @param {Number} [options.ttl] - Minimum block height to include the channel_create_tx
807
815
  * @param {String} options.host - Host of the responder's node
808
816
  * @param {Number} options.port - The port of the responders node
809
817
  * @param {Number} options.lockPeriod - Amount of blocks for disputing a solo close
810
- * @param {Number} [options.existingChannelId] - Existing channel id (required if reestablishing a channel)
811
- * @param {Number} [options.offchainTx] - Offchain transaction (required if reestablishing a channel)
812
- * @param {Number} [options.timeoutIdle] - The time waiting for a new event to be initiated (default: 600000)
813
- * @param {Number} [options.timeoutFundingCreate] - The time waiting for the initiator to produce the create channel transaction after the noise session had been established (default: 120000)
814
- * @param {Number} [options.timeoutFundingSign] - The time frame the other client has to sign an off-chain update after our client had initiated and signed it. This applies only for double signed on-chain intended updates: channel create transaction, deposit, withdrawal and etc. (default: 120000)
815
- * @param {Number} [options.timeoutFundingLock] - The time frame the other client has to confirm an on-chain transaction reaching maturity (passing minimum depth) after the local node has detected this. This applies only for double signed on-chain intended updates: channel create transaction, deposit, withdrawal and etc. (default: 360000)
816
- * @param {Number} [options.timeoutSign] - The time frame the client has to return a signed off-chain update or to decline it. This applies for all off-chain updates (default: 500000)
817
- * @param {Number} [options.timeoutAccept] - The time frame the other client has to react to an event. This applies for all off-chain updates that are not meant to land on-chain, as well as some special cases: opening a noise connection, mutual closing acknowledgement and reestablishing an existing channel (default: 120000)
818
- * @param {Number} [options.timeoutInitialized] - the time frame the responder has to accept an incoming noise session. Applicable only for initiator (default: timeout_accept's value)
819
- * @param {Number} [options.timeoutAwaitingOpen] - The time frame the initiator has to start an outgoing noise session to the responder's node. Applicable only for responder (default: timeout_idle's value)
818
+ * @param {Number} [options.existingChannelId] - Existing channel id (required if reestablishing a
819
+ * channel)
820
+ * @param {Number} [options.offchainTx] - Offchain transaction (required if reestablishing
821
+ * a channel)
822
+ * @param {Number} [options.timeoutIdle] - The time waiting for a new event to be initiated
823
+ * (default: 600000)
824
+ * @param {Number} [options.timeoutFundingCreate] - The time waiting for the initiator to produce
825
+ * the create channel transaction after the noise session had been established (default: 120000)
826
+ * @param {Number} [options.timeoutFundingSign] - The time frame the other client has to sign an
827
+ * off-chain update after our client had initiated and signed it. This applies only for double
828
+ * signed on-chain intended updates: channel create transaction, deposit, withdrawal and etc.
829
+ * (default: 120000)
830
+ * @param {Number} [options.timeoutFundingLock] - The time frame the other client has to confirm an
831
+ * on-chain transaction reaching maturity (passing minimum depth) after the local node has detected
832
+ * this. This applies only for double signed on-chain intended updates: channel create transaction,
833
+ * deposit, withdrawal and etc. (default: 360000)
834
+ * @param {Number} [options.timeoutSign] - The time frame the client has to return a signed
835
+ * off-chain update or to decline it. This applies for all off-chain updates (default: 500000)
836
+ * @param {Number} [options.timeoutAccept] - The time frame the other client has to react to an
837
+ * event. This applies for all off-chain updates that are not meant to land on-chain, as well as
838
+ * some special cases: opening a noise connection, mutual closing acknowledgement and
839
+ * reestablishing an existing channel (default: 120000)
840
+ * @param {Number} [options.timeoutInitialized] - the time frame the responder has to accept an
841
+ * incoming noise session. Applicable only for initiator (default: timeout_accept's value)
842
+ * @param {Number} [options.timeoutAwaitingOpen] - The time frame the initiator has to start an
843
+ * outgoing noise session to the responder's node. Applicable only for responder (default:
844
+ * timeout_idle's value)
820
845
  * @param {Number} [options.debug=false] - Log websocket communication
821
846
  * @param {Function} options.sign - Function which verifies and signs transactions
822
847
  * @return {Promise<Object>} Channel instance
@@ -838,7 +863,12 @@ async function reconnect (options, txParams) {
838
863
  */
839
864
  const Channel = AsyncInit.compose({
840
865
  async init (options) {
841
- initialize(this, options)
866
+ await initialize(
867
+ this,
868
+ options.existingFsmId ? handlers.awaitingReconnection : handlers.awaitingConnection,
869
+ handlers.channelOpen,
870
+ options
871
+ )
842
872
  },
843
873
  methods: {
844
874
  on,