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

feat: use submitAndAwait graphql endpoint #1615

Merged
merged 35 commits into from
Jan 18, 2024
Merged
Changes from 27 commits
Commits
Show all changes
35 commits
Select commit Hold shift + click to select a range
da7f580
feat: use submitAndAwait endpoint
nedsalk Jan 5, 2024
2414b82
feat: don't fetch when transaction is already executed
nedsalk Jan 5, 2024
45ea207
test: fix assumptions
nedsalk Jan 5, 2024
d82b092
Merge branch 'rc/salamander' into ns/feat/submit-and-await
nedsalk Jan 5, 2024
a243d44
fix: better typedocs
nedsalk Jan 5, 2024
bd56166
test: added tests to cover all methods that have `awaitExecution` on …
nedsalk Jan 5, 2024
f40a9e6
Merge remote-tracking branch 'origin/rc/salamander' into ns/feat/subm…
nedsalk Jan 5, 2024
14caf98
Merge branch 'rc/salamander' into ns/feat/submit-and-await
nedsalk Jan 5, 2024
8e68217
feat: use `submitAndAwait` by default in `BaseInvocationScope`
nedsalk Jan 9, 2024
d3a0cff
Merge branch 'rc/salamander' into ns/feat/submit-and-await
nedsalk Jan 9, 2024
bb9e117
refactor: use `for-await-of` instead of `ReadableStream` piping
nedsalk Jan 9, 2024
01267c0
Revert "refactor: use `for-await-of` instead of `ReadableStream` piping"
nedsalk Jan 9, 2024
9b3a057
feat: close stream when iterator finishes
nedsalk Jan 10, 2024
9a70a14
feat: don't allow `awaitExecution` passing in `BaseInvocationScope`
nedsalk Jan 10, 2024
7ff5d16
fix: stream not ending when connection is closed by node
nedsalk Jan 11, 2024
f5c120d
fix: add missing keep-alive message handling
nedsalk Jan 11, 2024
96b60ab
fix: throw `FuelError` in subscription
nedsalk Jan 11, 2024
9b78417
chore: revert changes on accounts
nedsalk Jan 11, 2024
471480c
chore: changeset
nedsalk Jan 11, 2024
e1a9b6a
chore: additional reverts
nedsalk Jan 11, 2024
35493ab
revert changes
nedsalk Jan 11, 2024
8cf8d98
fix: `awaitExecution` on account's sendTransaction
nedsalk Jan 11, 2024
f3d3c7f
chore: update changeset
nedsalk Jan 11, 2024
6bbb031
fix: failing build
nedsalk Jan 11, 2024
20ed3a5
reapply awaitExecution in utils and contract-factory
nedsalk Jan 11, 2024
6bbb6a4
Merge branch 'rc/salamander' into ns/feat/submit-and-await
nedsalk Jan 12, 2024
0b6e3a9
Merge branch 'rc/salamander' into ns/feat/submit-and-await
nedsalk Jan 12, 2024
73c9f6a
Merge branch 'rc/salamander' into ns/feat/submit-and-await
nedsalk Jan 15, 2024
282cb67
Merge branch 'rc/salamander' into ns/feat/submit-and-await
nedsalk Jan 15, 2024
9177c4f
Merge branch 'rc/salamander' into ns/feat/submit-and-await
nedsalk Jan 15, 2024
ca6b643
revert: bugfix, moved to #1597
nedsalk Jan 16, 2024
818d263
Merge branch 'rc/salamander' into ns/feat/submit-and-await
nedsalk Jan 16, 2024
2698eba
Merge branch 'rc/salamander' into ns/feat/submit-and-await
nedsalk Jan 16, 2024
e26e52a
Merge branch 'rc/salamander' into ns/feat/submit-and-await
nedsalk Jan 17, 2024
1fa9329
Merge branch 'rc/salamander' into ns/feat/submit-and-await
nedsalk Jan 17, 2024
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
10 changes: 10 additions & 0 deletions .changeset/fresh-terms-yell.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
---
"@fuel-ts/program": minor
"@fuel-ts/providers": minor
"@fuel-ts/wallet": minor
"@fuel-ts/predicate": minor
---

