Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(processor): any-declarative payment in NEAR #1428

Merged
merged 3 commits into from
Jul 15, 2024

Conversation

yomarion
Copy link
Member

@yomarion yomarion commented Jul 15, 2024

Description of the changes

payment-processor util to make a payment in NEAR (native) token on a declarative request, through low-level parameters: paymentAddress, paymentReference and network.

Summary by CodeRabbit

  • New Features

    • Introduced new payment functions for NEAR tokens: payNearAnyDeclarativeRequest, payNearAmountWithReference.
    • Added validatePaymentReference function for enhanced payment reference validation.
  • Improvements

    • Updated existing NEAR payment functions to use validatePaymentReference for better validation.
    • Enhanced getAmountToPay function to prioritize user-specified amounts, improving payment flexibility.
  • Bug Fixes

    • Updated error message in payNearConversionRequest tests for better clarity.

@yomarion yomarion requested a review from leoslr July 15, 2024 12:50
Copy link

coderabbitai bot commented Jul 15, 2024

Walkthrough

The updates to the Payment Processor package introduce enhanced validation for payment references and add support for NEAR network payments. Key changes include the addition of the validatePaymentReference function for consistent validation, updates to several payment functions to handle NEAR transactions, and a new payNearAmountWithReference function to facilitate NEAR token payments with references. The modifications streamline payment processing and ensure comprehensive validation across the system.

Changes

File Path Change Summary
.../payment/any-to-erc20-proxy.ts Introduced validatePaymentReference function for validating payment references.
.../payment/near-any-declarative.ts Added payNearAnyDeclarativeRequest function to handle NEAR token payments for declarative requests.
.../payment/near-conversion.ts, .../payment/near-fungible.ts, .../payment/near-input-data.ts Updated payment functions to use validatePaymentReference and handle NEAR network payments.
.../payment/utils.ts Enhanced getAmountToPay function to prioritize user-specified amounts.
.../utils/validation.ts Added validatePaymentReference function to ensure the presence of a payment reference.
.../test/payment/any-to-near.test.ts Updated error message in payNearWithConversionRequest test.
new-file: .../payment/near-amount-with-ref.ts Introduced payNearAmountWithReference function for NEAR payments with a reference.

Sequence Diagram(s)

sequenceDiagram
    participant User
    participant PaymentProcessor
    participant NearNetwork
    participant ValidationUtil

    User->>PaymentProcessor: Initiate Payment
    PaymentProcessor->>ValidationUtil: validatePaymentReference(paymentReference)
    ValidationUtil-->>PaymentProcessor: Validation Result
    PaymentProcessor->>NearNetwork: Process NEAR Payment
    NearNetwork-->>PaymentProcessor: Payment Confirmation
    PaymentProcessor-->>User: Confirm Payment
Loading

Poem

In the land of crypto near,
Payments flow without a fear.
With NEAR and tokens, references tight,
Validation ensures everything's right.
From wallet to network, transactions gleam,
Code so seamless, like a dream.
🐇✨💸


Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media?

