@alephium/web3 0.1.0-rc.3 → 0.1.0-rc.3-hc

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 (33) hide show
  1. package/dev/user.conf +1 -0
  2. package/dist/alephium-web3.min.js +1 -1
  3. package/dist/alephium-web3.min.js.map +1 -1
  4. package/dist/src/api/api-alephium.d.ts +63 -1
  5. package/dist/src/api/api-alephium.js +1 -1
  6. package/dist/src/api/api-explorer.d.ts +33 -6
  7. package/dist/src/contract/events.d.ts +7 -25
  8. package/dist/src/contract/events.js +18 -31
  9. package/dist/src/index.d.ts +1 -0
  10. package/dist/src/index.js +1 -0
  11. package/dist/src/transaction/index.d.ts +2 -0
  12. package/dist/src/transaction/index.js +31 -0
  13. package/dist/src/{utils/transaction.d.ts → transaction/sign-verify.d.ts} +0 -0
  14. package/dist/src/{utils/transaction.js → transaction/sign-verify.js} +1 -1
  15. package/dist/src/transaction/status.d.ts +10 -0
  16. package/dist/src/transaction/status.js +48 -0
  17. package/dist/src/utils/index.d.ts +1 -1
  18. package/dist/src/utils/index.js +1 -1
  19. package/dist/src/utils/subscription.d.ts +24 -0
  20. package/dist/src/utils/subscription.js +52 -0
  21. package/package.json +4 -4
  22. package/src/api/api-alephium.ts +108 -1
  23. package/src/api/api-explorer.ts +50 -6
  24. package/src/contract/events.ts +21 -48
  25. package/src/index.ts +1 -0
  26. package/src/transaction/index.ts +20 -0
  27. package/src/{utils/transaction.test.ts → transaction/sign-verify.test.ts} +1 -1
  28. package/src/{utils/transaction.ts → transaction/sign-verify.ts} +1 -1
  29. package/src/transaction/status.ts +58 -0
  30. package/src/utils/index.ts +1 -1
  31. package/src/utils/subscription.ts +72 -0
  32. package/test/events.test.ts +13 -14
  33. package/test/transaction.test.ts +72 -0
@@ -23,6 +23,8 @@ export interface AddressInfo {
23
23
 
24
24
  /** @format uint256 */
25
25
  lockedBalance: string
26
+
27
+ /** @format int32 */
26
28
  txNumber: number
27
29
  }
28
30
 
@@ -31,13 +33,22 @@ export interface BadRequest {
31
33
  }
32
34
 
