@alephium/web3 0.2.0-rc.1 → 0.2.0-rc.10

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 (89) hide show
  1. package/.eslintignore +2 -2
  2. package/README.md +2 -135
  3. package/contracts/greeter/greeter.ral +2 -2
  4. package/contracts/greeter/greeter_interface.ral +1 -0
  5. package/contracts/greeter_main.ral +0 -2
  6. package/dist/alephium-web3.min.js +1 -1
  7. package/dist/alephium-web3.min.js.LICENSE.txt +0 -17
  8. package/dist/alephium-web3.min.js.map +1 -1
  9. package/dist/scripts/create-project.js +3 -2
  10. package/dist/src/api/api-alephium.d.ts +44 -11
  11. package/dist/src/api/api-alephium.js +16 -0
  12. package/dist/src/api/api-explorer.d.ts +16 -16
  13. package/dist/src/api/index.d.ts +2 -2
  14. package/dist/src/api/index.js +18 -3
  15. package/dist/src/contract/contract.d.ts +101 -52
  16. package/dist/src/contract/contract.js +353 -219
  17. package/dist/src/contract/index.js +5 -1
  18. package/dist/src/global.d.ts +4 -0
  19. package/dist/src/global.js +38 -0
  20. package/dist/src/index.d.ts +2 -0
  21. package/dist/src/index.js +20 -1
  22. package/dist/src/signer/index.d.ts +0 -1
  23. package/dist/src/signer/index.js +5 -2
  24. package/dist/src/signer/signer.d.ts +1 -1
  25. package/dist/src/signer/signer.js +8 -3
  26. package/dist/src/transaction/index.d.ts +0 -1
  27. package/dist/src/transaction/index.js +5 -2
  28. package/dist/src/utils/index.d.ts +0 -1
  29. package/dist/src/utils/index.js +5 -2
  30. package/dist/src/utils/subscription.d.ts +0 -1
  31. package/dist/src/utils/subscription.js +2 -1
  32. package/dist/src/utils/utils.d.ts +5 -4
  33. package/dist/src/utils/utils.js +17 -16
  34. package/jest-config.json +11 -0
  35. package/package.json +6 -34
  36. package/scripts/create-project.ts +3 -2
  37. package/src/api/api-alephium.ts +53 -5
  38. package/src/api/index.ts +13 -3
  39. package/src/contract/contract.ts +505 -318
  40. package/src/contract/events.ts +2 -2
  41. package/src/{transaction/sign-verify.ts → global.ts} +14 -15
  42. package/src/index.ts +3 -0
  43. package/src/signer/index.ts +0 -1
  44. package/src/signer/signer.ts +3 -2
  45. package/src/transaction/index.ts +0 -1
  46. package/src/transaction/status.ts +1 -1
  47. package/src/utils/index.ts +0 -1
  48. package/src/utils/subscription.ts +3 -3
  49. package/src/utils/utils.ts +10 -10
  50. package/templates/base/package.json +2 -2
  51. package/templates/base/src/greeter.ts +8 -7
  52. package/templates/react/package.json +2 -2
  53. package/templates/react/src/App.tsx +2 -2
  54. package/.eslintrc.json +0 -21
  55. package/LICENSE +0 -165
  56. package/contracts/add/add.ral +0 -16
  57. package/contracts/main.ral +0 -6
  58. package/contracts/sub/sub.ral +0 -9
  59. package/dist/src/signer/node-wallet.d.ts +0 -13
  60. package/dist/src/signer/node-wallet.js +0 -60
  61. package/dist/src/test/index.d.ts +0 -7
  62. package/dist/src/test/index.js +0 -41
  63. package/dist/src/test/privatekey-wallet.d.ts +0 -12
  64. package/dist/src/test/privatekey-wallet.js +0 -68
  65. package/dist/src/transaction/sign-verify.d.ts +0 -2
  66. package/dist/src/transaction/sign-verify.js +0 -58
  67. package/dist/src/utils/password-crypto.d.ts +0 -2
  68. package/dist/src/utils/password-crypto.js +0 -69
  69. package/gitignore +0 -10
  70. package/src/contract/ralph.test.ts +0 -178
  71. package/src/fixtures/address.json +0 -36
  72. package/src/fixtures/balance.json +0 -9
  73. package/src/fixtures/self-clique.json +0 -19
  74. package/src/fixtures/transaction.json +0 -13
  75. package/src/fixtures/transactions.json +0 -179
  76. package/src/signer/fixtures/genesis.json +0 -26
  77. package/src/signer/fixtures/wallets.json +0 -26
  78. package/src/signer/node-wallet.ts +0 -74
  79. package/src/test/index.ts +0 -32
  80. package/src/test/privatekey-wallet.ts +0 -58
  81. package/src/transaction/sign-verify.test.ts +0 -50
  82. package/src/utils/address.test.ts +0 -47
  83. package/src/utils/djb2.test.ts +0 -35
  84. package/src/utils/password-crypto.test.ts +0 -27
  85. package/src/utils/password-crypto.ts +0 -77
  86. package/src/utils/utils.test.ts +0 -161
  87. package/test/contract.test.ts +0 -178
  88. package/test/events.test.ts +0 -138
  89. package/test/transaction.test.ts +0 -72
