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

Request Links #147

Merged
merged 22 commits into from
Sep 12, 2024
Merged

Request Links #147

merged 22 commits into from
Sep 12, 2024

Conversation

Hugo0
Copy link
Contributor

@Hugo0 Hugo0 commented Sep 12, 2024

same-chain and x-chain request links

Summary by CodeRabbit

Release Notes

  • New Features

    • Updated the Peanut SDK to version 0.4.66, enhancing overall functionality.
    • Introduced new interfaces and functions for managing cross-chain transactions and request links, improving user experience in blockchain interactions.
    • Added functionality for token price retrieval and amount calculations for cross-chain transfers.
  • Bug Fixes

    • Improved handling of URL parameters with encoded characters to ensure accurate parsing.
  • Tests

    • Established comprehensive tests for cross-chain request link creation and fulfillment.
    • Removed obsolete tests to streamline the testing framework.

Copy link

coderabbitai bot commented Sep 12, 2024

Walkthrough

Walkthrough

This pull request introduces several updates, including an increment in the version number of the @squirrel-labs/peanut-sdk package. It adds new interfaces and modifies existing ones to enhance cross-chain transaction handling. A new file, src/request.ts, implements functionalities for managing request links in blockchain transactions. Additionally, new functions for token price retrieval and amount calculation are introduced in src/util.ts. The changes are accompanied by new tests for cross-chain functionalities and modifications to existing tests, while some obsolete tests have been removed.

Changes

File Change Summary
package.json Updated version of @squirrel-labs/peanut-sdk from "0.4.62" to "0.4.66".
src/consts/interfaces.consts.ts Added IPrepareXchainRequestFulfillmentTransactionProps interface; modified ISquidRoute interface to include a to property of type string.
src/index.ts Added to property in the return type of getSquidRoute function; integrated request module into the peanut object.
src/request.ts Introduced new functionalities for managing request links, including interfaces and functions for creating, retrieving, and fulfilling request links.
src/util.ts Added getTokenPrice and prepareXchainFromAmountCalculation functions; modified getRawParamsFromLink to clean URL parameters.
test/basic/RequestLinkXChain.test.ts New test file validating cross-chain request link creation and fulfillment for ERC20 and native tokens.
test/basic/getParamsFromLink.test.ts Added test case for handling URL parameters with HTML entities.
test/basic/getSquidChainsAndTokens.test.ts Removed tests for getSquidChains and getSquidTokens functions.
test/basic/raffle.test.ts Removed a test case for retrieving raffle information using getRaffleInfo.
test/basic/supportsEIP1559.test.ts Removed test case for a provider that does not support EIP-1559 due to changes in the provider's capabilities.

Possibly related PRs

  • Fix amp issue #142: Modifications to the getRawParamsFromLink function in src/util.ts to clean up URL parameters, relevant to similar changes in this PR.
  • Develop #145: Similar updates to getRawParamsFromLink for enhanced processing of URL parameters, directly related to the changes made in this PR.

Recent review details

Configuration used: CodeRabbit UI
Review profile: CHILL

Commits

Files that changed from the base of the PR and between 3cc222e and af2849d.

Files selected for processing (1)
  • src/util.ts (3 hunks)
Additional comments not posted (2)
src/util.ts (2)

688-694: LGTM!

The getTokenPrice function is implemented correctly. It properly constructs the API URL using the provided tokenAddress and chainId parameters, handles the asynchronous API call using async/await, and returns the price field from the API response.


702-755: LGTM!

The prepareXchainFromAmountCalculation function is implemented correctly and follows good practices:

  • It handles the case when slippagePercentage is negative by logging an error and returning null.
  • It efficiently fetches the token prices concurrently using Promise.all.
  • It normalizes the token prices and amounts based on their decimal counts to ensure consistent calculations and avoid issues with scientific notation.
  • It uses ethers.BigNumber for calculations to handle large numbers and avoid floating-point precision issues.
  • The slippage calculation is performed correctly by converting the percentage to an integer fraction and applying it to the calculated amount.
  • It properly formats the final amount using the source token's decimals before returning it.
  • Error handling is implemented using a try-catch block, logging the error and returning null in case of failure.

