Skip to content

Commit

Permalink
Merge pull request #157 from peanutprotocol/test/update-request-tests
Browse files Browse the repository at this point in the history
[TASK-6263] feat: improve requests usability
  • Loading branch information
Hugo0 authored Oct 17, 2024
2 parents 030a1f4 + e7c87e5 commit caa5c39
Show file tree
Hide file tree
Showing 9 changed files with 356 additions and 193 deletions.
1 change: 1 addition & 0 deletions .env.example
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ export ALCHEMY_API_KEY=""
export MORALIS_API_KEY=""

# Peanut API key
export PEANUT_API_URL="https://api.staging.peanut.to"
export PEANUT_DEV_API_KEY=""
export ETHERSCAN_API_KEY=""

3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@
"type": "module",
"scripts": {
"test": "npm run pkg-move && jest test/basic --silent --coverage",
"test:basic": "npm run pkg-move && jest test/basic --silent --coverage",
"test:unit": "jest test/unit --silent --coverage",
"test:basic": "npm run pkg-move && jest 'test/(unit|basic)' --silent --coverage",
"test:live": "npm run pkg-move && jest test/live --silent --runInBand",
"test:full": "npm run pkg-move && jest --coverage --silent --runInBand",
"prettier": "prettier . --write",
Expand Down
2 changes: 1 addition & 1 deletion src/consts/interfaces.consts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ export interface IPrepareDepositTxsResponse {
unsignedTxs: IPeanutUnsignedTransaction[]
}

export interface IPrepareXchainRequestFulfillmentTransactionProps {
export interface IPrepareXchainRequestFulfillmentTransactionResponse {
unsignedTxs: IPeanutUnsignedTransaction[]
feeEstimation: string
estimatedFromAmount: string
Expand Down
54 changes: 33 additions & 21 deletions src/request.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { ethers, getDefaultProvider, utils } from 'ethersv5'
import { EPeanutLinkType, IPeanutUnsignedTransaction } from './consts/interfaces.consts'
import { ERC20_ABI, LATEST_STABLE_BATCHER_VERSION } from './data'
import { config, getSquidRoute, interfaces, prepareApproveERC20Tx, resolveFromEnsName } from '.'
import { prepareXchainFromAmountCalculation } from './util'
import { prepareXchainFromAmountCalculation, normalizePath } from './util'

// INTERFACES
export interface ICreateRequestLinkProps {
Expand Down Expand Up @@ -36,17 +36,27 @@ export interface IPrepareRequestLinkFulfillmentTransactionProps {
tokenDecimals: number
}

export interface IPrepareXchainRequestFulfillmentTransactionProps {
export type IPrepareXchainRequestFulfillmentTransactionProps = {
senderAddress: string
fromToken: string
fromTokenDecimals: number
fromChainId: string
link: string
squidRouterUrl: string
provider: ethers.providers.Provider
apiUrl?: string
tokenType: EPeanutLinkType
}
} & (
| {
link: string
apiUrl?: string
APIKey?: string
}
| {
linkDetails: Pick<
IGetRequestLinkDetailsResponse,
'chainId' | 'recipientAddress' | 'tokenAmount' | 'tokenDecimals' | 'tokenAddress'
>
}
)

export interface ISubmitRequestLinkFulfillmentProps {
hash: string
Expand Down Expand Up @@ -124,7 +134,7 @@ export async function createRequestLink({
if (tokenSymbol) formData.append('tokenSymbol', tokenSymbol)
if (attachment) formData.append('attachment', attachment)

const apiResponse = await fetch(`${apiUrl}/request-links`, {
const apiResponse = await fetch(normalizePath(`${apiUrl}/request-links`), {
method: 'POST',
body: formData,
headers: {
Expand All @@ -151,7 +161,7 @@ export async function getRequestLinkDetails({
}: IGetRequestLinkDetailsProps): Promise<IGetRequestLinkDetailsResponse> {
const uuid = getUuidFromLink(link)

const apiResponse = await fetch(`${apiUrl}/request-links/${uuid}`, {
const apiResponse = await fetch(normalizePath(`${apiUrl}/request-links/${uuid}`), {
method: 'GET',
headers: {
'api-key': APIKey!,
Expand All @@ -167,24 +177,26 @@ export async function getRequestLinkDetails({
return responseData
}

export async function prepareXchainRequestFulfillmentTransaction({
senderAddress,
fromToken,
fromTokenDecimals,
fromChainId,
link,
squidRouterUrl,
provider,
apiUrl = 'https://api.peanut.to/',
tokenType,
}: IPrepareXchainRequestFulfillmentTransactionProps): Promise<interfaces.IPrepareXchainRequestFulfillmentTransactionProps> {
const linkDetails = await getRequestLinkDetails({ link: link, apiUrl: apiUrl })
let { tokenAddress: destinationToken } = linkDetails
export async function prepareXchainRequestFulfillmentTransaction(
props: IPrepareXchainRequestFulfillmentTransactionProps
): Promise<interfaces.IPrepareXchainRequestFulfillmentTransactionResponse> {
let { senderAddress, fromToken, fromTokenDecimals, fromChainId, squidRouterUrl, provider, tokenType } = props
let linkDetails: Pick<
IGetRequestLinkDetailsResponse,
'chainId' | 'recipientAddress' | 'tokenAmount' | 'tokenDecimals' | 'tokenAddress'
>
if ('linkDetails' in props) {
linkDetails = props.linkDetails
} else {
const { link, apiUrl = 'https://api.peanut.to/', APIKey } = props
linkDetails = await getRequestLinkDetails({ link, apiUrl, APIKey })
}
let {
chainId: destinationChainId,
recipientAddress,
tokenAmount: destinationTokenAmount,
tokenDecimals: destinationTokenDecimals,
tokenAddress: destinationToken,
} = linkDetails
if (recipientAddress.endsWith('.eth')) {
recipientAddress = await resolveFromEnsName({ ensName: recipientAddress })
Expand Down Expand Up @@ -379,7 +391,7 @@ export async function submitRequestLinkFulfillment({
}: ISubmitRequestLinkFulfillmentProps): Promise<ISubmitRequestLinkFulfillmentResponse> {
try {
const uuid = getUuidFromLink(link)
const apiResponse = await fetch(`${apiUrl}request-links/${uuid}`, {
const apiResponse = await fetch(normalizePath(`${apiUrl}/request-links/${uuid}`), {
method: 'PATCH',
body: JSON.stringify({
destinationChainFulfillmentHash: hash,
Expand Down
11 changes: 11 additions & 0 deletions src/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -753,3 +753,14 @@ export async function prepareXchainFromAmountCalculation({
return null
}
}

export function normalizePath(url: string): string {
try {
const urlObject = new URL(url)
urlObject.pathname = urlObject.pathname.replace(/\/+/g, '/')
return urlObject.toString()
} catch (error: unknown) {
// Assume we are getting only a path
return url.replace(/\/+/g, '/')
}
}
Loading

0 comments on commit caa5c39

Please sign in to comment.