33
35
  export interface BlockEntryLite {
36
+ /** @format block-hash */
34
37
  hash: string
35
38
 
36
39
  /** @format int64 */
37
40
  timestamp: number
41
+
42
+ /** @format int32 */
38
43
  chainFrom: number
44
+
45
+ /** @format int32 */
39
46
  chainTo: number
47
+
48
+ /** @format int32 */
40
49
  height: number
50
+
51
+ /** @format int32 */
41
52
  txNumber: number
42
53
  mainChain: boolean
43
54
 
@@ -46,13 +57,18 @@ export interface BlockEntryLite {
46
57
  }
47
58
 
48
59
  export interface ConfirmedTransaction {
60
+ /** @format 32-byte-hash */
49
61
  hash: string
62
+
63
+ /** @format block-hash */
50
64
  blockHash: string
51
65
 
52
66
  /** @format int64 */
53
67
  timestamp: number
54
68
  inputs?: Input[]
55
69
  outputs?: Output[]
70
+
71
+ /** @format int32 */
56
72
  gasAmount: number
57
73
 
58
74
  /** @format uint256 */
@@ -68,13 +84,15 @@ export interface ExplorerInfo {
68
84
  export interface Hashrate {
69
85
  /** @format int64 */
70
86
  timestamp: number
71
- hashrate: string
72
- value: string
87
+ hashrate: number
88
+ value: number
73
89
  }
74
90
 
75
91
  export interface Input {
76
92
  outputRef: OutputRef
77
93
  unlockScript?: string
94
+
95
+ /** @format 32-byte-hash */
78
96
  txHashRef: string
79
97
  address: string
80
98
 
@@ -87,6 +105,7 @@ export interface InternalServerError {
87
105
  }
88
106
 
89
107
  export interface ListBlocks {
108
+ /** @format int32 */
90
109
  total: number
91
110
  blocks?: BlockEntryLite[]
92
111
  }
@@ -97,6 +116,7 @@ export interface NotFound {
97
116
  }
98
117
 
99
118
  export interface Output {
119
+ /** @format int32 */
100
120
  hint: number
101
121
 
102
122
  /** @format 32-byte-hash */
@@ -108,10 +128,13 @@ export interface Output {
108
128
 
109
129
  /** @format int64 */
110
130
  lockTime?: number
131
+
132
+ /** @format 32-byte-hash */
111
133
  spent?: string
112
134
  }
113
135
 
114
136
  export interface OutputRef {
137
+ /** @format int32 */
115
138
  hint: number
116
139
 
117
140
  /** @format 32-byte-hash */
@@ -119,7 +142,10 @@ export interface OutputRef {
119
142
  }
120
143
 
121
144
  export interface PerChainCount {
145
+ /** @format int32 */
122
146
  chainFrom: number
147
+
148
+ /** @format int32 */
123
149
  chainTo: number
124
150
 
125
151
  /** @format int64 */
@@ -127,7 +153,10 @@ export interface PerChainCount {
127
153
  }
128
154
 
129
155
  export interface PerChainDuration {
156
+ /** @format int32 */
130
157
  chainFrom: number
158
+
159
+ /** @format int32 */
131
160
  chainTo: number
132
161
 
133
162
  /** @format int64 */
@@ -138,7 +167,10 @@ export interface PerChainDuration {
138
167
  }
139
168
 
140
169
  export interface PerChainHeight {
170
+ /** @format int32 */
141
171
  chainFrom: number
172
+
173
+ /** @format int32 */
142
174
  chainTo: number
143
175
 
144
176
  /** @format int64 */
@@ -187,13 +219,18 @@ export interface TokenSupply {
187
219
  }
188
220
 
189
221
  export interface Transaction {
222
+ /** @format 32-byte-hash */
190
223
  hash: string
224
+
225
+ /** @format block-hash */
191
226
  blockHash: string
192
227
 
193
228
  /** @format int64 */
194
229
  timestamp: number
195
230
  inputs?: Input[]
196
231
  outputs?: Output[]
232
+
233
+ /** @format int32 */
197
234
  gasAmount: number
198
235
 
199
236
  /** @format uint256 */
@@ -221,11 +258,18 @@ export interface Unauthorized {
221
258
  }
222
259
 
223
260
  export interface UnconfirmedTransaction {
261
+ /** @format 32-byte-hash */
224
262
  hash: string
263
+
264
+ /** @format int32 */
225
265
  chainFrom: number
266
+
267
+ /** @format int32 */
226
268
  chainTo: number
227
269
  inputs?: UInput[]
228
270
  outputs?: UOutput[]
271
+
272
+ /** @format int32 */
229
273
  gasAmount: number
230
274
 
231
275
  /** @format uint256 */
@@ -647,7 +691,7 @@ export class Api<SecurityDataType extends unknown> extends HttpClient<SecurityDa
647
691
  * @request GET:/infos/supply/total-alph
648
692
  */
649
693
  getInfosSupplyTotalAlph: (params: RequestParams = {}) =>
650
- this.request<string, BadRequest | Unauthorized | NotFound | InternalServerError | ServiceUnavailable>({
694
+ this.request<number, BadRequest | Unauthorized | NotFound | InternalServerError | ServiceUnavailable>({
651
695
  path: `/infos/supply/total-alph`,
652
696
  method: 'GET',
653
697
  ...params
@@ -661,7 +705,7 @@ export class Api<SecurityDataType extends unknown> extends HttpClient<SecurityDa
661
705
  * @request GET:/infos/supply/circulating-alph
662
706
  */
663
707
  getInfosSupplyCirculatingAlph: (params: RequestParams = {}) =>
664
- this.request<string, BadRequest | Unauthorized | NotFound | InternalServerError | ServiceUnavailable>({
708
+ this.request<number, BadRequest | Unauthorized | NotFound | InternalServerError | ServiceUnavailable>({
665
709
  path: `/infos/supply/circulating-alph`,
666
710
  method: 'GET',
667
711
  ...params
@@ -675,7 +719,7 @@ export class Api<SecurityDataType extends unknown> extends HttpClient<SecurityDa
675
719
  * @request GET:/infos/supply/reserved-alph
676
720
  */
677
721
  getInfosSupplyReservedAlph: (params: RequestParams = {}) =>
678
- this.request<string, BadRequest | Unauthorized | NotFound | InternalServerError | ServiceUnavailable>({
722
+ this.request<number, BadRequest | Unauthorized | NotFound | InternalServerError | ServiceUnavailable>({
679
723
  path: `/infos/supply/reserved-alph`,
680
724
  method: 'GET',
681
725
  ...params
@@ -689,7 +733,7 @@ export class Api<SecurityDataType extends unknown> extends HttpClient<SecurityDa
689
733
  * @request GET:/infos/supply/locked-alph
690
734
  */
691
735
  getInfosSupplyLockedAlph: (params: RequestParams = {}) =>
692
- this.request<string, BadRequest | Unauthorized | NotFound | InternalServerError | ServiceUnavailable>({
736
+ this.request<number, BadRequest | Unauthorized | NotFound | InternalServerError | ServiceUnavailable>({
693
737
  path: `/infos/supply/locked-alph`,
694
738
  method: 'GET',
695
739
  ...params
@@ -16,64 +16,33 @@ You should have received a copy of the GNU Lesser General Public License
16
16
  along with the library. If not, see <http://www.gnu.org/licenses/>.
17
17
  */
18
18
 
19
- import EventEmitter from 'eventemitter3'
20
- import { node } from '../api'
21
- import { NodeProvider } from '../api'
19
+ import { ContractEvent } from '../api/api-alephium'
20
+ import { Subscription, SubscribeOptions } from '../utils'
22
21
 
23
- type EventCallback = (event: node.ContractEvent) => Promise<void>
24
- type ErrorCallback = (error: any, subscription: Subscription) => Promise<void>
25
-
26
- export interface SubscribeOptions {
27
- provider: NodeProvider
28
- contractAddress: string
29
- fromCount?: number
30
- pollingInterval: number
31
- eventCallback: EventCallback
32
- errorCallback: ErrorCallback
33
- }
34
-
35
- export class Subscription {
36
- provider: NodeProvider
22
+ export class EventSubscription extends Subscription<ContractEvent> {
37
23
  readonly contractAddress: string
38
- pollingInterval: number
39
-
40
24
  private fromCount: number
41
- private eventCallback: EventCallback
42
- private errorCallback: ErrorCallback
43
- private task: ReturnType<typeof setTimeout> | undefined
44
- private cancelled: boolean
45
- private eventEmitter: EventEmitter
46
25
 
47
- constructor(options: SubscribeOptions) {
48
- this.provider = options.provider
49
- this.contractAddress = options.contractAddress
50
- this.fromCount = typeof options.fromCount === 'undefined' ? 0 : options.fromCount
51
- this.pollingInterval = options.pollingInterval
52
- this.eventCallback = options.eventCallback
53
- this.errorCallback = options.errorCallback
54
- this.task = undefined
55
- this.cancelled = false
26
+ constructor(options: SubscribeOptions<ContractEvent>, contractAddress: string, fromCount?: number) {
27
+ super(options)
28
+ this.contractAddress = contractAddress
29
+ this.fromCount = typeof fromCount === 'undefined' ? 0 : fromCount
56
30
 
57
- this.eventEmitter = new EventEmitter()
31
+ this.startPolling()
32
+ }
33
+
34
+ override startPolling() {
58
35
  this.eventEmitter.on('tick', async () => {
59
- await this.fetchEvents()
36
+ await this.polling()
60
37
  })
61
38
  this.eventEmitter.emit('tick')
62
39
  }
63
40
 
64
- unsubscribe(): void {
65
- this.eventEmitter.removeAllListeners()
66
- this.cancelled = true
67
- if (typeof this.task !== 'undefined') {
68
- clearTimeout(this.task)
69
- }
70
- }
71
-
72
41
  currentEventCount(): number {
73
42
  return this.fromCount
74
43
  }
75
44
 
76
- private async fetchEvents() {
45
+ override async polling() {
77
46
  try {
78
47
  const events = await this.provider.events.getEventsContractContractaddress(this.contractAddress, {
79
48
  start: this.fromCount
@@ -87,16 +56,20 @@ export class Subscription {
87
56
  return
88
57
  }
89
58
 
90
- const promises = events.events.map((event) => this.eventCallback(event))
59
+ const promises = events.events.map((event) => this.messageCallback(event))
91
60
  await Promise.all(promises)
92
61
  this.fromCount = events.nextStart
93
- await this.fetchEvents()
62
+ await this.polling()
94
63
  } catch (err) {
95
64
  await this.errorCallback(err, this)
96
65
  }
97
66
  }
98
67
  }
99
68
 
100
- export function subscribe(options: SubscribeOptions): Subscription {
101
- return new Subscription(options)
69
+ export function subscribeToEvents(
70
+ options: SubscribeOptions<ContractEvent>,
71
+ contractAddress: string,
72
+ fromCount?: number
73
+ ): EventSubscription {
74
+ return new EventSubscription(options, contractAddress, fromCount)
102
75
  }
package/src/index.ts CHANGED
@@ -20,5 +20,6 @@ export * from './api'
20
20
  export * from './contract'
21
21
  export * from './signer'
22
22
  export * from './utils'
23
+ export * from './transaction'
23
24
 
24
25
  export * from './constants'
@@ -0,0 +1,20 @@
1
+ /*
2
+ Copyright 2018 - 2022 The Alephium Authors
3
+ This file is part of the alephium project.
4
+
5
+ The library is free software: you can redistribute it and/or modify
6
+ it under the terms of the GNU Lesser General Public License as published by
7
+ the Free Software Foundation, either version 3 of the License, or
8
+ (at your option) any later version.
9
+
10
+ The library is distributed in the hope that it will be useful,
11
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
12
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13
+ GNU Lesser General Public License for more details.
14
+
15
+ You should have received a copy of the GNU Lesser General Public License
16
+ along with the library. If not, see <http://www.gnu.org/licenses/>.
17
+ */
18
+
19
+ export * from './sign-verify'
20
+ export * from './status'
@@ -19,7 +19,7 @@ along with the library. If not, see <http://www.gnu.org/licenses/>.
19
19
  import EC from 'elliptic'
20
20
  import assert from 'assert'
21
21
 
22
- import { transactionSign, transactionVerifySignature } from './transaction'
22
+ import { transactionSign, transactionVerifySignature } from './sign-verify'
23
23
 
24
24
  describe('transaction', function () {
25
25
  it('should verify signature', () => {
@@ -18,7 +18,7 @@ along with the library. If not, see <http://www.gnu.org/licenses/>.
18
18
 
19
19
  import { ec as EC } from 'elliptic'
20
20
 
21
- import * as utils from './utils'
21
+ import * as utils from '../utils'
22
22
 
23
23
  const ec = new EC('secp256k1')
24
24
 
@@ -0,0 +1,58 @@
1
+ /*
2
+ Copyright 2018 - 2022 The Alephium Authors
3
+ This file is part of the alephium project.
4
+
5
+ The library is free software: you can redistribute it and/or modify
6
+ it under the terms of the GNU Lesser General Public License as published by
7
+ the Free Software Foundation, either version 3 of the License, or
8
+ (at your option) any later version.
9
+
10
+ The library is distributed in the hope that it will be useful,
11
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
12
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13
+ GNU Lesser General Public License for more details.
14
+
15
+ You should have received a copy of the GNU Lesser General Public License
16
+ along with the library. If not, see <http://www.gnu.org/licenses/>.
17
+ */
18
+
19
+ import { TxStatus } from '../api/api-alephium'
20
+ import { Subscription, SubscribeOptions } from '../utils'
21
+
22
+ export class TxStatusSubscription extends Subscription<TxStatus> {
23
+ readonly txId: string
24
+ readonly fromGroup?: number
25
+ readonly toGroup?: number
26
+
27
+ constructor(options: SubscribeOptions<TxStatus>, txId: string, fromGroup?: number, toGroup?: number) {
28
+ super(options)
29
+ this.txId = txId
30
+ this.fromGroup = fromGroup
31
+ this.toGroup = toGroup
32
+
33
+ this.startPolling()
34
+ }
35
+
36
+ override async polling() {
37
+ try {
38
+ const txStatus = await this.provider.transactions.getTransactionsStatus({
39
+ txId: this.txId,
40
+ fromGroup: this.fromGroup,
41
+ toGroup: this.toGroup
42
+ })
43
+
44
+ await this.messageCallback(txStatus)
45
+ } catch (err) {
46
+ await this.errorCallback(err, this)
47
+ }
48
+ }
49
+ }
50
+
51
+ export function subscribeToTxStatus(
52
+ options: SubscribeOptions<TxStatus>,
53
+ txId: string,
54
+ fromGroup?: number,
55
+ toGroup?: number
56
+ ): TxStatusSubscription {
57
+ return new TxStatusSubscription(options, txId, fromGroup, toGroup)
58
+ }
@@ -20,5 +20,5 @@ export * from './address'
20
20
  export * from './bs58'
21
21
  export * from './djb2'
22
22
  export * from './password-crypto'
23
- export * from './transaction'
24
23
  export * from './utils'
24
+ export * from './subscription'
@@ -0,0 +1,72 @@
1
+ /*
2
+ Copyright 2018 - 2022 The Alephium Authors
3
+ This file is part of the alephium project.
4
+
5
+ The library is free software: you can redistribute it and/or modify
6
+ it under the terms of the GNU Lesser General Public License as published by
7
+ the Free Software Foundation, either version 3 of the License, or
8
+ (at your option) any later version.
9
+
10
+ The library is distributed in the hope that it will be useful,
11
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
12
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13
+ GNU Lesser General Public License for more details.
14
+
15
+ You should have received a copy of the GNU Lesser General Public License
16
+ along with the library. If not, see <http://www.gnu.org/licenses/>.
17
+ */
18
+
19
+ import EventEmitter from 'eventemitter3'
20
+ import { NodeProvider } from '../api'
21
+
22
+ type MessageCallback<Message> = (message: Message) => Promise<void>
23
+ type ErrorCallback<Message> = (error: any, subscription: Subscription<Message>) => Promise<void>
24
+
25
+ export interface SubscribeOptions<Message> {
26
+ provider: NodeProvider
27
+ pollingInterval: number
28
+ messageCallback: MessageCallback<Message>
29
+ errorCallback: ErrorCallback<Message>
30
+ }
31
+
32
+ export abstract class Subscription<Message> {
33
+ provider: NodeProvider
34
+ pollingInterval: number
35
+
36
+ protected messageCallback: MessageCallback<Message>
37
+ protected errorCallback: ErrorCallback<Message>
38
+ protected task: ReturnType<typeof setTimeout> | undefined
39
+ protected eventEmitter: EventEmitter
40
+ protected cancelled: boolean
41
+
42
+ constructor(options: SubscribeOptions<Message>) {
43
+ this.provider = options.provider
44
+ this.pollingInterval = options.pollingInterval
45
+ this.messageCallback = options.messageCallback
46
+ this.errorCallback = options.errorCallback
47
+ this.task = undefined
48
+ this.cancelled = false
49
+ this.eventEmitter = new EventEmitter()
50
+ }
51
+
52
+ startPolling() {
53
+ this.eventEmitter.on('tick', async () => {
54
+ await this.polling()
55
+
56
+ if (!this.cancelled) {
57
+ this.task = setTimeout(() => this.eventEmitter.emit('tick'), this.pollingInterval)
58
+ }
59
+ })
60
+ this.eventEmitter.emit('tick')
61
+ }
62
+
63
+ unsubscribe(): void {
64
+ this.eventEmitter.removeAllListeners()
65
+ this.cancelled = true
66
+ if (typeof this.task !== 'undefined') {
67
+ clearTimeout(this.task)
68
+ }
69
+ }
70
+
71
+ abstract polling(): Promise<void>
72
+ }
@@ -17,11 +17,12 @@ along with the library. If not, see <http://www.gnu.org/licenses/>.
17
17
  */
18
18
 
19
19
  import { NodeProvider } from '../src/api'
20
- import { subscribe, Subscription } from '../src/contract/events'
20
+ import { subscribeToEvents } from '../src/contract/events'
21
21
  import { Contract, Script } from '../src/contract'
22
22
  import { NodeWallet, SignExecuteScriptTxParams } from '../src/signer'
23
- import { node } from '../src/api'
23
+ import { ContractEvent } from '../src/api/api-alephium'
24
24
  import { testWallet } from '../src/test'
25
+ import { SubscribeOptions } from '../src/utils'
25
26
 
26
27
  describe('events', function () {
27
28
  async function deployContract(provider: NodeProvider, signer: NodeWallet): Promise<[string, string]> {
@@ -56,22 +57,21 @@ describe('events', function () {
56
57
  const signer = await testWallet(provider)
57
58
 
58
59
  const [contractAddress, contractId] = await deployContract(provider, signer)
59
- const events: Array<node.ContractEvent> = []
60
- const subscriptOptions = {
60
+ const events: Array<ContractEvent> = []
61
+ const subscriptOptions: SubscribeOptions<ContractEvent> = {
61
62
  provider: provider,
62
- contractAddress: contractAddress,
63
63
  pollingInterval: 500,
64
- eventCallback: (event: node.ContractEvent): Promise<void> => {
64
+ messageCallback: (event: ContractEvent): Promise<void> => {
65
65
  events.push(event)
66
66
  return Promise.resolve()
67
67
  },
68
- errorCallback: (error: any, subscription: Subscription): Promise<void> => {
68
+ errorCallback: (error: any, subscription): Promise<void> => {
69
69
  console.log(error)
70
70
  subscription.unsubscribe()
71
71
  return Promise.resolve()
72
72
  }
73
73
  }
74
- const subscription = subscribe(subscriptOptions)
74
+ const subscription = subscribeToEvents(subscriptOptions, contractAddress)
75
75
  const script = await Script.fromSource(provider, 'main.ral')
76
76
  const scriptTxParams = await script.paramsForDeployment({
77
77
  initialFields: { addContractId: contractId },
@@ -90,29 +90,28 @@ describe('events', function () {
90
90
  expect(subscription.currentEventCount()).toEqual(events.length)
91
91
 
92
92
  subscription.unsubscribe()
93
- })
93
+ }, 15000)
94
94
 
95
95
  it('should cancel event subscription', async () => {
96
96
  const provider = new NodeProvider('http://127.0.0.1:22973')
97
97
  const signer = await testWallet(provider)
98
98
 
99
99
  const [contractAddress, contractId] = await deployContract(provider, signer)
100
- const events: Array<node.ContractEvent> = []
100
+ const events: Array<ContractEvent> = []
101
101
  const subscriptOptions = {
102
102
  provider: provider,
103
- contractAddress: contractAddress,
104
103
  pollingInterval: 500,
105
- eventCallback: (event: node.ContractEvent): Promise<void> => {
104
+ messageCallback: (event: ContractEvent): Promise<void> => {
106
105
  events.push(event)
107
106
  return Promise.resolve()
108
107
  },
109
- errorCallback: (error: any, subscription: Subscription): Promise<void> => {
108
+ errorCallback: (error: any, subscription): Promise<void> => {
110
109
  console.log(error)
111
110
  subscription.unsubscribe()
112
111
  return Promise.resolve()
113
112
  }
114
113
  }
115
- const subscription = subscribe(subscriptOptions)
114
+ const subscription = subscribeToEvents(subscriptOptions, contractAddress)
116
115
  const script = await Script.fromSource(provider, 'main.ral')
117
116
  const scriptTx0 = await script.transactionForDeployment(signer, {
118
117
  initialFields: { addContractId: contractId }
@@ -0,0 +1,72 @@
1
+ /*
2
+ Copyright 2018 - 2022 The Alephium Authors
3
+ This file is part of the alephium project.
4
+
5
+ The library is free software: you can redistribute it and/or modify
6
+ it under the terms of the GNU Lesser General Public License as published by
7
+ the Free Software Foundation, either version 3 of the License, or
8
+ (at your option) any later version.
9
+
10
+ The library is distributed in the hope that it will be useful,
11
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
12
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13
+ GNU Lesser General Public License for more details.
14
+
15
+ You should have received a copy of the GNU Lesser General Public License
16
+ along with the library. If not, see <http://www.gnu.org/licenses/>.
17
+ */
18
+
19
+ import { NodeProvider } from '../src/api'
20
+ import { subscribeToTxStatus } from '../src/transaction/status'
21
+ import { Contract } from '../src/contract'
22
+ import { TxStatus } from '../src/api/api-alephium'
23
+ import { testWallet } from '../src/test'
24
+ import { SubscribeOptions } from '../src/utils'
25
+
26
+ describe('transactions', function () {
27
+ it('should subscribe transaction status', async () => {
28
+ const provider = new NodeProvider('http://127.0.0.1:22973')
29
+ const sub = await Contract.fromSource(provider, 'sub.ral')
30
+ const signer = await testWallet(provider)
31
+ const subDeployTx = await sub.transactionForDeployment(signer, {
32
+ initialFields: { result: 0 },
33
+ initialTokenAmounts: []
34
+ })
35
+
36
+ let txStatus: TxStatus | undefined = undefined
37
+ let counter = 0
38
+ const subscriptOptions: SubscribeOptions<TxStatus> = {
39
+ provider: provider,
40
+ pollingInterval: 500,
41
+ messageCallback: (status: TxStatus): Promise<void> => {
42
+ txStatus = status
43
+ counter = counter + 1
44
+ return Promise.resolve()
45
+ },
46
+ errorCallback: (error: any, subscription): Promise<void> => {
47
+ console.log(error)
48
+ subscription.unsubscribe()
49
+ return Promise.resolve()
50
+ }
51
+ }
52
+
53
+ const counterBeforeSubscribe = counter
54
+
55
+ const subscription = subscribeToTxStatus(subscriptOptions, subDeployTx.txId)
56
+ await new Promise((resolve) => setTimeout(resolve, 1500))
57
+ expect(txStatus).toMatchObject({ type: 'TxNotFound' })
58
+
59
+ const subSubmitResult = await signer.submitTransaction(subDeployTx.unsignedTx, subDeployTx.txId)
60
+ await new Promise((resolve) => setTimeout(resolve, 1500))
61
+ expect(txStatus).toMatchObject({ type: 'Confirmed' })
62
+
63
+ expect(counterBeforeSubscribe).toBeLessThan(counter)
64
+
65
+ subscription.unsubscribe()
66
+
67
+ const counterAfterUnsubscribe = counter
68
+ await new Promise((resolve) => setTimeout(resolve, 1500))
69
+ expect(txStatus).toMatchObject({ type: 'Confirmed' })
70
+ expect(counterAfterUnsubscribe).toEqual(counter)
71
+ })
72
+ })