* Specifies or updates the transaction details to be sent. * * This method sets up a new transaction or updates the currently selected transaction with the provided parameters. It validates the input parameters and throws an error if any are invalid. If a transaction is already selected, it up
({
chainId,
to,
value = '0',
data = '0x',
}: TransactionParams)
| 877 | * - The transaction context is stored in the instance until it is named or added to a batch. |
| 878 | */ |
| 879 | transaction({ |
| 880 | chainId, |
| 881 | to, |
| 882 | value = '0', |
| 883 | data = '0x', |
| 884 | }: TransactionParams): ITransaction { |
| 885 | if (chainId === undefined || chainId === null) { |
| 886 | this.throwError( |
| 887 | 'transaction(): chainId is required. Please specify the target network explicitly.' |
| 888 | ); |
| 889 | } |
| 890 | |
| 891 | if (typeof chainId !== 'number' || !Number.isInteger(chainId)) { |
| 892 | this.throwError('transaction(): chainId must be a valid number.'); |
| 893 | } |
| 894 | |
| 895 | if (!to) { |
| 896 | this.throwError('transaction(): to is required.'); |
| 897 | } |
| 898 | |
| 899 | if (!isAddress(to)) { |
| 900 | this.throwError(`transaction(): '${to}' is not a valid address.`); |
| 901 | } |
| 902 | |
| 903 | let parsedValue: bigint; |
| 904 | try { |
| 905 | parsedValue = typeof value === 'bigint' ? value : BigInt(value); |
| 906 | if (parsedValue < BigInt(0)) { |
| 907 | throw new Error(); |
| 908 | } |
| 909 | } catch { |
| 910 | this.throwError( |
| 911 | 'transaction(): value must be a non-negative bigint or numeric string.' |
| 912 | ); |
| 913 | } |
| 914 | |
| 915 | // Start new transaction or update existing |
| 916 | if (this.selectedTransactionName) { |
| 917 | // Updating existing transaction |
| 918 | this.workingTransaction = { |
| 919 | ...this.workingTransaction, |
| 920 | chainId, |
| 921 | to, |
| 922 | value, |
| 923 | data, |
| 924 | }; |
| 925 | log( |
| 926 | 'transaction(): Updated existing transaction', |
| 927 | this.workingTransaction, |
| 928 | this.debugMode |
| 929 | ); |
| 930 | } else { |
| 931 | // Creating new transaction |
| 932 | this.workingTransaction = { |
| 933 | chainId, |
| 934 | to, |
| 935 | value, |
| 936 | data, |