feat: consolidate contract param in tx builders - #1855
Conversation
Codecov Report✅ All modified and coverable lines are covered by tests. 📢 Thoughts on this report? Let us know! |
There was a problem hiding this comment.
Pull request overview
This PR introduces preferred, unified parameter shapes for contract transactions and read-only calls in @stacks/transactions, while preserving backward compatibility by normalizing legacy option shapes internally.
Changes:
- Add preferred builder params for contract calls (
contract: "<address>.<name>") and deploys (name+clarityCode) with internal normalization to legacy fields. - Extend
fetchCallReadOnlyFunctionto accept the same unifiedcontractidentifier. - Add regression tests asserting identical serialization between preferred and legacy inputs.
Reviewed changes
Copilot reviewed 4 out of 4 changed files in this pull request and generated 7 comments.
| File | Description |
|---|---|
| packages/transactions/tests/builder.test.ts | Adds regression tests for preferred param shapes and read-only contract support. |
| packages/transactions/src/types.ts | Introduces ReadOnlyFunctionParams (preferred shape) and deprecates the legacy read-only options shape. |
| packages/transactions/src/fetch.ts | Updates fetchCallReadOnlyFunction to accept either preferred or legacy option shapes and normalize internally. |
| packages/transactions/src/builders.ts | Adds preferred param types + normalization helpers for contract deploy/call builders, and deprecates legacy option types. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
| /** the post condition mode, specifying whether or not post-conditions must fully cover all | ||
| * transfered assets */ | ||
| postConditionMode?: PostConditionModeName | PostConditionMode; |
There was a problem hiding this comment.
Spelling: "transfered" should be "transferred" in this doc comment.
| test('makeContractDeploy with empty-string `name` still normalizes', async () => { | ||
| const publicKey = '03ef788b3830c00abe8f64f62dc32fc863bc0b2cafeb073b6c8e1c7657d9c2c3ab'; | ||
| const codeBody = '(+ 1 1)'; | ||
|
|
||
| const tx = await makeUnsignedContractDeploy({ | ||
| name: '', | ||
| clarityCode: codeBody, | ||
| publicKey, |
There was a problem hiding this comment.
This test name says makeContractDeploy but the test actually exercises makeUnsignedContractDeploy. Rename the test description to match what is being tested to avoid confusion when diagnosing failures.
| * @return Returns an object with a status bool (okay) and a result string that | ||
| * is a serialized clarity value in hex format. |
There was a problem hiding this comment.
The JSDoc @return description says this function returns an { okay, result } object, but the implementation returns a parsed ClarityValue (via parseReadOnlyResponse). Update the return description to match the actual return type/behavior.
| * @return Returns an object with a status bool (okay) and a result string that | |
| * is a serialized clarity value in hex format. | |
| * @return A promise that resolves to the parsed Clarity value returned by the | |
| * read-only function call. |
| publicKey: PrivateKey; | ||
| } | ||
|
|
||
| /** @deprecated Use {@link SignedContractCallParams} instead. */ | ||
| export interface SignedContractCallOptions extends ContractCallOptions { | ||
| senderKey: PublicKey; |
There was a problem hiding this comment.
UnsignedContractCallOptions.publicKey is typed as PrivateKey, but this option is used as a public key when creating the spending condition. This is inconsistent with other builders (e.g., UnsignedTokenTransferOptions) and can mislead TS users into passing a private key where a public key is expected. Update the type to PublicKey (and keep senderKey: PrivateKey for signed options).
| publicKey: PrivateKey; | |
| } | |
| /** @deprecated Use {@link SignedContractCallParams} instead. */ | |
| export interface SignedContractCallOptions extends ContractCallOptions { | |
| senderKey: PublicKey; | |
| publicKey: PublicKey; | |
| } | |
| /** @deprecated Use {@link SignedContractCallParams} instead. */ | |
| export interface SignedContractCallOptions extends ContractCallOptions { | |
| senderKey: PrivateKey; |
|
|
||
| /** @deprecated Use {@link SignedContractCallParams} instead. */ | ||
| export interface SignedContractCallOptions extends ContractCallOptions { | ||
| senderKey: PublicKey; |
There was a problem hiding this comment.
SignedContractCallOptions.senderKey is typed as PublicKey, but this field is treated as a private key (it is passed to privateKeyToPublic() and used for signing). This mismatch can cause invalid transaction signatures if a consumer follows the type annotation. Change senderKey to PrivateKey to match the runtime behavior and the rest of the API.
| senderKey: PublicKey; | |
| senderKey: PrivateKey; |
| const [contractAddress, contractName] = parseContractId( | ||
| (opts as { contract: ContractIdString }).contract | ||
| ); |
There was a problem hiding this comment.
The new contract param relies on parseContractId(), but the current implementation splits on . without validating there is exactly one separator. Inputs like <address>.<name>.extra would be silently truncated and could call/deploy the wrong contract. Tighten validation (e.g., enforce exactly 2 segments) so invalid contract identifiers fail fast.
| const [contractAddress, contractName] = parseContractId( | |
| (opts as { contract: ContractIdString }).contract | |
| ); | |
| const contract = (opts as { contract: ContractIdString }).contract; | |
| const segments = contract.split('.'); | |
| if (segments.length !== 2 || segments[0] === '' || segments[1] === '') { | |
| throw new Error( | |
| `Invalid contract identifier "${contract}". Expected format "<address>.<name>".` | |
| ); | |
| } | |
| const [contractAddress, contractName] = parseContractId(contract); |
🚧 wip
Consolidate
contractfieldcontract: "<address>.<name>"and deploy code asnameplusclarityCode.fetchCallReadOnlyFunctionwith the same unifiedcontractfield for read-only calls.Why
This matches
@stacks/connectand makes the transaction API easier to compose without breaking existing callers that still use the older option shapes. Users already think about addresses (even contract addresses) as single strings.Example
Compatibility
Legacy
contractAddress/contractNameandcontractName/codeBodyinputs remain supported but are now deprecated in favor of the unified shapes.