Skip to content

Commit 088e9bd

Browse files
committed
Fixes after code review
1 parent 0b1e7f8 commit 088e9bd

File tree

6 files changed

+57
-47
lines changed

6 files changed

+57
-47
lines changed

src/components/Claim/Claim.tsx

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import * as assets from '@/assets'
1313
import * as consts from '@/constants'
1414
import * as _utils from './Claim.utils'
1515
import FlowManager from './Link/FlowManager'
16+
import { ActionType, estimatePoints } from '../utils/utils'
1617

1718
export const Claim = ({}) => {
1819
const [step, setStep] = useState<_consts.IClaimScreenState>(_consts.INIT_VIEW_STATE)
@@ -51,7 +52,7 @@ export const Claim = ({}) => {
5152
const [userType, setUserType] = useState<'NEW' | 'EXISTING' | undefined>(undefined)
5253
const [userId, setUserId] = useState<string | undefined>(undefined)
5354
const { address } = useAccount()
54-
const { getAttachmentInfo, estimatePoints } = useClaimLink()
55+
const { getAttachmentInfo } = useClaimLink()
5556

5657
const handleOnNext = () => {
5758
if (step.idx === _consts.CLAIM_SCREEN_FLOW.length - 1) return
@@ -153,6 +154,7 @@ export const Claim = ({}) => {
153154
address: address ?? '',
154155
chainId: linkDetails.chainId,
155156
amountUSD: Number(linkDetails.tokenAmount) * (tokenPrice?.price ?? 0),
157+
actionType: ActionType.CLAIM,
156158
})
157159
setEstimatedPoints(estimatedPoints)
158160
}

src/components/Claim/Link/Initial.view.tsx

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import * as _interfaces from '../Claim.interfaces'
1818
import * as _utils from '../Claim.utils'
1919
import { Popover } from '@headlessui/react'
2020
import { useAuth } from '@/context/authContext'
21+
import { ActionType, estimatePoints } from '@/components/utils/utils'
2122
export const InitialClaimLinkView = ({
2223
onNext,
2324
claimLinkData,
@@ -56,7 +57,7 @@ export const InitialClaimLinkView = ({
5657
context.tokenSelectorContext
5758
)
5859
const mappedData: _interfaces.CombinedType[] = _utils.mapToIPeanutChainDetailsArray(crossChainDetails)
59-
const { estimatePoints, claimLink } = useClaimLink()
60+
const { claimLink } = useClaimLink()
6061
const { open } = useWeb3Modal()
6162
const { isConnected, address } = useAccount()
6263
const { user } = useAuth()
@@ -123,6 +124,7 @@ export const InitialClaimLinkView = ({
123124
address: recipient.address ?? address ?? '',
124125
chainId: claimLinkData.chainId,
125126
amountUSD: USDValue,
127+
actionType: ActionType.CLAIM,
126128
})
127129
setEstimatedPoints(estimatedPoints)
128130
}

src/components/Claim/useClaimLink.tsx

Lines changed: 1 addition & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ export const useClaimLink = () => {
8686
currentChainId: String(currentChain?.id),
8787
setLoadingState,
8888
switchChainAsync: async ({ chainId }) => {
89-
await switchChainAsync({ chainId: chainId as number });
89+
await switchChainAsync({ chainId: chainId as number })
9090
},
9191
})
9292
console.log(`Switched to chain ${chainId}`)
@@ -96,46 +96,6 @@ export const useClaimLink = () => {
9696
}
9797
const checkTxStatus = async (txHash: string) => {}
9898

99-
const estimatePoints = async ({
100-
address,
101-
chainId,
102-
amountUSD,
103-
}: {
104-
address: string
105-
chainId: string
106-
amountUSD: number
107-
}) => {
108-
try {
109-
const response = await fetch(`${consts.PEANUT_API_URL}/calculate-pts-for-action`, {
110-
method: 'POST',
111-
headers: {
112-
'Content-Type': 'application/json',
113-
},
114-
body: JSON.stringify({
115-
actionType: 'CLAIM',
116-
userAddress: address,
117-
chainId: chainId,
118-
amountUsd: amountUSD,
119-
transaction: {
120-
from: address,
121-
to: address,
122-
data: '',
123-
value: '',
124-
},
125-
}),
126-
})
127-
if (!response.ok) {
128-
throw new Error(`HTTP error! status: ${response.status}`)
129-
}
130-
131-
const data = await response.json()
132-
return Math.round(data.points)
133-
} catch (error) {
134-
console.error('Failed to estimate points:', error)
135-
return 0
136-
}
137-
}
138-
13999
const getAttachmentInfo = async (link: string) => {
140100
try {
141101
const response = await fetch('/api/peanut/get-attachment-info', {
@@ -165,7 +125,6 @@ export const useClaimLink = () => {
165125
getSquidRoute,
166126
switchNetwork,
167127
checkTxStatus,
168-
estimatePoints,
169128
getAttachmentInfo,
170129
}
171130
}

src/components/Request/Pay/Pay.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import { peanut, interfaces as peanutInterfaces } from '@squirrel-labs/peanut-sd
88
import * as generalViews from './Views/GeneralViews'
99
import * as utils from '@/utils'
1010
import { useCreateLink } from '@/components/Create/useCreateLink'
11-
import useClaimLink from '@/components/Claim/useClaimLink'
11+
import { ActionType, estimatePoints } from '@/components/utils/utils'
1212

1313
export const PayRequestLink = () => {
1414
const [step, setStep] = useState<_consts.IPayScreenState>(_consts.INIT_VIEW_STATE)
@@ -20,7 +20,6 @@ export const PayRequestLink = () => {
2020
const [estimatedGasCost, setEstimatedGasCost] = useState<number | undefined>(undefined)
2121
const [transactionHash, setTransactionHash] = useState<string>('')
2222
const [unsignedTx, setUnsignedTx] = useState<peanutInterfaces.IPeanutUnsignedTransaction | undefined>(undefined)
23-
const { estimatePoints } = useClaimLink()
2423

2524
const fetchPointsEstimation = async (
2625
requestLinkDetails: { recipientAddress: any; chainId: any; tokenAmount: any },
@@ -30,6 +29,7 @@ export const PayRequestLink = () => {
3029
address: requestLinkDetails.recipientAddress,
3130
chainId: requestLinkDetails.chainId,
3231
amountUSD: Number(requestLinkDetails.tokenAmount) * (tokenPrice?.price ?? 0),
32+
actionType: ActionType.FULFILL,
3333
})
3434

3535
setEstimatedPoints(estimatedPoints)

src/components/Request/Pay/Views/Initial.view.tsx

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -162,7 +162,6 @@ export const InitialView = ({
162162
payerAddress: address ?? '',
163163
link: requestLinkData.link,
164164
apiUrl: '/api/proxy/patch/',
165-
estimatedPoints,
166165
amountUsd,
167166
})
168167

src/components/utils/utils.ts

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import axios from 'axios'
2+
import * as consts from '@/constants'
23

34
export async function checkTransactionStatus(txHash: string): Promise<void> {
45
try {
@@ -45,3 +46,50 @@ export async function loopUntilSuccess(
4546
}
4647
}, 5000)
4748
}
49+
50+
export enum ActionType {
51+
CLAIM = 'CLAIM',
52+
FULFILL = 'FULFILL',
53+
}
54+
55+
export const estimatePoints = async ({
56+
address,
57+
chainId,
58+
amountUSD,
59+
actionType,
60+
}: {
61+
address: string
62+
chainId: string
63+
amountUSD: number
64+
actionType: ActionType
65+
}) => {
66+
try {
67+
const response = await fetch(`${consts.PEANUT_API_URL}/calculate-pts-for-action`, {
68+
method: 'POST',
69+
headers: {
70+
'Content-Type': 'application/json',
71+
},
72+
body: JSON.stringify({
73+
actionType: actionType,
74+
userAddress: address,
75+
chainId: chainId,
76+
amountUsd: amountUSD,
77+
transaction: {
78+
from: address,
79+
to: address,
80+
data: '',
81+
value: '',
82+
},
83+
}),
84+
})
85+
if (!response.ok) {
86+
throw new Error(`HTTP error! status: ${response.status}`)
87+
}
88+
89+
const data = await response.json()
90+
return Math.round(data.points)
91+
} catch (error) {
92+
console.error('Failed to estimate points:', error)
93+
return 0
94+
}
95+
}

0 commit comments

Comments
 (0)