@alephium/web3 0.1.0-rc.3 → 0.2.0-test.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 (49) hide show
  1. package/contracts/{add.ral → add/add.ral} +1 -1
  2. package/contracts/{greeter.ral → greeter/greeter.ral} +0 -0
  3. package/contracts/{greeter_interface.ral → greeter/greeter_interface.ral} +0 -0
  4. package/contracts/greeter_main.ral +1 -1
  5. package/contracts/main.ral +1 -1
  6. package/contracts/{sub.ral → sub/sub.ral} +0 -0
  7. package/dev/user.conf +1 -0
  8. package/dist/alephium-web3.min.js +1 -1
  9. package/dist/alephium-web3.min.js.map +1 -1
  10. package/dist/src/api/api-alephium.d.ts +75 -4
  11. package/dist/src/api/api-alephium.js +1 -1
  12. package/dist/src/api/api-explorer.d.ts +33 -6
  13. package/dist/src/contract/contract.d.ts +18 -10
  14. package/dist/src/contract/contract.js +95 -56
  15. package/dist/src/contract/events.d.ts +7 -25
  16. package/dist/src/contract/events.js +18 -31
  17. package/dist/src/index.d.ts +1 -0
  18. package/dist/src/index.js +1 -0
  19. package/dist/src/transaction/index.d.ts +2 -0
  20. package/dist/src/transaction/index.js +31 -0
  21. package/dist/src/{utils/transaction.d.ts → transaction/sign-verify.d.ts} +0 -0
  22. package/dist/src/{utils/transaction.js → transaction/sign-verify.js} +1 -1
  23. package/dist/src/transaction/status.d.ts +10 -0
  24. package/dist/src/transaction/status.js +48 -0
  25. package/dist/src/utils/index.d.ts +1 -1
  26. package/dist/src/utils/index.js +1 -1
  27. package/dist/src/utils/subscription.d.ts +24 -0
  28. package/dist/src/utils/subscription.js +52 -0
  29. package/dist/src/utils/utils.d.ts +2 -0
  30. package/dist/src/utils/utils.js +9 -1
  31. package/package.json +3 -3
  32. package/src/api/api-alephium.ts +111 -4
  33. package/src/api/api-explorer.ts +50 -6
  34. package/src/contract/contract.ts +112 -66
  35. package/src/contract/events.ts +21 -48
  36. package/src/index.ts +1 -0
  37. package/src/transaction/index.ts +20 -0
  38. package/src/{utils/transaction.test.ts → transaction/sign-verify.test.ts} +1 -1
  39. package/src/{utils/transaction.ts → transaction/sign-verify.ts} +1 -1
  40. package/src/transaction/status.ts +58 -0
  41. package/src/utils/index.ts +1 -1
  42. package/src/utils/subscription.ts +72 -0
  43. package/src/utils/utils.test.ts +2 -1
  44. package/src/utils/utils.ts +8 -0
  45. package/templates/base/package.json +1 -1
  46. package/templates/react/package.json +1 -1
  47. package/test/contract.test.ts +24 -7
  48. package/test/events.test.ts +18 -19
  49. package/test/transaction.test.ts +72 -0
@@ -22,13 +22,14 @@ import * as path from 'path'
22
22
  import { NodeProvider } from '../src/api'
23
23
  import { Contract, Script, TestContractParams } from '../src/contract'
24
24
  import { testWallet } from '../src/test'
25
+ import { addressFromContractId } from '../src/utils'
25
26
 