Overall, the function is well-structured, follows best practices, and achieves its intended purpose.


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

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

CodeRabbit Configuration 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.

@Hugo0 Hugo0 requested a review from pawell24 September 12, 2024 18:36
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: 3

Outside diff range and nitpick comments (6)
test/basic/RequestLinkXChain.test.ts (1)

10-94: Use mock data and update the timeout value.

The test case follows the expected flow of creating a request link and fulfilling it cross-chain. Good job on structuring the test case well!

However, please consider the following suggestions:

  1. Use mock data instead of real wallet addresses and token addresses to avoid potential security risks.
  2. Update the timeout value to a more realistic value based on the expected network conditions. The current timeout of 120 seconds may not be sufficient for the cross-chain transaction to complete, especially on a busy network.
Tools
Gitleaks

27-27: Detected a Generic API Key, potentially exposing access to various services and sensitive operations.

(generic-api-key)


28-28: Detected a Generic API Key, potentially exposing access to various services and sensitive operations.

(generic-api-key)

src/request.ts (5)

94-144: LGTM!

The function is correctly implemented and follows best practices for error handling. However, consider the following improvements:

  • Add more detailed error messages to help with debugging.
  • Validate the input parameters to ensure they are of the correct type and format.

146-167: LGTM!

The function is correctly implemented and follows best practices for error handling. However, consider the following improvements:

  • Add more detailed error messages to help with debugging.
  • Validate the input parameters to ensure they are of the correct type and format.

303-325: LGTM!

The function is correctly implemented and follows best practices for error handling. However, consider the following improvements:

  • Add more detailed error messages to help with debugging.
  • Validate the input parameters to ensure they are of the correct type and format.
  • Add more comments to explain the purpose of each code block.

327-362: LGTM!

The function is correctly implemented and follows best practices for error handling. However, consider the following improvements:

  • Add more detailed error messages to help with debugging.
  • Validate the input parameters to ensure they are of the correct type and format.
  • Add more comments to explain the purpose of each code block.

400-404: LGTM!

The function is correctly implemented and follows best practices. However, consider adding error handling in case the id query parameter is missing.

Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

Commits

Files that changed from the base of the PR and between fba52e3 and 3cc222e.

Files ignored due to path filters (1)
  • pnpm-lock.yaml is excluded by !**/pnpm-lock.yaml
Files selected for processing (10)
  • package.json (1 hunks)
  • src/consts/interfaces.consts.ts (2 hunks)
  • src/index.ts (3 hunks)
  • src/request.ts (1 hunks)
  • src/util.ts (2 hunks)
  • test/basic/RequestLinkXChain.test.ts (1 hunks)
  • test/basic/getParamsFromLink.test.ts (1 hunks)
  • test/basic/getSquidChainsAndTokens.test.ts (0 hunks)
  • test/basic/raffle.test.ts (1 hunks)
  • test/basic/supportsEIP1559.test.ts (0 hunks)
Files not reviewed due to no reviewable changes (2)
  • test/basic/getSquidChainsAndTokens.test.ts
  • test/basic/supportsEIP1559.test.ts
Files skipped from review due to trivial changes (1)
  • package.json
Additional context used
Gitleaks
test/basic/getParamsFromLink.test.ts

64-64: Detected a Generic API Key, potentially exposing access to various services and sensitive operations.

(generic-api-key)

test/basic/RequestLinkXChain.test.ts

27-27: Detected a Generic API Key, potentially exposing access to various services and sensitive operations.

(generic-api-key)


28-28: Detected a Generic API Key, potentially exposing access to various services and sensitive operations.

(generic-api-key)


187-187: Detected a Generic API Key, potentially exposing access to various services and sensitive operations.

