Skip to content

Commit

Permalink
Merge branch 'master' into starc007/feat/integrate-vitest-matchers-gl…
Browse files Browse the repository at this point in the history
…obally
  • Loading branch information
starc007 authored Dec 10, 2024
2 parents f5b35b1 + 3a178b0 commit 0236c22
Show file tree
Hide file tree
Showing 23 changed files with 208 additions and 47 deletions.
5 changes: 5 additions & 0 deletions .changeset/mean-sheep-occur.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@fuel-ts/account": patch
---

chore: avoid re-estimate `gasPrice` at `estimateTxDependencies`
4 changes: 4 additions & 0 deletions .changeset/moody-terms-shake.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
---
---

chore: minimizing logs in test output
2 changes: 2 additions & 0 deletions .changeset/old-maps-fold.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
---
---
15 changes: 15 additions & 0 deletions apps/docs/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
- [Table of contents](#table-of-contents)
- [Building](#building)
- [Testing](#testing)
- [Referencing snippets](#referencing-snippets)

## Building

Expand Down Expand Up @@ -42,3 +43,17 @@ If no environment is specified, it will run in the browser and node environments
```sh
pnpm test
```

## Referencing snippets

To reference a snippet outside of the current directory, you need to use the following syntax (`<<< @/`):

```md
`<<< @/../../path/to/snippet.ts#snippet-name{language:line-numbers}
```

To reference a snippet in the current directory, you can use the following syntax (`<<< @./`):

```md
`<<< @./snippets/transaction-request/fetch-resources.ts#transaction-request-5{ts:line-numbers}
```
2 changes: 1 addition & 1 deletion apps/docs/src/guide/transactions/transaction-request.md
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ If needed, you can manually include specific coins or messages in the transactio

Imagine that you have a Sway script that manually calls a contract:

<<< @../../../../sway/script-call-contract/src/main.sw#transaction-request-7{rs:line-numbers}
<<< @/../../docs/sway/script-call-contract/src/main.sw#transaction-request-7{rs:line-numbers}

In those cases, you will need to add both an `InputContract` and `OutputContract` to the transaction request:

Expand Down
2 changes: 1 addition & 1 deletion packages/abi-typegen/src/cli.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,7 @@ describe('cli.ts', () => {
test('should handle errors when running cli action', () => {
const runTypegenError = new Error('Pretty message');

const logSpy = vi.spyOn(console, 'log');
const logSpy = vi.spyOn(console, 'log').mockImplementation(() => {});
const { exit } = mockDeps({ runTypegenError });

const inputs = ['*-no-abis-here.json'];
Expand Down
4 changes: 2 additions & 2 deletions packages/abi-typegen/src/runTypegen.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ describe('runTypegen.js', () => {
});

test('should log messages to stdout', async () => {
const logSpy = vi.spyOn(console, 'log');
const logSpy = vi.spyOn(console, 'log').mockImplementation(() => {});

// setup temp sway project
const project = getTypegenForcProject(AbiTypegenProjectsEnum.SCRIPT);
Expand Down Expand Up @@ -272,7 +272,7 @@ describe('runTypegen.js', () => {
cpSync(fromBin, toBin);

// mocking
const logSpy = vi.spyOn(console, 'log');
const logSpy = vi.spyOn(console, 'log').mockImplementation(() => {});

// executes program
const fn = () =>
Expand Down
1 change: 1 addition & 0 deletions packages/account/src/account.ts
Original file line number Diff line number Diff line change
Expand Up @@ -323,6 +323,7 @@ export class Account extends AbstractAccount {

const { maxFee } = await this.provider.estimateTxGasAndFee({
transactionRequest: requestToReestimate,
gasPrice,
});

request.maxFee = maxFee;
Expand Down
10 changes: 5 additions & 5 deletions packages/account/src/providers/provider.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -250,7 +250,7 @@ describe('Provider', () => {
});

// Spy on console.warn
const consoleWarnSpy = vi.spyOn(console, 'warn');
const consoleWarnSpy = vi.spyOn(console, 'warn').mockImplementation(() => {});

// Verify that only one transaction was returned (the known type)
const transaction = await mockProvider.getTransaction('0x1234567890abcdef');
Expand Down Expand Up @@ -298,7 +298,7 @@ describe('Provider', () => {
});

// Spy on console.warn
const consoleWarnSpy = vi.spyOn(console, 'warn');
const consoleWarnSpy = vi.spyOn(console, 'warn').mockImplementation(() => {});

// Verify that only one transaction was returned (the known type)
const { transactions } = await mockProvider.getTransactions();
Expand Down Expand Up @@ -448,7 +448,7 @@ describe('Provider', () => {
using launched = await setupTestProviderAndWallets();
const { provider } = launched;

const { cleanup, url } = await launchNode({ port: '0' });
const { cleanup, url } = await launchNode({ port: '0', loggingEnabled: false });

const spyFetchChainAndNodeInfo = vi.spyOn(Provider.prototype, 'fetchChainAndNodeInfo');

Expand Down Expand Up @@ -1181,7 +1181,7 @@ describe('Provider', () => {
const spy = vi.spyOn(fuelTsVersionsMod, 'checkFuelCoreVersionCompatibility');
spy.mockImplementationOnce(() => mock);

const consoleWarnSpy = vi.spyOn(console, 'warn');
const consoleWarnSpy = vi.spyOn(console, 'warn').mockImplementation(() => {});

using launched = await setupTestProviderAndWallets();
const { provider } = launched;
Expand Down Expand Up @@ -1216,7 +1216,7 @@ Supported fuel-core version: ${mock.supportedVersion}.`
const spy = vi.spyOn(fuelTsVersionsMod, 'checkFuelCoreVersionCompatibility');
spy.mockImplementationOnce(() => mock);

const consoleWarnSpy = vi.spyOn(console, 'warn');
const consoleWarnSpy = vi.spyOn(console, 'warn').mockImplementation(() => {});

using launched = await setupTestProviderAndWallets();
const { provider } = launched;
Expand Down
4 changes: 3 additions & 1 deletion packages/account/src/providers/provider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1011,6 +1011,7 @@ Supported fuel-core version: ${supportedVersion}.`
} = await this.operations.dryRun({
encodedTransactions: [hexlify(transactionRequest.toTransactionBytes())],
utxoValidation: false,
gasPrice: '0',
});

receipts = rawReceipts.map(processGqlReceipt);
Expand All @@ -1032,6 +1033,7 @@ Supported fuel-core version: ${supportedVersion}.`

const { maxFee } = await this.estimateTxGasAndFee({
transactionRequest,
gasPrice: bn(0),
});

// eslint-disable-next-line no-param-reassign
Expand Down Expand Up @@ -1204,7 +1206,7 @@ Supported fuel-core version: ${supportedVersion}.`
const { gasPriceFactor, maxGasPerTx } = this.getGasConfig();

const minGas = transactionRequest.calculateMinGas(chainInfo);
if (!gasPrice) {
if (!isDefined(gasPrice)) {
gasPrice = await this.estimateGasPrice(10);
}

Expand Down
6 changes: 3 additions & 3 deletions packages/account/src/test-utils/launchNode.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ describe('launchNode', () => {
});

test('should throw on error and log error message', { timeout: 15000 }, async () => {
const logSpy = vi.spyOn(console, 'log');
const logSpy = vi.spyOn(console, 'log').mockImplementation(() => {});

const invalidCoin = {
asset_id: 'whatever',
Expand All @@ -132,7 +132,7 @@ describe('launchNode', () => {
const error = await expectToThrowFuelError(
async () =>
launchNode({
loggingEnabled: false,
loggingEnabled: true,
snapshotConfig: {
...defaultSnapshotConfigs,
stateConfig: {
Expand Down Expand Up @@ -238,7 +238,7 @@ describe('launchNode', () => {

test('calling cleanup on externally killed node does not throw', async () => {
const mkdirSyncSpy = vi.spyOn(fsMod, 'mkdirSync');
const logSpy = vi.spyOn(console, 'log');
const logSpy = vi.spyOn(console, 'log').mockImplementation(() => {});

const { pid, cleanup } = await launchNode({ loggingEnabled: false });
expect(mkdirSyncSpy).toHaveBeenCalledTimes(1);
Expand Down
3 changes: 3 additions & 0 deletions packages/account/src/test-utils/launchNode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -278,6 +278,9 @@ export const launchNode = async ({
}
});

// Increase the max listeners to avoid a warning
process.setMaxListeners(100);

// Process exit.
process.on('exit', cleanup);

Expand Down
12 changes: 6 additions & 6 deletions packages/create-fuels/src/utils/logger.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,23 +28,23 @@ describe('logger', () => {
});

test('should log', () => {
const logSpy = vi.spyOn(console, 'log');
const logSpy = vi.spyOn(console, 'log').mockImplementation(() => {});
configureLogging({ isLoggingEnabled: true, isDebugEnabled: false });
log('message');
expect(logSpy).toHaveBeenCalledTimes(1);
expect(logSpy).toHaveBeenCalledWith('message');
});

test('should not log', () => {
const logSpy = vi.spyOn(console, 'log');
const logSpy = vi.spyOn(console, 'log').mockImplementation(() => {});

configureLogging({ isLoggingEnabled: false, isDebugEnabled: false });
log('any message');
expect(logSpy).not.toHaveBeenCalled();
});

test('should debug', () => {
const logSpy = vi.spyOn(console, 'log');
const logSpy = vi.spyOn(console, 'log').mockImplementation(() => {});

configureLogging({ isLoggingEnabled: true, isDebugEnabled: true });
debug('message');
Expand All @@ -53,22 +53,22 @@ describe('logger', () => {
});

test('should not log', () => {
const logSpy = vi.spyOn(console, 'log');
const logSpy = vi.spyOn(console, 'log').mockImplementation(() => {});

configureLogging({ isLoggingEnabled: false, isDebugEnabled: false });
loggerMod.debug('any debug message');
expect(logSpy).toHaveBeenCalledTimes(0);
});

test('should warn', () => {
const logSpy = vi.spyOn(console, 'log');
const logSpy = vi.spyOn(console, 'log').mockImplementation(() => {});
warn('message1', 'message2');
expect(logSpy).toHaveBeenCalledTimes(1);
expect(logSpy).toHaveBeenCalledWith(chalk.yellow('message1 message2'));
});

test('should error', () => {
const logSpy = vi.spyOn(console, 'log');
const logSpy = vi.spyOn(console, 'log').mockImplementation(() => {});
error('message1', 'message2');
expect(logSpy).toHaveBeenCalledTimes(1);
expect(logSpy).toHaveBeenCalledWith(chalk.red('message1 message2'));
Expand Down
79 changes: 79 additions & 0 deletions packages/fuel-gauge/src/fee.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import {
InputMessageCoder,
ScriptTransactionRequest,
Wallet,
getMintedAssetId,
getRandomB256,
hexlify,
isCoin,
Expand Down Expand Up @@ -40,6 +41,8 @@ describe('Fee', () => {
}
};

const SUB_ID = '0x4a778acfad1abc155a009dc976d2cf0db6197d3d360194d74b1fb92b96986b00';

it('should ensure fee is properly calculated when minting and burning coins', async () => {
using launched = await launchTestNode({
contractsConfigs: [
Expand Down Expand Up @@ -431,5 +434,81 @@ describe('Fee', () => {

expect(cost.dryRunStatus?.type).toBe('DryRunSuccessStatus');
});

it('should not run estimateGasPrice in between estimateTxDependencies dry run attempts', async () => {
using launched = await launchTestNode({
contractsConfigs: [
{
factory: MultiTokenContractFactory,
},
],
});

const {
contracts: [contract],
wallets: [wallet],
provider,
} = launched;

const assetId = getMintedAssetId(contract.id.toB256(), SUB_ID);

// Minting coins first
const mintCall = await contract.functions.mint_coins(SUB_ID, 10_000).call();
await mintCall.waitForResult();

const estimateGasPrice = vi.spyOn(provider, 'estimateGasPrice');
const dryRun = vi.spyOn(provider.operations, 'dryRun');

/**
* Sway transfer without adding `OutputVariable` which will result in
* 2 dry runs at the `Provider.estimateTxDependencies` method:
* - 1st dry run will fail due to missing `OutputVariable`
* - 2nd dry run will succeed
*/
const transferCall = await contract.functions
.transfer_to_address({ bits: wallet.address.toB256() }, { bits: assetId }, 10_000)
.call();

await transferCall.waitForResult();

expect(estimateGasPrice).toHaveBeenCalledOnce();
expect(dryRun).toHaveBeenCalledTimes(2);
});

it('should ensure estimateGasPrice runs only once when funding a transaction.', async () => {
const amountPerCoin = 100;

using launched = await launchTestNode({
walletsConfig: {
amountPerCoin, // Funding with multiple UTXOs so the fee will change after funding the TX.
coinsPerAsset: 250,
},
contractsConfigs: [
{
factory: MultiTokenContractFactory,
},
],
});

const {
wallets: [wallet],
provider,
} = launched;

const fund = vi.spyOn(wallet, 'fund');
const estimateGasPrice = vi.spyOn(provider, 'estimateGasPrice');

const tx = await wallet.transfer(
wallet.address,
amountPerCoin * 20,
provider.getBaseAssetId()
);
const { isStatusSuccess } = await tx.waitForResult();

expect(fund).toHaveBeenCalledOnce();
expect(estimateGasPrice).toHaveBeenCalledOnce();

expect(isStatusSuccess).toBeTruthy();
});
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ describe('buildSwayPrograms', () => {

test('logs to console when logging is enabled', async () => {
const { spawn } = mockAll();
const logSpy = vi.spyOn(console, 'log');
const logSpy = vi.spyOn(console, 'log').mockImplementation(() => {});

configureLogging({ isLoggingEnabled: true, isDebugEnabled: false });

Expand All @@ -70,7 +70,7 @@ describe('buildSwayPrograms', () => {

test('logs debug to console when debug is enabled', async () => {
const { spawn } = mockAll();
const logSpy = vi.spyOn(console, 'log');
const logSpy = vi.spyOn(console, 'log').mockImplementation(() => {});
configureLogging({ isLoggingEnabled: true, isDebugEnabled: true });

await buildSwayProgram(fuelsConfig, '/any/workspace/path');
Expand Down
Loading

0 comments on commit 0236c22

Please sign in to comment.