@aeternity/aepp-sdk 11.0.1 → 12.0.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 (376) hide show
  1. package/LICENSE +1 -1
  2. package/dist/aepp-sdk.browser-script.js +1 -1
  3. package/dist/aepp-sdk.browser-script.js.LICENSE.txt +12 -19
  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/AeSdk.d.ts +37 -0
  10. package/es/AeSdk.mjs +81 -0
  11. package/es/AeSdk.mjs.map +1 -0
  12. package/es/AeSdkAepp.d.ts +69 -0
  13. package/es/AeSdkAepp.mjs +175 -0
  14. package/es/AeSdkAepp.mjs.map +1 -0
  15. package/es/AeSdkBase.d.ts +212 -0
  16. package/es/AeSdkBase.mjs +310 -0
  17. package/es/AeSdkBase.mjs.map +1 -0
  18. package/es/AeSdkWallet.d.ts +103 -0
  19. package/es/AeSdkWallet.mjs +322 -0
  20. package/es/AeSdkWallet.mjs.map +1 -0
  21. package/es/Node.d.ts +44 -0
  22. package/es/Node.mjs +141 -0
  23. package/es/Node.mjs.map +1 -0
  24. package/es/account/Base.d.ts +65 -0
  25. package/es/account/Base.mjs +123 -0
  26. package/es/account/Base.mjs.map +1 -0
  27. package/es/account/Memory.d.ts +25 -0
  28. package/es/account/Memory.mjs +81 -0
  29. package/es/account/Memory.mjs.map +1 -0
  30. package/es/account/Rpc.d.ts +27 -0
  31. package/es/account/Rpc.mjs +79 -0
  32. package/es/account/Rpc.mjs.map +1 -0
  33. package/es/aens.d.ts +209 -0
  34. package/es/aens.mjs +332 -0
  35. package/es/aens.mjs.map +1 -0
  36. package/es/aepp-wallet-communication/connection/Browser.d.ts +32 -0
  37. package/es/aepp-wallet-communication/connection/Browser.mjs +70 -0
  38. package/es/aepp-wallet-communication/connection/Browser.mjs.map +1 -0
  39. package/es/aepp-wallet-communication/connection/BrowserRuntime.d.ts +17 -0
  40. package/es/aepp-wallet-communication/connection/BrowserRuntime.mjs +57 -0
  41. package/es/aepp-wallet-communication/connection/BrowserRuntime.mjs.map +1 -0
  42. package/es/aepp-wallet-communication/connection/BrowserWindowMessage.d.ts +35 -0
  43. package/es/aepp-wallet-communication/connection/BrowserWindowMessage.mjs +142 -0
  44. package/es/aepp-wallet-communication/connection/BrowserWindowMessage.mjs.map +1 -0
  45. package/es/aepp-wallet-communication/connection-proxy.d.ts +10 -0
  46. package/{src/utils/crypto-ts.ts → es/aepp-wallet-communication/connection-proxy.mjs} +15 -11
  47. package/es/aepp-wallet-communication/connection-proxy.mjs.map +1 -0
  48. package/es/aepp-wallet-communication/rpc/RpcClient.d.ts +33 -0
  49. package/es/aepp-wallet-communication/rpc/RpcClient.mjs +169 -0
  50. package/es/aepp-wallet-communication/rpc/RpcClient.mjs.map +1 -0
  51. package/es/aepp-wallet-communication/rpc/types.d.ts +77 -0
  52. package/es/aepp-wallet-communication/rpc/types.mjs +3 -0
  53. package/es/aepp-wallet-communication/rpc/types.mjs.map +1 -0
  54. package/es/aepp-wallet-communication/schema.d.ts +90 -46
  55. package/es/aepp-wallet-communication/schema.mjs +214 -0
  56. package/es/aepp-wallet-communication/schema.mjs.map +1 -0
  57. package/es/aepp-wallet-communication/wallet-detector.d.ts +24 -0
  58. package/es/aepp-wallet-communication/wallet-detector.mjs +63 -0
  59. package/es/aepp-wallet-communication/wallet-detector.mjs.map +1 -0
  60. package/es/apis/compiler/compiler.d.ts +92 -0
  61. package/es/apis/compiler/compiler.mjs +576 -0
  62. package/es/apis/compiler/compiler.mjs.map +1 -0
  63. package/es/apis/compiler/index.d.ts +2 -0
  64. package/es/apis/compiler/index.mjs +3 -0
  65. package/es/apis/compiler/index.mjs.map +1 -0
  66. package/es/apis/compiler/models/index.d.ts +248 -0
  67. package/es/apis/compiler/models/index.mjs +48 -0
  68. package/es/apis/compiler/models/index.mjs.map +1 -0
  69. package/es/apis/compiler/models/mappers.d.ts +23 -0
  70. package/es/apis/compiler/models/mappers.mjs +585 -0
  71. package/es/apis/compiler/models/mappers.mjs.map +1 -0
  72. package/es/apis/compiler/models/parameters.d.ts +13 -0
  73. package/es/apis/compiler/models/parameters.mjs +71 -0
  74. package/es/apis/compiler/models/parameters.mjs.map +1 -0
  75. package/es/apis/node/index.d.ts +2 -0
  76. package/es/apis/node/index.mjs +3 -0
  77. package/es/apis/node/index.mjs.map +1 -0
  78. package/es/apis/node/models/index.d.ts +1061 -0
  79. package/es/apis/node/models/index.mjs +2 -0
  80. package/es/apis/node/models/index.mjs.map +1 -0
  81. package/es/apis/node/models/mappers.d.ts +95 -0
  82. package/es/apis/node/models/mappers.mjs +4164 -0
  83. package/es/apis/node/models/mappers.mjs.map +1 -0
  84. package/es/apis/node/models/parameters.d.ts +17 -0
  85. package/es/apis/node/models/parameters.mjs +165 -0
  86. package/es/apis/node/models/parameters.mjs.map +1 -0
  87. package/es/apis/node/node.d.ts +215 -0
  88. package/es/apis/node/node.mjs +1165 -0
  89. package/es/apis/node/node.mjs.map +1 -0
  90. package/es/chain.d.ts +220 -0
  91. package/es/chain.mjs +524 -0
  92. package/es/chain.mjs.map +1 -0
  93. package/es/channel/handlers.d.ts +34 -0
  94. package/es/channel/handlers.mjs +147 -112
  95. package/es/channel/handlers.mjs.map +1 -1
  96. package/es/channel/index.d.ts +634 -0
  97. package/es/channel/index.mjs +898 -900
  98. package/es/channel/index.mjs.map +1 -1
  99. package/es/channel/internal.d.ts +100 -0
  100. package/es/channel/internal.mjs +97 -51
  101. package/es/channel/internal.mjs.map +1 -1
  102. package/es/contract/Compiler.d.ts +17 -0
  103. package/es/contract/Compiler.mjs +67 -0
  104. package/es/contract/Compiler.mjs.map +1 -0
  105. package/es/contract/aci.d.ts +125 -0
  106. package/es/contract/aci.mjs +199 -180
  107. package/es/contract/aci.mjs.map +1 -1
  108. package/es/contract/ga.d.ts +68 -0
  109. package/es/contract/ga.mjs +190 -0
  110. package/es/contract/ga.mjs.map +1 -0
  111. package/es/contract/methods.d.ts +68 -0
  112. package/es/contract/methods.mjs +101 -0
  113. package/es/contract/methods.mjs.map +1 -0
  114. package/es/index.d.ts +32 -0
  115. package/es/index.mjs +32 -40
  116. package/es/index.mjs.map +1 -1
  117. package/es/oracle.d.ts +154 -0
  118. package/es/oracle.mjs +288 -0
  119. package/es/oracle.mjs.map +1 -0
  120. package/es/spend.d.ts +61 -0
  121. package/es/spend.mjs +122 -0
  122. package/es/spend.mjs.map +1 -0
  123. package/es/tx/builder/constants.d.ts +41 -0
  124. package/es/tx/builder/constants.mjs +11 -19
  125. package/es/tx/builder/constants.mjs.map +1 -1
  126. package/es/tx/builder/field-types.d.ts +62 -0
  127. package/es/tx/builder/field-types.mjs +62 -10
  128. package/es/tx/builder/field-types.mjs.map +1 -1
  129. package/es/tx/builder/helpers.d.ts +131 -0
  130. package/es/tx/builder/helpers.mjs +71 -169
  131. package/es/tx/builder/helpers.mjs.map +1 -1
  132. package/es/tx/builder/index.d.ts +75 -0
  133. package/es/tx/builder/index.mjs +141 -323
  134. package/es/tx/builder/index.mjs.map +1 -1
  135. package/es/tx/builder/schema.d.ts +350 -0
  136. package/es/tx/builder/schema.mjs +173 -510
  137. package/es/tx/builder/schema.mjs.map +1 -1
  138. package/es/tx/index.d.ts +63 -0
  139. package/es/tx/index.mjs +171 -265
  140. package/es/tx/index.mjs.map +1 -1
  141. package/es/tx/min-fee.d.ts +20 -0
  142. package/es/tx/min-fee.mjs +148 -0
  143. package/es/tx/min-fee.mjs.map +1 -0
  144. package/es/tx/validator.d.ts +19 -0
  145. package/es/tx/validator.mjs +64 -71
  146. package/es/tx/validator.mjs.map +1 -1
  147. package/es/typings/@aeternity__argon2-browser/index.d.mjs +2 -0
  148. package/es/typings/@aeternity__argon2-browser/index.d.mjs.map +1 -0
  149. package/es/typings/@aeternity__uuid/index.d.mjs +2 -0
  150. package/es/typings/@aeternity__uuid/index.d.mjs.map +1 -0
  151. package/es/typings/bip32-path/index.d.mjs +2 -0
  152. package/es/typings/bip32-path/index.d.mjs.map +1 -0
  153. package/es/typings/blakejs/index.d.mjs.map +1 -1
  154. package/es/typings/tweetnacl-auth/index.d.mjs +2 -0
  155. package/es/typings/tweetnacl-auth/index.d.mjs.map +1 -0
  156. package/es/utils/amount-formatter.d.ts +42 -0
  157. package/es/utils/amount-formatter.mjs +28 -41
  158. package/es/utils/amount-formatter.mjs.map +1 -1
  159. package/es/utils/autorest.d.ts +6 -0
  160. package/es/utils/autorest.mjs +97 -0
  161. package/es/utils/autorest.mjs.map +1 -0
  162. package/es/utils/bignumber.d.ts +13 -0
  163. package/es/utils/bignumber.mjs +10 -8
  164. package/es/utils/bignumber.mjs.map +1 -1
  165. package/es/{bytes.d.ts → utils/bytes.d.ts} +10 -13
  166. package/es/utils/bytes.mjs +10 -14
  167. package/es/utils/bytes.mjs.map +1 -1
  168. package/es/utils/crypto.d.ts +103 -0
  169. package/es/utils/crypto.mjs +60 -68
  170. package/es/utils/crypto.mjs.map +1 -1
  171. package/es/utils/encoder.d.ts +26 -0
  172. package/es/utils/encoder.mjs +47 -51
  173. package/es/utils/encoder.mjs.map +1 -1
  174. package/es/{errors.d.ts → utils/errors.d.ts} +254 -57
  175. package/es/utils/errors.mjs +318 -125
  176. package/es/utils/errors.mjs.map +1 -1
  177. package/es/utils/hd-wallet.d.ts +34 -0
  178. package/es/utils/hd-wallet.mjs +27 -16
  179. package/es/utils/hd-wallet.mjs.map +1 -1
  180. package/es/utils/json-big.d.ts +5 -0
  181. package/es/utils/json-big.mjs +10 -14
  182. package/es/utils/json-big.mjs.map +1 -1
  183. package/es/utils/keystore.d.ts +51 -0
  184. package/es/utils/keystore.mjs +98 -193
  185. package/es/utils/keystore.mjs.map +1 -1
  186. package/es/{mptree.d.ts → utils/mptree.d.ts} +5 -8
  187. package/es/utils/mptree.mjs +13 -14
  188. package/es/utils/mptree.mjs.map +1 -1
  189. package/es/utils/other.d.ts +14 -0
  190. package/es/utils/other.mjs +21 -36
  191. package/es/utils/other.mjs.map +1 -1
  192. package/es/{semver-satisfies.d.ts → utils/semver-satisfies.d.ts} +0 -0
  193. package/es/utils/semver-satisfies.mjs.map +1 -1
  194. package/es/utils/string.d.ts +24 -0
  195. package/es/utils/string.mjs +8 -10
  196. package/es/utils/string.mjs.map +1 -1
  197. package/package.json +49 -39
  198. package/src/AeSdk.ts +65 -0
  199. package/src/AeSdkAepp.ts +167 -0
  200. package/src/AeSdkBase.ts +320 -0
  201. package/src/AeSdkWallet.ts +332 -0
  202. package/src/Node.ts +167 -0
  203. package/src/account/Base.ts +114 -0
  204. package/src/account/Memory.ts +83 -0
  205. package/src/account/Rpc.ts +67 -0
  206. package/src/aens.ts +391 -0
  207. package/src/aepp-wallet-communication/connection/Browser.ts +68 -0
  208. package/src/aepp-wallet-communication/connection/BrowserRuntime.ts +58 -0
  209. package/src/aepp-wallet-communication/connection/BrowserWindowMessage.ts +111 -0
  210. package/{es/utils/crypto-ts.mjs → src/aepp-wallet-communication/connection-proxy.ts} +16 -17
  211. package/src/aepp-wallet-communication/rpc/RpcClient.ts +137 -0
  212. package/src/aepp-wallet-communication/rpc/types.ts +66 -0
  213. package/src/aepp-wallet-communication/schema.ts +181 -0
  214. package/src/aepp-wallet-communication/wallet-detector.ts +71 -0
  215. package/src/apis/compiler/code-model-v4.yaml +2649 -0
  216. package/src/apis/compiler/compiler.ts +585 -0
  217. package/src/apis/compiler/index.ts +2 -0
  218. package/src/apis/compiler/models/index.ts +301 -0
  219. package/src/apis/compiler/models/mappers.ts +583 -0
  220. package/src/apis/compiler/models/parameters.ts +93 -0
  221. package/src/apis/node/code-model-v4.yaml +10046 -0
  222. package/src/apis/node/index.ts +2 -0
  223. package/src/apis/node/models/index.ts +1253 -0
  224. package/src/apis/node/models/mappers.ts +4278 -0
  225. package/src/apis/node/models/parameters.ts +188 -0
  226. package/src/apis/node/node.ts +1299 -0
  227. package/src/chain.ts +444 -0
  228. package/src/channel/{handlers.js → handlers.ts} +220 -128
  229. package/src/channel/index.ts +991 -0
  230. package/src/channel/internal.ts +345 -0
  231. package/src/contract/Compiler.ts +66 -0
  232. package/src/contract/aci.ts +529 -0
  233. package/src/contract/ga.ts +178 -0
  234. package/src/contract/methods.ts +118 -0
  235. package/src/index.ts +51 -0
  236. package/src/oracle.ts +342 -0
  237. package/src/spend.ts +152 -0
  238. package/src/tx/builder/{constants.js → constants.ts} +12 -16
  239. package/src/tx/builder/field-types.ts +111 -0
  240. package/src/tx/builder/helpers.ts +253 -0
  241. package/src/tx/builder/index.ts +394 -0
  242. package/src/tx/builder/schema.ts +771 -0
  243. package/src/tx/index.ts +199 -0
  244. package/src/tx/min-fee.ts +136 -0
  245. package/src/tx/validator.ts +226 -0
  246. package/src/typings/@aeternity__argon2-browser/index.d.ts +3 -0
  247. package/src/typings/@aeternity__uuid/index.d.ts +3 -0
  248. package/src/typings/bip32-path/index.d.ts +33 -0
  249. package/src/typings/tweetnacl-auth/index.d.ts +14 -0
  250. package/src/utils/amount-formatter.ts +29 -42
  251. package/src/utils/autorest.ts +90 -0
  252. package/src/utils/bignumber.ts +9 -11
  253. package/src/utils/bytes.ts +12 -14
  254. package/src/utils/crypto.ts +217 -0
  255. package/src/utils/encoder.ts +52 -55
  256. package/src/utils/errors.ts +268 -134
  257. package/src/utils/hd-wallet.ts +134 -0
  258. package/src/utils/json-big.ts +26 -0
  259. package/src/utils/keystore.ts +179 -0
  260. package/src/utils/mptree.ts +14 -14
  261. package/src/utils/other.ts +28 -43
  262. package/src/utils/string.ts +8 -10
  263. package/es/account/base.mjs +0 -179
  264. package/es/account/base.mjs.map +0 -1
  265. package/es/account/memory.mjs +0 -99
  266. package/es/account/memory.mjs.map +0 -1
  267. package/es/account/multiple.mjs +0 -179
  268. package/es/account/multiple.mjs.map +0 -1
  269. package/es/ae/aens.mjs +0 -404
  270. package/es/ae/aens.mjs.map +0 -1
  271. package/es/ae/aepp.mjs +0 -45
  272. package/es/ae/aepp.mjs.map +0 -1
  273. package/es/ae/contract.mjs +0 -159
  274. package/es/ae/contract.mjs.map +0 -1
  275. package/es/ae/index.mjs +0 -207
  276. package/es/ae/index.mjs.map +0 -1
  277. package/es/ae/oracle.mjs +0 -339
  278. package/es/ae/oracle.mjs.map +0 -1
  279. package/es/ae/universal.mjs +0 -45
  280. package/es/ae/universal.mjs.map +0 -1
  281. package/es/ae/wallet.mjs +0 -50
  282. package/es/ae/wallet.mjs.map +0 -1
  283. package/es/amount-formatter.d.ts +0 -58
  284. package/es/bignumber.d.ts +0 -18
  285. package/es/chain/index.mjs +0 -272
  286. package/es/chain/index.mjs.map +0 -1
  287. package/es/chain/node.mjs +0 -442
  288. package/es/chain/node.mjs.map +0 -1
  289. package/es/contract/compiler.mjs +0 -115
  290. package/es/contract/compiler.mjs.map +0 -1
  291. package/es/contract/ga/helpers.mjs +0 -30
  292. package/es/contract/ga/helpers.mjs.map +0 -1
  293. package/es/contract/ga/index.mjs +0 -178
  294. package/es/contract/ga/index.mjs.map +0 -1
  295. package/es/crypto-ts.d.ts +0 -9
  296. package/es/encoder.d.ts +0 -26
  297. package/es/node-pool/index.mjs +0 -188
  298. package/es/node-pool/index.mjs.map +0 -1
  299. package/es/node.mjs +0 -133
  300. package/es/node.mjs.map +0 -1
  301. package/es/other.d.ts +0 -31
  302. package/es/string.d.ts +0 -26
  303. package/es/tx/tx-object.mjs +0 -283
  304. package/es/tx/tx-object.mjs.map +0 -1
  305. package/es/tx/tx.mjs +0 -938
  306. package/es/tx/tx.mjs.map +0 -1
  307. package/es/utils/aepp-wallet-communication/connection/browser-runtime.mjs +0 -132
  308. package/es/utils/aepp-wallet-communication/connection/browser-runtime.mjs.map +0 -1
  309. package/es/utils/aepp-wallet-communication/connection/browser-window-message.mjs +0 -183
  310. package/es/utils/aepp-wallet-communication/connection/browser-window-message.mjs.map +0 -1
  311. package/es/utils/aepp-wallet-communication/connection/index.mjs +0 -87
  312. package/es/utils/aepp-wallet-communication/connection/index.mjs.map +0 -1
  313. package/es/utils/aepp-wallet-communication/content-script-bridge.mjs +0 -98
  314. package/es/utils/aepp-wallet-communication/content-script-bridge.mjs.map +0 -1
  315. package/es/utils/aepp-wallet-communication/helpers.mjs +0 -92
  316. package/es/utils/aepp-wallet-communication/helpers.mjs.map +0 -1
  317. package/es/utils/aepp-wallet-communication/rpc/aepp-rpc.mjs +0 -375
  318. package/es/utils/aepp-wallet-communication/rpc/aepp-rpc.mjs.map +0 -1
  319. package/es/utils/aepp-wallet-communication/rpc/rpc-client.mjs +0 -275
  320. package/es/utils/aepp-wallet-communication/rpc/rpc-client.mjs.map +0 -1
  321. package/es/utils/aepp-wallet-communication/rpc/wallet-rpc.mjs +0 -541
  322. package/es/utils/aepp-wallet-communication/rpc/wallet-rpc.mjs.map +0 -1
  323. package/es/utils/aepp-wallet-communication/schema.mjs +0 -102
  324. package/es/utils/aepp-wallet-communication/schema.mjs.map +0 -1
  325. package/es/utils/aepp-wallet-communication/wallet-detector.mjs +0 -110
  326. package/es/utils/aepp-wallet-communication/wallet-detector.mjs.map +0 -1
  327. package/es/utils/async-init.mjs +0 -64
  328. package/es/utils/async-init.mjs.map +0 -1
  329. package/es/utils/crypto-ts.mjs.map +0 -1
  330. package/es/utils/swagger.mjs +0 -164
  331. package/es/utils/swagger.mjs.map +0 -1
  332. package/src/account/base.js +0 -153
  333. package/src/account/memory.js +0 -83
  334. package/src/account/multiple.js +0 -162
  335. package/src/ae/aens.js +0 -361
  336. package/src/ae/aepp.js +0 -45
  337. package/src/ae/contract.js +0 -148
  338. package/src/ae/index.js +0 -168
  339. package/src/ae/oracle.js +0 -311
  340. package/src/ae/universal.js +0 -47
  341. package/src/ae/wallet.js +0 -50
  342. package/src/chain/index.js +0 -269
  343. package/src/chain/node.js +0 -320
  344. package/src/channel/index.js +0 -903
  345. package/src/channel/internal.js +0 -236
  346. package/src/contract/aci.js +0 -376
  347. package/src/contract/compiler.js +0 -100
  348. package/src/contract/ga/helpers.js +0 -19
  349. package/src/contract/ga/index.js +0 -150
  350. package/src/index.js +0 -89
  351. package/src/node-pool/index.js +0 -172
  352. package/src/node.js +0 -116
  353. package/src/tx/builder/field-types.js +0 -61
  354. package/src/tx/builder/helpers.js +0 -334
  355. package/src/tx/builder/index.js +0 -465
  356. package/src/tx/builder/schema.js +0 -1100
  357. package/src/tx/index.js +0 -301
  358. package/src/tx/tx-object.js +0 -187
  359. package/src/tx/tx.js +0 -631
  360. package/src/tx/validator.js +0 -181
  361. package/src/utils/aepp-wallet-communication/connection/browser-runtime.js +0 -119
  362. package/src/utils/aepp-wallet-communication/connection/browser-window-message.js +0 -165
  363. package/src/utils/aepp-wallet-communication/connection/index.js +0 -85
  364. package/src/utils/aepp-wallet-communication/content-script-bridge.js +0 -86
  365. package/src/utils/aepp-wallet-communication/helpers.js +0 -60
  366. package/src/utils/aepp-wallet-communication/rpc/aepp-rpc.js +0 -294
  367. package/src/utils/aepp-wallet-communication/rpc/rpc-client.js +0 -232
  368. package/src/utils/aepp-wallet-communication/rpc/wallet-rpc.js +0 -401
  369. package/src/utils/aepp-wallet-communication/schema.ts +0 -80
  370. package/src/utils/aepp-wallet-communication/wallet-detector.js +0 -92
  371. package/src/utils/async-init.js +0 -45
  372. package/src/utils/crypto.js +0 -219
  373. package/src/utils/hd-wallet.js +0 -100
  374. package/src/utils/json-big.js +0 -26
  375. package/src/utils/keystore.js +0 -219
  376. package/src/utils/swagger.js +0 -156