(generic-api-key)

Additional comments not posted (15)
test/basic/raffle.test.ts (3)

Line range hint 10-19: LGTM!

The test case for generating amounts distribution looks good. It's testing an important functionality and the implementation is correct.


Line range hint 21-38: LGTM!

The test case for handling an invalid raffle link looks good. It's testing an important error handling scenario and the implementation is correct.


Line range hint 1-40: Verify if the functionality for retrieving raffle information is still being tested adequately.

I noticed that the test case for retrieving raffle information using the getRaffleInfo function has been removed. Please ensure that this functionality is still being tested adequately elsewhere in the test suite.

Run the following script to verify the usage of the getRaffleInfo function:

Verification successful

Verification Successful: The getRaffleInfo function is still being tested adequately.

The getRaffleInfo function is used in the test file test/basic/getRaffleInfo.test.ts, confirming that the functionality for retrieving raffle information is still being tested in the test suite.

  • test/basic/getRaffleInfo.test.ts: Contains tests for getRaffleInfo.
Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Description: Verify if the `getRaffleInfo` function is being used in the test suite.

# Test: Search for the function usage. Expect: At least one occurrence.
rg --type typescript -A 5 $'getRaffleInfo'

Length of output: 76


Script:

#!/bin/bash
# Description: Verify if the `getRaffleInfo` function is being used in the test suite.

# Test: Search for the function usage. Expect: At least one occurrence.
rg --type ts -A 5 $'getRaffleInfo'

Length of output: 1477

test/basic/getParamsFromLink.test.ts (1)

55-67: Great job adding a relevant test case!

The new test case amp; issue is well-structured and enhances the robustness of the getParamsFromLink function by ensuring it can handle encoded characters in URLs. This is a common scenario in web applications, and it's great to see it being tested.

Regarding the static analysis hint about a potential generic API key, I wanted to clarify that the password used in the test case appears to be a mock value and not an actual API key. It is a common practice to use mock values in test cases.

Tools
Gitleaks

64-64: Detected a Generic API Key, potentially exposing access to various services and sensitive operations.

(generic-api-key)

test/basic/RequestLinkXChain.test.ts (2)

96-167: Use mock data and update the timeout value.

The test case follows the expected flow of creating a request link and fulfilling it cross-chain for native tokens. Good job on structuring the test case well!

However, please consider the following suggestions:

  1. Use mock data instead of real wallet addresses and token addresses to avoid potential security risks.
  2. Update the timeout value to a more realistic value based on the expected network conditions. The current timeout of 120 seconds may not be sufficient for the cross-chain transaction to complete, especially on a busy network.

169-234: Use mock data and update the timeout value.

The test case follows the expected flow of creating a request link and fulfilling it with native tokens to ERC20 tokens on the same chain. Good job on structuring the test case well!

However, please consider the following suggestions:

  1. Use mock data instead of real wallet addresses and token addresses to avoid potential security risks.
  2. Update the timeout value to a more realistic value based on the expected network conditions. The current timeout of 120 seconds may not be sufficient for the transaction to complete, especially on a busy network.
Tools
Gitleaks

187-187: Detected a Generic API Key, potentially exposing access to various services and sensitive operations.

(generic-api-key)

src/request.ts (1)

7-91: LGTM!

The interfaces are correctly defined and follow best practices for naming and structure.

src/consts/interfaces.consts.ts (2)

170-172: LGTM!

The new interface IPrepareXchainRequestFulfillmentTransactionProps is correctly defined with the unsignedTxs property of type IPeanutUnsignedTransaction[]. This interface will be useful for handling cross-chain request fulfillment transactions.


208-208: Looks good!

The to property of type string is a relevant addition to the ISquidRoute interface. It will provide the necessary destination address for routing transactions or data across different chains or systems.

src/util.ts (4)

233-234: LGTM!

The change to remove the "amp;" substring from the search string before parsing the URL parameters looks good. This should handle cases where the substring is present due to URL encoding or other reasons.


