Skip to content

Commit

Permalink
docs: improve examples with min gas price (#1371)
Browse files Browse the repository at this point in the history
  • Loading branch information
Torres-ssf authored Oct 25, 2023
1 parent cb477cb commit 5d9a2b3
Show file tree
Hide file tree
Showing 12 changed files with 129 additions and 69 deletions.
2 changes: 2 additions & 0 deletions .changeset/nervous-coins-tickle.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
---
---
16 changes: 10 additions & 6 deletions apps/docs-snippets/src/guide/contracts/call-parameters.test.ts
Original file line number Diff line number Diff line change
@@ -1,27 +1,28 @@
import type { Contract } from 'fuels';
import type { Contract, Provider } from 'fuels';
import { BN, BaseAssetId } from 'fuels';

import { SnippetProjectEnum } from '../../../projects';
import { createAndDeployContractFromProject } from '../../utils';

describe(__filename, () => {
let contract: Contract;
let gasPrice: BN;
let provider: Provider;
beforeAll(async () => {
contract = await createAndDeployContractFromProject(SnippetProjectEnum.RETURN_CONTEXT);
({ minGasPrice: gasPrice } = contract.provider.getGasConfig());
provider = contract.provider;
});

it('should successfully execute contract call with forwarded amount', async () => {
// #region call-params-1
const amountToForward = 10;
const { minGasPrice } = provider.getGasConfig();

const { value } = await contract.functions
.return_context_amount()
.callParams({
forward: [amountToForward, BaseAssetId],
})
.txParams({ gasPrice })
.txParams({ gasPrice: minGasPrice })
.call();

expect(new BN(value).toNumber()).toBe(amountToForward);
Expand All @@ -30,21 +31,24 @@ describe(__filename, () => {

it('should throw error due not enough gas', async () => {
// #region call-params-2
const { minGasPrice } = provider.getGasConfig();

await expect(
contract.functions
.return_context_amount()
.callParams({
forward: [10, BaseAssetId],
gasLimit: 1,
})
.txParams({ gasPrice })
.txParams({ gasPrice: minGasPrice })
.call()
).rejects.toThrow(/OutOfGas/);
// #endregion call-params-2
});

it('should successfully execute transaction with `txParams` and `callParams`', async () => {
// #region call-params-3
const { minGasPrice } = provider.getGasConfig();
const amountToForward = 10;
const contractCallGasLimit = 100;
const transactionGasLimit = 3_000_000;
Expand All @@ -57,7 +61,7 @@ describe(__filename, () => {
})
.txParams({
gasLimit: transactionGasLimit,
gasPrice,
gasPrice: minGasPrice,
})
.call();

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { BN, WalletUnlocked } from 'fuels';
import type { Provider, WalletUnlocked } from 'fuels';
import { ContractFactory } from 'fuels';

import { getSnippetProjectArtifacts, SnippetProjectEnum } from '../../../projects';
Expand All @@ -22,10 +22,10 @@ describe(__filename, () => {
},
};

let gasPrice: BN;
let provider: Provider;
beforeAll(async () => {
wallet = await getTestWallet();
({ minGasPrice: gasPrice } = wallet.provider.getGasConfig());
provider = wallet.provider;
});

it('should successfully set new values for all configurable constants', async () => {
Expand All @@ -43,9 +43,12 @@ describe(__filename, () => {

const factory = new ContractFactory(bin, abi, wallet);

const { minGasPrice, maxGasPerTx } = provider.getGasConfig();

const contract = await factory.deployContract({
configurableConstants,
gasPrice,
gasPrice: minGasPrice,
gasLimit: maxGasPerTx,
});
// #endregion configurable-constants-2

Expand All @@ -65,9 +68,12 @@ describe(__filename, () => {

const factory = new ContractFactory(bin, abi, wallet);

const { minGasPrice, maxGasPerTx } = provider.getGasConfig();

const contract = await factory.deployContract({
configurableConstants,
gasPrice,
gasPrice: minGasPrice,
gasLimit: maxGasPerTx,
});
// #endregion configurable-constants-3

Expand All @@ -89,8 +95,14 @@ describe(__filename, () => {

const factory = new ContractFactory(bin, abi, wallet);

const { minGasPrice, maxGasPerTx } = provider.getGasConfig();

await expect(
factory.deployContract({ configurableConstants, gasPrice })
factory.deployContract({
configurableConstants,
gasPrice: minGasPrice,
gasLimit: maxGasPerTx,
})
).rejects.toThrowError();
// #endregion configurable-constants-4
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,11 @@ import { createAndDeployContractFromProject } from '../../utils';

describe(__filename, () => {
let contract: Contract;
let gasPrice: BN;
let provider: Provider;

beforeAll(async () => {
provider = await Provider.create(FUEL_NETWORK_URL);
contract = await createAndDeployContractFromProject(SnippetProjectEnum.TRANSFER_TO_ADDRESS);
({ minGasPrice: gasPrice } = contract.provider.getGasConfig());
});

it('should successfully get a contract balance', async () => {
Expand All @@ -26,12 +24,17 @@ describe(__filename, () => {
provider,
});

const { minGasPrice, maxGasPerTx } = provider.getGasConfig();

await contract.functions
.transfer(amountToTransfer, BaseAssetId, recipient.address.toB256())
.callParams({
forward: [amountToForward, BaseAssetId],
})
.txParams({ gasPrice })
.txParams({
gasPrice: minGasPrice,
gasLimit: maxGasPerTx,
})
.call();

const contractBalance = await contract.getBalance(BaseAssetId);
Expand Down
33 changes: 24 additions & 9 deletions apps/docs-snippets/src/guide/contracts/cost-estimation.test.ts
Original file line number Diff line number Diff line change
@@ -1,23 +1,31 @@
import type { Contract } from 'fuels';
import type { Contract, Provider } from 'fuels';
import { BaseAssetId } from 'fuels';

import { SnippetProjectEnum } from '../../../projects';
import { createAndDeployContractFromProject } from '../../utils';

describe(__filename, () => {
let contract: Contract;
let provider: Provider;

beforeAll(async () => {
contract = await createAndDeployContractFromProject(SnippetProjectEnum.RETURN_CONTEXT);
provider = contract.provider;
});

it('should successfully get transaction cost estimate for a single contract call', async () => {
// #region cost-estimation-1
const { minGasPrice, maxGasPerTx } = provider.getGasConfig();

const cost = await contract.functions
.return_context_amount()
.callParams({
forward: [100, BaseAssetId],
})
.txParams({
gasPrice: minGasPrice,
gasLimit: maxGasPerTx,
})
.getTransactionCost();

expect(cost.fee).toBeDefined();
Expand All @@ -29,14 +37,21 @@ describe(__filename, () => {

it('should get transaction cost estimate for multi contract calls just fine', async () => {
// #region cost-estimation-2
const scope = contract.multiCall([
contract.functions.return_context_amount().callParams({
forward: [100, BaseAssetId],
}),
contract.functions.return_context_amount().callParams({
forward: [300, BaseAssetId],
}),
]);
const { minGasPrice, maxGasPerTx } = provider.getGasConfig();

const scope = contract
.multiCall([
contract.functions.return_context_amount().callParams({
forward: [100, BaseAssetId],
}),
contract.functions.return_context_amount().callParams({
forward: [300, BaseAssetId],
}),
])
.txParams({
gasPrice: minGasPrice,
gasLimit: maxGasPerTx,
});

const cost = await scope.getTransactionCost();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,8 @@ describe(__filename, () => {

beforeAll(async () => {
const wallet = await getTestWallet();

PRIVATE_KEY = wallet.privateKey;

projectsPath = join(__dirname, '../../../projects');

contractName = SnippetProjectEnum.ECHO_VALUES;
});

Expand All @@ -42,7 +39,9 @@ describe(__filename, () => {

// #region contract-setup-3
const factory = new ContractFactory(byteCode, abi, wallet);

const { minGasPrice: gasPrice } = wallet.provider.getGasConfig();

const contract = await factory.deployContract({ gasPrice });
// #endregion contract-setup-3

Expand Down
22 changes: 10 additions & 12 deletions apps/docs-snippets/src/guide/contracts/inter-contract-calls.test.ts
Original file line number Diff line number Diff line change
@@ -1,21 +1,19 @@
import type { Contract, WalletUnlocked } from 'fuels';
import type { Contract, Provider, WalletUnlocked } from 'fuels';
import { BN, ContractFactory } from 'fuels';

import { getSnippetProjectArtifacts, SnippetProjectEnum } from '../../../projects';
import { getTestWallet } from '../../utils';

describe(__filename, () => {
let wallet: WalletUnlocked;

let simpleToken: Contract;
let tokenDepositor: Contract;

let gasPrice: BN;
let provider: Provider;

beforeAll(async () => {
wallet = await getTestWallet();

({ minGasPrice: gasPrice } = wallet.provider.getGasConfig());
provider = wallet.provider;
const { minGasPrice } = provider.getGasConfig();

const tokenArtifacts = getSnippetProjectArtifacts(SnippetProjectEnum.SIMPLE_TOKEN);
const depositorArtifacts = getSnippetProjectArtifacts(SnippetProjectEnum.TOKEN_DEPOSITOR);
Expand All @@ -24,35 +22,35 @@ describe(__filename, () => {
tokenArtifacts.binHexlified,
tokenArtifacts.abiContents,
wallet
).deployContract({ gasPrice });
).deployContract({ gasPrice: minGasPrice });

tokenDepositor = await new ContractFactory(
depositorArtifacts.binHexlified,
depositorArtifacts.abiContents,
wallet
).deployContract({ gasPrice });
).deployContract({ gasPrice: minGasPrice });
});

it('should successfully make call to another contract', async () => {
// #region inter-contract-calls-3
const amountToDeposit = 70;

const { minGasPrice, maxGasPerTx } = provider.getGasConfig();
const { value: initialBalance } = await simpleToken.functions
.get_balance(wallet.address.toB256())
.txParams({ gasPrice })
.txParams({ gasPrice: minGasPrice, gasLimit: maxGasPerTx })
.call();

expect(new BN(initialBalance).toNumber()).toBe(0);

await tokenDepositor.functions
.deposit_to_simple_token(simpleToken.id.toB256(), amountToDeposit)
.addContracts([simpleToken])
.txParams({ gasPrice })
.txParams({ gasPrice: minGasPrice, gasLimit: maxGasPerTx })
.call();

const { value: finalBalance } = await simpleToken.functions
.get_balance(wallet.address.toB256())
.txParams({ gasPrice })
.txParams({ gasPrice: minGasPrice, gasLimit: maxGasPerTx })
.call();

expect(new BN(finalBalance).toNumber()).toBe(amountToDeposit);
Expand Down
10 changes: 6 additions & 4 deletions apps/docs-snippets/src/guide/contracts/logs.test.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
import type { Contract } from 'fuels';
import type { Contract, Provider } from 'fuels';
import { BN } from 'fuels';

import { SnippetProjectEnum } from '../../../projects';
import { createAndDeployContractFromProject } from '../../utils';

describe(__filename, () => {
let contract: Contract;
let gasPrice: BN;
let provider: Provider;

beforeAll(async () => {
contract = await createAndDeployContractFromProject(SnippetProjectEnum.LOG_VALUES);
({ minGasPrice: gasPrice } = contract.provider.getGasConfig());
provider = contract.provider;
});

it('should successfully execute contract call with forwarded amount', async () => {
Expand All @@ -20,9 +20,11 @@ describe(__filename, () => {
const value3 = 'Fuel';
const value4 = [1, 2, 3];

const { minGasPrice, maxGasPerTx } = provider.getGasConfig();

const { logs } = await contract.functions
.log_values(value1, value2, value3, value4)
.txParams({ gasPrice })
.txParams({ gasPrice: minGasPrice, gasLimit: maxGasPerTx })
.call();

expect(new BN(logs[0]).toNumber()).toBe(value1);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ describe(__filename, () => {

beforeAll(async () => {
wallet = await getTestWallet();

const factory = new ContractFactory(bin, abi, wallet);
const { minGasPrice: gasPrice } = wallet.provider.getGasConfig();
contract = await factory.deployContract({ gasPrice });
Expand Down
Loading

0 comments on commit 5d9a2b3

Please sign in to comment.