Skip to content

Commit

Permalink
feat: implement network switching (#8)
Browse files Browse the repository at this point in the history
* feat: add mainnet as option on sdk

* feat: add mainnet switch on demo app

* chore: add changeset

* fix: build warnings

* chore: update gitignore with test-related patterns

* feat(simple-app): add E2E testing setup with Vitest

* ci: improve GitHub Actions workflows for testing

* chore: update test scripts and turbo pipeline

* chore: add more tests

* ci: fix e2e action

* ci: fix package json deps

* ci: pnpm lock

* chore: add style for toast

* fix: network switching

* fix: code examples

* ci: fix changeset validation

* ci: disable some checks
  • Loading branch information
pedronauck authored Nov 26, 2024
1 parent e9a6522 commit 81a4fa0
Show file tree
Hide file tree
Showing 48 changed files with 2,998 additions and 2,767 deletions.
60 changes: 60 additions & 0 deletions .changeset/eighty-pants-cover.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
---
"@fuels/streams": minor
"simple-app": minor
---

Improve Stream API and enhance demo application UI

- **API Changes**:
- Rename `subscribeWithSubject` to `subscribe` for better API clarity
- Add `subscribeWithString` method for string-based subscriptions
- Remove namespace handling from stream creation
- Improve consumer subscription handling and configuration
- Update return type for subscriptions to use `ConsumerMessages`
- Add network selection during client initialization (`{ network: 'testnet' | 'mainnet' }`)
- Add `switchNetwork` method for dynamic network switching
- Remove `ClientOpts` requirement for simpler initialization

### Client Initialization

```typescript
// Before
const opts = new ClientOpts();
const client = await Client.connect(opts);

// After
const client = await Client.connect({ network: "testnet" }); // or 'mainnet'
```

### Stream Subscription

```typescript
// Before
const stream = await BlocksStream.init(client);
const subscription = await stream.subscribeWithSubject(subject);

// After - Using typed subjects
const stream = await BlocksStream.init(client);
const subscription = await stream.subscribe(BlocksSubject.build());

// After - Using string subjects
const subscription = await stream.subscribeWithString("my-subject");
```

### Network Switching

```typescript
const client = Client.getInstance();
await client.switchNetwork("testnet");
```

- **Demo App Improvements**:
- Add network switching support (mainnet/testnet)
- Implement new header with navigation and network controls
- Add toast notifications for network changes
- Improve UI layout and responsiveness
- Add loading states and better error handling
- Enhance code examples visualization
- Add E2E testing setup with Vitest
- Improve CI workflows for better test handling
- Update build and test scripts in Turbo pipeline
45 changes: 45 additions & 0 deletions .github/workflows/pr-tests.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
name: Tests

on:
pull_request:
branches: [main]
types: [opened, synchronize, reopened]
push:
branches:
- main

concurrency:
group: ${{ github.workflow }}-${{ github.event.pull_request.number || github.ref }}
cancel-in-progress: true

env:
NODE_VERSION: 20.11.0
PNPM_VERSION: 9.5.0

jobs:
test:
name: Tests
runs-on: buildjet-4vcpu-ubuntu-2204
timeout-minutes: 15

steps:
- name: Checkout
uses: actions/checkout@v4

- name: Setup Node
uses: FuelLabs/github-actions/setups/node@master
with:
node-version: ${{ env.NODE_VERSION }}
pnpm-version: ${{ env.PNPM_VERSION }}

- name: Build packages
run: pnpm build:libs

- name: Run unit tests
run: pnpm test

- name: Install Playwright Browsers
run: pnpm exec playwright install --with-deps chromium

- name: Run E2E tests
run: pnpm test:e2e
53 changes: 0 additions & 53 deletions .github/workflows/pr-tests.yml

This file was deleted.

84 changes: 38 additions & 46 deletions .github/workflows/pr.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -21,54 +21,43 @@ jobs:
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}

