Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Websockets on the http rest port #436

Draft
wants to merge 5 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion docker/devnet.conf
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@ alephium.node.event-log.index-by-tx-id = true
alephium.node.event-log.index-by-block-hash = true

alephium.network.rest-port = 22973
alephium.network.ws-port = 21973
alephium.network.miner-api-port = 20973
alephium.api.network-interface = "0.0.0.0"
alephium.api.api-key-enabled = false
Expand Down
1 change: 0 additions & 1 deletion docker/docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@ services:
- 19973:19973/tcp
- 19973:19973/udp
- 127.0.0.1:20973:20973
- 127.0.0.1:21973:21973
- 127.0.0.1:22973:22973
environment:
- ALEPHIUM_LOG_LEVEL=DEBUG
Expand Down
4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,14 @@
"@alephium/cli": "workspace:*",
"@alephium/web3": "workspace:*",
"@alephium/web3-test": "workspace:*",
"@alephium/web3-wallet": "workspace:*"
"@alephium/web3-wallet": "workspace:*",
"ws": "^8.5.4"
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The ws deps should be in web3 however can't make it work this way, js newbie

},
"devDependencies": {
"@swc/core": "^1.4.1",
"@types/jest": "^28.1.8",
"@types/node": "^16.18.23",
"@types/ws": "^8.5.4",
"bip39": "3.0.4",
"eslint": "^8.37.0",
"eslint-plugin-jest": "^27.2.1",
Expand Down
30 changes: 14 additions & 16 deletions packages/web3/src/api/api-alephium.ts
Original file line number Diff line number Diff line change
Expand Up @@ -910,8 +910,6 @@ export interface PeerAddress {
/** @format int32 */
restPort: number
/** @format int32 */
wsPort: number
/** @format int32 */
minerApiPort: number
}

Expand Down Expand Up @@ -1546,8 +1544,8 @@ export class HttpClient<SecurityDataType = unknown> {
property instanceof Blob
? property
: typeof property === 'object' && property !== null
? JSON.stringify(property)
: `${property}`
? JSON.stringify(property)
: `${property}`
)
return formData
}, new FormData()),
Expand Down Expand Up @@ -1627,18 +1625,18 @@ export class HttpClient<SecurityDataType = unknown> {
const data = !responseFormat
? r
: await response[responseFormat]()
.then((data) => {
if (r.ok) {
r.data = data
} else {
r.error = data
}
return r
})
.catch((e) => {
r.error = e
return r
})
.then((data) => {
if (r.ok) {
r.data = data
} else {
r.error = data
}
return r
})
.catch((e) => {
r.error = e
return r
})

if (cancelToken) {
this.abortControllers.delete(cancelToken)
Expand Down
3 changes: 1 addition & 2 deletions packages/web3/src/fixtures/self-clique.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
{
"address": "127.0.0.1",
"restPort": 12973,
"wsPort": 11973,
"minerApiPort": 10973
}
],
Expand All @@ -16,4 +15,4 @@
"groupNumPerBroker": 4,
"groups": 4
}
}
}
76 changes: 76 additions & 0 deletions test/websocket.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
/*
Copyright 2018 - 2022 The Alephium Authors
This file is part of the alephium project.

The library is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

The library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Lesser General Public License for more details.

You should have received a copy of the GNU Lesser General Public License
along with the library. If not, see <http://www.gnu.org/licenses/>.
*/

import WebSocket from 'ws';
import { DEFAULT_GAS_ALPH_AMOUNT, ONE_ALPH, NodeProvider, sleep, web3 } from '@alephium/web3';
import { getSigner } from '@alephium/web3-test';

const WS_ENDPOINT = 'ws://127.0.0.1:22973/ws';

describe('WebSocket and Transaction Test', () => {
let signer;

beforeAll(async () => {
web3.setCurrentNodeProvider('http://127.0.0.1:22973', undefined, fetch);
signer = await getSigner();
});

it('should connect, subscribe, and receive block event after a transaction is submitted', async () => {
const ws = new WebSocket(WS_ENDPOINT);

await new Promise<void>((resolve, reject) => {
ws.on('open', async () => {
try {
ws.send('subscribe:block');

const address = (await signer.getSelectedAccount()).address;
const fee = 0.01;
const attoAlphAmount = ONE_ALPH;

await signer.signAndSubmitTransferTx({
signerAddress: address,
destinations: [{ address, attoAlphAmount }],
fee,
});
} catch (error) {
console.error('Error during transaction submission:', error);
reject(error);
}
});

ws.on('message', (data) => {
try {
console.log('Received:', data.toString());
const parsedData = JSON.parse(data.toString());
expect(parsedData).toBeDefined();
expect(parsedData.method).toBe('block');
resolve();
} catch (error) {
reject(error);
} finally {
ws.close();
}
});

ws.on('error', (error) => {
console.error('WebSocket error:', error);
reject(error);
});
});
});
});
Loading