Skip to content

Commit 4d1f623

Browse files
danielbatenedsalk
andauthored
feat: improve tx preparation for a Script and implement txId helper (#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ć <[email protected]> --------- Co-authored-by: Nedim Salkić <[email protected]>
1 parent c9a8b33 commit 4d1f623

File tree

7 files changed

+72
-4
lines changed

7 files changed

+72
-4
lines changed

.changeset/gold-falcons-nail.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@fuel-ts/program": patch
3+
---
4+
5+
Add transaction id helper function to base invocation scope

apps/docs-snippets/src/guide/scripts/script-with-configurable.test.ts

Lines changed: 38 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,23 @@
1-
import type { WalletUnlocked } from 'fuels';
1+
import type { WalletUnlocked, Provider } from 'fuels';
22
import { Script, BN } from 'fuels';
33

4-
import { DocSnippetProjectsEnum, getDocsSnippetsForcProject } from '../../../test/fixtures/forc-projects';
4+
import {
5+
DocSnippetProjectsEnum,
6+
getDocsSnippetsForcProject,
7+
} from '../../../test/fixtures/forc-projects';
58
import { getTestWallet } from '../../utils';
69

710
describe(__filename, () => {
811
let wallet: WalletUnlocked;
912
let gasPrice: BN;
13+
let provider: Provider;
1014
const { abiContents, binHexlified } = getDocsSnippetsForcProject(
1115
DocSnippetProjectsEnum.SUM_SCRIPT
1216
);
1317

1418
beforeAll(async () => {
1519
wallet = await getTestWallet();
20+
provider = wallet.provider;
1621
({ minGasPrice: gasPrice } = wallet.provider.getGasConfig());
1722
});
1823

@@ -35,4 +40,35 @@ describe(__filename, () => {
3540
expect(new BN(value as number).toNumber()).toEqual(expectedTotal);
3641
// #endregion script-with-configurable-contants-2
3742
});
43+
44+
it('prepares a script and retrieves the id before submission', async () => {
45+
const argument = 10;
46+
const expected = 20;
47+
48+
// #region preparing-scripts
49+
const script = new Script(binHexlified, abiContents, wallet);
50+
const { minGasPrice, maxGasPerTx } = provider.getGasConfig();
51+
52+
const tx = await script.functions.main(argument);
53+
54+
// Set the call parameters
55+
tx.callParams({ gasLimit: 100 });
56+
57+
// Set the transaction parameters
58+
tx.txParams({ gasPrice: minGasPrice, gasLimit: maxGasPerTx });
59+
60+
// Get the entire transaction request prior to
61+
const txRequest = await tx.getTransactionRequest();
62+
63+
// Get the transaction ID
64+
const txId = await tx.getTransactionId();
65+
66+
// Retrieve the value of the call and the actual gas used
67+
const { value, gasUsed } = await tx.call();
68+
// #endregion preparing-scripts
69+
expect(txRequest).toBeDefined();
70+
expect(txId).toBeDefined();
71+
expect(new BN(value as number).toNumber()).toEqual(expected);
72+
expect(new BN(gasUsed).toNumber()).toBeGreaterThan(0);
73+
});
3874
});

apps/docs/.vitepress/config.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -363,6 +363,10 @@ export default defineConfig({
363363
text: 'Calling A Script',
364364
link: '/guide/scripts/calling-a-script',
365365
},
366+
{
367+
text: 'Preparing A Script',
368+
link: '/guide/scripts/preparing-a-script',
369+
},
366370
{
367371
text: 'Script With Configurable Constants',
368372
link: '/guide/scripts/script-with-configurable-constants',
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
# Preparing a Script Transaction
2+
3+
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.
4+
5+
<<< @/../../docs-snippets/src/guide/scripts/script-with-configurable.test.ts#preparing-scripts{ts:line-numbers}

packages/program/package.json

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,14 +39,15 @@
3939
"dependencies": {
4040
"@fuel-ts/abi-coder": "workspace:*",
4141
"@fuel-ts/address": "workspace:*",
42+
"@fuel-ts/errors": "workspace:*",
43+
"@fuel-ts/hasher": "workspace:^",
4244
"@fuel-ts/interfaces": "workspace:*",
4345
"@fuel-ts/math": "workspace:*",
4446
"@fuel-ts/providers": "workspace:*",
4547
"@fuel-ts/transactions": "workspace:*",
48+
"@fuel-ts/utils": "workspace:*",
4649
"@fuel-ts/versions": "workspace:*",
4750
"@fuel-ts/wallet": "workspace:*",
48-
"@fuel-ts/errors": "workspace:*",
49-
"@fuel-ts/utils": "workspace:*",
5051
"@fuels/vm-asm": "0.36.1",
5152
"ethers": "^6.7.1"
5253
},

packages/program/src/functions/base-invocation-scope.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
/* eslint-disable @typescript-eslint/no-explicit-any */
22
import type { InputValue } from '@fuel-ts/abi-coder';
33
import { ErrorCode, FuelError } from '@fuel-ts/errors';
4+
import { hashTransaction } from '@fuel-ts/hasher';
45
import type { AbstractContract, AbstractProgram } from '@fuel-ts/interfaces';
56
import type { BN } from '@fuel-ts/math';
67
import { bn, toNumber } from '@fuel-ts/math';
@@ -372,4 +373,17 @@ export class BaseInvocationScope<TReturn = any> {
372373

373374
return provider;
374375
}
376+
377+
/**
378+
* Obtains the ID of a transaction.
379+
*
380+
* @param chainId - the chainId to use to hash the transaction with
381+
* @returns the ID of the transaction.
382+
*/
383+
async getTransactionId(chainId?: number): Promise<string> {
384+
const chainIdToHash = chainId ?? (await this.getProvider().getChainId());
385+
386+
const transactionRequest = await this.getTransactionRequest();
387+
return hashTransaction(transactionRequest, chainIdToHash);
388+
}
375389
}

pnpm-lock.yaml

Lines changed: 3 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)