check-packages-changed:
name: Assign project changed
runs-on: ubuntu-latest
outputs:
changed: ${{ steps.packages-changed.outputs.any_changed }}
steps:
- uses: actions/checkout@v4
- name: Check external packages changes
id: packages-changed
uses: tj-actions/changed-files@v22.2
with:
files: |
**/packages/fuel-streams/**
validate-changeset:
name: Validate PR Changeset
needs: check-packages-changed
if: ${{ github.head_ref != 'changeset-release/main' && needs.check-packages-changed.outputs.changed == 'true' }}
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v4
with:
fetch-depth: 0

- name: Setup Node
uses: FuelLabs/github-actions/setups/node@master
with:
node-version: ${{ env.NODE_VERSION }}
pnpm-version: ${{ env.PNPM_VERSION }}

- name: Validate Changeset
run: pnpm changeset status --since=origin/${{ github.base_ref }}

audit:
name: Audit
runs-on: ubuntu-latest
if: false
steps:
- uses: actions/checkout@v4
- uses: FuelLabs/github-actions/setups/node@master
with:
node-version: ${{ env.NODE_VERSION }}
pnpm-version: ${{ env.PNPM_VERSION }}
- run: pnpm audit --prod
# check-packages-changed:
# name: Assign project changed
# runs-on: ubuntu-latest
# outputs:
# changed: ${{ steps.packages-changed.outputs.any_changed }}
# steps:
# - uses: actions/checkout@v4
# - name: Check external packages changes
# id: packages-changed
# uses: tj-actions/changed-files@v22.2
# with:
# files: |
# **/packages/fuel-streams/**
#
# validate-changeset:
# name: Validate PR Changeset
# needs: check-packages-changed
# # if: ${{ github.head_ref != 'changeset-release/main' && needs.check-packages-changed.outputs.changed == 'true' }}
# if: false
# runs-on: ubuntu-latest
# steps:
# - name: Checkout
# uses: actions/checkout@v4
# with:
# fetch-depth: 0
#
# - name: Setup Node
# uses: FuelLabs/github-actions/setups/node@master
# with:
# node-version: ${{ env.NODE_VERSION }}
# pnpm-version: ${{ env.PNPM_VERSION }}
#
# - name: Validate Changeset
# run: pnpm changeset status --since=origin/${{ github.base_ref }}

lint:
name: Lint
name: Lint & Type Check
runs-on: ubuntu-latest
permissions:
checks: write
Expand All @@ -84,6 +73,9 @@ jobs:
node-version: ${{ env.NODE_VERSION }}
pnpm-version: ${{ env.PNPM_VERSION }}

- name: Build packages
run: pnpm build:libs

- name: Run lint & ts:check
run: |
pnpm lint:ci
Expand Down
17 changes: 4 additions & 13 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ dist

# Coverage
coverage
*.lcov
.nyc_output

# Debug
npm-debug.log*
Expand All @@ -25,12 +27,13 @@ yarn-error.log*

# Test
/test-results/
/playwright-report/
playwright-report
/blob-report/
/playwright/.cache/
/e2e-tests/playwright-report
.env
/e2e-tests/test-results
.vitest-result

# Logs
logs
Expand All @@ -45,16 +48,4 @@ lerna-debug.log*
node_modules
dist
dist-ssr
*.local

# Editor directories and files
.vscode/*
!.vscode/extensions.json
.idea
.DS_Store
*.suo
*.ntvs*
*.njsproj
*.sln
*.sw?

48 changes: 33 additions & 15 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,28 +17,46 @@
"lint:ci": "biome lint --max-diagnostics=1000 --diagnostic-level=error .",
"packages:version": "node ./scripts/version.js",
"prepare": "husky",
"test": "vitest",
"test:ci": "turbo run test -- --run --coverage.enabled --coverage.reporter=json-summary --coverage.reporter=json",
"ts:check": "turbo run ts:check"
"test": "turbo test",
"test:e2e": "turbo test:e2e",
"test:coverage": "turbo test:coverage",
"test:ci": "turbo run test:coverage -- --coverage.enabled --coverage.reporter=json-summary --coverage.reporter=json",
"ts:check": "turbo run ts:check",
"deps:update": "updates -gu && pnpm -r exec updates -gu"
},
"devDependencies": {
"@biomejs/biome": "1.7.3",
"@biomejs/biome": "1.9.4",
"@changesets/changelog-github": "0.5.0",
"@changesets/cli": "2.27.1",
"@changesets/cli": "2.26.2",
"@fuels/ts-config": "0.25.1",
"@fuels/tsup-config": "0.25.1",
"@types/node": "^22.5.5",
"@playwright/experimental-ct-react": "1.49.0",
"@playwright/test": "1.49.0",
"@types/mocha": "10.0.10",
"@types/node": "^22.10.0",
"@types/react": "^18.3.12",
"@types/react-dom": "^18.3.1",
"@types/react-syntax-highlighter": "15.5.13",
"@types/voca": "1.4.6",
"@vitest/coverage-v8": "2.0.2",
"@vitejs/plugin-react": "4.3.4",
"@vitest/coverage-v8": "2.1.6",
"autoprefixer": "10.4.20",
"chalk": "^5.3.0",
"commander": "12.1.0",
"compare-versions": "6.1.0",
"husky": "9.0.11",
"lint-staged": "15.2.2",
"prettier": "3.3.3",
"tsx": "^4.16.2",
"turbo": "2.0.11",
"typescript": "~5.6.2",
"vitest": "~2.1.1",
"compare-versions": "6.1.1",
"husky": "9.1.7",
"lint-staged": "15.2.10",
"postcss": "8.4.49",
"prettier": "3.4.1",
"tailwindcss": "3.4.15",
"terser": "5.36.0",
"tsup": "8.3.5",
"tsx": "^4.19.2",
"turbo": "2.3.2",
"typescript": "~5.7.2",
"updates": "^16.4.0",
"vite": "^6.0.0",
"vitest": "~2.1.6",
"voca": "1.4.1",
"zx": "8.2.2"
},
Expand Down
9 changes: 4 additions & 5 deletions packages/fuel-streams/examples/stream/blocks.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,16 @@
import chalk from 'chalk';
import { BlocksStream, BlocksSubject, Client, ClientOpts } from '../../src';
import { BlocksStream, BlocksSubject, Client } from '../../src';
import { handleUnhandledError, printHeader } from '../helpers';

async function main() {
printHeader('Block Streams Example');

const opts = new ClientOpts();
const client = await Client.connect(opts);
const client = await Client.connect({ network: 'testnet' });
const stream = await BlocksStream.init(client);
const subscription = await stream.subscribeWithSubject(BlocksSubject.build());
const subscription = await stream.subscribe(BlocksSubject.build());

for await (const msg of subscription) {
console.log(chalk.blue(`Received block message: ${msg.key}`));
console.log(chalk.blue(`Received block message: ${msg.subject}`));
// biome-ignore lint/suspicious/noExplicitAny: <explanation>
const data = msg.json() as any;
console.log(data.payload.V1.header);
Expand Down
Loading

0 comments on commit 81a4fa0

Please sign in to comment.