@alephium/web3 0.2.0-test.1 → 0.2.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.
- package/.eslintignore +2 -2
- package/README.md +2 -135
- package/dist/alephium-web3.min.js +1 -1
- package/dist/alephium-web3.min.js.LICENSE.txt +0 -17
- package/dist/alephium-web3.min.js.map +1 -1
- package/dist/src/api/api-alephium.d.ts +115 -17
- package/dist/src/api/api-alephium.js +145 -80
- package/dist/src/api/api-explorer.d.ts +178 -51
- package/dist/src/api/api-explorer.js +172 -37
- package/dist/src/api/index.d.ts +40 -5
- package/dist/src/api/index.js +115 -7
- package/dist/src/api/types.d.ts +23 -0
- package/dist/src/api/types.js +235 -0
- package/dist/src/api/utils.d.ts +6 -0
- package/dist/{scripts/rename-gitignore.js → src/api/utils.js} +11 -6
- package/dist/src/contract/contract.d.ts +68 -55
- package/dist/src/contract/contract.js +235 -384
- package/dist/src/contract/events.d.ts +4 -4
- package/dist/src/contract/events.js +2 -1
- package/dist/src/contract/index.js +5 -1
- package/dist/src/contract/ralph.d.ts +5 -4
- package/dist/src/contract/ralph.js +27 -1
- package/dist/src/global.d.ts +6 -2
- package/dist/src/global.js +19 -3
- package/dist/src/index.d.ts +2 -1
- package/dist/src/index.js +23 -2
- package/dist/src/signer/index.d.ts +0 -1
- package/dist/src/signer/index.js +5 -2
- package/dist/src/signer/signer.d.ts +59 -60
- package/dist/src/signer/signer.js +99 -70
- package/dist/src/transaction/index.d.ts +0 -1
- package/dist/src/transaction/index.js +5 -2
- package/dist/src/transaction/status.d.ts +2 -1
- package/dist/src/transaction/status.js +2 -1
- package/dist/src/utils/bs58.d.ts +1 -0
- package/dist/src/utils/bs58.js +13 -1
- package/dist/src/utils/index.d.ts +0 -1
- package/dist/src/utils/index.js +5 -2
- package/dist/src/utils/subscription.d.ts +0 -2
- package/dist/src/utils/subscription.js +0 -2
- package/dist/src/utils/utils.d.ts +4 -9
- package/dist/src/utils/utils.js +20 -24
- package/jest-config.json +11 -0
- package/package.json +11 -45
- package/src/api/api-alephium.ts +162 -25
- package/src/api/api-explorer.ts +247 -54
- package/src/api/index.ts +140 -6
- package/src/api/types.ts +229 -0
- package/{scripts/rename-gitignore.js → src/api/utils.ts} +7 -6
- package/src/contract/contract.ts +398 -429
- package/src/contract/events.ts +6 -5
- package/src/contract/ralph.ts +29 -4
- package/src/global.ts +23 -3
- package/src/index.ts +7 -1
- package/src/signer/index.ts +0 -1
- package/src/signer/signer.ts +165 -135
- package/src/transaction/index.ts +0 -1
- package/src/transaction/status.ts +5 -2
- package/src/utils/bs58.ts +11 -0
- package/src/utils/index.ts +0 -1
- package/src/utils/subscription.ts +0 -4
- package/src/utils/utils.ts +11 -19
- package/webpack.config.js +3 -0
- package/.eslintrc.json +0 -21
- package/LICENSE +0 -165
- package/contracts/add/add.ral +0 -13
- package/contracts/greeter/greeter.ral +0 -7
- package/contracts/greeter/greeter_interface.ral +0 -4
- package/contracts/greeter_main.ral +0 -7
- package/contracts/main.ral +0 -4
- package/contracts/sub/sub.ral +0 -10
- package/contracts/test/metadata.ral +0 -18
- package/contracts/test/warnings.ral +0 -8
- package/dev/user.conf +0 -29
- package/dist/scripts/create-project.d.ts +0 -2
- package/dist/scripts/create-project.js +0 -125
- package/dist/scripts/rename-gitignore.d.ts +0 -1
- package/dist/scripts/start-devnet.d.ts +0 -1
- package/dist/scripts/start-devnet.js +0 -131
- package/dist/scripts/stop-devnet.d.ts +0 -1
- package/dist/scripts/stop-devnet.js +0 -32
- package/dist/src/signer/node-wallet.d.ts +0 -11
- package/dist/src/signer/node-wallet.js +0 -57
- package/dist/src/test/index.d.ts +0 -6
- package/dist/src/test/index.js +0 -41
- package/dist/src/test/privatekey-wallet.d.ts +0 -11
- package/dist/src/test/privatekey-wallet.js +0 -68
- package/dist/src/transaction/sign-verify.d.ts +0 -2
- package/dist/src/transaction/sign-verify.js +0 -58
- package/dist/src/utils/password-crypto.d.ts +0 -2
- package/dist/src/utils/password-crypto.js +0 -69
- package/gitignore +0 -9
- package/scripts/create-project.ts +0 -137
- package/scripts/start-devnet.js +0 -141
- package/scripts/stop-devnet.js +0 -32
- package/src/contract/ralph.test.ts +0 -178
- package/src/fixtures/address.json +0 -36
- package/src/fixtures/balance.json +0 -9
- package/src/fixtures/self-clique.json +0 -19
- package/src/fixtures/transaction.json +0 -13
- package/src/fixtures/transactions.json +0 -179
- package/src/signer/fixtures/genesis.json +0 -26
- package/src/signer/fixtures/wallets.json +0 -26
- package/src/signer/node-wallet.ts +0 -65
- package/src/test/index.ts +0 -31
- package/src/test/privatekey-wallet.ts +0 -57
- package/src/transaction/sign-verify.test.ts +0 -50
- package/src/transaction/sign-verify.ts +0 -39
- package/src/utils/address.test.ts +0 -47
- package/src/utils/djb2.test.ts +0 -35
- package/src/utils/password-crypto.test.ts +0 -27
- package/src/utils/password-crypto.ts +0 -77
- package/src/utils/utils.test.ts +0 -161
- package/templates/base/README.md +0 -34
- package/templates/base/package.json +0 -35
- package/templates/base/src/greeter.ts +0 -42
- package/templates/base/tsconfig.json +0 -19
- package/templates/react/README.md +0 -34
- package/templates/react/config-overrides.js +0 -18
- package/templates/react/package.json +0 -66
- package/templates/react/src/App.tsx +0 -42
- package/templates/react/src/artifacts/greeter.ral.json +0 -26
- package/templates/react/src/artifacts/greeter_main.ral.json +0 -22
- package/templates/shared/.eslintrc.json +0 -12
- package/templates/shared/scripts/header.js +0 -0
- package/test/contract.test.ts +0 -213
- package/test/events.test.ts +0 -141
- package/test/transaction.test.ts +0 -73
|
@@ -1,42 +0,0 @@
|
|
|
1
|
-
/*
|
|
2
|
-
* greeter.ts
|
|
3
|
-
*/
|
|
4
|
-
|
|
5
|
-
import { TestContractParams, setCurrentNodeProvider, Project } from '@alephium/web3'
|
|
6
|
-
import { testNodeWallet } from '@alephium/web3/test'
|
|
7
|
-
|
|
8
|
-
async function greet() {
|
|
9
|
-
setCurrentNodeProvider('http://127.0.0.1:22973')
|
|
10
|
-
await Project.build()
|
|
11
|
-
|
|
12
|
-
const greeter = Project.contract('greeter/greeter.ral')
|
|
13
|
-
|
|
14
|
-
const testParams: TestContractParams = {
|
|
15
|
-
initialFields: { btcPrice: 1 }
|
|
16
|
-
}
|
|
17
|
-
const testResult = await greeter.testPublicMethod('greet', testParams)
|
|
18
|
-
console.log(testResult)
|
|
19
|
-
|
|
20
|
-
const signer = await testNodeWallet()
|
|
21
|
-
|
|
22
|
-
const deployTx = await greeter.transactionForDeployment(signer, { initialFields: testParams.initialFields })
|
|
23
|
-
const greeterContractId = deployTx.contractId
|
|
24
|
-
console.log(deployTx.fromGroup)
|
|
25
|
-
console.log(deployTx.toGroup)
|
|
26
|
-
|
|
27
|
-
const submitResult = await signer.submitTransaction(deployTx.unsignedTx, deployTx.txId)
|
|
28
|
-
console.log(submitResult)
|
|
29
|
-
|
|
30
|
-
const main = Project.script('greeter_main.ral')
|
|
31
|
-
|
|
32
|
-
const mainScriptTx = await main.transactionForDeployment(signer, {
|
|
33
|
-
initialFields: { greeterContractId: greeterContractId }
|
|
34
|
-
})
|
|
35
|
-
console.log(mainScriptTx.fromGroup)
|
|
36
|
-
console.log(mainScriptTx.toGroup)
|
|
37
|
-
|
|
38
|
-
const mainSubmitResult = await signer.submitTransaction(mainScriptTx.unsignedTx, mainScriptTx.txId)
|
|
39
|
-
console.log(mainSubmitResult)
|
|
40
|
-
}
|
|
41
|
-
|
|
42
|
-
greet()
|
|
@@ -1,19 +0,0 @@
|
|
|
1
|
-
{
|
|
2
|
-
"compilerOptions": {
|
|
3
|
-
"outDir": "dist/",
|
|
4
|
-
"baseUrl": ".",
|
|
5
|
-
"module": "commonjs",
|
|
6
|
-
"target": "ES2015",
|
|
7
|
-
"lib": ["dom", "es2016"],
|
|
8
|
-
"declaration": true,
|
|
9
|
-
"resolveJsonModule": true,
|
|
10
|
-
"skipLibCheck": true,
|
|
11
|
-
"inlineSourceMap": true,
|
|
12
|
-
"inlineSources": true,
|
|
13
|
-
"declarationMap": true,
|
|
14
|
-
"noFallthroughCasesInSwitch": true,
|
|
15
|
-
"strictFunctionTypes": true,
|
|
16
|
-
"esModuleInterop": true
|
|
17
|
-
},
|
|
18
|
-
"exclude": ["node_modules", "templates"]
|
|
19
|
-
}
|
|
@@ -1,34 +0,0 @@
|
|
|
1
|
-
# My dApp
|
|
2
|
-
|
|
3
|
-
## Install
|
|
4
|
-
|
|
5
|
-
```
|
|
6
|
-
npm install
|
|
7
|
-
```
|
|
8
|
-
|
|
9
|
-
## Start the app
|
|
10
|
-
|
|
11
|
-
Compile the TypeScript files into JavaScript:
|
|
12
|
-
|
|
13
|
-
```
|
|
14
|
-
npm run start
|
|
15
|
-
```
|
|
16
|
-
|
|
17
|
-
## Start a devnet
|
|
18
|
-
|
|
19
|
-
```
|
|
20
|
-
npm run start-devnet // this will start a devnet for smart contract tests
|
|
21
|
-
```
|
|
22
|
-
|
|
23
|
-
## Stop/restart devnet
|
|
24
|
-
|
|
25
|
-
```
|
|
26
|
-
npm run stop-devnet
|
|
27
|
-
npm run restart-devnet
|
|
28
|
-
```
|
|
29
|
-
|
|
30
|
-
## Testing
|
|
31
|
-
|
|
32
|
-
```
|
|
33
|
-
npm test
|
|
34
|
-
```
|
|
@@ -1,18 +0,0 @@
|
|
|
1
|
-
const webpack = require('webpack')
|
|
2
|
-
|
|
3
|
-
module.exports = function override(config) {
|
|
4
|
-
const fallback = config.resolve.fallback || {}
|
|
5
|
-
Object.assign(fallback, {
|
|
6
|
-
fs: false,
|
|
7
|
-
crypto: require.resolve('crypto-browserify'),
|
|
8
|
-
stream: require.resolve('stream-browserify')
|
|
9
|
-
})
|
|
10
|
-
config.resolve.fallback = fallback
|
|
11
|
-
config.plugins = (config.plugins || []).concat([
|
|
12
|
-
new webpack.ProvidePlugin({
|
|
13
|
-
process: 'process/browser',
|
|
14
|
-
Buffer: ['buffer', 'Buffer']
|
|
15
|
-
})
|
|
16
|
-
])
|
|
17
|
-
return config
|
|
18
|
-
}
|
|
@@ -1,66 +0,0 @@
|
|
|
1
|
-
{
|
|
2
|
-
"name": "my-dapp",
|
|
3
|
-
"version": "0.1.0",
|
|
4
|
-
"config": {
|
|
5
|
-
"alephium_version": "1.5.0-rc6"
|
|
6
|
-
},
|
|
7
|
-
"dependencies": {
|
|
8
|
-
"@testing-library/jest-dom": "^5.16.4",
|
|
9
|
-
"@testing-library/react": "^13.0.1",
|
|
10
|
-
"@testing-library/user-event": "^13.5.0",
|
|
11
|
-
"@types/jest": "^27.4.1",
|
|
12
|
-
"@types/node": "^16.11.26",
|
|
13
|
-
"@types/react": "^18.0.3",
|
|
14
|
-
"@types/react-dom": "^18.0.0",
|
|
15
|
-
"@alephium/web3": "0.2.0-rc.7",
|
|
16
|
-
"react": "^18.0.0",
|
|
17
|
-
"react-dom": "^18.0.0",
|
|
18
|
-
"react-scripts": "5.0.1",
|
|
19
|
-
"typescript": "^4.6.3",
|
|
20
|
-
"web-vitals": "^2.1.4"
|
|
21
|
-
},
|
|
22
|
-
"scripts": {
|
|
23
|
-
"start": "react-app-rewired start",
|
|
24
|
-
"build": "react-app-rewired build",
|
|
25
|
-
"test": "react-app-rewired test",
|
|
26
|
-
"eject": "react-scripts eject",
|
|
27
|
-
"lint": "eslint . --ext ts",
|
|
28
|
-
"prettier": "prettier --write \"**/*.{js,jsx,ts,tsx,json,md}\""
|
|
29
|
-
},
|
|
30
|
-
"eslintConfig": {
|
|
31
|
-
"extends": [
|
|
32
|
-
"react-app",
|
|
33
|
-
"react-app/jest"
|
|
34
|
-
]
|
|
35
|
-
},
|
|
36
|
-
"browserslist": {
|
|
37
|
-
"production": [
|
|
38
|
-
">0.2%",
|
|
39
|
-
"not dead",
|
|
40
|
-
"not op_mini all"
|
|
41
|
-
],
|
|
42
|
-
"development": [
|
|
43
|
-
"last 1 chrome version",
|
|
44
|
-
"last 1 firefox version",
|
|
45
|
-
"last 1 safari version"
|
|
46
|
-
]
|
|
47
|
-
},
|
|
48
|
-
"devDependencies": {
|
|
49
|
-
"buffer": "^6.0.3",
|
|
50
|
-
"crypto-browserify": "^3.12.0",
|
|
51
|
-
"eslint-config-prettier": "^8.5.0",
|
|
52
|
-
"process": "^0.11.10",
|
|
53
|
-
"react-app-rewired": "^2.2.1",
|
|
54
|
-
"stream-browserify": "^3.0.0"
|
|
55
|
-
},
|
|
56
|
-
"prettier": {
|
|
57
|
-
"printWidth": 120,
|
|
58
|
-
"tabWidth": 2,
|
|
59
|
-
"useTabs": false,
|
|
60
|
-
"semi": false,
|
|
61
|
-
"singleQuote": true,
|
|
62
|
-
"jsxSingleQuote": true,
|
|
63
|
-
"bracketSameLine": false,
|
|
64
|
-
"trailingComma": "none"
|
|
65
|
-
}
|
|
66
|
-
}
|
|
@@ -1,42 +0,0 @@
|
|
|
1
|
-
import React, { useEffect, useState } from 'react'
|
|
2
|
-
import './App.css'
|
|
3
|
-
|
|
4
|
-
import { ExplorerProvider, 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 ExplorerProvider('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
|
-
}
|
|
File without changes
|
package/test/contract.test.ts
DELETED
|
@@ -1,213 +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
|
-
import { setCurrentNodeProvider } from '../src'
|
|
22
|
-
import { Contract, Project, Script, TestContractParams } from '../src/contract'
|
|
23
|
-
import { testNodeWallet } from '../src/test'
|
|
24
|
-
import { addressFromContractId } from '../src/utils'
|
|
25
|
-
|
|
26
|
-
describe('contract', function () {
|
|
27
|
-
async function testSuite1() {
|
|
28
|
-
setCurrentNodeProvider('http://127.0.0.1:22973')
|
|
29
|
-
await Project.build()
|
|
30
|
-
|
|
31
|
-
// ignore unused private function warnings
|
|
32
|
-
const add = Project.contract('add/add.ral', { errorOnWarnings: false })
|
|
33
|
-
const sub = Project.contract('sub/sub.ral')
|
|
34
|
-
|
|
35
|
-
const subState = sub.toState({ result: 0 }, { alphAmount: BigInt('1000000000000000000') })
|
|
36
|
-
const testParams: TestContractParams = {
|
|
37
|
-
initialFields: { sub: subState.contractId, result: 0 },
|
|
38
|
-
testArgs: { array: [2, 1] },
|
|
39
|
-
existingContracts: [subState]
|
|
40
|
-
}
|
|
41
|
-
const testResult = await add.testPublicMethod('add', testParams)
|
|
42
|
-
expect(testResult.returns).toEqual([[3, 1]])
|
|
43
|
-
expect(testResult.contracts[0].codeHash).toEqual(sub.codeHash)
|
|
44
|
-
expect(testResult.contracts[0].fields.result).toEqual(1)
|
|
45
|
-
expect(testResult.contracts[1].codeHash).toEqual(add.codeHash)
|
|
46
|
-
expect(testResult.contracts[1].fields.sub).toEqual(subState.contractId)
|
|
47
|
-
expect(testResult.contracts[1].fields.result).toEqual(3)
|
|
48
|
-
const events = testResult.events.sort((a, b) => a.name.localeCompare(b.name))
|
|
49
|
-
expect(events[0].name).toEqual('Add')
|
|
50
|
-
expect(events[0].fields.x).toEqual(2)
|
|
51
|
-
expect(events[0].fields.y).toEqual(1)
|
|
52
|
-
expect(events[1].name).toEqual('Sub')
|
|
53
|
-
expect(events[1].fields.x).toEqual(2)
|
|
54
|
-
expect(events[1].fields.y).toEqual(1)
|
|
55
|
-
|
|
56
|
-
const testResultPrivate = await add.testPrivateMethod('addPrivate', testParams)
|
|
57
|
-
expect(testResultPrivate.returns).toEqual([[3, 1]])
|
|
58
|
-
|
|
59
|
-
const signer = await testNodeWallet()
|
|
60
|
-
|
|
61
|
-
const subDeployTx = await sub.transactionForDeployment(signer, {
|
|
62
|
-
initialFields: { result: 0 },
|
|
63
|
-
initialTokenAmounts: []
|
|
64
|
-
})
|
|
65
|
-
const subContractId = subDeployTx.contractId
|
|
66
|
-
const subContractAddress = addressFromContractId(subContractId)
|
|
67
|
-
expect(subDeployTx.fromGroup).toEqual(0)
|
|
68
|
-
expect(subDeployTx.toGroup).toEqual(0)
|
|
69
|
-
const subSubmitResult = await signer.submitTransaction(subDeployTx.unsignedTx, subDeployTx.txId)
|
|
70
|
-
expect(subSubmitResult.fromGroup).toEqual(0)
|
|
71
|
-
expect(subSubmitResult.toGroup).toEqual(0)
|
|
72
|
-
expect(subSubmitResult.txId).toEqual(subDeployTx.txId)
|
|
73
|
-
|
|
74
|
-
const addDeployTx = await add.transactionForDeployment(signer, {
|
|
75
|
-
initialFields: { sub: subContractId, result: 0 },
|
|
76
|
-
initialTokenAmounts: []
|
|
77
|
-
})
|
|
78
|
-
expect(addDeployTx.fromGroup).toEqual(0)
|
|
79
|
-
expect(addDeployTx.toGroup).toEqual(0)
|
|
80
|
-
const addSubmitResult = await signer.submitTransaction(addDeployTx.unsignedTx, addDeployTx.txId)
|
|
81
|
-
expect(addSubmitResult.fromGroup).toEqual(0)
|
|
82
|
-
expect(addSubmitResult.toGroup).toEqual(0)
|
|
83
|
-
expect(addSubmitResult.txId).toEqual(addDeployTx.txId)
|
|
84
|
-
|
|
85
|
-
const addContractId = addDeployTx.contractId
|
|
86
|
-
const addContractAddress = addressFromContractId(addContractId)
|
|
87
|
-
|
|
88
|
-
// Check state for add/sub before main script is executed
|
|
89
|
-
let fetchedSubState = await sub.fetchState(subContractAddress, 0)
|
|
90
|
-
expect(fetchedSubState.fields.result).toEqual(0)
|
|
91
|
-
let fetchedAddState = await add.fetchState(addContractAddress, 0)
|
|
92
|
-
expect(fetchedAddState.fields.sub).toEqual(subContractId)
|
|
93
|
-
expect(fetchedAddState.fields.result).toEqual(0)
|
|
94
|
-
|
|
95
|
-
const main = Project.script('main.ral')
|
|
96
|
-
const mainScriptTx = await main.transactionForDeployment(signer, {
|
|
97
|
-
initialFields: { addContractId: addContractId }
|
|
98
|
-
})
|
|
99
|
-
expect(mainScriptTx.fromGroup).toEqual(0)
|
|
100
|
-
expect(mainScriptTx.toGroup).toEqual(0)
|
|
101
|
-
const mainSubmitResult = await signer.submitTransaction(mainScriptTx.unsignedTx, mainScriptTx.txId)
|
|
102
|
-
expect(mainSubmitResult.fromGroup).toEqual(0)
|
|
103
|
-
expect(mainSubmitResult.toGroup).toEqual(0)
|
|
104
|
-
|
|
105
|
-
// Check state for add/sub after main script is executed
|
|
106
|
-
fetchedSubState = await sub.fetchState(subContractAddress, 0)
|
|
107
|
-
expect(fetchedSubState.fields.result).toEqual(1)
|
|
108
|
-
fetchedAddState = await add.fetchState(addContractAddress, 0)
|
|
109
|
-
expect(fetchedAddState.fields.sub).toEqual(subContractId)
|
|
110
|
-
expect(fetchedAddState.fields.result).toEqual(3)
|
|
111
|
-
}
|
|
112
|
-
|
|
113
|
-
async function testSuite2() {
|
|
114
|
-
setCurrentNodeProvider('http://127.0.0.1:22973')
|
|
115
|
-
await Project.build()
|
|
116
|
-
|
|
117
|
-
const greeter = Project.contract('greeter/greeter.ral')
|
|
118
|
-
|
|
119
|
-
const testParams: TestContractParams = {
|
|
120
|
-
initialFields: { btcPrice: 1 }
|
|
121
|
-
}
|
|
122
|
-
const testResult = await greeter.testPublicMethod('greet', testParams)
|
|
123
|
-
expect(testResult.returns).toEqual([1])
|
|
124
|
-
expect(testResult.contracts[0].codeHash).toEqual(greeter.codeHash)
|
|
125
|
-
expect(testResult.contracts[0].fields.btcPrice).toEqual(1)
|
|
126
|
-
|
|
127
|
-
const signer = await testNodeWallet()
|
|
128
|
-
|
|
129
|
-
const deployTx = await greeter.transactionForDeployment(signer, {
|
|
130
|
-
initialFields: { btcPrice: 1 },
|
|
131
|
-
initialTokenAmounts: []
|
|
132
|
-
})
|
|
133
|
-
expect(deployTx.fromGroup).toEqual(0)
|
|
134
|
-
expect(deployTx.toGroup).toEqual(0)
|
|
135
|
-
const submitResult = await signer.submitTransaction(deployTx.unsignedTx, deployTx.txId)
|
|
136
|
-
expect(submitResult.fromGroup).toEqual(0)
|
|
137
|
-
expect(submitResult.toGroup).toEqual(0)
|
|
138
|
-
expect(submitResult.txId).toEqual(deployTx.txId)
|
|
139
|
-
|
|
140
|
-
const greeterContractId = deployTx.contractId
|
|
141
|
-
const main = Project.script('greeter_main.ral')
|
|
142
|
-
|
|
143
|
-
const mainScriptTx = await main.transactionForDeployment(signer, {
|
|
144
|
-
initialFields: { greeterContractId: greeterContractId }
|
|
145
|
-
})
|
|
146
|
-
expect(mainScriptTx.fromGroup).toEqual(0)
|
|
147
|
-
expect(mainScriptTx.toGroup).toEqual(0)
|
|
148
|
-
const mainSubmitResult = await signer.submitTransaction(mainScriptTx.unsignedTx, mainScriptTx.txId)
|
|
149
|
-
expect(mainSubmitResult.fromGroup).toEqual(0)
|
|
150
|
-
expect(mainSubmitResult.toGroup).toEqual(0)
|
|
151
|
-
}
|
|
152
|
-
|
|
153
|
-
it('should test contracts', async () => {
|
|
154
|
-
await testSuite1()
|
|
155
|
-
await testSuite2()
|
|
156
|
-
})
|
|
157
|
-
|
|
158
|
-
function loadJson(fileName: string) {
|
|
159
|
-
const filePath = path.resolve(process.cwd() + path.sep + fileName)
|
|
160
|
-
const rawData = fs.readFileSync(filePath).toString()
|
|
161
|
-
return JSON.parse(rawData)
|
|
162
|
-
}
|
|
163
|
-
|
|
164
|
-
function loadContract(fileName: string) {
|
|
165
|
-
Contract.fromJson(loadJson(fileName))
|
|
166
|
-
}
|
|
167
|
-
|
|
168
|
-
function loadScript(fileName: string) {
|
|
169
|
-
Script.fromJson(loadJson(fileName))
|
|
170
|
-
}
|
|
171
|
-
|
|
172
|
-
it('should load source files by order', async () => {
|
|
173
|
-
const sourceFiles = await Project['loadSourceFiles']('./contracts') // `loadSourceFiles` is a private method
|
|
174
|
-
expect(sourceFiles.length).toEqual(8)
|
|
175
|
-
sourceFiles.slice(0, 5).forEach((c) => expect(c.type).toEqual(0)) // contracts
|
|
176
|
-
sourceFiles.slice(5, 7).forEach((s) => expect(s.type).toEqual(1)) // scripts
|
|
177
|
-
sourceFiles.slice(7).forEach((i) => expect(i.type).toEqual(3)) // interfaces
|
|
178
|
-
})
|
|
179
|
-
|
|
180
|
-
it('should load contract from json', async () => {
|
|
181
|
-
loadContract('./artifacts/add/add.ral.json')
|
|
182
|
-
loadContract('./artifacts/sub/sub.ral.json')
|
|
183
|
-
loadScript('./artifacts/main.ral.json')
|
|
184
|
-
|
|
185
|
-
loadContract('./artifacts/greeter/greeter.ral.json')
|
|
186
|
-
loadScript('./artifacts/greeter_main.ral.json')
|
|
187
|
-
})
|
|
188
|
-
|
|
189
|
-
it('should extract metadata of contracts', async () => {
|
|
190
|
-
setCurrentNodeProvider('http://127.0.0.1:22973')
|
|
191
|
-
await Project.build()
|
|
192
|
-
|
|
193
|
-
const contract = Project.contract('test/metadata.ral', { errorOnWarnings: false })
|
|
194
|
-
expect(contract.functions.map((func) => func.name)).toEqual(['foo', 'bar', 'baz'])
|
|
195
|
-
expect(contract.publicFunctions()).toEqual(['foo'])
|
|
196
|
-
expect(contract.usingPreapprovedAssetsFunctions()).toEqual(['foo'])
|
|
197
|
-
expect(contract.usingAssetsInContractFunctions()).toEqual(['bar'])
|
|
198
|
-
})
|
|
199
|
-
|
|
200
|
-
it('should handle compiler warnings', async () => {
|
|
201
|
-
setCurrentNodeProvider('http://127.0.0.1:22973')
|
|
202
|
-
await Project.build()
|
|
203
|
-
const contract = Project.contract('test/warnings.ral', { errorOnWarnings: false })
|
|
204
|
-
expect(contract.publicFunctions()).toEqual(['foo'])
|
|
205
|
-
|
|
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'
|
|
208
|
-
)
|
|
209
|
-
expect(() => Project.contract('test/warnings.ral', { ignoreUnusedConstantsWarnings: 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'
|
|
211
|
-
)
|
|
212
|
-
})
|
|
213
|
-
})
|
package/test/events.test.ts
DELETED
|
@@ -1,141 +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 { subscribeToEvents } from '../src/contract/events'
|
|
20
|
-
import { Project } from '../src/contract'
|
|
21
|
-
import { NodeWallet, SignExecuteScriptTxParams } from '../src/signer'
|
|
22
|
-
import { ContractEvent } from '../src/api/api-alephium'
|
|
23
|
-
import { testNodeWallet } from '../src/test'
|
|
24
|
-
import { SubscribeOptions, timeout } from '../src/utils'
|
|
25
|
-
import { setCurrentNodeProvider } from '../src'
|
|
26
|
-
|
|
27
|
-
describe('events', function () {
|
|
28
|
-
async function deployContract(signer: NodeWallet): Promise<[string, string]> {
|
|
29
|
-
setCurrentNodeProvider('http://127.0.0.1:22973')
|
|
30
|
-
await Project.build()
|
|
31
|
-
const sub = Project.contract('sub/sub.ral')
|
|
32
|
-
const subDeployTx = await sub.transactionForDeployment(signer, {
|
|
33
|
-
initialFields: { result: 0 },
|
|
34
|
-
initialTokenAmounts: []
|
|
35
|
-
})
|
|
36
|
-
const subContractId = subDeployTx.contractId
|
|
37
|
-
const subSubmitResult = await signer.submitTransaction(subDeployTx.unsignedTx, subDeployTx.txId)
|
|
38
|
-
expect(subSubmitResult.txId).toEqual(subDeployTx.txId)
|
|
39
|
-
|
|
40
|
-
// ignore unused private function warnings
|
|
41
|
-
const add = Project.contract('add/add.ral', { errorOnWarnings: false })
|
|
42
|
-
const addDeployTx = await add.transactionForDeployment(signer, {
|
|
43
|
-
initialFields: { sub: subContractId, result: 0 },
|
|
44
|
-
initialTokenAmounts: []
|
|
45
|
-
})
|
|
46
|
-
const addSubmitResult = await signer.submitTransaction(addDeployTx.unsignedTx, addDeployTx.txId)
|
|
47
|
-
expect(addSubmitResult.txId).toEqual(addDeployTx.txId)
|
|
48
|
-
return [addDeployTx.contractAddress, addDeployTx.contractId]
|
|
49
|
-
}
|
|
50
|
-
|
|
51
|
-
async function executeScript(params: SignExecuteScriptTxParams, signer: NodeWallet, times: number) {
|
|
52
|
-
for (let i = 0; i < times; i++) {
|
|
53
|
-
const scriptTx = await signer.buildScriptTx(params)
|
|
54
|
-
await signer.submitTransaction(scriptTx.unsignedTx, scriptTx.txId)
|
|
55
|
-
}
|
|
56
|
-
}
|
|
57
|
-
|
|
58
|
-
it('should subscribe contract events', async () => {
|
|
59
|
-
setCurrentNodeProvider('http://127.0.0.1:22973')
|
|
60
|
-
await Project.build()
|
|
61
|
-
const signer = await testNodeWallet()
|
|
62
|
-
|
|
63
|
-
const [contractAddress, contractId] = await deployContract(signer)
|
|
64
|
-
const events: Array<ContractEvent> = []
|
|
65
|
-
const subscriptOptions: SubscribeOptions<ContractEvent> = {
|
|
66
|
-
pollingInterval: 500,
|
|
67
|
-
messageCallback: (event: ContractEvent): Promise<void> => {
|
|
68
|
-
events.push(event)
|
|
69
|
-
return Promise.resolve()
|
|
70
|
-
},
|
|
71
|
-
errorCallback: (error: any, subscription): Promise<void> => {
|
|
72
|
-
console.log(error)
|
|
73
|
-
subscription.unsubscribe()
|
|
74
|
-
return Promise.resolve()
|
|
75
|
-
}
|
|
76
|
-
}
|
|
77
|
-
const subscription = subscribeToEvents(subscriptOptions, contractAddress)
|
|
78
|
-
const script = Project.script('main.ral')
|
|
79
|
-
const scriptTxParams = await script.paramsForDeployment({
|
|
80
|
-
initialFields: { addContractId: contractId },
|
|
81
|
-
signerAddress: (await signer.getAccounts())[0].address
|
|
82
|
-
})
|
|
83
|
-
await executeScript(scriptTxParams, signer, 3)
|
|
84
|
-
await timeout(3000)
|
|
85
|
-
|
|
86
|
-
expect(events.length).toEqual(3)
|
|
87
|
-
events.forEach((event) => {
|
|
88
|
-
expect(event.fields).toEqual([
|
|
89
|
-
{ type: 'U256', value: '2' },
|
|
90
|
-
{ type: 'U256', value: '1' }
|
|
91
|
-
])
|
|
92
|
-
})
|
|
93
|
-
expect(subscription.currentEventCount()).toEqual(events.length)
|
|
94
|
-
|
|
95
|
-
subscription.unsubscribe()
|
|
96
|
-
}, 15000)
|
|
97
|
-
|
|
98
|
-
it('should cancel event subscription', async () => {
|
|
99
|
-
setCurrentNodeProvider('http://127.0.0.1:22973')
|
|
100
|
-
await Project.build()
|
|
101
|
-
const signer = await testNodeWallet()
|
|
102
|
-
|
|
103
|
-
const [contractAddress, contractId] = await deployContract(signer)
|
|
104
|
-
const events: Array<ContractEvent> = []
|
|
105
|
-
const subscriptOptions = {
|
|
106
|
-
pollingInterval: 500,
|
|
107
|
-
messageCallback: (event: ContractEvent): Promise<void> => {
|
|
108
|
-
events.push(event)
|
|
109
|
-
return Promise.resolve()
|
|
110
|
-
},
|
|
111
|
-
errorCallback: (error: any, subscription): Promise<void> => {
|
|
112
|
-
console.log(error)
|
|
113
|
-
subscription.unsubscribe()
|
|
114
|
-
return Promise.resolve()
|
|
115
|
-
}
|
|
116
|
-
}
|
|
117
|
-
const subscription = subscribeToEvents(subscriptOptions, contractAddress)
|
|
118
|
-
const script = Project.script('main.ral')
|
|
119
|
-
const scriptTx0 = await script.transactionForDeployment(signer, {
|
|
120
|
-
initialFields: { addContractId: contractId }
|
|
121
|
-
})
|
|
122
|
-
await signer.submitTransaction(scriptTx0.unsignedTx, scriptTx0.txId)
|
|
123
|
-
await timeout(1500)
|
|
124
|
-
subscription.unsubscribe()
|
|
125
|
-
|
|
126
|
-
expect(events.length).toEqual(1)
|
|
127
|
-
expect(events[0].txId).toEqual(scriptTx0.txId)
|
|
128
|
-
expect(events[0].fields).toEqual([
|
|
129
|
-
{ type: 'U256', value: '2' },
|
|
130
|
-
{ type: 'U256', value: '1' }
|
|
131
|
-
])
|
|
132
|
-
expect(subscription.currentEventCount()).toEqual(events.length)
|
|
133
|
-
|
|
134
|
-
const scriptTx1 = await script.transactionForDeployment(signer, {
|
|
135
|
-
initialFields: { addContractId: contractId }
|
|
136
|
-
})
|
|
137
|
-
await signer.submitTransaction(scriptTx1.unsignedTx, scriptTx1.txId)
|
|
138
|
-
await timeout(1500)
|
|
139
|
-
expect(events.length).toEqual(1)
|
|
140
|
-
})
|
|
141
|
-
})
|