-
Notifications
You must be signed in to change notification settings - Fork 25
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
Display redemptions in my activity #570
Merged
Merged
Changes from 9 commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
b607067
Refactor `buildRedemptionDetailsLink`
r-czajkowski c7361a2
Update the `bridgeActivity` method in TBTC impl
r-czajkowski eff211e
Subscribe to the `RedemptionRequested` event
r-czajkowski 4f374ec
Fix bridge activities sorting
r-czajkowski fb4038f
Improve params validation link builder
r-czajkowski fd6839a
Rename param in `bridgeActivity` fn
r-czajkowski fecc080
Simplify fn that fetches the redemptions
r-czajkowski fe8b96e
Add utils fn
r-czajkowski 865587e
Add comment in `BridgeActivity` interface
r-czajkowski 434e558
Remove TODO
r-czajkowski File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
import { useWeb3React } from "@web3-react/core" | ||
import { useSubscribeToContractEvent } from "../../web3/hooks" | ||
import { isSameETHAddress } from "../../web3/utils" | ||
import { useAppDispatch } from "../store" | ||
import { useBridgeContract } from "./useBridgeContract" | ||
import { tbtcSlice } from "../../store/tbtc" | ||
import { BigNumber, Event } from "ethers" | ||
import { useThreshold } from "../../contexts/ThresholdContext" | ||
import { fromSatoshiToTokenPrecision } from "../../threshold-ts/utils" | ||
|
||
export const useSubscribeToRedemptionRequestedEvent = () => { | ||
const contract = useBridgeContract() | ||
const dispatch = useAppDispatch() | ||
const { account } = useWeb3React() | ||
const threshold = useThreshold() | ||
|
||
useSubscribeToContractEvent( | ||
contract, | ||
"RedemptionRequested", | ||
//@ts-ignore | ||
async ( | ||
walletPublicKeyHash: string, | ||
redeemerOutputScript: string, | ||
redeemer: string, | ||
requestedAmount: BigNumber, | ||
treasuryFee: BigNumber, | ||
txMaxFee: BigNumber, | ||
event: Event | ||
) => { | ||
if (!account || !isSameETHAddress(redeemer, account)) return | ||
|
||
const redemptionKey = threshold.tbtc.buildRedemptionKey( | ||
walletPublicKeyHash, | ||
redeemerOutputScript | ||
) | ||
|
||
dispatch( | ||
tbtcSlice.actions.redemptionRequested({ | ||
// TODO: Take into account fees, see | ||
// https://github.com/threshold-network/token-dashboard/pull/569 | ||
amount: fromSatoshiToTokenPrecision(requestedAmount).toString(), | ||
txHash: event.transactionHash, | ||
redemptionKey, | ||
blockNumber: event.blockNumber, | ||
additionalData: { redeemerOutputScript, walletPublicKeyHash }, | ||
}) | ||
) | ||
}, | ||
[null, null, account] | ||
) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,7 +2,11 @@ import { createSlice } from "@reduxjs/toolkit" | |
import { PayloadAction } from "@reduxjs/toolkit/dist/createAction" | ||
import { MintingStep, TbtcStateKey, TbtcState } from "../../types/tbtc" | ||
import { UpdateStateActionPayload } from "../../types/state" | ||
import { BridgeActivityStatus, BridgeActivity } from "../../threshold-ts/tbtc" | ||
import { | ||
BridgeActivityStatus, | ||
BridgeActivity, | ||
UnminBridgeActivityAdditionalData, | ||
} from "../../threshold-ts/tbtc" | ||
import { featureFlags } from "../../constants" | ||
import { startAppListening } from "../listener" | ||
import { | ||
|
@@ -54,9 +58,10 @@ export const tbtcSlice = createSlice({ | |
amount: string | ||
depositor: string | ||
txHash: string | ||
blockNumber: number | ||
}> | ||
) => { | ||
const { amount, txHash, depositKey } = action.payload | ||
const { amount, txHash, depositKey, blockNumber } = action.payload | ||
const history = state.bridgeActivity.data | ||
const { itemToUpdate } = findActivityByDepositKey(history, depositKey) | ||
|
||
|
@@ -66,7 +71,14 @@ export const tbtcSlice = createSlice({ | |
|
||
// Add item only if there is no item with the same deposit key. | ||
state.bridgeActivity.data = [ | ||
{ amount, txHash, status: BridgeActivityStatus.PENDING, depositKey }, | ||
{ | ||
amount, | ||
txHash, | ||
status: BridgeActivityStatus.PENDING, | ||
activityKey: depositKey, | ||
bridgeProcess: "mint", | ||
blockNumber, | ||
}, | ||
...state.bridgeActivity.data, | ||
] | ||
}, | ||
|
@@ -108,6 +120,45 @@ export const tbtcSlice = createSlice({ | |
} | ||
}> | ||
) => {}, | ||
redemptionRequested: ( | ||
state, | ||
action: PayloadAction<{ | ||
redemptionKey: string | ||
blockNumber: number | ||
amount: string | ||
txHash: string | ||
additionalData: UnminBridgeActivityAdditionalData | ||
}> | ||
) => { | ||
const { | ||
payload: { amount, redemptionKey, blockNumber, txHash, additionalData }, | ||
} = action | ||
|
||
const { itemToUpdate } = findRedemptionActivity( | ||
state.bridgeActivity.data, | ||
redemptionKey, | ||
txHash | ||
) | ||
|
||
// Do not update an array if there is already an item with the same | ||
// redemption key and transaction hash- just in case duplicated Ethereum | ||
// events. | ||
if (itemToUpdate) return | ||
|
||
// Add item only if there is no item with the same deposit key. | ||
state.bridgeActivity.data = [ | ||
{ | ||
amount, | ||
txHash, | ||
status: BridgeActivityStatus.PENDING, | ||
activityKey: redemptionKey, | ||
bridgeProcess: "unmint", | ||
blockNumber, | ||
additionalData, | ||
}, | ||
...state.bridgeActivity.data, | ||
] | ||
}, | ||
}, | ||
}) | ||
|
||
|
@@ -116,7 +167,26 @@ function findActivityByDepositKey( | |
depositKey: string | ||
) { | ||
const activityIndexItemToUpdate = bridgeActivities.findIndex( | ||
(item) => item.depositKey === depositKey | ||
(item) => item.activityKey === depositKey | ||
) | ||
|
||
if (activityIndexItemToUpdate < 0) return { index: -1, itemToUpdate: null } | ||
|
||
const activityItemToUpdate = bridgeActivities[activityIndexItemToUpdate] | ||
|
||
return { | ||
index: activityIndexItemToUpdate, | ||
itemToUpdate: activityItemToUpdate, | ||
} | ||
} | ||
|
||
function findRedemptionActivity( | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If you agree with #570 (comment) then we might also rename this one to |
||
bridgeActivities: BridgeActivity[], | ||
redemptionKey: string, | ||
txHash: string | ||
) { | ||
const activityIndexItemToUpdate = bridgeActivities.findIndex( | ||
(item) => item.activityKey === redemptionKey && item.txHash === txHash | ||
) | ||
|
||
if (activityIndexItemToUpdate < 0) return { index: -1, itemToUpdate: null } | ||
|
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Let's not forget about this TODO
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
434e558