@@ -14,19 +14,12 @@
14
14
  * OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
15
15
  * PERFORMANCE OF THIS SOFTWARE.
16
16
  */
17
-
18
- /**
19
- * Channel module
20
- * @module @aeternity/aepp-sdk/es/channel/index
21
- * @export Channel
22
- * @example import { Channel } from '@aeternity/aepp-sdk'
23
- */
24
- import AsyncInit from "../utils/async-init.mjs";
25
17
  import { snakeToPascal } from "../utils/string.mjs";
26
18
  import { buildTx, unpackTx } from "../tx/builder/index.mjs";
27
19
  import { TX_TYPE } from "../tx/builder/schema.mjs";
28
20
  import * as handlers from "./handlers.mjs";
29
21
  import { eventEmitters, status as channelStatus, state as channelState, initialize, enqueueAction, send, channelId, call, disconnect as channelDisconnect, fsmId as channelFsmId } from "./internal.mjs";
22
+ import { UnknownChannelStateError, ChannelError } from "../utils/errors.mjs";
30
23
 
31
24
  function snakeToPascalObjKeys(obj) {
32
25
  return Object.entries(obj).reduce((result, _ref) => {
@@ -36,945 +29,950 @@ function snakeToPascalObjKeys(obj) {
36
29
  };
37
30
  }, {});
38
31
  }
