Skip to content

Commit

Permalink
test
Browse files Browse the repository at this point in the history
  • Loading branch information
tienkane committed Nov 25, 2024
1 parent 815e686 commit b7598c0
Show file tree
Hide file tree
Showing 2 changed files with 127 additions and 106 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@
"@reach/dialog": "^0.17.0",
"@reach/portal": "^0.17.0",
"@reduxjs/toolkit": "1.9.3",
"@safe-global/safe-apps-sdk": "^9.1.0",
"@sentry/react": "^8.34.0",
"@tanstack/react-query": "^5.52.1",
"@use-gesture/react": "^10.2.27",
Expand Down
232 changes: 126 additions & 106 deletions src/state/transactions/updater.tsx
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
import { BigNumber } from '@ethersproject/bignumber'
import { ChainId } from '@kyberswap/ks-sdk-core'
import SafeAppsSDK from '@safe-global/safe-apps-sdk'
import { ethers } from 'ethers'
import { findReplacementTx } from 'find-replacement-tx'
import { useEffect, useMemo } from 'react'
import { useDispatch, useSelector } from 'react-redux'

import { NotificationType } from 'components/Announcement/type'
import { CONNECTION } from 'components/Web3Provider'
import { useActiveWeb3React, useWeb3React } from 'hooks'
import useMixpanel, { MIXPANEL_TYPE, NEED_CHECK_SUBGRAPH_TRANSACTION_TYPES } from 'hooks/useMixpanel'
import { useBlockNumber, useKyberSwapConfig, useTransactionNotify } from 'state/application/hooks'
Expand All @@ -16,6 +18,18 @@ import { findTx } from 'utils'
import { checkedTransaction, finalizeTransaction, modifyTransaction, removeTx, replaceTx } from './actions'
import { SerializableTransactionReceipt, TRANSACTION_TYPE, TransactionDetails } from './type'

type Opts = {
allowedDomains?: RegExp[]
debug?: boolean
}

const opts: Opts = {
allowedDomains: [/^app\.safe\.global$/],
debug: false,
}

const appsSdk = new SafeAppsSDK(opts)

