Conversation
|
Size Change: 0 B Total Size: 1.76 MB ℹ️ View Unchanged
|
There was a problem hiding this comment.
Pull request overview
This PR introduces a generic xdr.Operation<T> typing flow so Operation.fromXDRObject() can preserve the specific operation result type across an XDR → Operation roundtrip, and it relocates generated XDR .d.ts wiring under src/generated to avoid declaration circularities.
Changes:
- Add a generic parameter to
Operation.fromXDRObject()and update many operation builders to returnxdr.Operation<SpecificResultType>. - Rework signer-related types (
SignerOpts,SetOptionsOpts<T>,SetOptionsResult<T>) to support more precise typing. - Update generated XDR
.d.tslink files and build/type tooling ignores (notably.prettierignore, type assertion test expectations).
Reviewed changes
Copilot reviewed 33 out of 37 changed files in this pull request and generated 4 comments.
Show a summary per file
| File | Description |
|---|---|
| types/test.ts | Updates $ExpectType assertions to reflect the new generic fromXDRObject typing. |
| test/unit/operations/clawback_claimable_balance.test.ts | Minor formatting change to an expectation. |
| src/operations/types.ts | Introduces new signer option types, updates result/opts types to use them, and adjusts OperationRecord docs. |
| src/operations/set_trustline_flags.ts | Returns xdr.Operation<SetTrustLineFlagsResult> for typed roundtrip. |
| src/operations/set_options.ts | Makes setOptions generic and returns xdr.Operation<SetOptionsResult<T>>. |
| src/operations/revoke_sponsorship.ts | Returns typed xdr.Operation<...Result> variants for revoke sponsorship ops. |
| src/operations/restore_footprint.ts | Returns xdr.Operation<RestoreFootprintResult>. |
| src/operations/payment.ts | Returns xdr.Operation<PaymentResult>. |
| src/operations/path_payment_strict_send.ts | Returns xdr.Operation<PathPaymentStrictSendResult>. |
| src/operations/path_payment_strict_receive.ts | Returns xdr.Operation<PathPaymentStrictReceiveResult>. |
| src/operations/manage_sell_offer.ts | Returns xdr.Operation<ManageSellOfferResult>. |
| src/operations/manage_data.ts | Returns xdr.Operation<ManageDataResult>. |
| src/operations/manage_buy_offer.ts | Returns xdr.Operation<ManageBuyOfferResult>. |
| src/operations/liquidity_pool_withdraw.ts | Returns xdr.Operation<LiquidityPoolWithdrawResult>. |
| src/operations/liquidity_pool_deposit.ts | Returns xdr.Operation<LiquidityPoolDepositResult>. |
| src/operations/invoke_host_function.ts | Returns xdr.Operation<InvokeHostFunctionResult> for host-function-related builders. |
| src/operations/inflation.ts | Returns xdr.Operation<InflationResult>. |
| src/operations/extend_footprint_ttl.ts | Returns xdr.Operation<ExtendFootprintTTLResult>. |
| src/operations/end_sponsoring_future_reserves.ts | Returns xdr.Operation<EndSponsoringFutureReservesResult>. |
| src/operations/create_passive_sell_offer.ts | Returns xdr.Operation<CreatePassiveSellOfferResult>. |
| src/operations/create_claimable_balance.ts | Returns xdr.Operation<CreateClaimableBalanceResult>. |
| src/operations/create_account.ts | Returns xdr.Operation<CreateAccountResult>. |
| src/operations/clawback_claimable_balance.ts | Returns xdr.Operation<ClawbackClaimableBalanceResult>. |
| src/operations/clawback.ts | Returns xdr.Operation<ClawbackResult>. |
| src/operations/claim_claimable_balance.ts | Returns xdr.Operation<ClaimClaimableBalanceResult>. |
| src/operations/change_trust.ts | Returns xdr.Operation<ChangeTrustResult>. |
| src/operations/bump_sequence.ts | Returns xdr.Operation<BumpSequenceResult>. |
| src/operations/begin_sponsoring_future_reserves.ts | Returns xdr.Operation<BeginSponsoringFutureReservesResult>. |
| src/operations/allow_trust.ts | Returns xdr.Operation<AllowTrustResult>. |
| src/operations/account_merge.ts | Returns xdr.Operation<AccountMergeResult>. |
| src/operation.ts | Makes fromXDRObject accept xdr.Operation<T> to preserve operation type through the roundtrip. |
| src/generated/next_generated.d.ts | Repoints xdr type exports to the local generated declarations. |
| src/generated/next.d.ts | Updates the Operation generic constraint import to reference OperationRecord. |
| src/generated/curr_generated.d.ts | Repoints xdr type exports to the local generated declarations. |
| src/generated/curr.d.ts | Updates the Operation generic constraint to use OperationRecord. |
| package.json | Adjusts build:types copying to only include src/generated/*.d.ts. |
| .prettierignore | Fixes ignore paths and adds types/test.ts to avoid Prettier breaking type assertions. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
| const newSignerXDR1 = StellarSdk.Operation.setOptions({ | ||
| signer: { ed25519PublicKey: sourceKey.publicKey(), weight: '1' } | ||
| }); | ||
| const newSigner1 = StellarSdk.Operation.fromXDRObject(newSignerXDR1); // $ExpectType OperationRecord | ||
| if (newSigner1.type === 'setOptions') { | ||
| newSigner1.signer; // $ExpectType Signer | undefined | ||
| } | ||
| StellarSdk.Operation.fromXDRObject(newSignerXDR1); // $ExpectType SetOptionsResult<{ ed25519PublicKey: string; weight: string; }> | ||
|
|
||
| const newSignerXDR2 = StellarSdk.Operation.setOptions({ | ||
| signer: { sha256Hash: Buffer.from(''), weight: '1' } | ||
| }); | ||
| const newSigner2 = StellarSdk.Operation.fromXDRObject(newSignerXDR2); // $ExpectType OperationRecord | ||
| if (newSigner2.type === 'setOptions') { | ||
| newSigner2.signer; // $ExpectType Signer | undefined | ||
| } | ||
| StellarSdk.Operation.fromXDRObject(newSignerXDR2); // $ExpectType SetOptionsResult<{ sha256Hash: Buffer<ArrayBuffer>; weight: string; }> |
There was a problem hiding this comment.
These $ExpectType assertions for Operation.fromXDRObject(Operation.setOptions(...)) appear to encode the input signer types (e.g. weight: string), but fromXDRObject() reads the weight from XDR and returns it as a number. If the goal is to preserve the operation kind (not the un-normalized input types), the expected types here should likely use weight: number (and Buffer<ArrayBufferLike> for sha256Hash, consistent with the rest of this file).
| export interface BaseSignerOpt { | ||
| weight?: number | string; | ||
| } |
There was a problem hiding this comment.
SignerOpts makes weight optional via BaseSignerOpt, but Operation.setOptions() requires signer.weight at runtime (it passes weight! into new xdr.Signer(...)). This allows code to type-check while producing an invalid XDR / runtime exception when weight is omitted. Suggestion: keep SignerKey-only types for cases like revoke-signer, and introduce a separate SetOptionsSignerOpts where weight is required (or make weight required in SignerOpts and use a different type for revoke-signer).
| export interface RevokeSignerSponsorshipOpts { | ||
| account: string; | ||
| signer: SignerKeyOptions; | ||
| signer: SignerOpts; | ||
| source?: string; |
There was a problem hiding this comment.
RevokeSignerSponsorshipOpts.signer is now typed as SignerOpts, which includes ed25519SignedPayload (and weight) even though revokeSignerSponsorship() only supports ed25519PublicKey, preAuthTx, and sha256Hash and ignores/throws for signed payload. This makes the public type contract broader than the implementation. Suggestion: use a dedicated RevokeSignerSponsorshipSignerOpts/SignerKeyOptions union that matches the supported signer key arms (or extend the implementation to handle ed25519SignedPayload).
| export function setOptions<T extends SignerOpts = never>( | ||
| this: OperationClass, | ||
| opts: SetOptionsOpts, | ||
| ): xdr.Operation { | ||
| opts: SetOptionsOpts<T>, | ||
| ): xdr.Operation<SetOptionsResult<T>> { | ||
| let inflationDest: xdr.AccountId | null = null; |
There was a problem hiding this comment.
The new generic signature ties the returned xdr.Operation type to the input signer shape (SetOptionsResult<T>). However, at runtime fromXDRObject() always returns signer.weight as a number (coming from XDR), even when the caller passed weight as a string. This can make the inferred return type from fromXDRObject(op) disagree with runtime values. Consider decoupling the generic from raw input types (e.g., return xdr.Operation<SetOptionsResult> with a normalized signer type) or normalizing the signer/weight types so the generic matches runtime.
| source?: string; | ||
| } | ||
| export interface BaseSignerOpt { | ||
| weight?: number | string; |
There was a problem hiding this comment.
Should weight be required?
There was a problem hiding this comment.
Yeah it definitely should I am surprised it was not required prior
What
./src/generatedto fix circular dependency in type declarationstypes/test.tsto prettierignore to prevent formatting breaking type assertions