Skip to content

Commit

Permalink
feat: make predicates functions accept request tx params (#383)
Browse files Browse the repository at this point in the history
  • Loading branch information
LuizAsFight committed Jul 9, 2022
1 parent e8758fc commit 3bed0aa
Show file tree
Hide file tree
Showing 3 changed files with 93 additions and 13 deletions.
36 changes: 36 additions & 0 deletions packages/contract/src/__test__/contract.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,24 @@ describe('Contract', () => {
expect(contract.provider).toEqual(provider);
});

it('should fail to execute call if gasLimit is too low', async () => {
const contract = await setup();

let failed;
try {
await contract.functions
.foo(1336)
.txParams({
gasLimit: 1,
})
.call();
} catch (e) {
failed = true;
}

expect(failed).toEqual(true);
});

it('submits multiple calls', async () => {
const contract = await setup();

Expand All @@ -89,6 +107,24 @@ describe('Contract', () => {
expect(results).toEqual([1337n, 1337n]);
});

it('should fail to execute multiple calls if gasLimit is too low', async () => {
const contract = await setup();

let failed;
try {
await contract
.multiCall([contract.functions.foo(1336), contract.functions.foo(1336)])
.txParams({
gasLimit: 1,
})
.call();
} catch (e) {
failed = true;
}

expect(failed).toEqual(true);
});

it('dryRuns multiple calls', async () => {
const contract = await setup();

Expand Down
33 changes: 33 additions & 0 deletions packages/predicate/src/predicate.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -256,4 +256,37 @@ describe('Predicate', () => {
]);
}).rejects.toThrow('Invalid Predicate');
});

// TODO: Enable this test once predicates start to consume gas
// FUELS-TS - https://github.com/FuelLabs/fuels-ts/issues/385
// SPEC - https://github.com/FuelLabs/fuel-specs/issues/119
it.skip('should fail if inform gasLimit too low', async () => {
const receiverAddress = hexlify(randomBytes(32));
const wallet = await setup();
const amountToPredicate = 10n;
const predicate = new Predicate(testPredicateStruct, StructAbiInputs);

const initialPredicateBalance = await setupPredicate(wallet, amountToPredicate, predicate);

const validation: Validation = {
has_account: true,
total_complete: 100n,
};

let failed;
try {
await predicate.submitSpendPredicate(
wallet,
initialPredicateBalance,
receiverAddress,
[validation],
undefined,
{ gasLimit: 1 }
);
} catch (e) {
failed = true;
}

expect(failed).toEqual(true);
});
});
37 changes: 24 additions & 13 deletions packages/predicate/src/predicate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,19 @@ import { AbiCoder } from '@fuel-ts/abi-coder';
import { NativeAssetId } from '@fuel-ts/constants';
import { ContractUtils } from '@fuel-ts/contract';
import type { BigNumberish } from '@fuel-ts/math';
import type { CoinQuantityLike, TransactionResult, Coin } from '@fuel-ts/providers';
import type {
CoinQuantityLike,
TransactionRequestLike,
TransactionResult,
Coin,
} from '@fuel-ts/providers';
import { ScriptTransactionRequest } from '@fuel-ts/providers';
import type { Wallet } from '@fuel-ts/wallet';

type BuildPredicateOptions = {
fundTransaction?: boolean;
} & Pick<TransactionRequestLike, 'gasLimit' | 'gasPrice' | 'bytePrice' | 'maturity'>;

export class Predicate {
bytes: Uint8Array;
address: BytesLike;
Expand All @@ -28,12 +37,15 @@ export class Predicate {
wallet: Wallet,
amountToPredicate: BigNumberish,
assetId: BytesLike = NativeAssetId,
options: {
fundTransaction?: boolean;
} = { fundTransaction: true }
predicateOptions?: BuildPredicateOptions
): Promise<ScriptTransactionRequest> {
const options = {
fundTransaction: true,
...predicateOptions,
};
const request = new ScriptTransactionRequest({
gasLimit: 1000000,
...options,
});

// output is locked behind predicate
Expand All @@ -57,9 +69,7 @@ export class Predicate {
wallet: Wallet,
amountToPredicate: BigNumberish,
assetId: BytesLike = NativeAssetId,
options: {
fundTransaction?: boolean;
} = { fundTransaction: true }
options?: BuildPredicateOptions
): Promise<TransactionResult<'success'>> {
const request = await this.buildPredicateTransaction(
wallet,
Expand All @@ -78,15 +88,18 @@ export class Predicate {
receiverAddress: BytesLike,
predicateData?: InputValue[],
assetId: BytesLike = NativeAssetId,
options: {
fundTransaction?: boolean;
} = { fundTransaction: true }
predicateOptions?: BuildPredicateOptions
): Promise<ScriptTransactionRequest> {
const predicateCoins: Coin[] = await wallet.provider.getCoinsToSpend(this.address, [
[amountToSpend, assetId],
]);
const options = {
fundTransaction: true,
...predicateOptions,
};
const request = new ScriptTransactionRequest({
gasLimit: 1000000,
...options,
});

let encoded: undefined | Uint8Array;
Expand Down Expand Up @@ -129,9 +142,7 @@ export class Predicate {
receiverAddress: BytesLike,
predicateData?: InputValue[],
assetId: BytesLike = NativeAssetId,
options: {
fundTransaction?: boolean;
} = { fundTransaction: true }
options?: BuildPredicateOptions
): Promise<TransactionResult<'success'>> {
const request = await this.buildSpendPredicate(
wallet,
Expand Down

0 comments on commit 3bed0aa

Please sign in to comment.