687-694: LGTM!

The getTokenPrice function is implemented correctly. It fetches the token price from an external API based on the provided tokenAddress and chainId, handles the asynchronous operation properly, and returns the price from the response data.


696-700: LGTM!

The TokenData interface is defined correctly with the appropriate property names and types. It represents the structure of token data, including the chain ID, token address, and the number of decimals for the token.


702-755: LGTM!

The prepareXchainFromAmountCalculation function is implemented correctly. It performs a cross-chain token amount calculation, taking into account the token prices and slippage percentage. The calculation logic is accurate and handles the different decimal counts between tokens by normalizing the prices and amounts. The slippage percentage is properly converted to an integer fraction and applied to the fromAmount. Error handling is in place, logging any errors to the console and returning null in case of failure. The function is using the ethers library for BigNumber operations and formatting, which is a good practice.

src/index.ts (2)

2320-2322: LGTM!

The addition of the to property to the returned object enhances the function's output by providing the target address of the transaction request. This change expands the functionality and provides additional context about the transaction.


Line range hint 3018-3120: Verify the integration and usage of the request module.

The spread of the request module exports into the peanut object expands its capabilities by incorporating additional features from request. This change potentially impacts how peanut is utilized elsewhere in the codebase.

Please ensure that:

  1. The request module is properly integrated and its functionalities align with the intended behavior of the peanut object.
  2. The usage of peanut throughout the codebase is reviewed and updated as necessary to accommodate the new functionalities provided by request.
  3. Thorough testing is performed to validate the behavior and compatibility of the integrated request module with the existing codebase.

Comment on lines +1 to +235
senderAddress: userSourceChainWallet.address,
fromToken: tokenAddress,
link,
squidRouterUrl: getSquidRouterUrl(true, false),
provider: sourceChainProvider,
apiUrl: 'https://staging.peanut.to/api/proxy/get',
tokenType: EPeanutLinkType.native,
fromTokenDecimals: 18,
})
console.log('Computed x chain unsigned fulfillment transactions', xchainUnsignedTxs)

const fee = await peanut.calculateCrossChainTxFee({
unsignedTxs: xchainUnsignedTxs.unsignedTxs,
isNativeTxValue: true,
fromAmount: amountToTestWith.toString(),
})
console.log('Fee1', ethers.utils.formatEther(fee))

for (const unsignedTx of xchainUnsignedTxs.unsignedTxs) {
const { tx, txHash } = await signAndSubmitTx({
unsignedTx,
structSigner: {
signer: userSourceChainWallet,
gasLimit: BigNumber.from(2_000_000),
},
})

console.log('Submitted a transaction to fulfill the request link with tx hash', txHash)
await tx.wait()
console.log('Request link fulfillment initiated!')
}
}, 120000)

