@alephium/web3 0.2.0-rc.3 → 0.2.0-rc.30

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 (125) hide show
  1. package/.eslintignore +2 -2
  2. package/README.md +2 -135
  3. package/dist/alephium-web3.min.js +1 -1
  4. package/dist/alephium-web3.min.js.LICENSE.txt +0 -17
  5. package/dist/alephium-web3.min.js.map +1 -1
  6. package/dist/src/api/api-alephium.d.ts +117 -17
  7. package/dist/src/api/api-alephium.js +145 -79
  8. package/dist/src/api/api-explorer.d.ts +163 -48
  9. package/dist/src/api/api-explorer.js +157 -34
  10. package/dist/src/api/index.d.ts +3 -2
  11. package/dist/src/api/index.js +22 -3
  12. package/dist/src/api/types.d.ts +23 -0
  13. package/dist/src/api/types.js +240 -0
  14. package/dist/src/api/utils.d.ts +6 -0
  15. package/dist/{scripts/rename-gitignore.js → src/api/utils.js} +11 -6
  16. package/dist/src/contract/contract.d.ts +107 -68
  17. package/dist/src/contract/contract.js +395 -451
  18. package/dist/src/contract/events.d.ts +4 -4
  19. package/dist/src/contract/events.js +2 -1
  20. package/dist/src/contract/index.js +5 -1
  21. package/dist/src/contract/ralph.d.ts +5 -4
  22. package/dist/src/contract/ralph.js +27 -1
  23. package/dist/src/global.d.ts +4 -0
  24. package/dist/{scripts/stop-devnet.js → src/global.js} +17 -11
  25. package/dist/src/index.d.ts +2 -0
  26. package/dist/src/index.js +23 -1
  27. package/dist/src/signer/index.d.ts +0 -1
  28. package/dist/src/signer/index.js +5 -2
  29. package/dist/src/signer/signer.d.ts +25 -13
  30. package/dist/src/signer/signer.js +51 -16
  31. package/dist/src/transaction/index.d.ts +0 -1
  32. package/dist/src/transaction/index.js +5 -2
  33. package/dist/src/transaction/status.d.ts +2 -1
  34. package/dist/src/transaction/status.js +2 -1
  35. package/dist/src/utils/bs58.d.ts +1 -0
  36. package/dist/src/utils/bs58.js +13 -1
  37. package/dist/src/utils/index.d.ts +0 -1
  38. package/dist/src/utils/index.js +5 -2
  39. package/dist/src/utils/subscription.d.ts +0 -3
  40. package/dist/src/utils/subscription.js +0 -1
  41. package/dist/src/utils/utils.d.ts +4 -9
  42. package/dist/src/utils/utils.js +20 -24
  43. package/jest-config.json +11 -0
  44. package/package.json +11 -47
  45. package/src/api/api-alephium.ts +169 -25
  46. package/src/api/api-explorer.ts +234 -51
  47. package/src/api/index.ts +14 -3
  48. package/src/api/types.ts +233 -0
  49. package/{scripts/rename-gitignore.js → src/api/utils.ts} +7 -6
  50. package/src/contract/contract.ts +579 -545
  51. package/src/contract/events.ts +6 -5
  52. package/src/contract/ralph.ts +29 -4
  53. package/src/{transaction/sign-verify.ts → global.ts} +14 -15
  54. package/src/index.ts +7 -0
  55. package/src/signer/index.ts +0 -1
  56. package/src/signer/signer.ts +79 -27
  57. package/src/transaction/index.ts +0 -1
  58. package/src/transaction/status.ts +5 -2
  59. package/src/utils/bs58.ts +11 -0
  60. package/src/utils/index.ts +0 -1
  61. package/src/utils/subscription.ts +1 -3
  62. package/src/utils/utils.ts +11 -19
  63. package/.eslintrc.json +0 -21
  64. package/LICENSE +0 -165
  65. package/contracts/add/add.ral +0 -16
  66. package/contracts/greeter/greeter.ral +0 -7
  67. package/contracts/greeter/greeter_interface.ral +0 -3
  68. package/contracts/greeter_main.ral +0 -9
  69. package/contracts/main.ral +0 -6
  70. package/contracts/sub/sub.ral +0 -9
  71. package/contracts/test/metadata.ral +0 -17
  72. package/contracts/test/warnings.ral +0 -5
  73. package/dev/user.conf +0 -29
  74. package/dist/scripts/create-project.d.ts +0 -2
  75. package/dist/scripts/create-project.js +0 -125
  76. package/dist/scripts/rename-gitignore.d.ts +0 -1
  77. package/dist/scripts/start-devnet.d.ts +0 -1
  78. package/dist/scripts/start-devnet.js +0 -131
  79. package/dist/scripts/stop-devnet.d.ts +0 -1
  80. package/dist/src/signer/node-wallet.d.ts +0 -13
  81. package/dist/src/signer/node-wallet.js +0 -60
  82. package/dist/src/test/index.d.ts +0 -7
  83. package/dist/src/test/index.js +0 -41
  84. package/dist/src/test/privatekey-wallet.d.ts +0 -12
  85. package/dist/src/test/privatekey-wallet.js +0 -68
  86. package/dist/src/transaction/sign-verify.d.ts +0 -2
  87. package/dist/src/transaction/sign-verify.js +0 -58
  88. package/dist/src/utils/password-crypto.d.ts +0 -2
  89. package/dist/src/utils/password-crypto.js +0 -69
  90. package/gitignore +0 -10
  91. package/scripts/create-project.ts +0 -137
  92. package/scripts/start-devnet.js +0 -141
  93. package/scripts/stop-devnet.js +0 -32
  94. package/src/contract/ralph.test.ts +0 -178
  95. package/src/fixtures/address.json +0 -36
  96. package/src/fixtures/balance.json +0 -9
  97. package/src/fixtures/self-clique.json +0 -19
  98. package/src/fixtures/transaction.json +0 -13
  99. package/src/fixtures/transactions.json +0 -179
  100. package/src/signer/fixtures/genesis.json +0 -26
  101. package/src/signer/fixtures/wallets.json +0 -26
  102. package/src/signer/node-wallet.ts +0 -74
  103. package/src/test/index.ts +0 -32
  104. package/src/test/privatekey-wallet.ts +0 -58
  105. package/src/transaction/sign-verify.test.ts +0 -50
  106. package/src/utils/address.test.ts +0 -47
  107. package/src/utils/djb2.test.ts +0 -35
  108. package/src/utils/password-crypto.test.ts +0 -27
  109. package/src/utils/password-crypto.ts +0 -77
  110. package/src/utils/utils.test.ts +0 -161
  111. package/templates/base/README.md +0 -34
  112. package/templates/base/package.json +0 -35
  113. package/templates/base/src/greeter.ts +0 -41
  114. package/templates/base/tsconfig.json +0 -19
  115. package/templates/react/README.md +0 -34
  116. package/templates/react/config-overrides.js +0 -18
  117. package/templates/react/package.json +0 -66
  118. package/templates/react/src/App.tsx +0 -42
  119. package/templates/react/src/artifacts/greeter.ral.json +0 -26
  120. package/templates/react/src/artifacts/greeter_main.ral.json +0 -22
  121. package/templates/shared/.eslintrc.json +0 -12
  122. package/templates/shared/scripts/header.js +0 -0
  123. package/test/contract.test.ts +0 -197
  124. package/test/events.test.ts +0 -138
  125. package/test/transaction.test.ts +0 -72
