Skip to content

Commit

Permalink
Merge branch 'rc/salamander' into db/chore/pin-gql-request-version
Browse files Browse the repository at this point in the history
  • Loading branch information
danielbate authored Dec 20, 2023
2 parents 267f178 + 8021b38 commit 58b8037
Show file tree
Hide file tree
Showing 39 changed files with 432 additions and 173 deletions.
6 changes: 6 additions & 0 deletions .changeset/serious-laws-judge.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
"@fuel-ts/providers": minor
"@fuel-ts/errors": patch
---

Implemented GraphQL subscriptions
7 changes: 6 additions & 1 deletion .eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,12 @@ module.exports = {
},
},
rules: {
'no-restricted-syntax': [
'off',
{
selector: 'ForOfStatement',
},
],
'@typescript-eslint/no-non-null-assertion': 1,
// Disable error on devDependencies importing since this isn't a TS library
'import/no-extraneous-dependencies': ['error', { devDependencies: true }],
Expand Down Expand Up @@ -56,7 +62,6 @@ module.exports = {
'warn',
{
argsIgnorePattern: '^_',
varsIgnorePattern: '^_',
},
],
'@typescript-eslint/no-explicit-any': 'error',
Expand Down
2 changes: 1 addition & 1 deletion .github/actions/ci-setup/action.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ name: "CI Setup"
inputs:
node-version:
description: "Node version"
default: 18.14.1
default: 18.18.2
pnpm-version:
description: "PNPM version"
default: 8.9.0
Expand Down
4 changes: 2 additions & 2 deletions .github/actions/test-setup/action.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ name: "Test Setup"
inputs:
node-version:
description: "Node version"
default: 18.14.1
default: 18.18.2
pnpm-version:
description: "PNPM version"
default: 8.9.0
Expand Down Expand Up @@ -31,4 +31,4 @@ runs:

- name: Build
run: pnpm build
shell: bash
shell: bash
2 changes: 1 addition & 1 deletion .nvmrc
Original file line number Diff line number Diff line change
@@ -1 +1 @@
18.14.1
18.18.2
7 changes: 1 addition & 6 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
"author": "Fuel Labs <[email protected]> (https://fuel.network/)",
"private": true,
"engines": {
"node": "^18.14.1",
"node": "^18.18.2",
"pnpm": "^8.9.0"
},
"packageManager": "[email protected]",
Expand Down Expand Up @@ -91,10 +91,5 @@
"tsx": "^3.12.7",
"turbo": "^1.8.8",
"typescript": "~5.2.2"
},
"pnpm": {
"overrides": {
"cross-fetch": "4.0.0"
}
}
}
2 changes: 1 addition & 1 deletion packages/abi-coder/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
"module": "dist/index.mjs",
"types": "dist/index.d.ts",
"engines": {
"node": "^18.14.1"
"node": "^18.18.2"
},
"exports": {
".": {
Expand Down
2 changes: 1 addition & 1 deletion packages/abi-typegen/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
"module": "dist/index.mjs",
"types": "dist/index.d.ts",
"engines": {
"node": "^18.14.1"
"node": "^18.18.2"
},
"exports": {
".": {
Expand Down
2 changes: 1 addition & 1 deletion packages/address/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
"module": "dist/index.mjs",
"types": "dist/index.d.ts",
"engines": {
"node": "^18.14.1"
"node": "^18.18.2"
},
"exports": {
".": {
Expand Down
2 changes: 1 addition & 1 deletion packages/contract/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
"module": "dist/index.mjs",
"types": "dist/index.d.ts",
"engines": {
"node": "^18.14.1"
"node": "^18.18.2"
},
"exports": {
".": {
Expand Down
2 changes: 1 addition & 1 deletion packages/crypto/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
"module": "dist/index.mjs",
"types": "dist/index.d.ts",
"engines": {
"node": "^18.14.1"
"node": "^18.18.2"
},
"browser": {
"./dist/index.mjs": "./dist/index.browser.mjs"
Expand Down
2 changes: 1 addition & 1 deletion packages/errors/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
"module": "dist/index.mjs",
"types": "dist/index.d.ts",
"engines": {
"node": "^18.14.1"
"node": "^18.18.2"
},
"exports": {
".": {
Expand Down
1 change: 1 addition & 0 deletions packages/errors/src/error-codes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ export enum ErrorCode {
CONVERTING_FAILED = 'converting-error',
ELEMENT_NOT_FOUND = 'element-not-found',
MISSING_REQUIRED_PARAMETER = 'missing-required-parameter',
INVALID_REQUEST = 'invalid-request',
UNEXPECTED_HEX_VALUE = 'unexpected-hex-value',

// transaction
Expand Down
38 changes: 27 additions & 11 deletions packages/fuel-gauge/src/transaction-response.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,14 @@
import { generateTestWallet } from '@fuel-ts/wallet/test-utils';
import type { BN, WalletUnlocked } from 'fuels';
import { BaseAssetId, FUEL_NETWORK_URL, Provider, TransactionResponse, Wallet } from 'fuels';
import { generateTestWallet, launchNode } from '@fuel-ts/wallet/test-utils';
import type { BN } from 'fuels';
import {
BaseAssetId,
FUEL_NETWORK_URL,
Provider,
TransactionResponse,
Wallet,
randomBytes,
WalletUnlocked,
} from 'fuels';

describe('TransactionSummary', () => {
let provider: Provider;
Expand Down Expand Up @@ -75,25 +83,33 @@ describe('TransactionSummary', () => {
});

it('should ensure waitForResult always waits for the transaction to be processed', async () => {
const destination = Wallet.generate({
provider,
const { cleanup, ip, port } = await launchNode({
args: ['--poa-interval-period', '750ms'],
});
const nodeProvider = await Provider.create(`http://${ip}:${port}/graphql`);

const { id: transactionId } = await adminWallet.transfer(
const genesisWallet = new WalletUnlocked(
process.env.GENESIS_SECRET || randomBytes(32),
nodeProvider
);

const destination = Wallet.generate({ provider: nodeProvider });

const { id: transactionId } = await genesisWallet.transfer(
destination.address,
100,
BaseAssetId,
{ gasPrice, gasLimit: 10_000 }
);
const response = await TransactionResponse.create(transactionId, nodeProvider);

const response = new TransactionResponse(transactionId, provider);

expect(response.gqlTransaction).toBeUndefined();
expect(response.gqlTransaction?.status?.type).toBe('SubmittedStatus');

await response.waitForResult();

expect(response.gqlTransaction?.status?.type).toBeDefined();
expect(response.gqlTransaction?.status?.type).not.toEqual('SubmittedStatus');
expect(response.gqlTransaction?.status?.type).toEqual('SuccessStatus');
expect(response.gqlTransaction?.id).toBe(transactionId);

cleanup();
});
});
2 changes: 1 addition & 1 deletion packages/fuels/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
"module": "dist/index.mjs",
"types": "dist/index.d.ts",
"engines": {
"node": "^18.14.1"
"node": "^18.18.2"
},
"exports": {
".": {
Expand Down
2 changes: 1 addition & 1 deletion packages/hasher/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
"module": "dist/index.mjs",
"types": "dist/index.d.ts",
"engines": {
"node": "^18.14.1"
"node": "^18.18.2"
},
"exports": {
".": {
Expand Down
2 changes: 1 addition & 1 deletion packages/hdwallet/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
"module": "dist/index.mjs",
"types": "dist/index.d.ts",
"engines": {
"node": "^18.14.1"
"node": "^18.18.2"
},
"exports": {
".": {
Expand Down
2 changes: 1 addition & 1 deletion packages/interfaces/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
"module": "dist/index.mjs",
"types": "dist/index.d.ts",
"engines": {
"node": "^18.14.1"
"node": "^18.18.2"
},
"exports": {
".": {
Expand Down
2 changes: 1 addition & 1 deletion packages/math/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
"module": "dist/index.mjs",
"types": "dist/index.d.ts",
"engines": {
"node": "^18.14.1"
"node": "^18.18.2"
},
"exports": {
".": {
Expand Down
2 changes: 1 addition & 1 deletion packages/merkle/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
"module": "dist/index.mjs",
"types": "dist/index.d.ts",
"engines": {
"node": "^18.14.1"
"node": "^18.18.2"
},
"exports": {
".": {
Expand Down
2 changes: 1 addition & 1 deletion packages/mnemonic/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
"module": "dist/index.mjs",
"types": "dist/index.d.ts",
"engines": {
"node": "^18.14.1"
"node": "^18.18.2"
},
"exports": {
".": {
Expand Down
2 changes: 1 addition & 1 deletion packages/predicate/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
"module": "dist/index.mjs",
"types": "dist/index.d.ts",
"engines": {
"node": "^18.14.1"
"node": "^18.18.2"
},
"exports": {
".": {
Expand Down
2 changes: 1 addition & 1 deletion packages/program/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
"module": "dist/index.mjs",
"types": "dist/index.d.ts",
"engines": {
"node": "^18.14.1"
"node": "^18.18.2"
},
"exports": {
".": {
Expand Down
2 changes: 1 addition & 1 deletion packages/providers/codegen.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
"plugins": [
{ "typescript": {} },
{ "typescript-operations": {} },
{ "typescript-graphql-request": {} }
{ "typescript-generic-sdk": {} }
],
"config": {
"scalars": {
Expand Down
8 changes: 4 additions & 4 deletions packages/providers/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
"module": "dist/index.mjs",
"types": "dist/index.d.ts",
"engines": {
"node": "^18.14.1"
"node": "^18.18.2"
},
"exports": {
".": {
Expand Down Expand Up @@ -48,9 +48,9 @@
"@fuel-ts/utils": "workspace:*",
"@graphql-codegen/cli": "^2.13.7",
"@graphql-codegen/typescript": "^2.8.0",
"@graphql-codegen/typescript-graphql-request": "^4.5.7",
"@graphql-codegen/typescript-operations": "^2.5.5",
"@types/ramda": "^0.29.3",
"get-graphql-schema": "^2.1.2"
"@graphql-codegen/typescript-generic-sdk": "^3.1.0",
"get-graphql-schema": "^2.1.2",
"@types/ramda": "^0.29.3"
}
}
81 changes: 81 additions & 0 deletions packages/providers/src/fuel-graphql-subscriber.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
import { FuelError } from '@fuel-ts/errors';
import type { DocumentNode } from 'graphql';
import { print } from 'graphql';

type FuelGraphQLSubscriberOptions = {
url: string;
query: DocumentNode;
variables?: Record<string, unknown>;
fetchFn: typeof fetch;
abortController?: AbortController;
};

class FuelSubscriptionStream implements TransformStream {
readable: ReadableStream<FuelError | Record<string, unknown>>;
writable: WritableStream<Uint8Array>;
private readableStreamController!: ReadableStreamController<FuelError | Record<string, unknown>>;
private static textDecoder = new TextDecoder();

constructor() {
this.readable = new ReadableStream({
start: (controller) => {
this.readableStreamController = controller;
},
});

this.writable = new WritableStream<Uint8Array>({
write: (bytes) => {
const text = FuelSubscriptionStream.textDecoder.decode(bytes);
// the fuel node sends keep-alive messages that should be ignored
if (text.startsWith('data:')) {
const { data, errors } = JSON.parse(text.split('data:')[1]);
if (Array.isArray(errors)) {
this.readableStreamController.enqueue(
new FuelError(
FuelError.CODES.INVALID_REQUEST,
errors.map((err) => err.message).join('\n\n')
)
);
} else {
this.readableStreamController.enqueue(data);
}
}
},
});
}
}

export async function* fuelGraphQLSubscriber({
url,
variables,
query,
fetchFn,
}: FuelGraphQLSubscriberOptions) {
const response = await fetchFn(`${url}-sub`, {
method: 'POST',
body: JSON.stringify({
query: print(query),
variables,
}),
headers: {
'Content-Type': 'application/json',
Accept: 'text/event-stream',
},
});

// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
const subscriptionStreamReader = response
.body!.pipeThrough(new FuelSubscriptionStream())
.getReader();

while (true) {
const { value, done } = await subscriptionStreamReader.read();
if (value instanceof FuelError) {
throw value;
}
yield value;
if (done) {
break;
}
}
}
Loading

0 comments on commit 58b8037

Please sign in to comment.