39
- /**
40
- * Register event listener function
41
- *
42
- * Possible events:
43
- *
44
- * - "error"
45
- * - "onChainTx"
46
- * - "ownWithdrawLocked"
47
- * - "withdrawLocked"
48
- * - "ownDepositLocked"
49
- * - "depositLocked"
50
- *
51
- * @param {String} event - Event name
52
- * @param {Function} callback - Callback function
53
- */
54
-
55
32
 
56
- function on(event, callback) {
57
- eventEmitters.get(this).on(event, callback);
58
- }
59
33
  /**
60
- * Remove event listener function
61
- *
62
- * @param {String} event - Event name
63
- * @param {Function} callback - Callback function
34
+ * Channel
35
+ * @example
36
+ * ```js
37
+ * await Channel.initialize({
38
+ * url: 'ws://localhost:3001',
39
+ * role: 'initiator'
40
+ * initiatorId: 'ak_Y1NRjHuoc3CGMYMvCmdHSBpJsMDR6Ra2t5zjhRcbtMeXXLpLH',
41
+ * responderId: 'ak_V6an1xhec1xVaAhLuak7QoEbi6t7w5hEtYWp9bMKaJ19i6A9E',
42
+ * initiatorAmount: 1e18,
43
+ * responderAmount: 1e18,
44
+ * pushAmount: 0,
45
+ * channelReserve: 0,
46
+ * ttl: 1000,
47
+ * host: 'localhost',
48
+ * port: 3002,
49
+ * lockPeriod: 10,
50
+ * async sign (tag, tx) => await account.signTransaction(tx)
51
+ * })
52
+ * ```
64
53
  */