- Transaction execution can now be await with the `{awaitExecution: true}` option on `Provider.sendTransaction`
- Added same functionality to accounts (unlocked wallet, predicate)
- `BaseInvocationScope` internally now uses `{awaitExecution: true}` to reduce amount of network calls
6 changes: 1 addition & 5 deletions apps/docs-snippets/src/utils.ts
Original file line number Diff line number Diff line change
@@ -45,11 +45,7 @@ export const getTestWallet = async (seedQuantities?: CoinQuantityLike[]) => {
// funding the transaction with the required quantities
await genesisWallet.fund(request, requiredQuantities, minFee);

// execute the transaction, transferring resources to the test wallet
const response = await genesisWallet.sendTransaction(request);

// wait for the transaction to be confirmed
await response.wait();
await genesisWallet.sendTransaction(request, { awaitExecution: true });
danielbate marked this conversation as resolved.
Show resolved Hide resolved

// return the test wallet
return testWallet;
5 changes: 3 additions & 2 deletions packages/contract/src/contract-factory.ts
Original file line number Diff line number Diff line change
@@ -152,8 +152,9 @@ export default class ContractFactory {
transactionRequest.maxFee = this.account.provider.getGasConfig().maxGasPerTx;

await this.account.fund(transactionRequest, requiredQuantities, maxFee);
const response = await this.account.sendTransaction(transactionRequest);
await response.wait();
await this.account.sendTransaction(transactionRequest, {
awaitExecution: true,
});

return new Contract(contractId, this.interface, this.account);
}
97 changes: 97 additions & 0 deletions packages/fuel-gauge/src/await-execution.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
import { launchNode } from '@fuel-ts/wallet/test-utils';
import {
Provider,
WalletUnlocked,
randomBytes,
Wallet,
BaseAssetId,
FUEL_NETWORK_URL,
} from 'fuels';

/**
* @group node
*/
describe('await-execution', () => {
test('awaiting execution of a transaction on the provider works', async () => {
const { cleanup, ip, port } = await launchNode({
args: ['--poa-interval-period', '400ms'],
});
const nodeProvider = await Provider.create(`http://${ip}:${port}/graphql`);
danielbate marked this conversation as resolved.
Show resolved Hide resolved

const genesisWallet = new WalletUnlocked(
process.env.GENESIS_SECRET || randomBytes(32),
nodeProvider
);

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

const transfer = await genesisWallet.createTransfer(destination.address, 100, BaseAssetId, {
gasPrice: nodeProvider.getGasConfig().minGasPrice,
gasLimit: 10_000,
});

transfer.updateWitnessByOwner(
genesisWallet.address,
await genesisWallet.signTransaction(transfer)
);

const response = await nodeProvider.sendTransaction(transfer, { awaitExecution: true });

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

cleanup();
});

test.skip('transferring funds with awaitExecution works', async () => {
nedsalk marked this conversation as resolved.
Show resolved Hide resolved
const provider = await Provider.create(FUEL_NETWORK_URL);
const genesisWallet = new WalletUnlocked(
process.env.GENESIS_SECRET || randomBytes(32),
provider
);

const sendTransactionSpy = vi.spyOn(provider, 'sendTransaction');

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

await genesisWallet.transfer(
destination.address,
100,
BaseAssetId,
{
gasPrice: provider.getGasConfig().minGasPrice,
gasLimit: 10_000,
}
// { awaitExecution: true }
);

expect(sendTransactionSpy).toHaveBeenCalledTimes(1);
const awaitExecutionArg = sendTransactionSpy.mock.calls[0][1];
expect(awaitExecutionArg).toMatchObject({ awaitExecution: true });
});

test.skip('withdrawToBaseLayer works with awaitExecution', async () => {
nedsalk marked this conversation as resolved.
Show resolved Hide resolved
const provider = await Provider.create(FUEL_NETWORK_URL);
const genesisWallet = new WalletUnlocked(
process.env.GENESIS_SECRET || randomBytes(32),
provider
);

const sendTransactionSpy = vi.spyOn(provider, 'sendTransaction');

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

await genesisWallet.withdrawToBaseLayer(
destination.address,
100,
{
gasPrice: provider.getGasConfig().minGasPrice,
gasLimit: 10_000,
}
// { awaitExecution: true }
);

expect(sendTransactionSpy).toHaveBeenCalledTimes(1);
const awaitExecutionArg = sendTransactionSpy.mock.calls[0][1];
expect(awaitExecutionArg).toMatchObject({ awaitExecution: true });
});
});
31 changes: 30 additions & 1 deletion packages/fuel-gauge/src/edge-cases.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import { getSetupContract } from './utils';
import { generateTestWallet } from '@fuel-ts/wallet/test-utils';
import { Provider, Wallet, BaseAssetId, FUEL_NETWORK_URL, TransactionResponse } from 'fuels';

import { getSetupContract } from './utils';
/**
* @group node
*/
@@ -15,4 +17,31 @@ describe('Edge Cases', () => {
).value.toNumber()
).toEqual(12345);
});

test("SSE subscriptions that are closed by the node don't hang a for-await-of loop", async () => {
const provider = await Provider.create(FUEL_NETWORK_URL);
const adminWallet = await generateTestWallet(provider, [[500_000]]);

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

const { id: transactionId } = await adminWallet.transfer(
destination.address,
100,
BaseAssetId,
{ gasPrice: provider.getGasConfig().minGasPrice, gasLimit: 10_000 }
);

const response = new TransactionResponse(transactionId, provider);

await response.waitForResult();

const subsciption = provider.operations.statusChange({ transactionId });

// eslint-disable-next-line @typescript-eslint/no-unused-vars
for await (const iterator of subsciption) {
// we leave this intentionally empty so that we test that the subscription will end the loop when the connection is closed
}
});
});
Original file line number Diff line number Diff line change
@@ -17,8 +17,7 @@ export const fundPredicate = async <T extends InputValue[]>(
request.gasLimit = gasUsed;
await wallet.fund(request, requiredQuantities, minFee);

const tx = await wallet.sendTransaction(request);
await tx.waitForResult();
await wallet.sendTransaction(request, { awaitExecution: true });

return predicate.getBalance();
};
4 changes: 2 additions & 2 deletions packages/interfaces/src/index.ts
Original file line number Diff line number Diff line change
@@ -60,7 +60,7 @@ export abstract class AbstractAccount {
abstract address: AbstractAddress;
abstract provider: unknown;
abstract getResourcesToSpend(quantities: any[], options?: any): any;
abstract sendTransaction(transactionRequest: any): any;
abstract sendTransaction(transactionRequest: any, options?: any): any;
abstract simulateTransaction(transactionRequest: any): any;
abstract fund(transactionRequest: any, quantities: any, fee: any): Promise<void>;
}
@@ -74,7 +74,7 @@ export abstract class AbstractProgram {
};

