Skip to content

Commit

Permalink
Merge pull request #1121 from flashburst/fix/policy-receipt
Browse files Browse the repository at this point in the history
Fix policy receipt data stored in local storage
  • Loading branch information
flashburst authored Dec 19, 2023
2 parents 69146e9 + 7382075 commit b5335c9
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 52 deletions.
4 changes: 2 additions & 2 deletions src/config/constants.js
Original file line number Diff line number Diff line change
Expand Up @@ -81,8 +81,8 @@ export const SUBGRAPH_API_URLS = {
42161: process.env.NEXT_PUBLIC_ARBITRUM_SUBGRAPH_URL,
// testnet
// 43113: process.env.NEXT_PUBLIC_FUJI_SUBGRAPH_URL,
80001: process.env.NEXT_PUBLIC_MUMBAI_SUBGRAPH_URL,
84531: process.env.NEXT_PUBLIC_BASE_GOERLI_SUBGRAPH_URL
80001: process.env.NEXT_PUBLIC_MUMBAI_SUBGRAPH_URL
// 84531: process.env.NEXT_PUBLIC_BASE_GOERLI_SUBGRAPH_URL
}

export const TEST_URL = process.env.NEXT_PUBLIC_TEST_URL || 'https://test.neptunemutual.net'
Expand Down
101 changes: 59 additions & 42 deletions src/hooks/useFetchCoverPurchasedEvent.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,46 +4,62 @@ import {
} from 'react'

import DateLib from '@/lib/date/DateLib'
import { getNetworkId } from '@/src/config/environment'
import { useNetwork } from '@/src/context/Network'
import { getPolicyReceipt } from '@/src/services/api/policy/receipt'
import { toBN } from '@/utils/bn'
import { Interface } from '@ethersproject/abi'
import sdk from '@neptunemutual/sdk'

export const storePurchaseEvent = (event, from) => {
const txHash = event.transactionHash

const {
args: { coverKey, productKey, onBehalfOf, amountToCover, referralCode },
cxToken,
fee,
platformFee,
expiresOn,
policyId
} = event.args

const value = {
event: {
id: txHash,
coverKey: coverKey.toString(),
productKey: productKey.toString(),
onBehalfOf: onBehalfOf.toString(),
cxToken: cxToken.toString(),
fee: fee.toString(),
platformFee: platformFee.toString(),
amountToCover: amountToCover.toString(),
expiresOn: expiresOn.toString(),
referralCode: referralCode.toString(),
policyId: policyId.toString(),
createdAtTimestamp: DateLib.unix(),

transaction: {
from
}
},
expiry: DateLib.toUnix(DateLib.addMinutes(new Date(), 5))
}
export const storePurchaseEvent = (receipt) => {
const iface = new Interface(sdk.config.abis.IPolicy)

for (let i = 0; i < receipt.logs.length; i++) {
const log = receipt.logs[i]

if (!log.topics.includes(iface.getEventTopic('CoverPurchased'))) {
continue
}

const parsed = iface.parseLog(log)

if (parsed.name !== 'CoverPurchased') {
return
}

localStorage.setItem(txHash, JSON.stringify(value))
// Should be same as backend
const data = {
id: receipt.transactionHash,
transactionHash: receipt.transactionHash,
address: receipt.to,
blockTimestamp: DateLib.unix(),
blockNumber: receipt.blockNumber.toString(),
transactionSender: receipt.from,
chainId: getNetworkId().toString(),
transactionStablecoinAmount: toBN(parsed.args.fee).minus(parsed.args.platformFee).toString(),
transactionNpmAmount: null,
gasPrice: receipt.effectiveGasPrice.toString(),
eventName: 'CoverPurchased',
couponCode: parsed.args.args.referralCode,
ck: parsed.args.args.coverKey,
pk: parsed.args.args.productKey,
onBehalfOf: parsed.args.args.onBehalfOf,
coverKey: parsed.args.args.coverKey,
productKey: parsed.args.args.productKey,
coverDuration: parsed.args.args.coverDuration.toString(),
amountToCover: parsed.args.args.amountToCover.toString(),
referralCode: parsed.args.args.referralCode,
cxToken: parsed.args.cxToken,
fee: parsed.args.fee.toString(),
platformFee: parsed.args.platformFee.toString(),
expiresOn: parsed.args.expiresOn.toString(),
policyId: parsed.args.policyId.toString()
}

localStorage.setItem(receipt.transactionHash, JSON.stringify(data))

return txHash
return
}
}

const getEventFromApi = async (networkId, txHash) => {
Expand All @@ -57,24 +73,25 @@ const getEventFromStorage = async (txHash) => {
const str = localStorage.getItem(txHash)
const data = JSON.parse(str)

if (data.expiry < DateLib.unix()) {
// Delete after 30 minutes
if (!data.blockTimestamp || data.blockTimestamp + 30 * 60 < DateLib.unix()) {
localStorage.removeItem(txHash)
}

return data.event
return data
} catch (error) {}

return null
}

const getEvent = async (networkId, txHash) => {
return getEventFromStorage(txHash).then((data) => {
if (!data) {
return getEventFromApi(networkId, txHash)
}
const data = getEventFromStorage(txHash)

if (data) {
return data
})
}

return getEventFromApi(networkId, txHash)
}

export const useFetchCoverPurchasedEvent = ({ txHash }) => {
Expand Down
11 changes: 3 additions & 8 deletions src/hooks/usePurchasePolicy.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -332,17 +332,12 @@ export const usePurchasePolicy = ({
})

tx.wait(1)
// Delay as subgraph takes time to index
// Delay as subgraph takes time to index
.then((receipt) => { return delay(receipt) })
.then(async (receipt) => {
if (receipt) {
const events = receipt.events
const event = events.find(
(x) => { return x.event === 'CoverPurchased' }
)
const txHash = storePurchaseEvent(event, receipt.from)

setTxHash(txHash)
storePurchaseEvent(receipt)
setTxHash(receipt.transactionHash)
}
})

Expand Down

0 comments on commit b5335c9

Please sign in to comment.