54
+ export default class Channel {
55
+ /**
56
+ * @param options - Channel params
57
+ * @param options.url - Channel url (for example: "ws://localhost:3001")
58
+ * @param options.role - Participant role ("initiator" or "responder")
59
+ * @param options.initiatorId - Initiator's public key
60
+ * @param options.responderId - Responder's public key
61
+ * @param options.pushAmount - Initial deposit in favour of the responder by the initiator
62
+ * @param options.initiatorAmount - Amount of coins the initiator has committed to
63
+ * the channel
64
+ * @param options.responderAmount - Amount of coins the responder has committed to
65
+ * the channel
66
+ * @param options.channelReserve - The minimum amount both peers need to maintain
67
+ * @param options.ttl - Minimum block height to include the channel_create_tx
68
+ * @param options.host - Host of the responder's node
69
+ * @param options.port - The port of the responders node
70
+ * @param options.lockPeriod - Amount of blocks for disputing a solo close
71
+ * @param options.existingChannelId - Existing channel id (required if reestablishing a
72
+ * channel)
73
+ * @param options.offchainTx - Offchain transaction (required if reestablishing
74
+ * a channel)
75
+ * @param options.timeoutIdle - The time waiting for a new event to be initiated
76
+ * (default: 600000)
77
+ * @param options.timeoutFundingCreate - The time waiting for the initiator to produce
78
+ * the create channel transaction after the noise session had been established (default: 120000)
79
+ * @param options.timeoutFundingSign - The time frame the other client has to sign an
80
+ * off-chain update after our client had initiated and signed it. This applies only for double
81
+ * signed on-chain intended updates: channel create transaction, deposit, withdrawal and etc.
82
+ * (default: 120000)
83
+ * @param options.timeoutFundingLock - The time frame the other client has to confirm an
84
+ * on-chain transaction reaching maturity (passing minimum depth) after the local node has
85
+ * detected this. This applies only for double signed on-chain intended updates: channel create
86
+ * transaction, deposit, withdrawal and etc. (default: 360000)
87
+ * @param options.timeoutSign - The time frame the client has to return a signed
88
+ * off-chain update or to decline it. This applies for all off-chain updates (default: 500000)
89
+ * @param options.timeoutAccept - The time frame the other client has to react to an
90
+ * event. This applies for all off-chain updates that are not meant to land on-chain, as well as
91
+ * some special cases: opening a noise connection, mutual closing acknowledgement and
92
+ * reestablishing an existing channel (default: 120000)
93
+ * @param options.timeoutInitialized - the time frame the responder has to accept an
94
+ * incoming noise session. Applicable only for initiator (default: timeout_accept's value)
95
+ * @param options.timeoutAwaitingOpen - The time frame the initiator has to start an
96
+ * outgoing noise session to the responder's node. Applicable only for responder (default:
97
+ * timeout_idle's value)
98
+ * @param options.debug=false - Log websocket communication
99
+ * @param options.sign - Function which verifies and signs transactions
100
+ */
101
+ static async initialize(options) {
102
+ const channel = new Channel();
103
+ await initialize(channel, options.existingFsmId != null ? handlers.awaitingReconnection : handlers.awaitingConnection, handlers.channelOpen, options);
104
+ return channel;
105
+ }
106
+ /**
107
+ * Register event listener function
108
+ *
109
+ * Possible events:
110
+ *
111
+ * - "error"
112
+ * - "onChainTx"
113
+ * - "ownWithdrawLocked"
114
+ * - "withdrawLocked"
115
+ * - "ownDepositLocked"
116
+ * - "depositLocked"
117
+ *
118
+ * TODO: the event list looks outdated
119
+ *
120
+ * @param eventName - Event name
121
+ * @param callback - Callback function
122
+ */
123
+ // TODO define specific callback type depending on the event name
124
+
125
+
126
+ on(eventName, callback) {
127
+ const eventEmitter = eventEmitters.get(this);
128
+ if (eventEmitter == null) throw new UnknownChannelStateError();
129
+ eventEmitter.on(eventName, callback);
130
+ }
131
+ /**
132
+ * Remove event listener function
133
+ * @param eventName - Event name
134
+ * @param callback - Callback function
135
+ */
65
136
 
66
137
 
67
- function off(event, callback) {
68
- eventEmitters.get(this).removeListener(event, callback);
69
- }
70
- /**
71
- * Close the connection
72
- */
138
+ off(eventName, callback) {
139
+ const eventEmitter = eventEmitters.get(this);
140
+ if (eventEmitter == null) throw new UnknownChannelStateError();
141
+ eventEmitter.removeListener(eventName, callback);
142
+ }
143
+ /**
144
+ * Close the connection
145
+ */
73
146
 
74
147
 
75
- function disconnect() {
76
- return channelDisconnect(this);
77
- }
78
- /**
79
- * Get current status
80
- *
81
- * @return {String}
82
- */
148
+ disconnect() {
149
+ return channelDisconnect(this);
150
+ }
151
+ /**
152
+ * Get current status
153
+ *
154
+ */
83
155
 
84
156
 
85
- function status() {
86
- return channelStatus.get(this);
87
- }
88
- /**
89
- * Get current state
90
- *
91
- * @return {Promise<Object>}
92
- */
157
+ status() {
158
+ const status = channelStatus.get(this);
159
+ if (status == null) throw new UnknownChannelStateError();
160
+ return status;
161
+ }
162
+ /**
163
+ * Get current state
164
+ */
93
165
 
94
166
 
95
- async function state() {
96
- return snakeToPascalObjKeys(await call(this, 'channels.get.offchain_state', {}));
97
- }
98
- /**
99
- * Get current round
100
- *
101
- * If round cannot be determined (for example when channel has not been opened)
102
- * it will return `null`.
103
- *
104
- * @return {Number}
105
- */
167
+ async state() {
168
+ return await snakeToPascalObjKeys(await call(this, 'channels.get.offchain_state', {}));
169
+ }
170
+ /**
171
+ * Get current round
172
+ *
173
+ * If round cannot be determined (for example when channel has not been opened)
174
+ * it will return `null`.
175
+ *
176
+ */
106
177
 
107
178
 
108
- function round() {
109
- const state = channelState.get(this);
179
+ round() {
180
+ const state = channelState.get(this);
110
181
 
111
- if (!state) {
112
- return null;
113
- }
182
+ if (state == null) {
183
+ return null;
184
+ }
114
185
 
115
- const {
116
- txType,
117
- tx
118
- } = unpackTx(channelState.get(this)).tx.encodedTx;
186
+ const {
187
+ txType,
188
+ tx
189
+ } = unpackTx(state, TX_TYPE.signed).tx.encodedTx;
119
190
 
120
- switch (txType) {
121
- case TX_TYPE.channelCreate:
122
- return 1;
191
+ switch (txType) {
192
+ case TX_TYPE.channelCreate:
193
+ return 1;
123
194
 
124
- case TX_TYPE.channelOffChain:
125
- case TX_TYPE.channelWithdraw:
126
- case TX_TYPE.channelDeposit:
127
- return parseInt(tx.round, 10);
195
+ case TX_TYPE.channelOffChain:
196
+ case TX_TYPE.channelWithdraw:
197
+ case TX_TYPE.channelDeposit:
198
+ return +tx.round;
128
199
 
129
- default:
130
- return null;
200
+ default:
201
+ return null;
202
+ }
131
203
  }
132
- }
133
- /**
134
- * Get channel id
135
- *
136
- * @return {String}
137
- */
204
+ /**
205
+ * Get channel id
206
+ *
207
+ */
138
208
 
139
209
 
140
- function id() {
141
- return channelId.get(this);
142
- }
143
- /**
144
- * Get channel's fsm id
145
- *
146
- * @return {String}
147
- */
148
-
149
-
150
- function fsmId() {
151
- return channelFsmId.get(this);
152
- }
153
- /**
154
- * Trigger a transfer update
155
- *
156
- * The transfer update is moving tokens from one channel account to another.
157
- * The update is a change to be applied on top of the latest state.
158
- *
159
- * Sender and receiver are the channel parties. Both the initiator and responder
160
- * can take those roles. Any public key outside of the channel is considered invalid.
161
- *
162
- * @param {String} from - Sender's public address
163
- * @param {String} to - Receiver's public address
164
- * @param {Number} amount - Transaction amount
165
- * @param {Function} sign - Function which verifies and signs offchain transaction
166
- * @param {Array<String>} metadata
167
- * @return {Promise<Object>}
168
- * @example channel.update(
169
- * 'ak_Y1NRjHuoc3CGMYMvCmdHSBpJsMDR6Ra2t5zjhRcbtMeXXLpLH',
170
- * 'ak_V6an1xhec1xVaAhLuak7QoEbi6t7w5hEtYWp9bMKaJ19i6A9E',
171
- * 10,
172
- * async (tx) => await account.signTransaction(tx)
173
- * ).then(({ accepted, signedTx }) =>
174
- * if (accepted) {
175
- * console.log('Update has been accepted')
176
- * }
177
- * )
178
- */
210
+ id() {
211
+ const id = channelId.get(this);
212
+ if (id == null) throw new ChannelError('Channel is not initialized');
213
+ return id;
214
+ }
215
+ /**
216
+ * Get channel's fsm id
217
+ *
218
+ */
179
219
 
180
220
 
181
- function update(from, to, amount, sign, metadata) {
182
- return new Promise((resolve, reject) => {
183
- enqueueAction(this, (channel, state) => state.handler === handlers.channelOpen, (channel, state) => {
184
- send(channel, {
185
- jsonrpc: '2.0',
186
- method: 'channels.update.new',
187
- params: {
188
- from,
189
- to,
190
- amount,
191
- meta: metadata
192
- }
221
+ fsmId() {
222
+ const id = channelFsmId.get(this);
223
+ if (id == null) throw new ChannelError('Channel is not initialized');
224
+ return id;
225
+ }
226
+ /**
227
+ * Trigger a transfer update
228
+ *
229
+ * The transfer update is moving coins from one channel account to another.
230
+ * The update is a change to be applied on top of the latest state.
231
+ *
232
+ * Sender and receiver are the channel parties. Both the initiator and responder
233
+ * can take those roles. Any public key outside of the channel is considered invalid.
234
+ *
235
+ * @param from - Sender's public address
236
+ * @param to - Receiver's public address
237
+ * @param amount - Transaction amount
238
+ * @param sign - Function which verifies and signs offchain transaction
239
+ * @param metadata - Metadata
240
+ * @example
241
+ * ```js
242
+ * channel.update(
243
+ * 'ak_Y1NRjHuoc3CGMYMvCmdHSBpJsMDR6Ra2t5zjhRcbtMeXXLpLH',
244
+ * 'ak_V6an1xhec1xVaAhLuak7QoEbi6t7w5hEtYWp9bMKaJ19i6A9E',
245
+ * 10,
246
+ * async (tx) => await account.signTransaction(tx)
247
+ * ).then(({ accepted, signedTx }) =>
248
+ * if (accepted) {
249
+ * console.log('Update has been accepted')
250
+ * }
251
+ * )
252
+ * ```
253
+ */
254
+
255
+
256
+ async update(from, to, amount, sign) {
257
+ let metadata = arguments.length > 4 && arguments[4] !== undefined ? arguments[4] : [];
258
+ return await new Promise((resolve, reject) => {
259
+ enqueueAction(this, (_channel, state) => (state === null || state === void 0 ? void 0 : state.handler) === handlers.channelOpen, (channel, _state) => {
260
+ send(channel, {
261
+ jsonrpc: '2.0',
262
+ method: 'channels.update.new',
263
+ params: {
264
+ from,
265
+ to,
266
+ amount,
267
+ meta: metadata
268
+ }
269
+ });
270
+ return {
271
+ handler: handlers.awaitingOffChainTx,
272
+ state: {
273
+ resolve,
274
+ reject,
275
+ sign
276
+ }
277
+ };
193
278
  });
194
- return {
195
- handler: handlers.awaitingOffChainTx,
196
- state: {
197
- resolve,
198
- reject,
199
- sign
200
- }
201
- };
202
279
  });
203
- });
204
- }
205
- /**
206
- * Get proof of inclusion
207
- *
208
- * If a certain address of an account or a contract is not found
209
- * in the state tree - the response is an error.
210
- *
211
- * @param {Object} addresses
212
- * @param {Array<String>} [addresses.accounts] - List of account addresses to include in poi
213
- * @param {Array<String>} [addresses.contracts] - List of contract addresses to include in poi
214
- * @return {Promise<String>}
215
- * @example channel.poi({
216
- * accounts: [
217
- * 'ak_Y1NRjHuoc3CGMYMvCmdHSBpJsMDR6Ra2t5zjhRcbtMeXXLpLH',
218
- * 'ak_V6an1xhec1xVaAhLuak7QoEbi6t7w5hEtYWp9bMKaJ19i6A9E'
219
- * ],
220
- * contracts: ['ct_2dCUAWYZdrWfACz3a2faJeKVTVrfDYxCQHCqAt5zM15f3u2UfA']
221
- * }).then(poi => console.log(poi))
222
- */
223
-
224
-
225
- async function poi(_ref2) {
226
- let {
227
- accounts,
228
- contracts
229
- } = _ref2;
230
- return (await call(this, 'channels.get.poi', {
231
- accounts,
232
- contracts
233
- })).poi;
234
- }
235
- /**
236
- * Get balances
237
- *
238
- * The accounts paramcontains a list of addresses to fetch balances of.
239
- * Those can be either account balances or a contract ones, encoded as an account addresses.
240
- *
241
- * If a certain account address had not being found in the state tree - it is simply
242
- * skipped in the response.
243
- *
244
- * @param {Array<String>} accounts - List of addresses to fetch balances from
245
- * @return {Promise<Object>}
246
- * @example channel.balances([
247
- * 'ak_Y1NRjHuoc3CGMYMvCmdHSBpJsMDR6Ra2t5zjhRcbtMeXXLpLH',
248
- * 'ak_V6an1xhec1xVaAhLuak7QoEbi6t7w5hEtYWp9bMKaJ19i6A9E'
249
- * 'ct_2dCUAWYZdrWfACz3a2faJeKVTVrfDYxCQHCqAt5zM15f3u2UfA'
250
- * ]).then(balances =>
251
- * console.log(balances['ak_Y1NRjHuoc3CGMYMvCmdHSBpJsMDR6Ra2t5zjhRcbtMeXXLpLH'])
252
- * )
253
- */
254
-
255
-
256
- async function balances(accounts) {
257
- return Object.fromEntries((await call(this, 'channels.get.balances', {
258
- accounts
259
- })).map(item => [item.account, item.balance]));
260
- }
261
- /**
262
- * Leave channel
263
- *
264
- * It is possible to leave a channel and then later reestablish the channel
265
- * off-chain state and continue operation. When a leave method is called,
266
- * the channel fsm passes it on to the peer fsm, reports the current mutually
267
- * signed state and then terminates.
268
- *
269
- * The channel can be reestablished by instantiating another Channel instance
270
- * with two extra params: existingChannelId and offchainTx (returned from leave
271
- * method as channelId and signedTx respectively).
272
- *
273
- * @return {Promise<Object>}
274
- * @example channel.leave().then(({ channelId, signedTx }) => {
275
- * console.log(channelId)
276
- * console.log(signedTx)
277
- * })
278
- */
279
-
280
-
281
- function leave() {
282
- return new Promise((resolve, reject) => {
283
- enqueueAction(this, (channel, state) => state.handler === handlers.channelOpen, (channel, state) => {
284
- send(channel, {
285
- jsonrpc: '2.0',
286
- method: 'channels.leave',
287
- params: {}
280
+ }
281
+ /**
282
+ * Get proof of inclusion
283
+ *
284
+ * If a certain address of an account or a contract is not found
285
+ * in the state tree - the response is an error.
286
+ *
287
+ * @param addresses - Addresses
288
+ * @param addresses.accounts - List of account addresses to include in poi
289
+ * @param addresses.contracts - List of contract addresses to include in poi
290
+ * @example
291
+ * ```js
292
+ * channel.poi({
293
+ * accounts: [
294
+ * 'ak_Y1NRjHuoc3CGMYMvCmdHSBpJsMDR6Ra2t5zjhRcbtMeXXLpLH',
295
+ * 'ak_V6an1xhec1xVaAhLuak7QoEbi6t7w5hEtYWp9bMKaJ19i6A9E'
296
+ * ],
297
+ * contracts: ['ct_2dCUAWYZdrWfACz3a2faJeKVTVrfDYxCQHCqAt5zM15f3u2UfA']
298
+ * }).then(poi => console.log(poi))
299
+ * ```
300
+ */
301
+
302
+
303
+ async poi(_ref2) {
304
+ let {
305
+ accounts,
306
+ contracts
307
+ } = _ref2;
308
+ return (await call(this, 'channels.get.poi', {
309
+ accounts,
310
+ contracts
311
+ })).poi;
312
+ }
313
+ /**
314
+ * Get balances
315
+ *
316
+ * The accounts paramcontains a list of addresses to fetch balances of.
317
+ * Those can be either account balances or a contract ones, encoded as an account addresses.
318
+ *
319
+ * If a certain account address had not being found in the state tree - it is simply
320
+ * skipped in the response.
321
+ *
322
+ * @param accounts - List of addresses to fetch balances from
323
+ * @example
324
+ * ```js
325
+ * channel.balances([
326
+ * 'ak_Y1NRjHuoc3CGMYMvCmdHSBpJsMDR6Ra2t5zjhRcbtMeXXLpLH',
327
+ * 'ak_V6an1xhec1xVaAhLuak7QoEbi6t7w5hEtYWp9bMKaJ19i6A9E'
328
+ * 'ct_2dCUAWYZdrWfACz3a2faJeKVTVrfDYxCQHCqAt5zM15f3u2UfA'
329
+ * ]).then(balances =>
330
+ * console.log(balances['ak_Y1NRjHuoc3CGMYMvCmdHSBpJsMDR6Ra2t5zjhRcbtMeXXLpLH'])
331
+ * )
332
+ * ```
333
+ */
334
+
335
+
336
+ async balances(accounts) {
337
+ return Object.fromEntries((await call(this, 'channels.get.balances', {
338
+ accounts
339
+ })).map(item => [item.account, item.balance]));
340
+ }
341
+ /**
342
+ * Leave channel
343
+ *
344
+ * It is possible to leave a channel and then later reestablish the channel
345
+ * off-chain state and continue operation. When a leave method is called,
346
+ * the channel fsm passes it on to the peer fsm, reports the current mutually
347
+ * signed state and then terminates.
348
+ *
349
+ * The channel can be reestablished by instantiating another Channel instance
350
+ * with two extra params: existingChannelId and offchainTx (returned from leave
351
+ * method as channelId and signedTx respectively).
352
+ *
353
+ * @example
354
+ * ```js
355
+ * channel.leave().then(({ channelId, signedTx }) => {
356
+ * console.log(channelId)
357
+ * console.log(signedTx)
358
+ * })
359
+ * ```
360
+ */
361
+
362
+
363
+ async leave() {
364
+ return await new Promise((resolve, reject) => {
365
+ enqueueAction(this, (channel, state) => (state === null || state === void 0 ? void 0 : state.handler) === handlers.channelOpen, (channel, _state) => {
366
+ send(channel, {
367
+ jsonrpc: '2.0',
368
+ method: 'channels.leave',
369
+ params: {}
370
+ });
371
+ return {
372
+ handler: handlers.awaitingLeave,
373
+ state: {
374
+ resolve,
375
+ reject
376
+ }
377
+ };
288
378
  });
289
- return {
290
- handler: handlers.awaitingLeave,
291
- state: {
292
- resolve,
293
- reject
294
- }
295
- };
296
379
  });
297
- });
298
- }
299
- /**
300
- * Trigger mutual close
301
- *
302
- * At any moment after the channel is opened, a closing procedure can be triggered.
303
- * This can be done by either of the parties. The process is similar to the off-chain updates.
304
- *
305
- * @param {Function} sign - Function which verifies and signs mutual close transaction
306
- * @return {Promise<String>}
307
- * @example channel.shutdown(
308
- * async (tx) => await account.signTransaction(tx)
309
- * ).then(tx => console.log('on_chain_tx', tx))
310
- */
311
-
312
-
313
- function shutdown(sign) {
314
- return new Promise((resolve, reject) => {
315
- enqueueAction(this, (channel, state) => state.handler === handlers.channelOpen, (channel, state) => {
316
- send(channel, {
317
- jsonrpc: '2.0',
318
- method: 'channels.shutdown',
319
- params: {}
380
+ }
381
+ /**
382
+ * Trigger mutual close
383
+ *
384
+ * At any moment after the channel is opened, a closing procedure can be triggered.
385
+ * This can be done by either of the parties. The process is similar to the off-chain updates.
386
+ *
387
+ * @param sign - Function which verifies and signs mutual close transaction
388
+ * @example
389
+ * ```js
390
+ * channel.shutdown(
391
+ * async (tx) => await account.signTransaction(tx)
392
+ * ).then(tx => console.log('on_chain_tx', tx))
393
+ * ```
394
+ */
395
+
396
+
397
+ async shutdown(sign) {
398
+ return await new Promise((resolve, reject) => {
399
+ enqueueAction(this, (_channel, state) => (state === null || state === void 0 ? void 0 : state.handler) === handlers.channelOpen, channel => {
400
+ send(channel, {
401
+ jsonrpc: '2.0',
402
+ method: 'channels.shutdown',
403
+ params: {}
404
+ });
405
+ return {
406
+ handler: handlers.awaitingShutdownTx,
407
+ state: {
408
+ sign,
409
+ resolve,
410
+ reject
411
+ }
412
+ };
320
413
  });
321
- return {
322
- handler: handlers.awaitingShutdownTx,
323
- state: {
324
- sign,
325
- resolve,
326
- reject
327
- }
328
- };
329
414
  });
330
- });
331
- }
332
- /**
333
- * Withdraw tokens from the channel
334
- *
335
- * After the channel had been opened any of the participants can initiate a withdrawal.
336
- * The process closely resembles the update. The most notable difference is that the
337
- * transaction has been co-signed: it is channel_withdraw_tx and after the procedure
338
- * is finished - it is being posted on-chain.
339
- *
340
- * Any of the participants can initiate a withdrawal. The only requirements are:
341
- *
342
- * - Channel is already opened
343
- * - No off-chain update/deposit/withdrawal is currently being performed
344
- * - Channel is not being closed or in a solo closing state
345
- * - The withdrawal amount must be equal to or greater than zero, and cannot exceed
346
- * the available balance on the channel (minus the channel_reserve)
347
- *
348
- * After the other party had signed the withdraw transaction, the transaction is posted
349
- * on-chain and onOnChainTx callback is called with on-chain transaction as first argument.
350
- * After computing transaction hash it can be tracked on the chain: entering the mempool,
351
- * block inclusion and a number of confirmations.
352
- *
353
- * After the minimum_depth block confirmations onOwnWithdrawLocked callback is called
354
- * (without any arguments).
355
- *
356
- * When the other party had confirmed that the block height needed is reached
357
- * onWithdrawLocked callback is called (without any arguments).
358
- *
359
- * @param {Number} amount - Amount of tokens to withdraw
360
- * @param {Function} sign - Function which verifies and signs withdraw transaction
361
- * @param {Object} [callbacks]
362
- * @param {Function} [callbacks.onOnChainTx] - Called when withdraw transaction has been posted
363
- * on chain
364
- * @param {Function} [callbacks.onOwnWithdrawLocked]
365
- * @param {Function} [callbacks.onWithdrawLocked]
366
- * @return {Promise<Object>}
367
- * @example channel.withdraw(
368
- * 100,
369
- * async (tx) => await account.signTransaction(tx),
370
- * { onOnChainTx: (tx) => console.log('on_chain_tx', tx) }
371
- * ).then(({ accepted, signedTx }) => {
372
- * if (accepted) {
373
- * console.log('Withdrawal has been accepted')
374
- * } else {
375
- * console.log('Withdrawal has been rejected')
376
- * }
377
- * })
378
- */
379
-
380
-
381
- function withdraw(amount, sign) {
382
- let {
383
- onOnChainTx,
384
- onOwnWithdrawLocked,
385
- onWithdrawLocked
386
- } = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : {};
387
- return new Promise((resolve, reject) => {
388
- enqueueAction(this, (channel, state) => state.handler === handlers.channelOpen, (channel, state) => {
389
- send(channel, {
390
- jsonrpc: '2.0',
391
- method: 'channels.withdraw',
392
- params: {
393
- amount
394
- }
415
+ }
416
+ /**
417
+ * Withdraw coins from the channel
418
+ *
419
+ * After the channel had been opened any of the participants can initiate a withdrawal.
420
+ * The process closely resembles the update. The most notable difference is that the
421
+ * transaction has been co-signed: it is channel_withdraw_tx and after the procedure
422
+ * is finished - it is being posted on-chain.
423
+ *
424
+ * Any of the participants can initiate a withdrawal. The only requirements are:
425
+ *
426
+ * - Channel is already opened
427
+ * - No off-chain update/deposit/withdrawal is currently being performed
428
+ * - Channel is not being closed or in a solo closing state
429
+ * - The withdrawal amount must be equal to or greater than zero, and cannot exceed
430
+ * the available balance on the channel (minus the channel_reserve)
431
+ *
432
+ * After the other party had signed the withdraw transaction, the transaction is posted
433
+ * on-chain and onOnChainTx callback is called with on-chain transaction as first argument.
434
+ * After computing transaction hash it can be tracked on the chain: entering the mempool,
435
+ * block inclusion and a number of confirmations.
436
+ *
437
+ * After the minimum_depth block confirmations onOwnWithdrawLocked callback is called
438
+ * (without any arguments).
439
+ *
440
+ * When the other party had confirmed that the block height needed is reached
441
+ * onWithdrawLocked callback is called (without any arguments).
442
+ *
443
+ * @param amount - Amount of coins to withdraw
444
+ * @param sign - Function which verifies and signs withdraw transaction
445
+ * @param callbacks - Callbacks
446
+ * @param callbacks.onOnChainTx - Called when withdraw transaction has been posted
447
+ * on chain
448
+ * @param callbacks.onOwnWithdrawLocked
449
+ * @param callbacks.onWithdrawLocked
450
+ * @example
451
+ * ```js
452
+ * channel.withdraw(
453
+ * 100,
454
+ * async (tx) => await account.signTransaction(tx),
455
+ * { onOnChainTx: (tx) => console.log('on_chain_tx', tx) }
456
+ * ).then(({ accepted, signedTx }) => {
457
+ * if (accepted) {
458
+ * console.log('Withdrawal has been accepted')
459
+ * } else {
460
+ * console.log('Withdrawal has been rejected')
461
+ * }
462
+ * })
463
+ * ```
464
+ */
465
+
466
+
467
+ async withdraw(amount, sign) {
468
+ let {
469
+ onOnChainTx,
470
+ onOwnWithdrawLocked,
471
+ onWithdrawLocked
472
+ } = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : {};
473
+ return await new Promise((resolve, reject) => {
474
+ enqueueAction(this, (_channel, state) => (state === null || state === void 0 ? void 0 : state.handler) === handlers.channelOpen, channel => {
475
+ send(channel, {
476
+ jsonrpc: '2.0',
477
+ method: 'channels.withdraw',
478
+ params: {
479
+ amount
480
+ }
481
+ });
482
+ return {
483
+ handler: handlers.awaitingWithdrawTx,
484
+ state: {
485
+ sign,
486
+ resolve,
487
+ reject,
488
+ onOnChainTx,
489
+ onOwnWithdrawLocked,
490
+ onWithdrawLocked
491
+ }
492
+ };
395
493
  });
396
- return {
397
- handler: handlers.awaitingWithdrawTx,
398
- state: {
399
- sign,
400
- resolve,
401
- reject,
402
- onOnChainTx,
403
- onOwnWithdrawLocked,
404
- onWithdrawLocked
405
- }
406
- };
407
494
  });
408
- });
409
- }
410
- /**
411
- * Deposit tokens into the channel
412
- *
413
- * After the channel had been opened any of the participants can initiate a deposit.
414
- * The process closely resembles the update. The most notable difference is that the
415
- * transaction has been co-signed: it is channel_deposit_tx and after the procedure
416
- * is finished - it is being posted on-chain.
417
- *
418
- * Any of the participants can initiate a deposit. The only requirements are:
419
- *
420
- * - Channel is already opened
421
- * - No off-chain update/deposit/withdrawal is currently being performed
422
- * - Channel is not being closed or in a solo closing state
423
- * - The deposit amount must be equal to or greater than zero, and cannot exceed
424
- * the available balance on the channel (minus the channel_reserve)
425
- *
426
- * After the other party had signed the deposit transaction, the transaction is posted
427
- * on-chain and onOnChainTx callback is called with on-chain transaction as first argument.
428
- * After computing transaction hash it can be tracked on the chain: entering the mempool,
429
- * block inclusion and a number of confirmations.
430
- *
431
- * After the minimum_depth block confirmations onOwnDepositLocked callback is called
432
- * (without any arguments).
433
- *
434
- * When the other party had confirmed that the block height needed is reached
435
- * onDepositLocked callback is called (without any arguments).
436
- *
437
- * @param {Number} amount - Amount of tokens to deposit
438
- * @param {Function} sign - Function which verifies and signs deposit transaction
439
- * @param {Object} [callbacks]
440
- * @param {Function} [callbacks.onOnChainTx] - Called when deposit transaction has been posted
441
- * on chain
442
- * @param {Function} [callbacks.onOwnDepositLocked]
443
- * @param {Function} [callbacks.onDepositLocked]
444
- * @return {Promise<Object>}
445
- * @example channel.deposit(
446
- * 100,
447
- * async (tx) => await account.signTransaction(tx),
448
- * { onOnChainTx: (tx) => console.log('on_chain_tx', tx) }
449
- * ).then(({ accepted, state }) => {
450
- * if (accepted) {
451
- * console.log('Deposit has been accepted')
452
- * console.log('The new state is:', state)
453
- * } else {
454
- * console.log('Deposit has been rejected')
455
- * }
456
- * })
457
- */
458
-
459
-
460
- function deposit(amount, sign) {
461
- let {
462
- onOnChainTx,
463
- onOwnDepositLocked,
464
- onDepositLocked
465
- } = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : {};
466
- return new Promise((resolve, reject) => {
467
- enqueueAction(this, (channel, state) => state.handler === handlers.channelOpen, (channel, state) => {
468
- send(channel, {
469
- jsonrpc: '2.0',
470
- method: 'channels.deposit',
471
- params: {
472
- amount
473
- }
495
+ }
496
+ /**
497
+ * Deposit coins into the channel
498
+ *
499
+ * After the channel had been opened any of the participants can initiate a deposit.
500
+ * The process closely resembles the update. The most notable difference is that the
501
+ * transaction has been co-signed: it is channel_deposit_tx and after the procedure
502
+ * is finished - it is being posted on-chain.
503
+ *
504
+ * Any of the participants can initiate a deposit. The only requirements are:
505
+ *
506
+ * - Channel is already opened
507
+ * - No off-chain update/deposit/withdrawal is currently being performed
508
+ * - Channel is not being closed or in a solo closing state
509
+ * - The deposit amount must be equal to or greater than zero, and cannot exceed
510
+ * the available balance on the channel (minus the channel_reserve)
511
+ *
512
+ * After the other party had signed the deposit transaction, the transaction is posted
513
+ * on-chain and onOnChainTx callback is called with on-chain transaction as first argument.
514
+ * After computing transaction hash it can be tracked on the chain: entering the mempool,
515
+ * block inclusion and a number of confirmations.
516
+ *
517
+ * After the minimum_depth block confirmations onOwnDepositLocked callback is called
518
+ * (without any arguments).
519
+ *
520
+ * When the other party had confirmed that the block height needed is reached
521
+ * onDepositLocked callback is called (without any arguments).
522
+ *
523
+ * @param amount - Amount of coins to deposit
524
+ * @param sign - Function which verifies and signs deposit transaction
525
+ * @param callbacks - Callbacks
526
+ * @param callbacks.onOnChainTx - Called when deposit transaction has been posted
527
+ * on chain
528
+ * @param callbacks.onOwnDepositLocked
529
+ * @param callbacks.onDepositLocked
530
+ * @example
531
+ * ```js
532
+ * channel.deposit(
533
+ * 100,
534
+ * async (tx) => await account.signTransaction(tx),
535
+ * { onOnChainTx: (tx) => console.log('on_chain_tx', tx) }
536
+ * ).then(({ accepted, state }) => {
537
+ * if (accepted) {
538
+ * console.log('Deposit has been accepted')
539
+ * console.log('The new state is:', state)
540
+ * } else {
541
+ * console.log('Deposit has been rejected')
542
+ * }
543
+ * })
544
+ * ```
545
+ */
546
+
547
+
548
+ async deposit(amount, sign) {
549
+ let {
550
+ onOnChainTx,
551
+ onOwnDepositLocked,
552
+ onDepositLocked
553
+ } = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : {};
554
+ return await new Promise((resolve, reject) => {
555
+ enqueueAction(this, (_channel, state) => (state === null || state === void 0 ? void 0 : state.handler) === handlers.channelOpen, channel => {
556
+ send(channel, {
557
+ jsonrpc: '2.0',
558
+ method: 'channels.deposit',
559
+ params: {
560
+ amount
561
+ }
562
+ });
563
+ return {
564
+ handler: handlers.awaitingDepositTx,
565
+ state: {
566
+ sign,
567
+ resolve,
568
+ reject,
569
+ onOnChainTx,
570
+ onOwnDepositLocked,
571
+ onDepositLocked
572
+ }
573
+ };
474
574
  });
475
- return {
476
- handler: handlers.awaitingDepositTx,
477
- state: {
478
- sign,
479
- resolve,
480
- reject,
481
- onOnChainTx,
482
- onOwnDepositLocked,
483
- onDepositLocked
484
- }
485
- };
486
575
  });
487
- });
488
- }
489
- /**
490
- * Trigger create contract update
491
- *
492
- * The create contract update is creating a contract inside the channel's internal state tree.
493
- * The update is a change to be applied on top of the latest state.
494
- *
495
- * That would create a contract with the poster being the owner of it. Poster commits initially
496
- * a deposit amount of tokens to the new contract.
497
- *
498
- * @param {Object} options
499
- * @param {String} options.code - Api encoded compiled AEVM byte code
500
- * @param {String} options.callData - Api encoded compiled AEVM call data for the code
501
- * @param {Number} options.deposit - Initial amount the owner of the contract commits to it
502
- * @param {Number} options.vmVersion - Version of the Virtual Machine
503
- * @param {Number} options.abiVersion - Version of the Application Binary Interface
504
- * @param {Function} sign - Function which verifies and signs create contract transaction
505
- * @return {Promise<Object>}
506
- * @example channel.createContract({
507
- * code: 'cb_HKtpipK4aCgYb17wZ...',
508
- * callData: 'cb_1111111111111111...',
509
- * deposit: 10,
510
- * vmVersion: 3,
511
- * abiVersion: 1
512
- * }).then(({ accepted, signedTx, address }) => {
513
- * if (accepted) {
514
- * console.log('New contract has been created')
515
- * console.log('Contract address:', address)
516
- * } else {
517
- * console.log('New contract has been rejected')
518
- * }
519
- * })
520
- */
521
-
522
-
523
- function createContract(_ref3, sign) {
524
- let {
525
- code,
526
- callData,
527
- deposit,
528
- vmVersion,
529
- abiVersion
530
- } = _ref3;
531
- return new Promise((resolve, reject) => {
532
- enqueueAction(this, (channel, state) => state.handler === handlers.channelOpen, (channel, state) => {
533
- send(channel, {
534
- jsonrpc: '2.0',
535
- method: 'channels.update.new_contract',
536
- params: {
537
- code,
538
- call_data: callData,
539
- deposit,
540
- vm_version: vmVersion,
541
- abi_version: abiVersion
542
- }
576
+ }
577
+ /**
578
+ * Trigger create contract update
579
+ *
580
+ * The create contract update is creating a contract inside the channel's internal state tree.
581
+ * The update is a change to be applied on top of the latest state.
582
+ *
583
+ * That would create a contract with the poster being the owner of it. Poster commits initially
584
+ * a deposit amount of coins to the new contract.
585
+ *
586
+ * @param options - Options
587
+ * @param options.code - Api encoded compiled AEVM byte code
588
+ * @param options.callData - Api encoded compiled AEVM call data for the code
589
+ * @param options.deposit - Initial amount the owner of the contract commits to it
590
+ * @param options.vmVersion - Version of the Virtual Machine
591
+ * @param options.abiVersion - Version of the Application Binary Interface
592
+ * @param sign - Function which verifies and signs create contract transaction
593
+ * @example
594
+ * ```js
595
+ * channel.createContract({
596
+ * code: 'cb_HKtpipK4aCgYb17wZ...',
597
+ * callData: 'cb_1111111111111111...',
598
+ * deposit: 10,
599
+ * vmVersion: 3,
600
+ * abiVersion: 1
601
+ * }).then(({ accepted, signedTx, address }) => {
602
+ * if (accepted) {
603
+ * console.log('New contract has been created')
604
+ * console.log('Contract address:', address)
605
+ * } else {
606
+ * console.log('New contract has been rejected')
607
+ * }
608
+ * })
609
+ * ```
610
+ */
611
+
612
+
613
+ async createContract(_ref3, sign) {
614
+ let {
615
+ code,
616
+ callData,
617
+ deposit,
618
+ vmVersion,
619
+ abiVersion
620
+ } = _ref3;
621
+ return await new Promise((resolve, reject) => {
622
+ enqueueAction(this, (_channel, state) => (state === null || state === void 0 ? void 0 : state.handler) === handlers.channelOpen, channel => {
623
+ send(channel, {
624
+ jsonrpc: '2.0',
625
+ method: 'channels.update.new_contract',
626
+ params: {
627
+ code,
628
+ call_data: callData,
629
+ deposit,
630
+ vm_version: vmVersion,
631
+ abi_version: abiVersion
632
+ }
633
+ });
634
+ return {
635
+ handler: handlers.awaitingNewContractTx,
636
+ state: {
637
+ sign,
638
+ resolve,
639
+ reject
640
+ }
641
+ };
543
642
  });
544
- return {
545
- handler: handlers.awaitingNewContractTx,
546
- state: {
547
- sign,
548
- resolve,
549
- reject
550
- }
551
- };
552
643
  });
553
- });
554
- }
555
- /**
556
- * Trigger call a contract update
557
- *
558
- * The call contract update is calling a preexisting contract inside the channel's
559
- * internal state tree. The update is a change to be applied on top of the latest state.
560
- *
561
- * That would call a contract with the poster being the caller of it. Poster commits
562
- * an amount of tokens to the contract.
563
- *
564
- * The call would also create a call object inside the channel state tree. It contains
565
- * the result of the contract call.
566
- *
567
- * It is worth mentioning that the gas is not consumed, because this is an off-chain
568
- * contract call. It would be consumed if it were a on-chain one. This could happen
569
- * if a call with a similar computation amount is to be forced on-chain.
570
- *
571
- * @param {Object} options
572
- * @param {String} [options.amount] - Amount the caller of the contract commits to it
573
- * @param {String} [options.callData] - ABI encoded compiled AEVM call data for the code
574
- * @param {Number} [options.contract] - Address of the contract to call
575
- * @param {Number} [options.abiVersion] - Version of the ABI
576
- * @param {Function} sign - Function which verifies and signs contract call transaction
577
- * @return {Promise<Object>}
578
- * @example channel.callContract({
579
- * contract: 'ct_9sRA9AVE4BYTAkh5RNfJYmwQe1NZ4MErasQLXZkFWG43TPBqa',
580
- * callData: 'cb_1111111111111111...',
581
- * amount: 0,
582
- * abiVersion: 1
583
- * }).then(({ accepted, signedTx }) => {
584
- * if (accepted) {
585
- * console.log('Contract called succesfully')
586
- * } else {
587
- * console.log('Contract call has been rejected')
588
- * }
589
- * })
590
- */
591
-
592
-
593
- function callContract(_ref4, sign) {
594
- let {
595
- amount,
596
- callData,
597
- contract,
598
- abiVersion
599
- } = _ref4;
600
- return new Promise((resolve, reject) => {
601
- enqueueAction(this, (channel, state) => state.handler === handlers.channelOpen, (channel, state) => {
602
- send(channel, {
603
- jsonrpc: '2.0',
604
- method: 'channels.update.call_contract',
605
- params: {
606
- amount,
607
- call_data: callData,
608
- contract_id: contract,
609
- abi_version: abiVersion
610
- }
644
+ }
645
+ /**
646
+ * Trigger call a contract update
647
+ *
648
+ * The call contract update is calling a preexisting contract inside the channel's
649
+ * internal state tree. The update is a change to be applied on top of the latest state.
650
+ *
651
+ * That would call a contract with the poster being the caller of it. Poster commits
652
+ * an amount of coins to the contract.
653
+ *
654
+ * The call would also create a call object inside the channel state tree. It contains
655
+ * the result of the contract call.
656
+ *
657
+ * It is worth mentioning that the gas is not consumed, because this is an off-chain
658
+ * contract call. It would be consumed if it were an on-chain one. This could happen
659
+ * if a call with a similar computation amount is to be forced on-chain.
660
+ *
661
+ * @param options - Options
662
+ * @param options.amount - Amount the caller of the contract commits to it
663
+ * @param options.callData - ABI encoded compiled AEVM call data for the code
664
+ * @param options.contract - Address of the contract to call
665
+ * @param options.abiVersion - Version of the ABI
666
+ * @param sign - Function which verifies and signs contract call transaction
667
+ * @example
668
+ * ```js
669
+ * channel.callContract({
670
+ * contract: 'ct_9sRA9AVE4BYTAkh5RNfJYmwQe1NZ4MErasQLXZkFWG43TPBqa',
671
+ * callData: 'cb_1111111111111111...',
672
+ * amount: 0,
673
+ * abiVersion: 1
674
+ * }).then(({ accepted, signedTx }) => {
675
+ * if (accepted) {
676
+ * console.log('Contract called succesfully')
677
+ * } else {
678
+ * console.log('Contract call has been rejected')
679
+ * }
680
+ * })
681
+ * ```
682
+ */
683
+
684
+
685
+ async callContract(_ref4, sign) {
686
+ let {
687
+ amount,
688
+ callData,
689
+ contract,
690
+ abiVersion
691
+ } = _ref4;
692
+ return await new Promise((resolve, reject) => {
693
+ enqueueAction(this, (_channel, state) => (state === null || state === void 0 ? void 0 : state.handler) === handlers.channelOpen, channel => {
694
+ send(channel, {
695
+ jsonrpc: '2.0',
696
+ method: 'channels.update.call_contract',
697
+ params: {
698
+ amount,
699
+ call_data: callData,
700
+ contract_id: contract,
701
+ abi_version: abiVersion
702
+ }
703
+ });
704
+ return {
705
+ handler: handlers.awaitingCallContractUpdateTx,
706
+ state: {
707
+ resolve,
708
+ reject,
709
+ sign
710
+ }
711
+ };
611
712
  });
612
- return {
613
- handler: handlers.awaitingCallContractUpdateTx,
614
- state: {
615
- resolve,
616
- reject,
617
- sign
618
- }
619
- };
620
713
  });
621
- });
622
- }
623
- /**
624
- * Trigger a force progress contract call
625
- * This call is going on-chain
626
- * @param {Object} options
627
- * @param {String} [options.amount] - Amount the caller of the contract commits to it
628
- * @param {String} [options.callData] - ABI encoded compiled AEVM call data for the code
629
- * @param {Number} [options.contract] - Address of the contract to call
630
- * @param {Number} [options.abiVersion] - Version of the ABI
631
- * @param {Number} [options.gasPrice=1000000000] - Gas price
632
- * @param {Number} [options.gas=1000000] - Gas limit
633
- * @param {Function} sign - Function which verifies and signs contract force progress transaction
634
- * @param {{ onOnChainTxL: Function }} callbacks
635
- * @return {Promise<Object>}
636
- * @example channel.forceProgress({
637
- * contract: 'ct_9sRA9AVE4BYTAkh5RNfJYmwQe1NZ4MErasQLXZkFWG43TPBqa',
638
- * callData: 'cb_1111111111111111...',
639
- * amount: 0,
640
- * abiVersion: 1,
641
- * gasPrice: 1000005554
642
- * }).then(({ accepted, signedTx }) => {
643
- * if (accepted) {
644
- * console.log('Contract force progress call successful')
645
- * } else {
646
- * console.log('Contract force progress call has been rejected')
647
- * }
648
- * })
649
- */
650
-
651
-
652
- function forceProgress(_ref5, sign) {
653
- let {
654
- amount,
655
- callData,
656
- contract,
657
- abiVersion,
658
- gas = 1000000,
659
- gasPrice = 1000000000,
660
- nonce
661
- } = _ref5;
662
- let {
663
- onOnChainTx
664
- } = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : {};
665
- return new Promise((resolve, reject) => {
666
- enqueueAction(this, (channel, state) => state.handler === handlers.channelOpen, (channel, state) => {
667
- send(channel, {
668
- jsonrpc: '2.0',
669
- method: 'channels.force_progress',
670
- params: {
671
- amount,
672
- call_data: callData,
673
- contract_id: contract,
674
- abi_version: abiVersion,
675
- gas_price: gasPrice,
676
- gas
677
- }
714
+ }
715
+ /**
716
+ * Trigger a force progress contract call
717
+ * This call is going on-chain
718
+ * @param options - Options
719
+ * @param options.amount - Amount the caller of the contract commits to it
720
+ * @param options.callData - ABI encoded compiled AEVM call data for the code
721
+ * @param options.contract - Address of the contract to call
722
+ * @param options.abiVersion - Version of the ABI
723
+ * @param options.gasPrice=1000000000]
724
+ * @param options.gasLimit=1000000]
725
+ * @param sign - Function which verifies and signs contract force progress transaction
726
+ * @param callbacks - Callbacks
727
+ * @example
728
+ * ```js
729
+ * channel.forceProgress({
730
+ * contract: 'ct_9sRA9AVE4BYTAkh5RNfJYmwQe1NZ4MErasQLXZkFWG43TPBqa',
731
+ * callData: 'cb_1111111111111111...',
732
+ * amount: 0,
733
+ * abiVersion: 1,
734
+ * gasPrice: 1000005554
735
+ * }).then(({ accepted, signedTx }) => {
736
+ * if (accepted) {
737
+ * console.log('Contract force progress call successful')
738
+ * } else {
739
+ * console.log('Contract force progress call has been rejected')
740
+ * }
741
+ * })
742
+ * ```
743
+ */
744
+
745
+
746
+ async forceProgress(_ref5, sign) {
747
+ let {
748
+ amount,
749
+ callData,
750
+ contract,
751
+ abiVersion,
752
+ gasLimit = 1000000,
753
+ gasPrice = 1000000000
754
+ } = _ref5;
755
+ let {
756
+ onOnChainTx
757
+ } = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : {};
758
+ return await new Promise((resolve, reject) => {
759
+ enqueueAction(this, (_channel, state) => (state === null || state === void 0 ? void 0 : state.handler) === handlers.channelOpen, channel => {
760
+ send(channel, {
761
+ jsonrpc: '2.0',
762
+ method: 'channels.force_progress',
763
+ params: {
764
+ amount,
765
+ call_data: callData,
766
+ contract_id: contract,
767
+ abi_version: abiVersion,
768
+ gas_price: gasPrice,
769
+ gas: gasLimit
770
+ }
771
+ });
772
+ return {
773
+ handler: handlers.awaitingCallContractForceProgressUpdate,
774
+ state: {
775
+ resolve,
776
+ reject,
777
+ sign,
778
+ onOnChainTx
779
+ }
780
+ };
678
781
  });
679
- return {
680
- handler: handlers.awaitingCallContractForceProgressUpdate,
681
- state: {
682
- resolve,
683
- reject,
684
- sign,
685
- onOnChainTx
686
- }
687
- };
688
782
  });
689
- });
690
- }
691
- /**
692
- * Call contract using dry-run
693
- *
694
- * In order to get the result of a potential contract call, one might need to
695
- * dry-run a contract call. It takes the exact same arguments as a call would
696
- * and returns the call object.
697
- *
698
- * The call is executed in the channel's state but it does not impact the state
699
- * whatsoever. It uses as an environment the latest channel's state and the current
700
- * top of the blockchain as seen by the node.
701
- *
702
- * @param {Object} options
703
- * @param {String} [options.amount] - Amount the caller of the contract commits to it
704
- * @param {String} [options.callData] - ABI encoded compiled AEVM call data for the code
705
- * @param {Number} [options.contract] - Address of the contract to call
706
- * @param {Number} [options.abiVersion] - Version of the ABI
707
- * @return {Promise<Object>}
708
- * @example channel.callContractStatic({
709
- * contract: 'ct_9sRA9AVE4BYTAkh5RNfJYmwQe1NZ4MErasQLXZkFWG43TPBqa',
710
- * callData: 'cb_1111111111111111...',
711
- * amount: 0,
712
- * abiVersion: 1
713
- * }).then(({ returnValue, gasUsed }) => {
714
- * console.log('Returned value:', returnValue)
715
- * console.log('Gas used:', gasUsed)
716
- * })
717
- */
718
-
719
-
720
- async function callContractStatic(_ref6) {
721
- let {
722
- amount,
723
- callData,
724
- contract,
725
- abiVersion
726
- } = _ref6;
727
- return snakeToPascalObjKeys(await call(this, 'channels.dry_run.call_contract', {
728
- amount,
729
- call_data: callData,
730
- contract_id: contract,
731
- abi_version: abiVersion
732
- }));
733
- }
734
- /**
735
- * Get contract call result
736
- *
737
- * The combination of a caller, contract and a round of execution determines the
738
- * contract call. Providing an incorrect set of those results in an error response.
739
- *
740
- * @param {Object} options
741
- * @param {String} [options.caller] - Address of contract caller
742
- * @param {String} [options.contract] - Address of the contract
743
- * @param {Number} [options.round] - Round when contract was called
744
- * @return {Promise<Object>}
745
- * @example channel.getContractCall({
746
- * caller: 'ak_Y1NRjHuoc3CGMYMvCmdHSBpJsMDR6Ra2t5zjhRcbtMeXXLpLH',
747
- * contract: 'ct_9sRA9AVE4BYTAkh5RNfJYmwQe1NZ4MErasQLXZkFWG43TPBqa',
748
- * round: 3
749
- * }).then(({ returnType, returnValue }) => {
750
- * if (returnType === 'ok') console.log(returnValue)
751
- * })
752
- */
753
-
754
-
755
- async function getContractCall(_ref7) {
756
- let {
757
- caller,
758
- contract,
759
- round
760
- } = _ref7;
761
- return snakeToPascalObjKeys(await call(this, 'channels.get.contract_call', {
762
- caller_id: caller,
763
- contract_id: contract,
764
- round
765
- }));
766
- }
767
- /**
768
- * Get contract latest state
769
- *
770
- * @param {String} contract - Address of the contract
771
- * @return {Promise<Object>}
772
- * @example channel.getContractState(
773
- * 'ct_9sRA9AVE4BYTAkh5RNfJYmwQe1NZ4MErasQLXZkFWG43TPBqa'
774
- * ).then(({ contract }) => {
775
- * console.log('deposit:', contract.deposit)
776
- * })
777
- */
778
-
779
-
780
- async function getContractState(contract) {
781
- const result = await call(this, 'channels.get.contract', {
782
- pubkey: contract
783
- });
784
- return snakeToPascalObjKeys({ ...result,
785
- contract: snakeToPascalObjKeys(result.contract)
786
- });
787
- }
788
- /**
789
- * Clean up all locally stored contract calls
790
- *
791
- * Contract calls are kept locally in order for the participant to be able to look them up.
792
- * They consume memory and in order for the participant to free it - one can prune all messages.
793
- * This cleans up all locally stored contract calls and those will no longer be available for
794
- * fetching and inspection.
795
- *
796
- * @return {Promise}
797
- */
798
-
799
-
800
- function cleanContractCalls() {
801
- return new Promise((resolve, reject) => {
802
- enqueueAction(this, (channel, state) => state.handler === handlers.channelOpen, (channel, state) => {
803
- send(channel, {
804
- jsonrpc: '2.0',
805
- method: 'channels.clean_contract_calls',
806
- params: {}
783
+ }
784
+ /**
785
+ * Call contract using dry-run
786
+ *
787
+ * In order to get the result of a potential contract call, one might need to
788
+ * dry-run a contract call. It takes the exact same arguments as a call would
789
+ * and returns the call object.
790
+ *
791
+ * The call is executed in the channel's state but it does not impact the state
792
+ * whatsoever. It uses as an environment the latest channel's state and the current
793
+ * top of the blockchain as seen by the node.
794
+ *
795
+ * @param options - Options
796
+ * @param options.amount - Amount the caller of the contract commits to it
797
+ * @param options.callData - ABI encoded compiled AEVM call data for the code
798
+ * @param options.contract - Address of the contract to call
799
+ * @param options.abiVersion - Version of the ABI
800
+ * @example
801
+ * ```js
802
+ * channel.callContractStatic({
803
+ * contract: 'ct_9sRA9AVE4BYTAkh5RNfJYmwQe1NZ4MErasQLXZkFWG43TPBqa',
804
+ * callData: 'cb_1111111111111111...',
805
+ * amount: 0,
806
+ * abiVersion: 1
807
+ * }).then(({ returnValue, gasUsed }) => {
808
+ * console.log('Returned value:', returnValue)
809
+ * console.log('Gas used:', gasUsed)
810
+ * })
811
+ * ```
812
+ */
813
+
814
+
815
+ async callContractStatic(_ref6) {
816
+ let {
817
+ amount,
818
+ callData,
819
+ contract,
820
+ abiVersion
821
+ } = _ref6;
822
+ return await snakeToPascalObjKeys(await call(this, 'channels.dry_run.call_contract', {
823
+ amount,
824
+ call_data: callData,
825
+ contract_id: contract,
826
+ abi_version: abiVersion
827
+ }));
828
+ }
829
+ /**
830
+ * Get contract call result
831
+ *
832
+ * The combination of a caller, contract and a round of execution determines the
833
+ * contract call. Providing an incorrect set of those results in an error response.
834
+ *
835
+ * @param options - Options
836
+ * @param options.caller - Address of contract caller
837
+ * @param options.contract - Address of the contract
838
+ * @param options.round - Round when contract was called
839
+ * @example
840
+ * ```js
841
+ * channel.getContractCall({
842
+ * caller: 'ak_Y1NRjHuoc3CGMYMvCmdHSBpJsMDR6Ra2t5zjhRcbtMeXXLpLH',
843
+ * contract: 'ct_9sRA9AVE4BYTAkh5RNfJYmwQe1NZ4MErasQLXZkFWG43TPBqa',
844
+ * round: 3
845
+ * }).then(({ returnType, returnValue }) => {
846
+ * if (returnType === 'ok') console.log(returnValue)
847
+ * })
848
+ * ```
849
+ */
850
+
851
+
852
+ async getContractCall(_ref7) {
853
+ let {
854
+ caller,
855
+ contract,
856
+ round
857
+ } = _ref7;
858
+ return await snakeToPascalObjKeys(await call(this, 'channels.get.contract_call', {
859
+ caller_id: caller,
860
+ contract_id: contract,
861
+ round
862
+ }));
863
+ }
864
+ /**
865
+ * Get contract latest state
866
+ *
867
+ * @param contract - Address of the contract
868
+ * @example
869
+ * ```js
870
+ * channel.getContractState(
871
+ * 'ct_9sRA9AVE4BYTAkh5RNfJYmwQe1NZ4MErasQLXZkFWG43TPBqa'
872
+ * ).then(({ contract }) => {
873
+ * console.log('deposit:', contract.deposit)
874
+ * })
875
+ * ```
876
+ */
877
+
878
+
879
+ async getContractState(contract) {
880
+ const result = await call(this, 'channels.get.contract', {
881
+ pubkey: contract
882
+ });
883
+ return await snakeToPascalObjKeys({ ...result,
884
+ contract: snakeToPascalObjKeys(result.contract)
885
+ });
886
+ }
887
+ /**
888
+ * Clean up all locally stored contract calls
889
+ *
890
+ * Contract calls are kept locally in order for the participant to be able to look them up.
891
+ * They consume memory and in order for the participant to free it - one can prune all messages.
892
+ * This cleans up all locally stored contract calls and those will no longer be available for
893
+ * fetching and inspection.
894
+ */
895
+
896
+
897
+ async cleanContractCalls() {
898
+ return await new Promise((resolve, reject) => {
899
+ enqueueAction(this, (_channel, state) => (state === null || state === void 0 ? void 0 : state.handler) === handlers.channelOpen, channel => {
900
+ send(channel, {
901
+ jsonrpc: '2.0',
902
+ method: 'channels.clean_contract_calls',
903
+ params: {}
904
+ });
905
+ return {
906
+ handler: handlers.awaitingCallsPruned,
907
+ state: {
908
+ resolve,
909
+ reject
910
+ }
911
+ };
807
912
  });
808
- return {
809
- handler: handlers.awaitingCallsPruned,
810
- state: {
811
- resolve,
812
- reject
813
- }
814
- };
815
913
  });
816
- });
817
- }
818
- /**
819
- * Send generic message
820
- *
821
- * If message is an object it will be serialized into JSON string
822
- * before sending.
823
- *
824
- * If there is ongoing update that has not yet been finished the message
825
- * will be sent after that update is finalized.
826
- *
827
- * @param {String|Object} message
828
- * @param {String} recipient - Address of the recipient
829
- * @example channel.sendMessage(
830
- * 'hello world',
831
- * 'ak_Y1NRjHuoc3CGMYMvCmdHSBpJsMDR6Ra2t5zjhRcbtMeXXLpLH'
832
- * )
833
- */
834
-
914
+ }
915
+ /**
916
+ * Send generic message
917
+ *
918
+ * If message is an object it will be serialized into JSON string
919
+ * before sending.
920
+ *
921
+ * If there is ongoing update that has not yet been finished the message
922
+ * will be sent after that update is finalized.
923
+ *
924
+ * @param message - Message
925
+ * @param recipient - Address of the recipient
926
+ * @example
927
+ * ```js
928
+ * channel.sendMessage(
929
+ * 'hello world',
930
+ * 'ak_Y1NRjHuoc3CGMYMvCmdHSBpJsMDR6Ra2t5zjhRcbtMeXXLpLH'
931
+ * )
932
+ * ```
933
+ */
934
+
935
+
936
+ sendMessage(message, recipient) {
937
+ let info = message;
938
+
939
+ if (typeof message === 'object') {
940
+ info = JSON.stringify(message);
941
+ }
835
942
 
836
- function sendMessage(message, recipient) {
837
- let info = message;
943
+ const doSend = channel => send(channel, {
944
+ jsonrpc: '2.0',
945
+ method: 'channels.message',
946
+ params: {
947
+ info,
948
+ to: recipient
949
+ }
950
+ });
838
951
 
839
- if (typeof message === 'object') {
840
- info = JSON.stringify(message);
841
- }
952
+ if (this.status() === 'connecting') {
953
+ const onStatusChanged = status => {
954
+ if (status !== 'connecting') {
955
+ // For some reason we can't immediately send a message when connection is
956
+ // established. Thus we wait 500ms which seems to work.
957
+ setTimeout(() => doSend(this), 500);
958
+ this.off('statusChanged', onStatusChanged);
959
+ }
960
+ };
842
961
 
843
- const doSend = channel => send(channel, {
844
- jsonrpc: '2.0',
845
- method: 'channels.message',
846
- params: {
847
- info,
848
- to: recipient
962
+ this.on('statusChanged', onStatusChanged);
963
+ } else {
964
+ doSend(this);
849
965
  }
850
- });
851
-
852
- if (this.status() === 'connecting') {
853
- const onStatusChanged = status => {
854
- if (status !== 'connecting') {
855
- // For some reason we can't immediately send a message when connection is
856
- // established. Thus we wait 500ms which seems to work.
857
- setTimeout(() => doSend(this), 500);
858
- this.off('statusChanged', onStatusChanged);
859
- }
860
- };
966
+ }
861
967
 
862
- this.on('statusChanged', onStatusChanged);
863
- } else {
864
- doSend(this);
968
+ static async reconnect(options, txParams) {
969
+ const {
970
+ sign
971
+ } = options;
972
+ return await Channel.initialize({ ...options,
973
+ reconnectTx: await sign('reconnect', buildTx(txParams, TX_TYPE.channelReconnect).tx)
974
+ });
865
975
  }
866
- }
867
976
 
868
- async function reconnect(options, txParams) {
869
- const {
870
- sign
871
- } = options;
872
- return Channel({ ...options,
873
- reconnectTx: await sign('reconnect', buildTx(txParams, TX_TYPE.channelReconnect).tx)
874
- });
875
977
  }
876
- /**
877
- * Channel
878
- *
879
- * @function
880
- * @alias module:@aeternity/aepp-sdk/es/channel/index
881
- * @rtype Channel
882
- * @param {Object} options - Channel params
883
- * @param {String} options.url - Channel url (for example: "ws://localhost:3001")
884
- * @param {String} options.role - Participant role ("initiator" or "responder")
885
- * @param {String} options.initiatorId - Initiator's public key
886
- * @param {String} options.responderId - Responder's public key
887
- * @param {Number} options.pushAmount - Initial deposit in favour of the responder by the initiator
888
- * @param {Number} options.initiatorAmount - Amount of tokens the initiator has committed to
889
- * the channel
890
- * @param {Number} options.responderAmount - Amount of tokens the responder has committed to
891
- * the channel
892
- * @param {Number} options.channelReserve - The minimum amount both peers need to maintain
893
- * @param {Number} [options.ttl] - Minimum block height to include the channel_create_tx
894
- * @param {String} options.host - Host of the responder's node
895
- * @param {Number} options.port - The port of the responders node
896
- * @param {Number} options.lockPeriod - Amount of blocks for disputing a solo close
897
- * @param {Number} [options.existingChannelId] - Existing channel id (required if reestablishing a
898
- * channel)
899
- * @param {Number} [options.offchainTx] - Offchain transaction (required if reestablishing
900
- * a channel)
901
- * @param {Number} [options.timeoutIdle] - The time waiting for a new event to be initiated
902
- * (default: 600000)
903
- * @param {Number} [options.timeoutFundingCreate] - The time waiting for the initiator to produce
904
- * the create channel transaction after the noise session had been established (default: 120000)
905
- * @param {Number} [options.timeoutFundingSign] - The time frame the other client has to sign an
906
- * off-chain update after our client had initiated and signed it. This applies only for double
907
- * signed on-chain intended updates: channel create transaction, deposit, withdrawal and etc.
908
- * (default: 120000)
909
- * @param {Number} [options.timeoutFundingLock] - The time frame the other client has to confirm an
910
- * on-chain transaction reaching maturity (passing minimum depth) after the local node has detected
911
- * this. This applies only for double signed on-chain intended updates: channel create transaction,
912
- * deposit, withdrawal and etc. (default: 360000)
913
- * @param {Number} [options.timeoutSign] - The time frame the client has to return a signed
914
- * off-chain update or to decline it. This applies for all off-chain updates (default: 500000)
915
- * @param {Number} [options.timeoutAccept] - The time frame the other client has to react to an
916
- * event. This applies for all off-chain updates that are not meant to land on-chain, as well as
917
- * some special cases: opening a noise connection, mutual closing acknowledgement and
918
- * reestablishing an existing channel (default: 120000)
919
- * @param {Number} [options.timeoutInitialized] - the time frame the responder has to accept an
920
- * incoming noise session. Applicable only for initiator (default: timeout_accept's value)
921
- * @param {Number} [options.timeoutAwaitingOpen] - The time frame the initiator has to start an
922
- * outgoing noise session to the responder's node. Applicable only for responder (default:
923
- * timeout_idle's value)
924
- * @param {Number} [options.debug=false] - Log websocket communication
925
- * @param {Function} options.sign - Function which verifies and signs transactions
926
- * @return {Promise<Object>} Channel instance
927
- * @example Channel({
928
- * url: 'ws://localhost:3001',
929
- * role: 'initiator'
930
- * initiatorId: 'ak_Y1NRjHuoc3CGMYMvCmdHSBpJsMDR6Ra2t5zjhRcbtMeXXLpLH',
931
- * responderId: 'ak_V6an1xhec1xVaAhLuak7QoEbi6t7w5hEtYWp9bMKaJ19i6A9E',
932
- * initiatorAmount: 1e18,
933
- * responderAmount: 1e18,
934
- * pushAmount: 0,
935
- * channelReserve: 0,
936
- * ttl: 1000,
937
- * host: 'localhost',
938
- * port: 3002,
939
- * lockPeriod: 10,
940
- * async sign (tag, tx) => await account.signTransaction(tx)
941
- * })
942
- */
943
-
944
-
945
- const Channel = AsyncInit.compose({
946
- async init(options) {
947
- await initialize(this, options.existingFsmId ? handlers.awaitingReconnection : handlers.awaitingConnection, handlers.channelOpen, options);
948
- },
949
-
950
- methods: {
951
- on,
952
- off,
953
- status,
954
- state,
955
- round,
956
- id,
957
- fsmId,
958
- update,
959
- poi,
960
- balances,
961
- leave,
962
- shutdown,
963
- sendMessage,
964
- withdraw,
965
- deposit,
966
- createContract,
967
- callContract,
968
- callContractStatic,
969
- getContractCall,
970
- getContractState,
971
- disconnect,
972
- cleanContractCalls,
973
- forceProgress
974
- },
975
- statics: {
976
- reconnect
977
- }
978
- });
979
- export default Channel;
980
978
  //# sourceMappingURL=index.mjs.map