@@ -1,42 +0,0 @@
1
- import React, { useEffect, useState } from 'react'
2
- import './App.css'
3
-
4
- import { explorer, Contract, Script } from '@alephium/web3'
5
- import contractJson from './artifacts/greeter.ral.json'
6
- import scriptJson from './artifacts/greeter_main.ral.json'
7
-
8
- function Dashboard() {
9
- const api = new explorer.Api<null>({ baseUrl: 'https://mainnet-backend.alephium.org' })
10
- const contract = Contract.fromJson(contractJson).toString()
11
- const script = Script.fromJson(scriptJson).toString()
12
- const [blocks, setBlocks] = useState('')
13
-
14
- useEffect(() => {
15
- async function fetchBlocks() {
16
- const blocks = (await api.blocks.getBlocks({ page: 1 })).total
17
- setBlocks(JSON.stringify(blocks))
18
- }
19
-
20
- fetchBlocks()
21
- })
22
-
23
- return (
24
- <div>
25
- <p>blocks: {blocks}</p>
26
- <p>contract: {contract}</p>
27
- <p>script: {script}</p>
28
- </div>
29
- )
30
- }
31
-
32
- function App() {
33
- return (
34
- <div className='App'>
35
- <header className='App-header'>
36
- <Dashboard />
37
- </header>
38
- </div>
39
- )
40
- }
41
-
42
- export default App
@@ -1,26 +0,0 @@
1
- {
2
- "sourceCodeSha256": "10fe90ccf08b6b3fd2a653bd4258628f5dd165788cc48a83f9d6ff8039404588",
3
- "functions": [
4
- {
5
- "name": "greet",
6
- "signature": "pub greet()->(U256)",
7
- "argNames": [],
8
- "argTypes": [],
9
- "returnTypes": [
10
- "U256"
11
- ]
12
- }
13
- ],
14
- "bytecode": "010109010000000102a00002",
15
- "codeHash": "39debc1122591f4c96e2f807632acc6d137ea9bba303a4e04476cd8cade35242",
16
- "fieldsSig": {
17
- "signature": "TxContract Greeter(btcPrice:U256)",
18
- "names": [
19
- "btcPrice"
20
- ],
21
- "types": [
22
- "U256"
23
- ]
24
- },
25
- "eventsSig": []
26
- }
@@ -1,22 +0,0 @@
1
- {
2
- "sourceCodeSha256": "c1f3b4c4a8c6832d0c9b3e2c7c623de85331d8b0d853daf2acc4288e5655aff7",
3
- "functions": [
4
- {
5
- "name": "main",
6
- "signature": "pub payable main()->()",
7
- "argNames": [],
8
- "argTypes": [],
9
- "returnTypes": []
10
- }
11
- ],
12
- "bytecodeTemplate": "0101010002000e{0}1700160001000d2f4d{0}1701160101000d2f4d",
13
- "fieldsSig": {
14
- "signature": "TxScript Main(greeterContractId:ByteVec)",
15
- "names": [
16
- "greeterContractId"
17
- ],
18
- "types": [
19
- "ByteVec"
20
- ]
21
- }
22
- }
@@ -1,12 +0,0 @@
1
- {
2
- "extends": ["prettier", "plugin:react/recommended"],
3
- "rules": {
4
- "header/header": [0]
5
- },
6
- "parserOptions": {
7
- "project": "tsconfig.json",
8
- "ecmaVersion": 2020,
9
- "sourceType": "module"
10
- },
11
- "parser": "@typescript-eslint/parser"
12
- }
File without changes
@@ -1,197 +0,0 @@
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 * as fs from 'fs'
20
- import * as path from 'path'
21
-
22
- import { NodeProvider } from '../src/api'
23
- import { Contract, Script, TestContractParams } from '../src/contract'
24
- import { testWallet } from '../src/test'
25
- import { addressFromContractId } from '../src/utils'
26
-
27
- describe('contract', function () {
28
- async function testSuite1() {
29
- const provider = new NodeProvider('http://127.0.0.1:22973')
30
-
31
- const add = await Contract.fromSource(provider, 'add/add.ral')
32
- const sub = await Contract.fromSource(provider, 'sub/sub.ral')
33
-
34
- const subState = sub.toState({ result: 0 }, { alphAmount: BigInt('1000000000000000000') })
35
- const testParams: TestContractParams = {
36
- initialFields: { subContractId: subState.contractId, result: 0 },
37
- testArgs: { array: [2, 1] },
38
- existingContracts: [subState]
39
- }
40
- const testResult = await add.testPublicMethod(provider, 'add', testParams)
41
- expect(testResult.returns).toEqual([[3, 1]])
42
- expect(testResult.contracts[0].codeHash).toEqual(sub.codeHash)
43
- expect(testResult.contracts[0].fields.result).toEqual(1)
44
- expect(testResult.contracts[1].codeHash).toEqual(add.codeHash)
45
- expect(testResult.contracts[1].fields.subContractId).toEqual(subState.contractId)
46
- expect(testResult.contracts[1].fields.result).toEqual(3)
47
- const events = testResult.events.sort((a, b) => a.name.localeCompare(b.name))
48
- expect(events[0].name).toEqual('Add')
49
- expect(events[0].fields.x).toEqual(2)
50
- expect(events[0].fields.y).toEqual(1)
51
- expect(events[1].name).toEqual('Sub')
52
- expect(events[1].fields.x).toEqual(2)
53
- expect(events[1].fields.y).toEqual(1)
54
-
55
- const testResultPrivate = await add.testPrivateMethod(provider, 'addPrivate', testParams)
56
- expect(testResultPrivate.returns).toEqual([[3, 1]])
57
-
58
- const signer = await testWallet(provider)
59
-
60
- const subDeployTx = await sub.transactionForDeployment(signer, {
61
- initialFields: { result: 0 },
62
- initialTokenAmounts: []
63
- })
64
- const subContractId = subDeployTx.contractId
65
- const subContractAddress = addressFromContractId(subContractId)
66
- expect(subDeployTx.fromGroup).toEqual(0)
67
- expect(subDeployTx.toGroup).toEqual(0)
68
- const subSubmitResult = await signer.submitTransaction(subDeployTx.unsignedTx, subDeployTx.txId)
69
- expect(subSubmitResult.fromGroup).toEqual(0)
70
- expect(subSubmitResult.toGroup).toEqual(0)
71
- expect(subSubmitResult.txId).toEqual(subDeployTx.txId)
72
-
73
- const addDeployTx = await add.transactionForDeployment(signer, {
74
- initialFields: { subContractId: subContractId, result: 0 },
75
- initialTokenAmounts: []
76
- })
77
- expect(addDeployTx.fromGroup).toEqual(0)
78
- expect(addDeployTx.toGroup).toEqual(0)
79
- const addSubmitResult = await signer.submitTransaction(addDeployTx.unsignedTx, addDeployTx.txId)
80
- expect(addSubmitResult.fromGroup).toEqual(0)
81
- expect(addSubmitResult.toGroup).toEqual(0)
82
- expect(addSubmitResult.txId).toEqual(addDeployTx.txId)
83
-
84
- const addContractId = addDeployTx.contractId
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)
93
-
94
- const main = await Script.fromSource(provider, 'main.ral')
95
- const mainScriptTx = await main.transactionForDeployment(signer, {
96
- initialFields: { addContractId: addContractId }
97
- })
98
- expect(mainScriptTx.fromGroup).toEqual(0)
99
- expect(mainScriptTx.toGroup).toEqual(0)
100
- const mainSubmitResult = await signer.submitTransaction(mainScriptTx.unsignedTx, mainScriptTx.txId)
101
- expect(mainSubmitResult.fromGroup).toEqual(0)
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)
110
- }
111
-
112
- async function testSuite2() {
113
- const provider = new NodeProvider('http://127.0.0.1:22973')
114
-
115
- const greeter = await Contract.fromSource(provider, 'greeter/greeter.ral')
116
-
117
- const testParams: TestContractParams = {
118
- initialFields: { btcPrice: 1 }
119
- }
120
- const testResult = await greeter.testPublicMethod(provider, 'greet', testParams)
121
- expect(testResult.returns).toEqual([1])
122
- expect(testResult.contracts[0].codeHash).toEqual(greeter.codeHash)
123
- expect(testResult.contracts[0].fields.btcPrice).toEqual(1)
124
-
125
- const signer = await testWallet(provider)
126
-
127
- const deployTx = await greeter.transactionForDeployment(signer, {
128
- initialFields: { btcPrice: 1 },
129
- initialTokenAmounts: []
130
- })
131
- expect(deployTx.fromGroup).toEqual(0)
132
- expect(deployTx.toGroup).toEqual(0)
133
- const submitResult = await signer.submitTransaction(deployTx.unsignedTx, deployTx.txId)
134
- expect(submitResult.fromGroup).toEqual(0)
135
- expect(submitResult.toGroup).toEqual(0)
136
- expect(submitResult.txId).toEqual(deployTx.txId)
137
-
138
- const greeterContractId = deployTx.contractId
139
- const main = await Script.fromSource(provider, 'greeter_main.ral')
140
-
141
- const mainScriptTx = await main.transactionForDeployment(signer, {
142
- initialFields: { greeterContractId: greeterContractId }
143
- })
144
- expect(mainScriptTx.fromGroup).toEqual(0)
145
- expect(mainScriptTx.toGroup).toEqual(0)
146
- const mainSubmitResult = await signer.submitTransaction(mainScriptTx.unsignedTx, mainScriptTx.txId)
147
- expect(mainSubmitResult.fromGroup).toEqual(0)
148
- expect(mainSubmitResult.toGroup).toEqual(0)
149
- }
150
-
151
- it('should test contracts', async () => {
152
- await testSuite1()
153
- await testSuite2()
154
- })
155
-
156
- function loadJson(fileName: string) {
157
- const filePath = path.resolve(process.cwd() + path.sep + fileName)
158
- const rawData = fs.readFileSync(filePath).toString()
159
- return JSON.parse(rawData)
160
- }
161
-
162
- function loadContract(fileName: string) {
163
- Contract.fromJson(loadJson(fileName))
164
- }
165
-
166
- function loadScript(fileName: string) {
167
- Script.fromJson(loadJson(fileName))
168
- }
169
-
170
- it('should load contract from json', async () => {
171
- loadContract('./artifacts/add/add.ral.json')
172
- loadContract('./artifacts/sub/sub.ral.json')
173
- loadScript('./artifacts/main.ral.json')
174
-
175
- loadContract('./artifacts/greeter/greeter.ral.json')
176
- loadScript('./artifacts/greeter_main.ral.json')
177
- })
178
-
179
- it('should extract metadata of contracts', async () => {
180
- const provider = new NodeProvider('http://127.0.0.1:22973')
181
- const contract = await Contract.fromSource(provider, 'test/metadata.ral')
182
- expect(contract.functions.map((func) => func.name)).toEqual(['foo', 'bar', 'baz'])
183
- expect(contract.publicFunctions()).toEqual(['foo'])
184
- expect(contract.usingPreapprovedAssetsFunctions()).toEqual(['foo'])
185
- expect(contract.usingAssetsInContractFunctions()).toEqual(['bar'])
186
- })
187
-
188
- it('should handle compiler warnings', async () => {
189
- const provider = new NodeProvider('http://127.0.0.1:22973')
190
- const contract = await Contract.fromSource(provider, 'test/warnings.ral', false)
191
- expect(contract.publicFunctions()).toEqual(['foo'])
192
-
193
- await expect(Contract.fromSource(provider, 'test/warnings.ral')).rejects.toThrowError(
194
- 'Compilation warnings:\n - Found unused variables in function foo: foo.y\n - Found unused fields: b'
195
- )
196
- })
197
- })
@@ -1,138 +0,0 @@
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 { subscribeToEvents } from '../src/contract/events'
21
- import { Contract, Script } from '../src/contract'
22
- import { NodeWallet, SignExecuteScriptTxParams } from '../src/signer'
23
- import { ContractEvent } from '../src/api/api-alephium'
24
- import { testWallet } from '../src/test'
25
- import { SubscribeOptions, timeout } from '../src/utils'
26
-
27
- describe('events', function () {
28
- async function deployContract(provider: NodeProvider, signer: NodeWallet): Promise<[string, string]> {
29
- const sub = await Contract.fromSource(provider, 'sub/sub.ral')
30
- const subDeployTx = await sub.transactionForDeployment(signer, {
31
- initialFields: { result: 0 },
32
- initialTokenAmounts: []
33
- })
34
- const subContractId = subDeployTx.contractId
35
- const subSubmitResult = await signer.submitTransaction(subDeployTx.unsignedTx, subDeployTx.txId)
36
- expect(subSubmitResult.txId).toEqual(subDeployTx.txId)
37
-
38
- const add = await Contract.fromSource(provider, 'add/add.ral')
39
- const addDeployTx = await add.transactionForDeployment(signer, {
40
- initialFields: { subContractId: subContractId, result: 0 },
41
- initialTokenAmounts: []
42
- })
43
- const addSubmitResult = await signer.submitTransaction(addDeployTx.unsignedTx, addDeployTx.txId)
44
- expect(addSubmitResult.txId).toEqual(addDeployTx.txId)
45
- return [addDeployTx.contractAddress, addDeployTx.contractId]
46
- }
47
-
48
- async function executeScript(params: SignExecuteScriptTxParams, signer: NodeWallet, times: number) {
49
- for (let i = 0; i < times; i++) {
50
- const scriptTx = await signer.buildScriptTx(params)
51
- await signer.submitTransaction(scriptTx.unsignedTx, scriptTx.txId)
52
- }
53
- }
54
-
55
- it('should subscribe contract events', async () => {
56
- const provider = new NodeProvider('http://127.0.0.1:22973')
57
- const signer = await testWallet(provider)
58
-
59
- const [contractAddress, contractId] = await deployContract(provider, signer)
60
- const events: Array<ContractEvent> = []
61
- const subscriptOptions: SubscribeOptions<ContractEvent> = {
62
- provider: provider,
63
- pollingInterval: 500,
64
- messageCallback: (event: ContractEvent): Promise<void> => {
65
- events.push(event)
66
- return Promise.resolve()
67
- },
68
- errorCallback: (error: any, subscription): Promise<void> => {
69
- console.log(error)
70
- subscription.unsubscribe()
71
- return Promise.resolve()
72
- }
73
- }
74
- const subscription = subscribeToEvents(subscriptOptions, contractAddress)
75
- const script = await Script.fromSource(provider, 'main.ral')
76
- const scriptTxParams = await script.paramsForDeployment({
77
- initialFields: { addContractId: contractId },
78
- signerAddress: (await signer.getAccounts())[0].address
79
- })
80
- await executeScript(scriptTxParams, signer, 3)
81
- await timeout(3000)
82
-
83
- expect(events.length).toEqual(3)
84
- events.forEach((event) => {
85
- expect(event.fields).toEqual([
86
- { type: 'U256', value: '2' },
87
- { type: 'U256', value: '1' }
88
- ])
89
- })
90
- expect(subscription.currentEventCount()).toEqual(events.length)
91
-
92
- subscription.unsubscribe()
93
- }, 15000)
94
-
95
- it('should cancel event subscription', async () => {
96
- const provider = new NodeProvider('http://127.0.0.1:22973')
97
- const signer = await testWallet(provider)
98
-
99
- const [contractAddress, contractId] = await deployContract(provider, signer)
100
- const events: Array<ContractEvent> = []
101
- const subscriptOptions = {
102
- provider: provider,
103
- pollingInterval: 500,
104
- messageCallback: (event: ContractEvent): Promise<void> => {
105
- events.push(event)
106
- return Promise.resolve()
107
- },
108
- errorCallback: (error: any, subscription): Promise<void> => {
109
- console.log(error)
110
- subscription.unsubscribe()
111
- return Promise.resolve()
112
- }
113
- }
114
- const subscription = subscribeToEvents(subscriptOptions, contractAddress)
115
- const script = await Script.fromSource(provider, 'main.ral')
116
- const scriptTx0 = await script.transactionForDeployment(signer, {
117
- initialFields: { addContractId: contractId }
118
- })
119
- await signer.submitTransaction(scriptTx0.unsignedTx, scriptTx0.txId)
120
- await timeout(1500)
121
- subscription.unsubscribe()
122
-
123
- expect(events.length).toEqual(1)
124
- expect(events[0].txId).toEqual(scriptTx0.txId)
125
- expect(events[0].fields).toEqual([
126
- { type: 'U256', value: '2' },
127
- { type: 'U256', value: '1' }
128
- ])
129
- expect(subscription.currentEventCount()).toEqual(events.length)
130
-
131
- const scriptTx1 = await script.transactionForDeployment(signer, {
132
- initialFields: { addContractId: contractId }
133
- })
134
- await signer.submitTransaction(scriptTx1.unsignedTx, scriptTx1.txId)
135
- await timeout(1500)
136
- expect(events.length).toEqual(1)
137
- })
138
- })
@@ -1,72 +0,0 @@
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/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
- 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
- })