js-stellar-sdk icon indicating copy to clipboard operation
js-stellar-sdk copied to clipboard

modified the AssembledTransaction and it's tests

Open PoulavBhowmick03 opened this issue 4 months ago • 4 comments

Fixes #1059

Description:

  1. Corrected isReadOnly Method Invocation:

    • Issue: Previously, the isReadOnly method was incorrectly invoked on an instance of ScSpecFunctionV0, which does not possess this method.
    • Solution: Updated the invocation to use the Spec instance (this.spec.isReadOnly(method)) ensuring proper type safety and functionality.
  2. Enhanced AssembledTransaction Class:

    • Feature Addition: Modified the sign method within the AssembledTransaction class to fail fast if no publicKey is provided.
    • Improved UX: Instead of using a generic error message or defaulting to a hardcoded NULL_ACCOUNT, the sign method now throws a descriptive error:
      "Public key not provided. Have you forgotten to set the `publicKey` in AssembledTransactionOptions?"
      
    • Benefit: This change ensures developers receive clear guidance when they omit the publicKey, enhancing the developer experience and preventing unintended transaction behaviors.
  3. Updated Test Files to Reflect Changes:

    • assembled_transaction_test.js:
      • Imported and instantiated the Spec class with the contract's specification entries.
      • Updated the buildFootprintRestoreTransaction method to pass the Spec instance.
      • Changed account instantiation from StellarSdk.Account to xdr.Account to align with expected types.
      • Ensured transaction fees are passed as strings to match method signatures.
    • test-swap.js:
      • Imported and instantiated the Spec class with the appropriate contract specifications.
      • Updated transaction building and signing processes to utilize the Spec instance.
      • Adjusted error assertions to correctly reference the updated error types.
      • Ensured consistent account types and fee formatting in transaction methods.
  4. Enhanced Spec Class (spec.ts):

    • Implemented the isReadOnly method to accurately determine if a contract function is read-only based on naming conventions or metadata.
    • Added comprehensive documentation for better maintainability and clarity.
  5. General Improvements:

    • Type Safety: Ensured all method invocations and parameter types align with TypeScript standards, preventing potential runtime errors.
    • Descriptive Error Messages: Enhanced error messages to be more actionable, facilitating easier debugging.
    • Comprehensive Testing: Updated test cases to cover both read-only and write transaction scenarios, ensuring robust test coverage.

Benefits:

  • Resolved TypeScript Error: Eliminates the Property 'isReadOnly' does not exist on type 'ScSpecFunctionV0'.ts(2339) error by correctly invoking isReadOnly on the Spec instance.
  • Improved Developer Experience: Clear and descriptive error messages guide developers to provide necessary configurations, reducing frustration and potential bugs.
  • Enhanced Code Quality: Strengthens the codebase through proper type handling, error management, and comprehensive testing.
  • Reliable Transaction Handling: Ensures transactions behave as expected, with appropriate checks in place to prevent unintended operations.

Testing:

  • Unit Tests: All existing unit tests pass, and new tests cover the scenarios where publicKey is omitted, ensuring the sign method behaves correctly.
  • Integration Tests: Verified that transactions requiring publicKey fail gracefully when omitted and succeed when provided.
  • Error Handling: Confirmed that descriptive errors are thrown, aiding in quicker debugging and issue resolution.

PoulavBhowmick03 avatar Oct 02 '24 06:10 PoulavBhowmick03