Skip to content

Commit

Permalink
refactor: purging constant MAX_GAS_PER_TX (#1272)
Browse files Browse the repository at this point in the history
  • Loading branch information
Torres-ssf authored Sep 25, 2023
1 parent f0e506b commit 30d1d8e
Show file tree
Hide file tree
Showing 7 changed files with 59 additions and 30 deletions.
9 changes: 9 additions & 0 deletions .changeset/silly-paws-leave.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
---
"@fuel-ts/contract": minor
"@fuel-ts/errors": minor
"@fuel-ts/program": minor
"@fuel-ts/transactions": minor
"@fuel-ts/wallet": minor
---

purging constant MAX_GAS_PER_TX
13 changes: 10 additions & 3 deletions packages/contract/src/contract-factory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import { Contract } from '@fuel-ts/program';
import type { CreateTransactionRequestLike, Provider } from '@fuel-ts/providers';
import { CreateTransactionRequest } from '@fuel-ts/providers';
import type { StorageSlot } from '@fuel-ts/transactions';
import { MAX_GAS_PER_TX } from '@fuel-ts/transactions/configs';
import type { Account } from '@fuel-ts/wallet';

import { getContractId, getContractStorageRoot, includeHexPrefix } from './util';
Expand Down Expand Up @@ -81,7 +80,7 @@ export default class ContractFactory {
* @param provider - The provider to be associated with the factory.
* @returns A new ContractFactory instance.
*/
connect(provider: Provider | null) {
connect(provider: Provider) {
return new ContractFactory(this.bytecode, this.interface, provider);
}

Expand All @@ -105,11 +104,19 @@ export default class ContractFactory {
storageSlots: storageSlots || [],
};

if (!this.provider) {
throw new FuelError(
ErrorCode.MISSING_PROVIDER,
'Cannot create transaction request without provider'
);
}

const { maxGasPerTx } = this.provider.getGasConfig();
const stateRoot = options.stateRoot || getContractStorageRoot(options.storageSlots);
const contractId = getContractId(this.bytecode, options.salt, stateRoot);
const transactionRequest = new CreateTransactionRequest({
gasPrice: 0,
gasLimit: MAX_GAS_PER_TX,
gasLimit: maxGasPerTx,
bytecodeWitnessIndex: 0,
witnesses: [this.bytecode],
...options,
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 @@ -24,6 +24,7 @@ export enum ErrorCode {
INVALID_URL = 'invalid-url',
CHAIN_INFO_CACHE_EMPTY = 'chain-info-cache-empty',
NODE_INFO_CACHE_EMPTY = 'node-info-cache-empty',
MISSING_PROVIDER = 'missing-provider',

// wallet
INSUFFICIENT_BALANCE = 'insufficient-balance',
Expand Down
44 changes: 29 additions & 15 deletions packages/fuel-gauge/src/contract-factory.test.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { FuelError, ErrorCode } from '@fuel-ts/errors';
import { expectToThrowFuelError } from '@fuel-ts/errors/test-utils';
import { generateTestWallet } from '@fuel-ts/wallet/test-utils';
import { readFileSync } from 'fs';
import {
Expand All @@ -13,26 +15,26 @@ import { join } from 'path';

import storageSlots from '../fixtures/forc-projects/storage-test-contract/out/debug/storage-test-storage_slots.json';

// load the byteCode of the contract, generated from Sway source
const byteCode = readFileSync(
join(__dirname, '../fixtures/forc-projects/storage-test-contract/out/debug/storage-test.bin')
);

// load the JSON abi of the contract, generated from Sway source
const abi = JSON.parse(
readFileSync(
join(
__dirname,
'../fixtures/forc-projects/storage-test-contract/out/debug/storage-test-abi.json'
)
).toString()
);

describe('Contract Factory', () => {
const createContractFactory = async () => {
const provider = await Provider.create(FUEL_NETWORK_URL);
const wallet = await generateTestWallet(provider, [[5_000_000, BaseAssetId]]);

// load the byteCode of the contract, generated from Sway source
const byteCode = readFileSync(
join(__dirname, '../fixtures/forc-projects/storage-test-contract/out/debug/storage-test.bin')
);

// load the JSON abi of the contract, generated from Sway source
const abi = JSON.parse(
readFileSync(
join(
__dirname,
'../fixtures/forc-projects/storage-test-contract/out/debug/storage-test-abi.json'
)
).toString()
);

// send byteCode and ABI to ContractFactory to load
const factory = new ContractFactory(byteCode, abi, wallet);
return factory;
Expand Down Expand Up @@ -188,4 +190,16 @@ describe('Contract Factory', () => {
const { value: vB256 } = await contract.functions.return_b256().simulate();
expect(vB256).toEqual(b256);
});

it('should throws if calls createTransactionRequest is called when provider is not set', async () => {
const factory = new ContractFactory(byteCode, abi);

await expectToThrowFuelError(
() => factory.createTransactionRequest(),
new FuelError(
ErrorCode.MISSING_PROVIDER,
'Cannot create transaction request without provider'
)
);
});
});
6 changes: 4 additions & 2 deletions packages/program/src/functions/base-invocation-scope.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import { bn, toNumber } from '@fuel-ts/math';
import type { Provider, CoinQuantity, TransactionRequest } from '@fuel-ts/providers';
import { transactionRequestify, ScriptTransactionRequest } from '@fuel-ts/providers';
import { InputType } from '@fuel-ts/transactions';
import { MAX_GAS_PER_TX } from '@fuel-ts/transactions/configs';
import type { BaseWalletUnlocked } from '@fuel-ts/wallet';
import * as asm from '@fuels/vm-asm';

Expand Down Expand Up @@ -63,8 +62,11 @@ export class BaseInvocationScope<TReturn = any> {
constructor(program: AbstractProgram, isMultiCall: boolean) {
this.program = program;
this.isMultiCall = isMultiCall;

const provider = program.provider as Provider;
const { maxGasPerTx } = provider.getGasConfig();
this.transactionRequest = new ScriptTransactionRequest({
gasLimit: MAX_GAS_PER_TX,
gasLimit: maxGasPerTx,
});
}

Expand Down
5 changes: 0 additions & 5 deletions packages/transactions/src/configs.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,9 @@
import { bn } from '@fuel-ts/math';

/** Maximum contract size, in bytes. */
export const CONTRACT_MAX_SIZE = 16 * 1024;

/** Maximum number of witnesses. */
export const MAX_WITNESSES = 16;

/** Maximum gas per transaction. */
export const MAX_GAS_PER_TX = bn(10_000_000);

/**
* Gas Price factor this is used to calculate
* This is used to calculate the gas fee in Native Coins.
Expand Down
11 changes: 6 additions & 5 deletions packages/wallet/src/account.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ import {
ScriptTransactionRequest,
transactionRequestify,
} from '@fuel-ts/providers';
import { MAX_GAS_PER_TX } from '@fuel-ts/transactions/configs';

import {
composeScriptForTransferringToContract,
Expand Down Expand Up @@ -232,8 +231,8 @@ export class Account extends AbstractAccount {
/** Tx Params */
txParams: TxParamsType = {}
): Promise<TransactionResponse> {
const params: TxParamsType = { gasLimit: MAX_GAS_PER_TX, ...txParams };

const { maxGasPerTx } = this.provider.getGasConfig();
const params: TxParamsType = { gasLimit: maxGasPerTx, ...txParams };
const request = new ScriptTransactionRequest(params);
request.addCoinOutput(destination, amount, assetId);

Expand Down Expand Up @@ -282,8 +281,9 @@ export class Account extends AbstractAccount {
assetId
);

const { maxGasPerTx } = this.provider.getGasConfig();
const request = new ScriptTransactionRequest({
gasLimit: MAX_GAS_PER_TX,
gasLimit: maxGasPerTx,
...txParams,
script,
scriptData,
Expand Down Expand Up @@ -340,7 +340,8 @@ export class Account extends AbstractAccount {
]);

// build the transaction
const params = { script, gasLimit: MAX_GAS_PER_TX, ...txParams };
const { maxGasPerTx } = this.provider.getGasConfig();
const params = { script, gasLimit: maxGasPerTx, ...txParams };
const request = new ScriptTransactionRequest(params);

const { gasPriceFactor } = this.provider.getGasConfig();
Expand Down

0 comments on commit 30d1d8e

Please sign in to comment.