-
Notifications
You must be signed in to change notification settings - Fork 72
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
import { BigNumberish } from 'ethers'; | ||
import { WalletConnection } from 'near-api-js'; | ||
|
||
import { CurrencyTypes } from '@requestnetwork/types'; | ||
|
||
import { INearTransactionCallback, processNearPayment } from './utils-near'; | ||
import { NearChains } from '@requestnetwork/currency'; | ||
|
||
/** | ||
* 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. | ||
export async function payNearAmountWithReference( | ||
paymentAddress: string, | ||
paymentReference: string, | ||
network: CurrencyTypes.NearChainName, | ||
walletConnection: WalletConnection, | ||
amount: BigNumberish, | ||
callback?: INearTransactionCallback, | ||
): Promise<void> { | ||
NearChains.assertChainSupported(network); | ||
|
||
return processNearPayment( | ||
walletConnection, | ||
network, | ||
amount, | ||
paymentAddress, | ||
paymentReference, | ||
'0.2.0', | ||
callback, | ||
); | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -12,12 +12,13 @@ import { | |
import { INearTransactionCallback, processNearPaymentWithConversion } from './utils-near'; | ||
import { IConversionPaymentSettings } from '.'; | ||
import { CurrencyManager, NearChains, UnsupportedCurrencyError } from '@requestnetwork/currency'; | ||
import { validatePaymentReference } from '../utils/validation'; | ||
|
||
/** | ||
* Processes the transaction to pay a request in NEAR with on-chain conversion. | ||
* @param request the request to pay | ||
* @param walletConnection the Web3 provider, or signer. Defaults to window.ethereum. | ||
* @param amount optionally, the amount to pay. Defaults to remaining amount of the request. | ||
* @param walletConnection the Near provider. | ||
* @param amount optionally, the amount to pay in request currency. Defaults to remaining amount of the request. | ||
Comment on lines
+20
to
+21
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Tip Codebase Verification Ensure The
Analysis chainVerify the usage of Ensure that all instances where Scripts executedThe 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 There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 There was a problem hiding this comment. Choose a reason for hiding this commentThe 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
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
*/ | ||
export async function payNearConversionRequest( | ||
request: ClientTypes.IRequestData, | ||
|
@@ -37,13 +38,7 @@ export async function payNearConversionRequest( | |
throw new UnsupportedCurrencyError(request.currencyInfo); | ||
} | ||
|
||
if (!paymentReference) { | ||
throw new Error('Cannot pay without a paymentReference'); | ||
} | ||
|
||
if (!network || !NearChains.isChainSupported(network)) { | ||
throw new Error('Should be a Near network'); | ||
} | ||
yomarion marked this conversation as resolved.
Show resolved
Hide resolved
|
||
validatePaymentReference(paymentReference); | ||
NearChains.assertChainSupported(network); | ||
|
||
const amountToPay = getAmountToPay(request, amount).toString(); | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
/** Validates the presence of the payment reference for payment. */ | ||
export function validatePaymentReference( | ||
paymentReference?: string, | ||
): asserts paymentReference is string { | ||
if (!paymentReference) { | ||
throw new Error('Cannot pay without a paymentReference'); | ||
} | ||
} |
There was a problem hiding this comment.
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?