it('Create a request link and fulfill it cross-chain native token', async function () {
peanut.toggleVerbose(true)
const userPrivateKey = process.env.TEST_WALLET_X_CHAIN_USER!
// const relayerPrivateKey = process.env.TEST_WALLET_X_CHAIN_RELAYER!

// Parameters that affect the test behaviour
const sourceChainId = '10' // Arbitrum
const destinationChainId = '10' // Optimism
const amountToTestWith = 0.1
// const tokenDecimals = '18'
const APIKey = process.env.PEANUT_DEV_API_KEY!
const sourceChainProvider = await getDefaultProvider(sourceChainId)
console.log('Source chain provider', sourceChainProvider)

const userSourceChainWallet = new ethers.Wallet(userPrivateKey, sourceChainProvider)

const recipientAddress = '0x42A5DC31169Da17639468e7Ffa271e90Fdb5e85A'
const tokenAddress = '0x0000000000000000000000000000000000000000' // ETH on Optimism
const destinationToken = '0x0b2C639c533813f4Aa9D7837CAf62653d097Ff85' // USDC on Optimism

const { link } = await peanut.createRequestLink({
chainId: destinationChainId,
tokenAddress: destinationToken,
tokenAmount: amountToTestWith.toString(),
tokenType: EPeanutLinkType.erc20,
tokenDecimals: '6',
recipientAddress,
APIKey,
apiUrl: 'https://staging.peanut.to/api/proxy/withFormData',
})
console.log('Created a request link on the source chain!', link)

const linkDetails = await peanut.getRequestLinkDetails({
link,
APIKey,
apiUrl: 'https://staging.peanut.to/api/proxy/get',
})
console.log('Got the link details!', linkDetails)

const xchainUnsignedTxs = await peanut.prepareXchainRequestFulfillmentTransaction({
fromChainId: sourceChainId,
senderAddress: userSourceChainWallet.address,
fromToken: tokenAddress,
link,
squidRouterUrl: getSquidRouterUrl(true, false),
provider: sourceChainProvider,
apiUrl: 'https://staging.peanut.to/api/proxy/get',
tokenType: EPeanutLinkType.native,
fromTokenDecimals: 18,
})
console.log('Computed x chain unsigned fulfillment transactions', xchainUnsignedTxs)

for (const unsignedTx of xchainUnsignedTxs.unsignedTxs) {
const { tx, txHash } = await signAndSubmitTx({
unsignedTx,
structSigner: {
signer: userSourceChainWallet,
gasLimit: BigNumber.from(2_000_000),
},
})

console.log('Submitted a transaction to fulfill the request link with tx hash', txHash)
await tx.wait()
console.log('Request link fulfillment initiated!')
}
}, 120000)
})
Copy link

Choose a reason for hiding this comment

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

Refactor the tests to use mock data and environments.

The tests use real private keys and API keys from environment variables, which is a potential security risk. If the keys are accidentally committed to the repository or exposed in any way, they can be used to access sensitive data and perform unauthorized actions.

To mitigate this risk, refactor the tests to use mock data and environments:

  1. Create a mock environment with test accounts and API keys specifically for testing purposes.
  2. Use the mock environment in the tests instead of the real environment.
  3. Store the test data in a separate configuration file that is not committed to the repository.
  4. Use a library like dotenv-safe to load the environment variables and ensure that all required variables are present.

Do you want me to open a GitHub issue to track this refactoring task?

Tools
Gitleaks

27-27: Detected a Generic API Key, potentially exposing access to various services and sensitive operations.

(generic-api-key)


28-28: Detected a Generic API Key, potentially exposing access to various services and sensitive operations.

(generic-api-key)


187-187: Detected a Generic API Key, potentially exposing access to various services and sensitive operations.

(generic-api-key)

Comment on lines +169 to +301
fromToken = '0xEeeeeEeeeEeEeeEeEeEeeEEEeeeeEeeeeeeeEEeE'
}

if (destinationToken == '0x0000000000000000000000000000000000000000') {
// Update for Squid compatibility
config.verbose && console.log('Destination token is 0x0000, converting to 0xEeee..')
destinationToken = '0xEeeeeEeeeEeEeeEeEeEeeEEEeeeeEeeeeeeeEEeE'
}

const fromTokenData = {
address: fromToken,
chainId: fromChainId,
decimals: fromTokenDecimals,
}
const toTokenData = {
address: destinationToken,
chainId: destinationChainId,
decimals: destinationTokenDecimals,
}
const estimatedFromAmount = await prepareXchainFromAmountCalculation({
fromToken: fromTokenData,
toAmount: destinationTokenAmount,
toToken: toTokenData,
})

console.log('estimatedFromAmount', estimatedFromAmount)
if (!estimatedFromAmount) {
throw new Error('Failed to estimate from amount')
}
// get wei of amount being withdrawn and send as string (e.g. "10000000000000000")
const tokenAmount = utils.parseUnits(estimatedFromAmount, fromTokenDecimals)
config.verbose && console.log('Getting squid info..')
const unsignedTxs: interfaces.IPeanutUnsignedTransaction[] = []

