Skip to content

Commit

Permalink
chore: improve fuel-gauge tests (#2789)
Browse files Browse the repository at this point in the history
Co-authored-by: Chad Nehemiah <chad.nehemiah94@gmail.com>
  • Loading branch information
nedsalk and maschad authored Jul 18, 2024
1 parent 6321c16 commit 35d2e65
Show file tree
Hide file tree
Showing 11 changed files with 109 additions and 290 deletions.
41 changes: 0 additions & 41 deletions packages/fuel-gauge/src/await-execution.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,45 +38,4 @@ describe('await-execution', () => {

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

test.skip('transferring funds with awaitExecution works', async () => {
using launched = await launchTestNode();

const { provider } = launched;

const {
wallets: [genesisWallet],
} = launched;

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

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

await genesisWallet.transfer(destination.address, 100, provider.getBaseAssetId(), {
gasLimit: 10_000,
});

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

test('withdrawToBaseLayer works with awaitExecution', async () => {
using launched = await launchTestNode();

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

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

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

await genesisWallet.withdrawToBaseLayer(destination.address, 100, {
gasLimit: 44442,
});

expect(sendTransactionSpy).toHaveBeenCalledTimes(1);
});
});
36 changes: 14 additions & 22 deletions packages/fuel-gauge/src/call-test-contract.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@ import bytecode from '../test/typegen/contracts/CallTestContractAbi.hex';

import { launchTestContract } from './utils';

function setupContract() {
return launchTestContract({ deployer: CallTestContractAbi__factory, bytecode });
}
const U64_MAX = bn(2).pow(64).sub(1);

/**
Expand All @@ -15,8 +18,7 @@ const U64_MAX = bn(2).pow(64).sub(1);
*/
describe('CallTestContract', () => {
it.each([0, 1337, U64_MAX.sub(1)])('can call a contract with u64 (%p)', async (num) => {
using contract = await launchTestContract({ deployer: CallTestContractAbi__factory, bytecode });

using contract = await setupContract();
const { waitForResult } = await contract.functions.foo(num).call();
const { value } = await waitForResult();
expect(value.toHex()).toEqual(bn(num).add(1).toHex());
Expand All @@ -30,16 +32,15 @@ describe('CallTestContract', () => {
[{ a: false, b: U64_MAX.sub(1) }],
[{ a: true, b: U64_MAX.sub(1) }],
])('can call a contract with structs (%p)', async (struct) => {
using contract = await launchTestContract({ deployer: CallTestContractAbi__factory, bytecode });

using contract = await setupContract();
const { waitForResult } = await contract.functions.boo(struct).call();
const { value } = await waitForResult();
expect(value.a).toEqual(!struct.a);
expect(value.b.toHex()).toEqual(bn(struct.b).add(1).toHex());
});

it('can call a function with empty arguments', async () => {
using contract = await launchTestContract({ deployer: CallTestContractAbi__factory, bytecode });
using contract = await setupContract();

const call1 = await contract.functions.empty().call();
const { value: empty } = await call1.waitForResult();
Expand All @@ -60,7 +61,7 @@ describe('CallTestContract', () => {
});

it('function with empty return should resolve undefined', async () => {
using contract = await launchTestContract({ deployer: CallTestContractAbi__factory, bytecode });
using contract = await setupContract();

// Call method with no params but with no result and no value on config
const { waitForResult } = await contract.functions.return_void().call();
Expand Down Expand Up @@ -138,10 +139,7 @@ describe('CallTestContract', () => {
async (method, { values, expected }) => {
// Type cast to Contract because of the dynamic nature of the test
// But the function names are type-constrained to correct Contract's type
using contract = await launchTestContract({
deployer: CallTestContractAbi__factory,
bytecode,
});
using contract = await setupContract();

const { waitForResult } = await (contract as Contract).functions[method](...values).call();
const { value } = await waitForResult();
Expand All @@ -155,7 +153,7 @@ describe('CallTestContract', () => {
);

it('Forward amount value on contract call', async () => {
using contract = await launchTestContract({ deployer: CallTestContractAbi__factory, bytecode });
using contract = await setupContract();
const baseAssetId = contract.provider.getBaseAssetId();
const { waitForResult } = await contract.functions
.return_context_amount()
Expand All @@ -170,7 +168,7 @@ describe('CallTestContract', () => {
});

it('Forward asset_id on contract call', async () => {
using contract = await launchTestContract({ deployer: CallTestContractAbi__factory, bytecode });
using contract = await setupContract();

const assetId = ASSET_A;
const { waitForResult } = await contract.functions
Expand All @@ -186,7 +184,7 @@ describe('CallTestContract', () => {
});

it('Forward asset_id on contract simulate call', async () => {
using contract = await launchTestContract({ deployer: CallTestContractAbi__factory, bytecode });
using contract = await setupContract();

const assetId = ASSET_A;
const { waitForResult } = await contract.functions
Expand All @@ -202,7 +200,7 @@ describe('CallTestContract', () => {
});

it('can make multiple calls', async () => {
using contract = await launchTestContract({ deployer: CallTestContractAbi__factory, bytecode });
using contract = await setupContract();

const num = 1337;
const numC = 10;
Expand Down Expand Up @@ -240,20 +238,14 @@ describe('CallTestContract', () => {
});

it('Calling a simple contract function does only one dry run', async () => {
using contract = await launchTestContract({
deployer: CallTestContractAbi__factory,
bytecode,
});
using contract = await setupContract();
const dryRunSpy = vi.spyOn(contract.provider.operations, 'dryRun');
await contract.functions.no_params().call();
expect(dryRunSpy).toHaveBeenCalledOnce();
});

it('Simulating a simple contract function does two dry runs', async () => {
using contract = await launchTestContract({
deployer: CallTestContractAbi__factory,
bytecode,
});
using contract = await setupContract();
const dryRunSpy = vi.spyOn(contract.provider.operations, 'dryRun');

await contract.functions.no_params().simulate();
Expand Down
63 changes: 5 additions & 58 deletions packages/fuel-gauge/src/doc-examples.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { Bech32Address, BigNumberish, Bytes, JsonAbi, WalletLocked } from 'fuels';
import type { Bech32Address, BigNumberish, Bytes, WalletLocked } from 'fuels';
import {
Predicate,
bn,
Expand Down Expand Up @@ -277,67 +277,14 @@ describe('Doc Examples', () => {

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

const AbiInputs: JsonAbi = {
types: [
{
typeId: 0,
type: 'bool',
components: null,
typeParameters: null,
},
{
typeId: 1,
type: 'struct B512',
components: null,
typeParameters: null,
},
{
typeId: 2,
type: '[_; 3]',
components: [
{
name: '__array_element',
type: 1,
typeArguments: null,
},
],

typeParameters: null,
},
],
functions: [
{
inputs: [
{
name: 'data',
type: 2,
typeArguments: null,
},
],
name: 'main',
output: {
name: '',
type: 0,
typeArguments: null,
},
attributes: null,
},
],
loggedTypes: [],
configurables: [],
messagesTypes: [],
};
const dataToSign = '0x0000000000000000000000000000000000000000000000000000000000000000';
const signature1 = await wallet1.signMessage(dataToSign);
const signature2 = await wallet2.signMessage(dataToSign);
const signature3 = await wallet3.signMessage(dataToSign);
const signatures = [signature1, signature2, signature3];
const predicate = new Predicate({
bytecode: PredicateTripleSigAbi__factory.bin,
provider,
abi: AbiInputs,
inputData: [signatures],
});
const predicate = PredicateTripleSigAbi__factory.createInstance(provider, [
[signature1, signature2, signature3],
]);

const amountToPredicate = 600_000;
const amountToReceiver = 100;

Expand Down
28 changes: 11 additions & 17 deletions packages/fuel-gauge/src/fee.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { ContractFactory, Predicate, ScriptTransactionRequest, Wallet, getRandomB256 } from 'fuels';
import { ContractFactory, ScriptTransactionRequest, Wallet, getRandomB256 } from 'fuels';
import type { BN } from 'fuels';
import { launchTestNode, ASSET_A, ASSET_B, expectToBeInRange } from 'fuels/test-utils';

Expand Down Expand Up @@ -193,21 +193,20 @@ describe('Fee', () => {
});

it('should ensure fee is properly calculated on a contract call', async () => {
using launched = await launchTestNode();
using launched = await launchTestNode({
contractsConfigs: [
{
deployer: CallTestContractAbi__factory,
bytecode: CallTestContractAbiHex,
},
],
});

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

const factory = new ContractFactory(
CallTestContractAbiHex,
CallTestContractAbi__factory.abi,
wallet
);

const deploy = await factory.deployContract();
const { contract } = await deploy.waitForResult();

const balanceBefore = await wallet.getBalance();

const { waitForResult } = await contract.functions.sum_multparams(1, 2, 3, 4, 5).call();
Expand Down Expand Up @@ -319,12 +318,7 @@ describe('Fee', () => {
wallets: [wallet],
} = launched;

const predicate = new Predicate({
bytecode: PredicateU32Abi__factory.bin,
abi: PredicateU32Abi__factory.abi,
provider,
inputData: [1078],
});
const predicate = PredicateU32Abi__factory.createInstance(provider, [1078]);

const tx1 = await wallet.transfer(predicate.address, 1_000_000, provider.getBaseAssetId());
await tx1.wait();
Expand Down
3 changes: 0 additions & 3 deletions packages/fuel-gauge/src/funding-transaction.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,6 @@ describe('Funding Transactions', () => {
const initialAmount = 500_000;
using launched = await launchTestNode({
walletsConfig: {
count: 2,
amountPerCoin: initialAmount,
},
});
Expand Down Expand Up @@ -163,7 +162,6 @@ describe('Funding Transactions', () => {
const initialAmount = 500_000;
using launched = await launchTestNode({
walletsConfig: {
count: 2,
amountPerCoin: initialAmount,
},
});
Expand Down Expand Up @@ -216,7 +214,6 @@ describe('Funding Transactions', () => {
const initialAmount = 100_000;
using launched = await launchTestNode({
walletsConfig: {
count: 2,
amountPerCoin: initialAmount,
},
});
Expand Down
27 changes: 12 additions & 15 deletions packages/fuel-gauge/src/is-function-readonly.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,40 +3,37 @@ import StorageTestContractAbiHex from '../test/typegen/contracts/StorageTestCont

import { launchTestContract } from './utils';

function setupContract() {
return launchTestContract({
deployer: StorageTestContractAbi__factory,
bytecode: StorageTestContractAbiHex,
});
}
/**
* @group node
* @group browser
*/
describe('isReadOnly', () => {
test('isReadOnly returns true for a read-only function', async () => {
using contractInstance = await launchTestContract({
deployer: StorageTestContractAbi__factory,
bytecode: StorageTestContractAbiHex,
});
using contract = await setupContract();

const isReadOnly = contractInstance.functions.counter.isReadOnly();
const isReadOnly = contract.functions.counter.isReadOnly();

expect(isReadOnly).toBe(true);
});

test('isReadOnly returns false for a function containing write operations', async () => {
using contractInstance = await launchTestContract({
deployer: StorageTestContractAbi__factory,
bytecode: StorageTestContractAbiHex,
});
using contract = await setupContract();

const isReadOnly = contractInstance.functions.increment_counter.isReadOnly();
const isReadOnly = contract.functions.increment_counter.isReadOnly();

expect(isReadOnly).toBe(false);
});

test('isReadOnly does not throw a runtime error for a function that does not use storage', async () => {
using contractInstance = await launchTestContract({
deployer: StorageTestContractAbi__factory,
bytecode: StorageTestContractAbiHex,
});
using contract = await setupContract();

const isReadOnly = contractInstance.functions.return_true.isReadOnly();
const isReadOnly = contract.functions.return_true.isReadOnly();

expect(isReadOnly).toBe(true);
});
Expand Down
Loading

0 comments on commit 35d2e65

Please sign in to comment.