Share
Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>.
    • Generate unit testing code for this file.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai generate unit testing code for this file.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai generate interesting stats about this repository and render them as a table.
    • @coderabbitai show all the console.log statements in this repository.
    • @coderabbitai read src/utils.ts and generate unit testing code.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (invoked as PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Additionally, you can add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.

CodeRabbit Configration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 1

Review details

Configuration used: CodeRabbit UI
Review profile: ASSERTIVE

Commits

Files that changed from the base of the PR and between 9558a49 and ac361e8.

Files selected for processing (8)
  • packages/payment-processor/src/payment/any-to-erc20-proxy.ts (2 hunks)
  • packages/payment-processor/src/payment/near-any-declarative.ts (1 hunks)
  • packages/payment-processor/src/payment/near-conversion.ts (2 hunks)
  • packages/payment-processor/src/payment/near-fungible.ts (2 hunks)
  • packages/payment-processor/src/payment/near-input-data.ts (2 hunks)
  • packages/payment-processor/src/payment/utils.ts (1 hunks)
  • packages/payment-processor/src/utils/validation.ts (1 hunks)
  • packages/payment-processor/test/payment/any-to-near.test.ts (1 hunks)
Additional comments not posted (12)
packages/payment-processor/src/utils/validation.ts (1)

1-8: LGTM!

The validatePaymentReference function is well-implemented and ensures that a payment reference is present before proceeding with payment processing.

packages/payment-processor/src/payment/near-any-declarative.ts (1)

1-39: LGTM!

The payNearAnyDeclarativeRequest function is well-structured and ensures proper validation and processing of NEAR token transactions. The use of validatePaymentReference and assertChainSupported enhances the robustness of the function.

packages/payment-processor/src/payment/near-input-data.ts (1)

Line range hint 1-38:
LGTM!

The payNearInputDataRequest function includes proper validation steps and the use of validatePaymentReference improves the robustness of the function. The changes are consistent with the overall structure.

packages/payment-processor/src/payment/near-fungible.ts (1)

Line range hint 1-41:
LGTM!

The payNearFungibleRequest function includes proper validation steps and the use of validatePaymentReference improves the robustness of the function. The changes are consistent with the overall structure.

packages/payment-processor/src/payment/near-conversion.ts (1)

15-15: LGTM!

The addition of validatePaymentReference ensures that the payment reference is validated before proceeding.

packages/payment-processor/test/payment/any-to-near.test.ts (4)

Line range hint 49-91:
LGTM!

The test case correctly verifies the functionality of payNearConversionRequest and ensures that the correct parameters are passed to processNearPaymentWithConversion.


Line range hint 92-117:
LGTM!

The test case correctly verifies that payNearConversionRequest throws an error when trying to pay another payment extension.


Line range hint 118-144:
LGTM!

The test case correctly verifies that payNearConversionRequest throws an error when trying to pay with an unsupported currency.


Line range hint 145-173:
LGTM!

The test case correctly verifies that payNearConversionRequest throws an error when the network is not near and the error message has been updated to reflect the new message.

packages/payment-processor/src/payment/any-to-erc20-proxy.ts (2)

20-20: LGTM!

The addition of validatePaymentReference ensures that the payment reference is validated before proceeding.


157-157: LGTM!

The addition of validatePaymentReference ensures that the payment reference is validated before proceeding.

packages/payment-processor/src/payment/utils.ts (1)

356-368: LGTM!

The function now correctly prioritizes a user-specified amount over the default calculation based on the request balance.

Comment on lines +20 to +21
* @param walletConnection the Near provider.
* @param amount optionally, the amount to pay in request currency. Defaults to remaining amount of the request.
Copy link

@coderabbitai coderabbitai bot Jul 15, 2024

Choose a reason for hiding this comment

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

Tip

Codebase Verification

Ensure mockedNearWalletConnection conforms to NearProvider type

The mockedNearWalletConnection in the test file packages/payment-processor/test/payment/any-to-near.test.ts is currently cast to any, which bypasses type checking. To ensure type safety and correctness, it should be explicitly typed to conform to the NearProvider type.

  • Update the definition of mockedNearWalletConnection to explicitly use the NearProvider type.
Analysis chain

Verify the usage of NearProvider type for walletConnection.

Ensure that all instances where payNearConversionRequest is called have been updated to use NearProvider for the walletConnection parameter.

Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Description: Verify all function calls to `payNearConversionRequest` use `NearProvider` for the `walletConnection` parameter.

# Test: Search for the function usage. Expect: Only occurances of the new signature.
rg --type typescript -A 5 $'payNearConversionRequest'

Length of output: 87


Script:

#!/bin/bash
# Description: Verify all function calls to `payNearConversionRequest` use `NearProvider` for the `walletConnection` parameter.

# Test: Search for the function usage. Expect: Only occurrences of the new signature.
rg --type ts -A 5 $'payNearConversionRequest'

Length of output: 3780


Script:

#!/bin/bash
# Description: Verify the type of `mockedNearWalletConnection` used in `payNearConversionRequest` calls.

# Test: Search for the definition or initialization of `mockedNearWalletConnection`.
rg 'mockedNearWalletConnection' packages/payment-processor/test/payment/any-to-near.test.ts

Length of output: 577


Script:

#!/bin/bash
# Description: Verify the type of `mockedNearWalletConnection` used in `payNearConversionRequest` calls by inspecting its full definition.

# Test: Search for the complete definition of `mockedNearWalletConnection` and capture context lines.
rg 'const mockedNearWalletConnection' -A 10 packages/payment-processor/test/payment/any-to-near.test.ts

Length of output: 1468

Copy link
Member

Choose a reason for hiding this comment

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

This seems to be a correct observation - the typing in the test could use NearProvider instead of any 👍

Copy link

Choose a reason for hiding this comment

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

@MantisClone, great! I'm glad you agree with the observation. I'll make a note of this preference for future reviews.


Learnings added
Learnt from: MantisClone
PR: RequestNetwork/requestNetwork#1428
File: packages/payment-processor/src/payment/near-conversion.ts:20-21
Timestamp: 2024-07-15T14:13:22.837Z
Learning: Ensure that `mockedNearWalletConnection` in test files uses the `NearProvider` type instead of `any`.

Note: Learnings are effective only in the context of similar code segments. To apply general coding guidelines, please configure review instructions. You can manage existing learnings in the app.

Copy link
Member Author

Choose a reason for hiding this comment

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

I don't understand how a more precise type for a mocked value in the tests positively impacts the library. Can you explain? I'll merge this one because of emergencies and the feedback is not related to the core of my change, but happy to later improve.

Copy link

Choose a reason for hiding this comment

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

Seems like the humans are having a chat. I'll hop back into my burrow for now. If you need me again, just tag @coderabbitai in a new comment, and I'll come hopping out!

@yomarion yomarion requested a review from leoslr July 15, 2024 13:53
Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 1

Review details

Configuration used: CodeRabbit UI
Review profile: ASSERTIVE

Commits

Files that changed from the base of the PR and between ac361e8 and 902c9da.

Files selected for processing (1)
  • packages/payment-processor/src/payment/near-amount-with-ref.ts (1 hunks)
Additional comments not posted (3)
packages/payment-processor/src/payment/near-amount-with-ref.ts (3)

1-8: Imports look good.

The imported modules and functions are appropriate and necessary for the functionality.


21-28: Function signature looks good.

The function signature is appropriate for the described functionality.


29-39: Function body looks good.

The function body is concise and uses the processNearPayment utility correctly.

Comment on lines +9 to +20
/**
* Processes a transaction to make a payment in NEAR token with a reference.
*
* @notice This is used to pay a declarative request, with low-level arguments.
*
* @param paymentAddress must be a valid NEAR address on the given network.
* @param network e.g. 'near'
* @param paymentReference used to index payments.
* @param walletConnection the Near provider.
* @param amount amount to pay, in NEAR tokens.
*/
// FIXME: We could improve the method's interface by enforcing a type on `paymentInfo` for declarative requests.
Copy link

Choose a reason for hiding this comment

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

Documentation is clear and helpful.

The function documentation provides a clear description of the function's purpose and parameters.

Offer assistance for FIXME comment.

The FIXME comment suggests improving the method's interface by enforcing a type on paymentInfo.

Would you like me to help enforce a type on paymentInfo for declarative requests?

Comment on lines +20 to +21
* @param walletConnection the Near provider.
* @param amount optionally, the amount to pay in request currency. Defaults to remaining amount of the request.
Copy link
Member

Choose a reason for hiding this comment

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

This seems to be a correct observation - the typing in the test could use NearProvider instead of any 👍

@yomarion yomarion merged commit a23a847 into master Jul 15, 2024
24 checks passed
@yomarion yomarion deleted the feat/processor-near-any-declarative-payment branch July 15, 2024 14:27
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