const routeResult = await getSquidRoute({
squidRouterUrl,
fromChain: fromChainId,
fromToken: fromToken,
fromAmount: tokenAmount.toString(),
toChain: destinationChainId,
toToken: destinationToken,
fromAddress: senderAddress,
toAddress: recipientAddress,
enableBoost: true,
})
if (tokenType == EPeanutLinkType.native) {
txOptions = {
...txOptions,
value: tokenAmount,
}
} else if (tokenType == EPeanutLinkType.erc20) {
config.verbose && console.log('checking allowance...')
try {
const approveTx: interfaces.IPeanutUnsignedTransaction = await prepareApproveERC20Tx(
senderAddress,
destinationChainId,
fromToken,
tokenAmount,
fromTokenDecimals,
true,
LATEST_STABLE_BATCHER_VERSION,
provider,
routeResult.to
)

if (approveTx) {
approveTx.from = senderAddress
unsignedTxs.push(approveTx)
config.verbose && console.log('approveTx:', approveTx)
}
} catch (error) {
throw new interfaces.SDKStatus(
interfaces.EPrepareCreateTxsStatusCodes.ERROR_PREPARING_APPROVE_ERC20_TX,
'Error preparing the approve ERC20 tx, please make sure you have enough balance and have approved the contract to spend your tokens'
)
}
}

config.verbose && console.log('Squid route calculated :)', { routeResult })

let unsignedTx: IPeanutUnsignedTransaction = {}

if (tokenType == EPeanutLinkType.native) {
unsignedTx = {
data: routeResult.calldata,
to: routeResult.to,
value: BigInt(routeResult.value.toString()) + BigInt(txOptions.value.toString()),
}
} else if (tokenType == EPeanutLinkType.erc20) {
unsignedTx = {
data: routeResult.calldata,
to: routeResult.to,
value: BigInt(routeResult.value.toString()),
}
}

unsignedTxs.push(unsignedTx)

return { unsignedTxs }
}
Copy link

Choose a reason for hiding this comment

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

LGTM, but consider refactoring and adding more comments.

The function is correctly implemented and follows best practices for error handling. However, consider the following improvements:

  • Break down the function into smaller functions for better readability and maintainability.
  • Add more detailed error messages to help with debugging.
  • Validate the input parameters to ensure they are of the correct type and format.
  • Add more comments to explain the purpose of each code block.

Comment on lines +364 to +398
export async function submitRequestLinkFulfillment({
chainId,
hash,
payerAddress,
signedTx,
apiUrl = 'https://api.peanut.to/',
link,
}: ISubmitRequestLinkFulfillmentProps): Promise<ISubmitRequestLinkFulfillmentResponse> {
try {
const uuid = getUuidFromLink(link)
const apiResponse = await fetch(`${apiUrl}request-links/${uuid}`, {
method: 'PATCH',
body: JSON.stringify({
destinationChainFulfillmentHash: hash,
payerAddress,
chainId,
// signedTx,
}),
headers: {
'Content-Type': 'application/json',
},
})

if (apiResponse.status !== 200) {
throw new Error('Failed to submit request link fulfillment')
}

const data = await apiResponse.json()

return data
} catch (error) {
console.error('Error submitting request link fulfillment:', error)
throw error
} // TODO: handle error
}
Copy link

Choose a reason for hiding this comment

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

LGTM, but address the TODO comment.

The function is correctly implemented and follows best practices for error handling. However, consider the following improvements:

  • Add more detailed error messages to help with debugging.
  • Validate the input parameters to ensure they are of the correct type and format.
  • Add more comments to explain the purpose of each code block.
  • Address the TODO comment by implementing proper error handling.

@Hugo0
Copy link
Contributor Author

Hugo0 commented Sep 12, 2024

Nice! ignoring tests, (almost) all run locally and work

@Hugo0 Hugo0 merged commit 77666f6 into main Sep 12, 2024
1 check failed
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