Skip to content

Commit

Permalink
feat: improve tx preparation for a Script and implement txId helper (
Browse files Browse the repository at this point in the history
…#1466)

* feat: add helper function to retrieve transaction Id

* feat: install hasher in program,

* feat: add docs for tx preparation in scripts

* chore: linting

* chore: changeset

* chore: fix grammar

Co-authored-by: Nedim Salkić <nedim.salkic@fuel.sh>

---------

Co-authored-by: Nedim Salkić <nedim.salkic@fuel.sh>
  • Loading branch information
danielbate and nedsalk authored Nov 28, 2023
1 parent c9a8b33 commit 4d1f623
Show file tree
Hide file tree
Showing 7 changed files with 72 additions and 4 deletions.
5 changes: 5 additions & 0 deletions .changeset/gold-falcons-nail.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@fuel-ts/program": patch
---

Add transaction id helper function to base invocation scope
Original file line number Diff line number Diff line change
@@ -1,18 +1,23 @@
import type { WalletUnlocked } from 'fuels';
import type { WalletUnlocked, Provider } from 'fuels';
import { Script, BN } from 'fuels';

import { DocSnippetProjectsEnum, getDocsSnippetsForcProject } from '../../../test/fixtures/forc-projects';
import {
DocSnippetProjectsEnum,
getDocsSnippetsForcProject,
} from '../../../test/fixtures/forc-projects';
import { getTestWallet } from '../../utils';

describe(__filename, () => {
let wallet: WalletUnlocked;
let gasPrice: BN;
let provider: Provider;
const { abiContents, binHexlified } = getDocsSnippetsForcProject(
DocSnippetProjectsEnum.SUM_SCRIPT
);

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

Expand All @@ -35,4 +40,35 @@ describe(__filename, () => {
expect(new BN(value as number).toNumber()).toEqual(expectedTotal);
// #endregion script-with-configurable-contants-2
});

it('prepares a script and retrieves the id before submission', async () => {
const argument = 10;
const expected = 20;

// #region preparing-scripts
const script = new Script(binHexlified, abiContents, wallet);
const { minGasPrice, maxGasPerTx } = provider.getGasConfig();

const tx = await script.functions.main(argument);

// Set the call parameters
tx.callParams({ gasLimit: 100 });

// Set the transaction parameters
tx.txParams({ gasPrice: minGasPrice, gasLimit: maxGasPerTx });

// Get the entire transaction request prior to
const txRequest = await tx.getTransactionRequest();

// Get the transaction ID
const txId = await tx.getTransactionId();

// Retrieve the value of the call and the actual gas used
const { value, gasUsed } = await tx.call();
// #endregion preparing-scripts
expect(txRequest).toBeDefined();
expect(txId).toBeDefined();
expect(new BN(value as number).toNumber()).toEqual(expected);
expect(new BN(gasUsed).toNumber()).toBeGreaterThan(0);
});
});
4 changes: 4 additions & 0 deletions apps/docs/.vitepress/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -363,6 +363,10 @@ export default defineConfig({
text: 'Calling A Script',
link: '/guide/scripts/calling-a-script',
},
{
text: 'Preparing A Script',
link: '/guide/scripts/preparing-a-script',
},
{
text: 'Script With Configurable Constants',
link: '/guide/scripts/script-with-configurable-constants',
Expand Down
5 changes: 5 additions & 0 deletions apps/docs/src/guide/scripts/preparing-a-script.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# Preparing a Script Transaction

Akin to Contracts, we can configure the [call parameters](../contracts/call-parameters.md) and [transaction parameters](../contracts/transaction-parameters.md) for Scripts, as well as retrieve the entire transaction request or transaction ID prior to submission.

<<< @/../../docs-snippets/src/guide/scripts/script-with-configurable.test.ts#preparing-scripts{ts:line-numbers}
5 changes: 3 additions & 2 deletions packages/program/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -39,14 +39,15 @@
"dependencies": {
"@fuel-ts/abi-coder": "workspace:*",
"@fuel-ts/address": "workspace:*",
"@fuel-ts/errors": "workspace:*",
"@fuel-ts/hasher": "workspace:^",
"@fuel-ts/interfaces": "workspace:*",
"@fuel-ts/math": "workspace:*",
"@fuel-ts/providers": "workspace:*",
"@fuel-ts/transactions": "workspace:*",
"@fuel-ts/utils": "workspace:*",
"@fuel-ts/versions": "workspace:*",
"@fuel-ts/wallet": "workspace:*",
"@fuel-ts/errors": "workspace:*",
"@fuel-ts/utils": "workspace:*",
"@fuels/vm-asm": "0.36.1",
"ethers": "^6.7.1"
},
Expand Down
14 changes: 14 additions & 0 deletions packages/program/src/functions/base-invocation-scope.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
/* eslint-disable @typescript-eslint/no-explicit-any */
import type { InputValue } from '@fuel-ts/abi-coder';
import { ErrorCode, FuelError } from '@fuel-ts/errors';
import { hashTransaction } from '@fuel-ts/hasher';
import type { AbstractContract, AbstractProgram } from '@fuel-ts/interfaces';
import type { BN } from '@fuel-ts/math';
import { bn, toNumber } from '@fuel-ts/math';
Expand Down Expand Up @@ -372,4 +373,17 @@ export class BaseInvocationScope<TReturn = any> {

return provider;
}

/**
* Obtains the ID of a transaction.
*
* @param chainId - the chainId to use to hash the transaction with
* @returns the ID of the transaction.
*/
async getTransactionId(chainId?: number): Promise<string> {
const chainIdToHash = chainId ?? (await this.getProvider().getChainId());

const transactionRequest = await this.getTransactionRequest();
return hashTransaction(transactionRequest, chainIdToHash);
}
}
3 changes: 3 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 4d1f623

Please sign in to comment.