abstract provider: {
sendTransaction(transactionRequest: any): any;
sendTransaction(transactionRequest: any, options?: any): any;
} | null;
}

8 changes: 6 additions & 2 deletions packages/predicate/src/predicate.ts
Original file line number Diff line number Diff line change
@@ -14,6 +14,7 @@ import type { BigNumberish } from '@fuel-ts/math';
import type {
CallResult,
Provider,
ProviderSendTxParams,
TransactionRequest,
TransactionRequestLike,
TransactionResponse,
@@ -113,9 +114,12 @@ export class Predicate<ARGS extends InputValue[]> extends Account implements Abs
* @param transactionRequestLike - The transaction request-like object.
* @returns A promise that resolves to the transaction response.
*/
sendTransaction(transactionRequestLike: TransactionRequestLike): Promise<TransactionResponse> {
sendTransaction(
transactionRequestLike: TransactionRequestLike,
options?: Pick<ProviderSendTxParams, 'awaitExecution'>
): Promise<TransactionResponse> {
const transactionRequest = this.populateTransactionPredicateData(transactionRequestLike);
return super.sendTransaction(transactionRequest);
return super.sendTransaction(transactionRequest, options);
}

/**
4 changes: 3 additions & 1 deletion packages/program/src/functions/base-invocation-scope.ts
Original file line number Diff line number Diff line change
@@ -297,7 +297,9 @@ export class BaseInvocationScope<TReturn = any> {

await this.fundWithRequiredCoins(maxFee);

const response = await this.program.account.sendTransaction(transactionRequest);
const response = await this.program.account.sendTransaction(transactionRequest, {
awaitExecution: true,
});

return FunctionInvocationResult.build<T>(
this.functionInvocationScopes,
136 changes: 78 additions & 58 deletions packages/providers/src/fuel-graphql-subscriber.ts
Original file line number Diff line number Diff line change
@@ -2,80 +2,100 @@ import { FuelError } from '@fuel-ts/errors';
import type { DocumentNode } from 'graphql';
import { print } from 'graphql';

type FuelGraphQLSubscriberOptions = {
export 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>>;
export class FuelGraphqlSubscriber implements AsyncIterator<unknown> {
private stream!: ReadableStreamDefaultReader<Uint8Array>;
private static textDecoder = new TextDecoder();

constructor() {
this.readable = new ReadableStream({
start: (controller) => {
this.readableStreamController = controller;
},
});
private static parseBytesStream(
bytes: Uint8Array | undefined
): Record<string, unknown> | FuelError | undefined {
if (bytes === undefined) {
return undefined;
}

const text = this.textDecoder.decode(bytes);
if (!text.startsWith('data:')) {
// the text can sometimes be a keep-alive message
return undefined;
}

const { data, errors } = JSON.parse(text.split('data:')[1]);

if (Array.isArray(errors)) {
return new FuelError(
FuelError.CODES.INVALID_REQUEST,
errors.map((err) => err.message).join('\n\n')
);
}

return data as Record<string, unknown>;
}

public constructor(private options: FuelGraphQLSubscriberOptions) {}

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);
}
}
private async setStream() {
const { url, query, variables, fetchFn } = this.options;
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
this.stream = response.body!.getReader();
}
}

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();
private async readStream(): Promise<IteratorResult<unknown, unknown>> {
const { value, done } = await this.stream.read();

const parsed = FuelGraphqlSubscriber.parseBytesStream(value);

if (parsed === undefined && !done) {
// this is in the case of e.g. a keep-alive message
// we recursively wait for the next message until it's a proper gql response
// or the stream is done (e.g. closed by the server)
return this.readStream();
}

return { value: parsed, done };
}

async next(): Promise<IteratorResult<unknown, unknown>> {
if (!this.stream) {
await this.setStream();
}

const { value, done } = await this.readStream();

if (value instanceof FuelError) {
throw value;
}
yield value;
if (done) {
break;
}

return { value, done };
}

/**
* Gets called when `break` is called in a `for-await-of` loop.
*/
async return(): Promise<IteratorResult<unknown, undefined>> {
await this.stream.cancel();
return { done: true, value: undefined };
}

[Symbol.asyncIterator](): AsyncIterator<unknown, unknown, undefined> {
return this;
}
}
Loading