Skip to content

Commit

Permalink
adding tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Torres-ssf committed May 9, 2024
1 parent 1b2a3d9 commit f8bed39
Show file tree
Hide file tree
Showing 2 changed files with 130 additions and 1 deletion.
46 changes: 46 additions & 0 deletions packages/account/src/account.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { ZeroBytes32 } from '@fuel-ts/address/configs';
import { ErrorCode, FuelError } from '@fuel-ts/errors';
import { expectToThrowFuelError } from '@fuel-ts/errors/test-utils';
import { bn } from '@fuel-ts/math';
import { PolicyType } from '@fuel-ts/transactions';
import { ASSET_A, ASSET_B } from '@fuel-ts/utils/test-utils';

import { Account } from './account';
Expand Down Expand Up @@ -409,6 +410,28 @@ describe('Account', () => {
expect(receiverBalances).toEqual([{ assetId: baseAssetId, amount: bn(1) }]);
});

it('can set "gasLimit" and "maxFee" when transferring amounts', async () => {
const sender = await generateTestWallet(provider, [[500_000, baseAssetId]]);
const receiver = Address.fromRandom();

const gasLimit = 30_000;
const maxFee = 15_000;

const request = await sender.createTransfer(receiver, 1, baseAssetId, {
gasLimit,
maxFee,
});

const response = await sender.sendTransaction(request);
const { transaction } = await response.wait();

const { scriptGasLimit, policies } = transaction;
const maxFeePolicy = policies?.find((policy) => policy.type === PolicyType.MaxFee);

expect(scriptGasLimit?.toNumber()).toBe(gasLimit);
expect(bn(maxFeePolicy?.data).toNumber()).toBe(maxFee);
});

it('can transfer with custom TX Params', async () => {
const sender = await generateTestWallet(provider, [[50_000, baseAssetId]]);
const receiver = Wallet.generate({ provider });
Expand Down Expand Up @@ -591,6 +614,29 @@ describe('Account', () => {
expect(senderBalances).toEqual([{ assetId: baseAssetId, amount: bn(expectedRemaining) }]);
});

it('can set "gasLimit" and "maxFee" when withdrawing to base layer', async () => {
const sender = Wallet.generate({
provider,
});

await seedTestWallet(sender, [[500_000, baseAssetId]]);

const recipient = Address.fromRandom();
const amount = 110;

const gasLimit = 100_000;
const maxFee = 50_000;

const tx = await sender.withdrawToBaseLayer(recipient, amount, { gasLimit, maxFee });
const { transaction } = await tx.wait();

const { scriptGasLimit, policies } = transaction;
const maxFeePolicy = policies?.find((policy) => policy.type === PolicyType.MaxFee);

expect(scriptGasLimit?.toNumber()).toBe(gasLimit);
expect(bn(maxFeePolicy?.data).toNumber()).toBe(maxFee);
});

it('should ensure gas price and gas limit are validated when transfering amounts', async () => {
const sender = await generateTestWallet(provider, [[1000, baseAssetId]]);
const receiver = Wallet.generate({ provider });
Expand Down
85 changes: 84 additions & 1 deletion packages/fuel-gauge/src/contract.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import {
ZeroBytes32,
FUEL_NETWORK_URL,
Predicate,
PolicyType,
} from 'fuels';

import { FuelGaugeProjectsEnum, getFuelGaugeForcProject } from '../test/fixtures';
Expand Down Expand Up @@ -817,7 +818,7 @@ describe('Contract', () => {
* to move them to another test suite when addressing https://github.com/FuelLabs/fuels-ts/issues/1043.
*/
it('should tranfer asset to a deployed contract just fine (NATIVE ASSET)', async () => {
const wallet = await generateTestWallet(provider, [[10_000_000_000, baseAssetId]]);
const wallet = await generateTestWallet(provider, [[10_000_000, baseAssetId]]);

const contract = await setupContract();

Expand All @@ -835,6 +836,28 @@ describe('Contract', () => {
expect(finalBalance).toBe(initialBalance + amountToContract.toNumber());
});

it('should set "gasLimit" and "maxFee" when transferring amounts to contract just fine', async () => {
const wallet = await generateTestWallet(provider, [[10_000_000, baseAssetId]]);
const contract = await setupContract();
const amountToContract = 5_000;

const gasLimit = 80_000;
const maxFee = 40_000;

const tx = await wallet.transferToContract(contract.id, amountToContract, baseAssetId, {
gasLimit,
maxFee,
});

const { transaction } = await tx.waitForResult();

const { scriptGasLimit, policies } = transaction;
const maxFeePolicy = policies?.find((policy) => policy.type === PolicyType.MaxFee);

expect(scriptGasLimit?.toNumber()).toBe(gasLimit);
expect(bn(maxFeePolicy?.data).toNumber()).toBe(maxFee);
});

it('should ensure gas price and gas limit are validated when transfering to contract', async () => {
const wallet = await generateTestWallet(provider, [[1000, baseAssetId]]);

Expand Down Expand Up @@ -1144,4 +1167,64 @@ describe('Contract', () => {

expect(value.toNumber()).toBe(initialCounterValue);
});

it('should ensure "maxFee" and "gasLimit" can be set for a contract call', async () => {
const { abiContents, binHexlified } = getFuelGaugeForcProject(
FuelGaugeProjectsEnum.STORAGE_TEST_CONTRACT
);

const wallet = await generateTestWallet(provider, [[150_000, baseAssetId]]);
const factory = new ContractFactory(binHexlified, abiContents, wallet);

const storageContract = await factory.deployContract();

const gasLimit = 200_000;
const maxFee = 100_000;

const {
transactionResult: { transaction },
} = await storageContract.functions
.counter()
.txParams({
gasLimit,
maxFee,
})
.call();

const maxFeePolicy = transaction.policies?.find((policy) => policy.type === PolicyType.MaxFee);
const scriptGasLimit = transaction.scriptGasLimit;

expect(scriptGasLimit?.toNumber()).toBe(gasLimit);
expect(bn(maxFeePolicy?.data).toNumber()).toBe(maxFee);
});

it('should ensure "maxFee" and "gasLimit" can be set on a multicall', async () => {
const contract = await setupContract();

const gasLimit = 500_000;
const maxFee = 250_000;

const {
transactionResult: { transaction },
} = await contract
.multiCall([
contract.functions.foo(1336),
contract.functions.foo(1336),
contract.functions.foo(1336),
contract.functions.foo(1336),
contract.functions.foo(1336),
contract.functions.foo(1336),
contract.functions.foo(1336),
contract.functions.foo(1336),
])
.txParams({ gasLimit, maxFee })
.call();

const { scriptGasLimit, policies } = transaction;

const maxFeePolicy = policies?.find((policy) => policy.type === PolicyType.MaxFee);

expect(scriptGasLimit?.toNumber()).toBe(gasLimit);
expect(bn(maxFeePolicy?.data).toNumber()).toBe(maxFee);
});
});

0 comments on commit f8bed39

Please sign in to comment.