@@ -1,161 +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 EC from 'elliptic'
20
- import assert from 'assert'
21
-
22
- import * as utils from './utils'
23
-
24
- describe('utils', function () {
25
- it('should throw an error when decoding invalid signature', () => {
26
- const ec = new EC.ec('secp256k1')
27
- const signature = 'signature-with-wrong-length'
28
-
29
- expect(() => utils.signatureDecode(ec, signature)).toThrowError('Invalid signature length')
30
- })
31
-
32
- it('should compress signature', () => {
33
- const vectors = [
34
- [
35
- 'Satoshi Nakamoto',
36
- '0000000000000000000000000000000000000000000000000000000000000001',
37
- '934b1ea10a4b3c1757e2b0c017d0b6143ce3c9a7e6a4a49860d7a6ab210ee3d82442ce9d2b916064108014783e923ec36b49743e2ffa1c4496f01a512aafd9e5'
38
- ],
39
- [
40
- 'Everything should be made as simple as possible, but not simpler.',
41
- '0000000000000000000000000000000000000000000000000000000000000001',
42
- '33a69cd2065432a30f3d1ce4eb0d59b8ab58c74f27c41a7fdb5696ad4e6108c96f807982866f785d3f6418d24163ddae117b7db4d5fdf0071de069fa54342262'
43
- ],
44
- [
45
- 'All those moments will be lost in time, like tears in rain. Time to die...',
46
- '0000000000000000000000000000000000000000000000000000000000000001',
47
- '8600dbd41e348fe5c9465ab92d23e3db8b98b873beecd930736488696438cb6b547fe64427496db33bf66019dacbf0039c04199abb0122918601db38a72cfc21'
48
- ],
49
- [
50
- 'Satoshi Nakamoto',
51
- 'fffffffffffffffffffffffffffffffebaaedce6af48a03bbfd25e8cd0364140',
52
- 'fd567d121db66e382991534ada77a6bd3106f0a1098c231e47993447cd6af2d06b39cd0eb1bc8603e159ef5c20a5c8ad685a45b06ce9bebed3f153d10d93bed5'
53
- ],
54
- [
55
- 'Alan Turing',
56
- 'f8b8af8ce3c7cca5e300d33939540c10d45ce001b8f252bfbc57ba0342904181',
57
- '7063ae83e7f62bbb171798131b4a0564b956930092b33b07b395615d9ec7e15c58dfcc1e00a35e1572f366ffe34ba0fc47db1e7189759b9fb233c5b05ab388ea'
58
- ],
59
- [
60
- "There is a computer disease that anybody who works with computers knows about. It's a very serious disease and it interferes completely with the work. The trouble with computers is that you 'play' with them!",
61
- 'e91671c46231f833a6406ccbea0e3e392c76c167bac1cb013f6f1013980455c2',
62
- 'b552edd27580141f3b2a5463048cb7cd3e047b97c9f98076c32dbdf85a68718b279fa72dd19bfae05577e06c7c0c1900c371fcd5893f7e1d56a37d30174671f6'
63
- ]
64
- ]
65
-
66
- const ec = new EC.ec('secp256k1')
67
-
68
- vectors.forEach((vector) => {
69
- const privateKey = vector[1]
70
- const signatureExpected = vector[2]
71
- const keyPair = ec.keyFromPrivate(privateKey)
72
-
73
- const message = vector[0]
74
- const sha256 = ec.hash().update(message).digest()
75
- const signature = utils.signatureEncode(keyPair.sign(sha256))
76
- assert.deepStrictEqual(signature, signatureExpected)
77
- })
78
- })
79
-
80
- it('should calculate the group of addresses', () => {
81
- expect(utils.groupOfAddress('15EM5rGtt7dPRZScE4Z9oL2EDfj84JnoSgq3NNgdcGFyu')).toBe(0),
82
- expect(utils.groupOfAddress('1D59jXR9NpD9ZQqZTRVcVbKVh6ko5TUMt89WvkA8P9P7w')).toBe(1),
83
- expect(utils.groupOfAddress('14tAT3nm7UqVP7gZ35icSdT3AEffv1kaUUMbWQK5PFygr')).toBe(2),
84
- expect(utils.groupOfAddress('12F5aVQoQ7cNrgsVN2YPciwYvwmtJp4ohLa2x4R5KgLbG')).toBe(3),
85
- expect(
86
- utils.groupOfAddress(
87
- '2jW1n2icPtc55Cdm8TF9FjGH681cWthsaZW3gaUFekFZepJoeyY3ZbY7y5SCtAjyCjLL24c4L2Vnfv3KDdAypCddfAY'
88
- )
89
- ).toBe(0),
90
- expect(
91
- utils.groupOfAddress(
92
- '2jXboVD9p66wrAHkPHx2AQocAzYXUWeppmRT3PuVT3ccxX9u8puTnwLeQ2VbTd4sNkgSEgk1cLbyVGLFshGweJCk1Mr'
93
- )
94
- ).toBe(1),
95
- expect(
96
- utils.groupOfAddress(
97
- '2je1yvQHpg8bKCDmvr1koELSNbty5DHrHYRkXomiRNvP5VcsZTK3WisBco2sCtCULM2YbxRxPd7QwhdP2hz9PEQwB1S'
98
- )
99
- ).toBe(2),
100
- expect(
101
- utils.groupOfAddress(
102
- '2jWukVCejM4Zifz9LvMG4dfR6SEecHLX8VqbswhGwnu61d28B861UhLu3ZmTHu4N14m1kk9rbxreBYzcxta1WPawKzG'
103
- )
104
- ).toBe(3),
105
- expect(utils.groupOfAddress('eBrjfQNeyUCuxE4zpbfMZcbS3PuvbMJDQBCyk4HRHtX4')).toBe(0),
106
- expect(utils.groupOfAddress('euWxyF55nGTxavL6mgGeMrFdvSRzHor8AmhgPXm8Lm9D')).toBe(1),
107
- expect(utils.groupOfAddress('n2pYTzmA27tkp7UNFPhMJpjz3jr5vgessxqJ7kwomBMF')).toBe(2),
108
- expect(utils.groupOfAddress('tLf6hDfrUugmxZhKxGoZMpAUBt3NcZ2hrTspTCmZ6JdQ')).toBe(3)
109
- })
110
-
111
- it('should extract token id from addresses', () => {
112
- expect(utils.binToHex(utils.tokenIdFromAddress('wCTeteGBeSEC54GpkS8jWBzYiYNTBUuTW3WzxGd9yExT'))).toBe(
113
- '25469eb0d0d0a55deea832924547b7b166c70a3554fe321e81886d3c18f19d64'
114
- ),
115
- expect(utils.binToHex(utils.tokenIdFromAddress('xrY8dxgVm38QCQXhiUFcivFutLFUNMoo8qu8vYf7wJps'))).toBe(
116
- '3de370f893cb1383c828c0eb22c89aceb13fa56ddced1848db27ce7fa419c80c'
117
- ),
118
- expect(() => utils.tokenIdFromAddress('eBrjfQNeyUCuxE4zpbfMZcbS3PuvbMJDQBCyk4HRHtX4')).toThrow(
119
- 'Invalid contract address type: 2'
120
- ),
121
- expect(() => utils.tokenIdFromAddress('..')).toThrow('Non-base58 character')
122
- })
123
-
124
- it('should compute public key from private key', () => {
125
- expect(utils.publicKeyFromPrivateKey('91411e484289ec7e8b3058697f53f9b26fa7305158b4ef1a81adfbabcf090e45')).toBe(
126
- '030f9f042a9410969f1886f85fa20f6e43176ae23fc5e64db15b3767c84c5db2dc'
127
- )
128
- })
129
-
130
- it('should compute address from public key', () => {
131
- expect(utils.publicKeyFromPrivateKey('91411e484289ec7e8b3058697f53f9b26fa7305158b4ef1a81adfbabcf090e45')).toBe(
132
- '030f9f042a9410969f1886f85fa20f6e43176ae23fc5e64db15b3767c84c5db2dc'
133
- )
134
- expect(utils.addressFromPublicKey('030f9f042a9410969f1886f85fa20f6e43176ae23fc5e64db15b3767c84c5db2dc')).toBe(
135
- '1ACCkgFfmTif46T3qK12znuWjb5Bk9jXpqaeWt2DXx8oc'
136
- )
137
- })
138
-
139
- it('should convert between contract id and address', () => {
140
- expect(utils.addressFromContractId('1f6b937b935d7fac894fb22ffe2b974cae9c8c166501372f1b9155144e0ff4ae')).toBe(
141
- 'vobthYg1e9tPKhmF96rpkv3akCj7vhvgPpsP4qwZqDw3'
142
- )
143
- expect(utils.binToHex(utils.contractIdFromAddress('vobthYg1e9tPKhmF96rpkv3akCj7vhvgPpsP4qwZqDw3'))).toBe(
144
- '1f6b937b935d7fac894fb22ffe2b974cae9c8c166501372f1b9155144e0ff4ae'
145
- )
146
- })
147
-
148
- it('should compute id of the sub contract', () => {
149
- expect(
150
- utils.subContractId(
151
- '0a38bc48fbb4300f1e305b201cd6129372d867122efb814d871d18c0bfe43b56',
152
- '4f51cd1f0af97cf5ec9c7a3397eaeea549d55a93c216e54f2ab4a8cf29f6f865'
153
- )
154
- ).toBe('0e28f15ca290002c31d691aa008aa56ac12356b0380efb6c88fff929b6a268a9')
155
- })
156
-
157
- it('should convert from string to hex and back', () => {
158
- expect(utils.stringToHex('Hello Alephium!')).toBe('48656c6c6f20416c65706869756d21')
159
- expect(utils.hexToString('48656c6c6f20416c65706869756d21')).toBe('Hello Alephium!')
160
- })
161
- })
@@ -1,178 +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
- })
@@ -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
- 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
- })