function shouldCheck(
lastBlockNumber: number,
tx?: { addedTime: number; receipt?: SerializableTransactionReceipt; lastCheckedBlockNumber?: number },
Expand Down Expand Up @@ -72,8 +86,7 @@ export default function Updater(): null {

uniqueTransactions
.filter(hash => shouldCheck(lastBlockNumber, findTx(transactions, hash)))
.forEach((hash, index) => {
console.log(`index${index}`, hash)
.forEach(hash => {
// Check if tx was replaced
readProvider
.getTransaction(hash)
Expand Down Expand Up @@ -114,123 +127,130 @@ export default function Updater(): null {
}
})
.catch(console.warn)
readProvider
.getTransactionReceipt(hash)
.then(receipt => {
console.log('receipt', receipt)
if (!receipt) {
dispatch(checkedTransaction({ chainId, hash, blockNumber: lastBlockNumber }))
return
}

const transaction = findTx(transactions, receipt.transactionHash)
if (!transaction) return
dispatch(
finalizeTransaction({
chainId,
hash: receipt.transactionHash,
receipt: {
blockHash: receipt.blockHash,
status: receipt.status,
},
needCheckSubgraph: NEED_CHECK_SUBGRAPH_TRANSACTION_TYPES.includes(transaction.type),
}),
)

transactionNotify({
hash: receipt.transactionHash,
type: receipt.status === 1 ? NotificationType.SUCCESS : NotificationType.ERROR,
account: account ?? '',
})
if (receipt.status === 1) {
// Swapped (address sender, address srcToken, address dstToken, address dstReceiver, uint256 spentAmount, uint256 returnAmount)
const swapEventTopic = ethers.utils.id('Swapped(address,address,address,address,uint256,uint256)')
const swapLogs = receipt.logs.filter(log => log.topics[0] === swapEventTopic)
// take the last swap event
const lastSwapEvent = swapLogs.slice(-1)[0]

// decode the data
const swapInterface = new ethers.utils.Interface([
'event Swapped (address sender, address srcToken, address dstToken, address dstReceiver, uint256 spentAmount, uint256 returnAmount)',
])
const parsed = swapInterface.parseLog(lastSwapEvent)

if (
(transaction.extraInfo as any)?.tokenAmountOut &&
transaction.extraInfo?.arbitrary?.outputDecimals !== undefined
) {
const extraInfo = { ...transaction.extraInfo }
;(extraInfo as any).tokenAmountOut = ethers.utils.formatUnits(
parsed.args.returnAmount.toString(),
transaction.extraInfo?.arbitrary?.outputDecimals,
)
dispatch(
modifyTransaction({
chainId: transaction.chainId,
hash: transaction.hash,
extraInfo,
}),
)
if (connector?.id === CONNECTION.SAFE_CONNECTOR_ID) {
appsSdk.txs.getBySafeTxHash(hash).then(tx => {
console.log('tx', tx)
})
} else {
readProvider
.getTransactionReceipt(hash)
.then(receipt => {
if (!receipt) {
dispatch(checkedTransaction({ chainId, hash, blockNumber: lastBlockNumber }))
return
}

const arbitrary = transaction.extraInfo?.arbitrary
switch (transaction.type) {
case TRANSACTION_TYPE.SWAP: {
if (!arbitrary) return
if (account && arbitrary.isPermitSwap) {
dispatch(revokePermit({ chainId, address: arbitrary.inputAddress, account }))
}
mixpanelHandler(MIXPANEL_TYPE.SWAP_COMPLETED, {
arbitrary,
actual_gas: receipt.gasUsed || BigNumber.from(0),
gas_price: receipt.effectiveGasPrice || BigNumber.from(0),
tx_hash: receipt.transactionHash,
feeInfo: arbitrary.feeInfo,
})
break
const transaction = findTx(transactions, receipt.transactionHash)
if (!transaction) return
dispatch(
finalizeTransaction({
chainId,
hash: receipt.transactionHash,
receipt: {
blockHash: receipt.blockHash,
status: receipt.status,
},
needCheckSubgraph: NEED_CHECK_SUBGRAPH_TRANSACTION_TYPES.includes(transaction.type),
}),
)

transactionNotify({
hash: receipt.transactionHash,
type: receipt.status === 1 ? NotificationType.SUCCESS : NotificationType.ERROR,
account: account ?? '',
})
if (receipt.status === 1) {
// Swapped (address sender, address srcToken, address dstToken, address dstReceiver, uint256 spentAmount, uint256 returnAmount)
const swapEventTopic = ethers.utils.id('Swapped(address,address,address,address,uint256,uint256)')
const swapLogs = receipt.logs.filter(log => log.topics[0] === swapEventTopic)
// take the last swap event
const lastSwapEvent = swapLogs.slice(-1)[0]

// decode the data
const swapInterface = new ethers.utils.Interface([
'event Swapped (address sender, address srcToken, address dstToken, address dstReceiver, uint256 spentAmount, uint256 returnAmount)',
])
const parsed = swapInterface.parseLog(lastSwapEvent)

if (
(transaction.extraInfo as any)?.tokenAmountOut &&
transaction.extraInfo?.arbitrary?.outputDecimals !== undefined
) {
const extraInfo = { ...transaction.extraInfo }
;(extraInfo as any).tokenAmountOut = ethers.utils.formatUnits(
parsed.args.returnAmount.toString(),
transaction.extraInfo?.arbitrary?.outputDecimals,
)
dispatch(
modifyTransaction({
chainId: transaction.chainId,
hash: transaction.hash,
extraInfo,
}),
)
}
case TRANSACTION_TYPE.BRIDGE: {
if (arbitrary) {
mixpanelHandler(MIXPANEL_TYPE.BRIDGE_TRANSACTION_SUBMIT, {
...arbitrary,

const arbitrary = transaction.extraInfo?.arbitrary
switch (transaction.type) {
case TRANSACTION_TYPE.SWAP: {
if (!arbitrary) return
if (account && arbitrary.isPermitSwap) {
dispatch(revokePermit({ chainId, address: arbitrary.inputAddress, account }))
}
mixpanelHandler(MIXPANEL_TYPE.SWAP_COMPLETED, {
arbitrary,
actual_gas: receipt.gasUsed || BigNumber.from(0),
gas_price: receipt.effectiveGasPrice || BigNumber.from(0),
tx_hash: receipt.transactionHash,
feeInfo: arbitrary.feeInfo,
})
break
}
break
}
case TRANSACTION_TYPE.ELASTIC_COLLECT_FEE: {
if (arbitrary) {
mixpanelHandler(MIXPANEL_TYPE.ELASTIC_COLLECT_FEES_COMPLETED, arbitrary)
case TRANSACTION_TYPE.BRIDGE: {
if (arbitrary) {
mixpanelHandler(MIXPANEL_TYPE.BRIDGE_TRANSACTION_SUBMIT, {
...arbitrary,
tx_hash: receipt.transactionHash,
})
}
break
}
break
}
case TRANSACTION_TYPE.ELASTIC_INCREASE_LIQUIDITY: {
if (arbitrary) {
mixpanelHandler(MIXPANEL_TYPE.ELASTIC_INCREASE_LIQUIDITY_COMPLETED, {
...arbitrary,
tx_hash: receipt.transactionHash,
})
case TRANSACTION_TYPE.ELASTIC_COLLECT_FEE: {
if (arbitrary) {
mixpanelHandler(MIXPANEL_TYPE.ELASTIC_COLLECT_FEES_COMPLETED, arbitrary)
}
break
}
break
}
case TRANSACTION_TYPE.CANCEL_LIMIT_ORDER: {
if (arbitrary) {
mixpanelHandler(MIXPANEL_TYPE.LO_CANCEL_ORDER_SUBMITTED, {
...arbitrary,
tx_hash: receipt.transactionHash,
})
case TRANSACTION_TYPE.ELASTIC_INCREASE_LIQUIDITY: {
if (arbitrary) {
mixpanelHandler(MIXPANEL_TYPE.ELASTIC_INCREASE_LIQUIDITY_COMPLETED, {
...arbitrary,
tx_hash: receipt.transactionHash,
})
}
break
}
break
case TRANSACTION_TYPE.CANCEL_LIMIT_ORDER: {
if (arbitrary) {
mixpanelHandler(MIXPANEL_TYPE.LO_CANCEL_ORDER_SUBMITTED, {
...arbitrary,
tx_hash: receipt.transactionHash,
})
}
break
}
default:
break
}
default:
break
}
}
})
.catch((error: any) => {
console.error(`failed to check transaction hash: ${hash}`, error)
})
})
.catch((error: any) => {
console.error(`failed to check transaction hash: ${hash}`, error)
})
}
})

uniqueTransactions
.filter(hash => findTx(transactions, hash)?.extraInfo?.needCheckSubgraph)
.forEach(async (hash: string) => {
Expand Down

0 comments on commit b7598c0

Please sign in to comment.