@alephium/web3 0.2.0-rc.4 → 0.2.0-rc.5

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.
@@ -3,7 +3,7 @@
3
3
  "version": "0.1.0",
4
4
  "license": "GPL",
5
5
  "config": {
6
- "alephium_version": "1.5.0-rc4"
6
+ "alephium_version": "1.5.0-rc5"
7
7
  },
8
8
  "scripts": {
9
9
  "build": "rm -rf dist && npx tsc --build .",
@@ -12,7 +12,7 @@
12
12
  "stop-devnet": "node scripts/stop-devnet.js"
13
13
  },
14
14
  "dependencies": {
15
- "@alephium/web3": "0.2.0-rc.4"
15
+ "@alephium/web3": "0.2.0-rc.5"
16
16
  },
17
17
  "devDependencies": {
18
18
  "@types/elliptic": "^6.4.13",
@@ -2,7 +2,7 @@
2
2
  "name": "my-dapp",
3
3
  "version": "0.1.0",
4
4
  "config": {
5
- "alephium_version": "1.5.0-rc4"
5
+ "alephium_version": "1.5.0-rc5"
6
6
  },
7
7
  "dependencies": {
8
8
  "@testing-library/jest-dom": "^5.16.4",
@@ -12,7 +12,7 @@
12
12
  "@types/node": "^16.11.26",
13
13
  "@types/react": "^18.0.3",
14
14
  "@types/react-dom": "^18.0.0",
15
- "@alephium/web3": "0.2.0-rc.4",
15
+ "@alephium/web3": "0.2.0-rc.5",
16
16
  "react": "^18.0.0",
17
17
  "react-dom": "^18.0.0",
18
18
  "react-scripts": "5.0.1",
@@ -20,16 +20,18 @@ import * as fs from 'fs'
20
20
  import * as path from 'path'
21
21
 
22
22
  import { NodeProvider } from '../src/api'
23
- import { Contract, Script, TestContractParams } from '../src/contract'
23
+ import { Contract, Project, Script, TestContractParams } from '../src/contract'
24
24
  import { testWallet } from '../src/test'
25
25
  import { addressFromContractId } from '../src/utils'
26
26
 
27
27
  describe('contract', function () {
28
28
  async function testSuite1() {
29
29
  const provider = new NodeProvider('http://127.0.0.1:22973')
30
+ await Project.build(provider)
30
31
 
31
- const add = await Contract.fromSource(provider, 'add/add.ral')
32
- const sub = await Contract.fromSource(provider, 'sub/sub.ral')
32
+ // ignore unused private function warnings
33
+ const add = Project.contract('add/add.ral', false)
34
+ const sub = Project.contract('sub/sub.ral')
33
35
 
34
36
  const subState = sub.toState({ result: 0 }, { alphAmount: BigInt('1000000000000000000') })
35
37
  const testParams: TestContractParams = {
@@ -37,7 +39,7 @@ describe('contract', function () {
37
39
  testArgs: { array: [2, 1] },
38
40
  existingContracts: [subState]
39
41
  }
40
- const testResult = await add.testPublicMethod(provider, 'add', testParams)
42
+ const testResult = await add.testPublicMethod('add', testParams)
41
43
  expect(testResult.returns).toEqual([[3, 1]])
42
44
  expect(testResult.contracts[0].codeHash).toEqual(sub.codeHash)
43
45
  expect(testResult.contracts[0].fields.result).toEqual(1)
@@ -52,7 +54,7 @@ describe('contract', function () {
52
54
  expect(events[1].fields.x).toEqual(2)
53
55
  expect(events[1].fields.y).toEqual(1)
54
56
 
55
- const testResultPrivate = await add.testPrivateMethod(provider, 'addPrivate', testParams)
57
+ const testResultPrivate = await add.testPrivateMethod('addPrivate', testParams)
56
58
  expect(testResultPrivate.returns).toEqual([[3, 1]])
57
59
 
58
60
  const signer = await testWallet(provider)
@@ -85,13 +87,13 @@ describe('contract', function () {
85
87
  const addContractAddress = addressFromContractId(addContractId)
86
88
 
87
89
  // Check state for add/sub before main script is executed
88
- let fetchedSubState = await sub.fetchState(provider, subContractAddress, 0)
90
+ let fetchedSubState = await sub.fetchState(subContractAddress, 0)
89
91
  expect(fetchedSubState.fields.result).toEqual(0)
90
- let fetchedAddState = await add.fetchState(provider, addContractAddress, 0)
92
+ let fetchedAddState = await add.fetchState(addContractAddress, 0)
91
93
  expect(fetchedAddState.fields.subContractId).toEqual(subContractId)
92
94
  expect(fetchedAddState.fields.result).toEqual(0)
93
95
 
94
- const main = await Script.fromSource(provider, 'main.ral')
96
+ const main = Project.script('main.ral')
95
97
  const mainScriptTx = await main.transactionForDeployment(signer, {
96
98
  initialFields: { addContractId: addContractId }
97
99
  })
@@ -102,22 +104,23 @@ describe('contract', function () {
102
104
  expect(mainSubmitResult.toGroup).toEqual(0)
103
105
 
104
106
  // Check state for add/sub after main script is executed
105
- fetchedSubState = await sub.fetchState(provider, subContractAddress, 0)
107
+ fetchedSubState = await sub.fetchState(subContractAddress, 0)
106
108
  expect(fetchedSubState.fields.result).toEqual(1)
107
- fetchedAddState = await add.fetchState(provider, addContractAddress, 0)
109
+ fetchedAddState = await add.fetchState(addContractAddress, 0)
108
110
  expect(fetchedAddState.fields.subContractId).toEqual(subContractId)
109
111
  expect(fetchedAddState.fields.result).toEqual(3)
110
112
  }
111
113
 
112
114
  async function testSuite2() {
113
115
  const provider = new NodeProvider('http://127.0.0.1:22973')
116
+ await Project.build(provider)
114
117
 
115
- const greeter = await Contract.fromSource(provider, 'greeter/greeter.ral')
118
+ const greeter = Project.contract('greeter/greeter.ral')
116
119
 
117
120
  const testParams: TestContractParams = {
118
121
  initialFields: { btcPrice: 1 }
119
122
  }
120
- const testResult = await greeter.testPublicMethod(provider, 'greet', testParams)
123
+ const testResult = await greeter.testPublicMethod('greet', testParams)
121
124
  expect(testResult.returns).toEqual([1])
122
125
  expect(testResult.contracts[0].codeHash).toEqual(greeter.codeHash)
123
126
  expect(testResult.contracts[0].fields.btcPrice).toEqual(1)
@@ -136,7 +139,7 @@ describe('contract', function () {
136
139
  expect(submitResult.txId).toEqual(deployTx.txId)
137
140
 
138
141
  const greeterContractId = deployTx.contractId
139
- const main = await Script.fromSource(provider, 'greeter_main.ral')
142
+ const main = Project.script('greeter_main.ral')
140
143
 
141
144
  const mainScriptTx = await main.transactionForDeployment(signer, {
142
145
  initialFields: { greeterContractId: greeterContractId }
@@ -167,6 +170,14 @@ describe('contract', function () {
167
170
  Script.fromJson(loadJson(fileName))
168
171
  }
169
172
 
173
+ it('should load source files by order', async () => {
174
+ const sourceFiles = await Project['loadSourceFiles']('./contracts') // `loadSourceFiles` is a private method
175
+ expect(sourceFiles.length).toEqual(8)
176
+ sourceFiles.slice(0, 5).forEach((c) => expect(c.type).toEqual(0)) // contracts
177
+ sourceFiles.slice(5, 7).forEach((s) => expect(s.type).toEqual(1)) // scripts
178
+ sourceFiles.slice(7).forEach((i) => expect(i.type).toEqual(3)) // interfaces
179
+ })
180
+
170
181
  it('should load contract from json', async () => {
171
182
  loadContract('./artifacts/add/add.ral.json')
172
183
  loadContract('./artifacts/sub/sub.ral.json')
@@ -178,7 +189,8 @@ describe('contract', function () {
178
189
 
179
190
  it('should extract metadata of contracts', async () => {
180
191
  const provider = new NodeProvider('http://127.0.0.1:22973')
181
- const contract = await Contract.fromSource(provider, 'test/metadata.ral')
192
+ await Project.build(provider)
193
+ const contract = Project.contract('test/metadata.ral', false)
182
194
  expect(contract.functions.map((func) => func.name)).toEqual(['foo', 'bar', 'baz'])
183
195
  expect(contract.publicFunctions()).toEqual(['foo'])
184
196
  expect(contract.usingPreapprovedAssetsFunctions()).toEqual(['foo'])
@@ -187,14 +199,15 @@ describe('contract', function () {
187
199
 
188
200
  it('should handle compiler warnings', async () => {
189
201
  const provider = new NodeProvider('http://127.0.0.1:22973')
190
- const contract = await Contract.fromSource(provider, 'test/warnings.ral', false)
202
+ await Project.build(provider)
203
+ const contract = Project.contract('test/warnings.ral', false)
191
204
  expect(contract.publicFunctions()).toEqual(['foo'])
192
205
 
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'
206
+ expect(() => Project.contract('test/warnings.ral')).toThrowError(
207
+ 'Compilation warnings:\n - Found unused variables in Warnings: foo.y\n - Found unused fields in Warnings: b'
195
208
  )
196
- await expect(Contract.fromSource(provider, 'test/warnings.ral', true, false)).rejects.toThrowError(
197
- 'Compilation warnings:\n - Found unused variables in function foo: foo.y\n - Found unused constants: C\n - Found unused fields: b'
209
+ expect(() => Project.contract('test/warnings.ral', true, false)).toThrowError(
210
+ 'Compilation warnings:\n - Found unused variables in Warnings: foo.y\n - Found unused constants in Warnings: C\n - Found unused fields in Warnings: b'
198
211
  )
199
212
  })
200
213
  })
@@ -18,15 +18,17 @@ along with the library. If not, see <http://www.gnu.org/licenses/>.
18
18
 
19
19
  import { NodeProvider } from '../src/api'
20
20
  import { subscribeToEvents } from '../src/contract/events'
21
- import { Contract, Script } from '../src/contract'
21
+ import { Project, Script } from '../src/contract'
22
22
  import { NodeWallet, SignExecuteScriptTxParams } from '../src/signer'
23
23
  import { ContractEvent } from '../src/api/api-alephium'
24
24
  import { testWallet } from '../src/test'
25
25
  import { SubscribeOptions, timeout } from '../src/utils'
26
26
 
27
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')
28
+ async function deployContract(signer: NodeWallet): Promise<[string, string]> {
29
+ const provider = new NodeProvider('http://127.0.0.1:22973')
30
+ await Project.build(provider)
31
+ const sub = Project.contract('sub/sub.ral')
30
32
  const subDeployTx = await sub.transactionForDeployment(signer, {
31
33
  initialFields: { result: 0 },
32
34
  initialTokenAmounts: []
@@ -35,7 +37,8 @@ describe('events', function () {
35
37
  const subSubmitResult = await signer.submitTransaction(subDeployTx.unsignedTx, subDeployTx.txId)
36
38
  expect(subSubmitResult.txId).toEqual(subDeployTx.txId)
37
39
 
38
- const add = await Contract.fromSource(provider, 'add/add.ral')
40
+ // ignore unused private function warnings
41
+ const add = Project.contract('add/add.ral', false)
39
42
  const addDeployTx = await add.transactionForDeployment(signer, {
40
43
  initialFields: { subContractId: subContractId, result: 0 },
41
44
  initialTokenAmounts: []
@@ -54,9 +57,10 @@ describe('events', function () {
54
57
 
55
58
  it('should subscribe contract events', async () => {
56
59
  const provider = new NodeProvider('http://127.0.0.1:22973')
60
+ await Project.build(provider)
57
61
  const signer = await testWallet(provider)
58
62
 
59
- const [contractAddress, contractId] = await deployContract(provider, signer)
63
+ const [contractAddress, contractId] = await deployContract(signer)
60
64
  const events: Array<ContractEvent> = []
61
65
  const subscriptOptions: SubscribeOptions<ContractEvent> = {
62
66
  provider: provider,
@@ -72,7 +76,7 @@ describe('events', function () {
72
76
  }
73
77
  }
74
78
  const subscription = subscribeToEvents(subscriptOptions, contractAddress)
75
- const script = await Script.fromSource(provider, 'main.ral')
79
+ const script = Project.script('main.ral')
76
80
  const scriptTxParams = await script.paramsForDeployment({
77
81
  initialFields: { addContractId: contractId },
78
82
  signerAddress: (await signer.getAccounts())[0].address
@@ -94,9 +98,10 @@ describe('events', function () {
94
98
 
95
99
  it('should cancel event subscription', async () => {
96
100
  const provider = new NodeProvider('http://127.0.0.1:22973')
101
+ await Project.build(provider)
97
102
  const signer = await testWallet(provider)
98
103
 
99
- const [contractAddress, contractId] = await deployContract(provider, signer)
104
+ const [contractAddress, contractId] = await deployContract(signer)
100
105
  const events: Array<ContractEvent> = []
101
106
  const subscriptOptions = {
102
107
  provider: provider,
@@ -112,7 +117,7 @@ describe('events', function () {
112
117
  }
113
118
  }
114
119
  const subscription = subscribeToEvents(subscriptOptions, contractAddress)
115
- const script = await Script.fromSource(provider, 'main.ral')
120
+ const script = Project.script('main.ral')
116
121
  const scriptTx0 = await script.transactionForDeployment(signer, {
117
122
  initialFields: { addContractId: contractId }
118
123
  })
@@ -18,7 +18,7 @@ along with the library. If not, see <http://www.gnu.org/licenses/>.
18
18
 
19
19
  import { NodeProvider } from '../src/api'
20
20
  import { subscribeToTxStatus } from '../src/transaction/status'
21
- import { Contract } from '../src/contract'
21
+ import { Project } from '../src/contract'
22
22
  import { TxStatus } from '../src/api/api-alephium'
23
23
  import { testWallet } from '../src/test'
24
24
  import { SubscribeOptions, timeout } from '../src/utils'
@@ -26,7 +26,8 @@ import { SubscribeOptions, timeout } from '../src/utils'
26
26
  describe('transactions', function () {
27
27
  it('should subscribe transaction status', async () => {
28
28
  const provider = new NodeProvider('http://127.0.0.1:22973')
29
- const sub = await Contract.fromSource(provider, 'sub/sub.ral')
29
+ await Project.build(provider)
30
+ const sub = Project.contract('sub/sub.ral')
30
31
  const signer = await testWallet(provider)
31
32
  const subDeployTx = await sub.transactionForDeployment(signer, {
32
33
  initialFields: { result: 0 },
@@ -67,6 +68,7 @@ describe('transactions', function () {
67
68
  const counterAfterUnsubscribe = counter
68
69
  await timeout(1500)
69
70
  expect(txStatus).toMatchObject({ type: 'Confirmed' })
70
- expect(counterAfterUnsubscribe).toEqual(counter)
71
+ // There maybe a pending request when we unsubscribe
72
+ expect([counter, counter - 1]).toContain(counterAfterUnsubscribe)
71
73
  }, 10000)
72
74
  })