26
27
  describe('contract', function () {
27
28
  async function testSuite1() {
28
29
  const provider = new NodeProvider('http://127.0.0.1:22973')
29
30
 
30
- const add = await Contract.fromSource(provider, 'add.ral')
31
- const sub = await Contract.fromSource(provider, 'sub.ral')
31
+ const add = await Contract.fromSource(provider, 'add/add.ral')
32
+ const sub = await Contract.fromSource(provider, 'sub/sub.ral')
32
33
 
33
34
  const subState = sub.toState({ result: 0 }, { alphAmount: BigInt('1000000000000000000') })
34
35
  const testParams: TestContractParams = {
@@ -61,6 +62,7 @@ describe('contract', function () {
61
62
  initialTokenAmounts: []
62
63
  })
63
64
  const subContractId = subDeployTx.contractId
65
+ const subContractAddress = addressFromContractId(subContractId)
64
66
  expect(subDeployTx.fromGroup).toEqual(0)
65
67
  expect(subDeployTx.toGroup).toEqual(0)
66
68
  const subSubmitResult = await signer.submitTransaction(subDeployTx.unsignedTx, subDeployTx.txId)
@@ -80,8 +82,16 @@ describe('contract', function () {
80
82
  expect(addSubmitResult.txId).toEqual(addDeployTx.txId)
81
83
 
82
84
  const addContractId = addDeployTx.contractId
83
- const main = await Script.fromSource(provider, 'main.ral')
85
+ const addContractAddress = addressFromContractId(addContractId)
86
+
87
+ // Check state for add/sub before main script is executed
88
+ let fetchedSubState = await sub.fetchState(provider, subContractAddress, 0)
89
+ expect(fetchedSubState.fields.result).toEqual(0)
90
+ let fetchedAddState = await add.fetchState(provider, addContractAddress, 0)
91
+ expect(fetchedAddState.fields.subContractId).toEqual(subContractId)
92
+ expect(fetchedAddState.fields.result).toEqual(0)
84
93
 
94
+ const main = await Script.fromSource(provider, 'main.ral')
85
95
  const mainScriptTx = await main.transactionForDeployment(signer, {
86
96
  initialFields: { addContractId: addContractId }
87
97
  })
@@ -90,12 +100,19 @@ describe('contract', function () {
90
100
  const mainSubmitResult = await signer.submitTransaction(mainScriptTx.unsignedTx, mainScriptTx.txId)
91
101
  expect(mainSubmitResult.fromGroup).toEqual(0)
92
102
  expect(mainSubmitResult.toGroup).toEqual(0)
103
+
104
+ // Check state for add/sub after main script is executed
105
+ fetchedSubState = await sub.fetchState(provider, subContractAddress, 0)
106
+ expect(fetchedSubState.fields.result).toEqual(1)
107
+ fetchedAddState = await add.fetchState(provider, addContractAddress, 0)
108
+ expect(fetchedAddState.fields.subContractId).toEqual(subContractId)
109
+ expect(fetchedAddState.fields.result).toEqual(3)
93
110
  }
94
111
 
95
112
  async function testSuite2() {
96
113
  const provider = new NodeProvider('http://127.0.0.1:22973')
97
114
 
98
- const greeter = await Contract.fromSource(provider, 'greeter.ral')
115
+ const greeter = await Contract.fromSource(provider, 'greeter/greeter.ral')
99
116
 
100
117
  const testParams: TestContractParams = {
101
118
  initialFields: { btcPrice: 1 }
@@ -151,11 +168,11 @@ describe('contract', function () {
151
168
  }
152
169
 
153
170
  it('should load contract from json', async () => {
154
- loadContract('./artifacts/add.ral.json')
155
- loadContract('./artifacts/sub.ral.json')
171
+ loadContract('./artifacts/add/add.ral.json')
172
+ loadContract('./artifacts/sub/sub.ral.json')
156
173
  loadScript('./artifacts/main.ral.json')
157
174
 
158
- loadContract('./artifacts/greeter.ral.json')
175
+ loadContract('./artifacts/greeter/greeter.ral.json')
159
176
  loadScript('./artifacts/greeter_main.ral.json')
160
177
  })
161
178
  })
@@ -17,15 +17,16 @@ 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, timeout } from '../src/utils'
25
26
 
26
27
  describe('events', function () {
27
28
  async function deployContract(provider: NodeProvider, signer: NodeWallet): Promise<[string, string]> {
28
- const sub = await Contract.fromSource(provider, 'sub.ral')
29
+ const sub = await Contract.fromSource(provider, 'sub/sub.ral')
29
30
  const subDeployTx = await sub.transactionForDeployment(signer, {
30
31
  initialFields: { result: 0 },
31
32
  initialTokenAmounts: []
@@ -34,7 +35,7 @@ describe('events', function () {
34
35
  const subSubmitResult = await signer.submitTransaction(subDeployTx.unsignedTx, subDeployTx.txId)
35
36
  expect(subSubmitResult.txId).toEqual(subDeployTx.txId)
36
37
 
37
- const add = await Contract.fromSource(provider, 'add.ral')
38
+ const add = await Contract.fromSource(provider, 'add/add.ral')
38
39
  const addDeployTx = await add.transactionForDeployment(signer, {
39
40
  initialFields: { subContractId: subContractId, result: 0 },
40
41
  initialTokenAmounts: []
@@ -56,29 +57,28 @@ 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 },
78
78
  signerAddress: (await signer.getAccounts())[0].address
79
79
  })
80
80
  await executeScript(scriptTxParams, signer, 3)
81
- await new Promise((resolve) => setTimeout(resolve, 3000))
81
+ await timeout(3000)
82
82
 
83
83
  expect(events.length).toEqual(3)
84
84
  events.forEach((event) => {
@@ -90,35 +90,34 @@ 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 }
119
118
  })
120
119
  await signer.submitTransaction(scriptTx0.unsignedTx, scriptTx0.txId)
121
- await new Promise((resolve) => setTimeout(resolve, 1500))
120
+ await timeout(1500)
122
121
  subscription.unsubscribe()
123
122
 
124
123
  expect(events.length).toEqual(1)
@@ -133,7 +132,7 @@ describe('events', function () {
133
132
  initialFields: { addContractId: contractId }
134
133
  })
135
134
  await signer.submitTransaction(scriptTx1.unsignedTx, scriptTx1.txId)
136
- await new Promise((resolve) => setTimeout(resolve, 1500))
135
+ await timeout(1500)
137
136
  expect(events.length).toEqual(1)
138
137
  })
139
138
  })
@@ -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, timeout } 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 timeout(1500)
57
+ expect(txStatus).toMatchObject({ type: 'TxNotFound' })
58
+
59
+ const subSubmitResult = await signer.submitTransaction(subDeployTx.unsignedTx, subDeployTx.txId)
60
+ await timeout(1500)
61
+ expect(txStatus).toMatchObject({ type: 'Confirmed' })
62
+
63
+ expect(counterBeforeSubscribe).toBeLessThan(counter)
64
+
65
+ subscription.unsubscribe()
66
+
67
+ const counterAfterUnsubscribe = counter
68
+ await timeout(1500)
69
+ expect(txStatus).toMatchObject({ type: 'Confirmed' })
70
+ expect(counterAfterUnsubscribe).toEqual(counter)
71
+ }, 10000)
72
+ })