Skip to content

feat: consolidate contract param in tx builders - #1855

Draft
jannik-stacks wants to merge 11 commits into
mainfrom
param-consolidate
Draft

feat: consolidate contract param in tx builders#1855
jannik-stacks wants to merge 11 commits into
mainfrom
param-consolidate

Conversation

@jannik-stacks

Copy link
Copy Markdown
Collaborator

🚧 wip

Consolidate contract field

  • Add preferred builder params for contract calls and deploys so callers can pass contract: "<address>.<name>" and deploy code as name plus clarityCode.
  • Extend fetchCallReadOnlyFunction with the same unified contract field for read-only calls.
  • Keep the legacy split fields working by normalizing both shapes internally, with the new fields taking precedence when both are present.
  • Add regression tests that prove the new shapes serialize identically to the legacy inputs for signed and unsigned transactions.

Why

This matches @stacks/connect and 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

await makeContractCall({
  contract: 'ST3KC0MTNW34S1ZXD36JYKFD3JJMWA01M55DSJ4JE.kv-store',
  functionName: 'get-value',
  functionArgs: [bufferCV(utf8ToBytes('foo'))],
  senderKey,
  network: STACKS_TESTNET,
});

Compatibility

Legacy contractAddress/contractName and contractName/codeBody inputs remain supported but are now deprecated in favor of the unified shapes.

@codecov-commenter

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.

📢 Thoughts on this report? Let us know!

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 fetchCallReadOnlyFunction to accept the same unified contract identifier.
  • 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.

Comment on lines +250 to +252
/** the post condition mode, specifying whether or not post-conditions must fully cover all
* transfered assets */
postConditionMode?: PostConditionModeName | PostConditionMode;

Copilot AI Apr 16, 2026

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Spelling: "transfered" should be "transferred" in this doc comment.

Copilot uses AI. Check for mistakes.
Comment thread packages/transactions/src/builders.ts Outdated
Comment on lines +3119 to +3126
test('makeContractDeploy with empty-string `name` still normalizes', async () => {
const publicKey = '03ef788b3830c00abe8f64f62dc32fc863bc0b2cafeb073b6c8e1c7657d9c2c3ab';
const codeBody = '(+ 1 1)';

const tx = await makeUnsignedContractDeploy({
name: '',
clarityCode: codeBody,
publicKey,

Copilot AI Apr 16, 2026

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copilot uses AI. Check for mistakes.
Comment thread packages/transactions/src/fetch.ts Outdated
Comment on lines 290 to 291
* @return Returns an object with a status bool (okay) and a result string that
* is a serialized clarity value in hex format.

Copilot AI Apr 16, 2026

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Suggested change
* @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.

Copilot uses AI. Check for mistakes.
Comment thread packages/transactions/src/builders.ts Outdated
Comment on lines 540 to 545
publicKey: PrivateKey;
}

/** @deprecated Use {@link SignedContractCallParams} instead. */
export interface SignedContractCallOptions extends ContractCallOptions {
senderKey: PublicKey;

Copilot AI Apr 16, 2026

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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).

Suggested change
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;

Copilot uses AI. Check for mistakes.
Comment thread packages/transactions/src/builders.ts Outdated

/** @deprecated Use {@link SignedContractCallParams} instead. */
export interface SignedContractCallOptions extends ContractCallOptions {
senderKey: PublicKey;

Copilot AI Apr 16, 2026

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Suggested change
senderKey: PublicKey;
senderKey: PrivateKey;

Copilot uses AI. Check for mistakes.
Comment on lines +559 to +561
const [contractAddress, contractName] = parseContractId(
(opts as { contract: ContractIdString }).contract
);

Copilot AI Apr 16, 2026

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Suggested change
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);

Copilot uses AI. Check for mistakes.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants