-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
ALM manual signatures execution (#471)
- Loading branch information
1 parent
5327688
commit bbc68f9
Showing
22 changed files
with
576 additions
and
515 deletions.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -49,3 +49,6 @@ monitor/responses/* | |
monitor/cache/* | ||
!monitor/cache/.gitkeep | ||
!monitor/.gitkeep | ||
|
||
# Local Netlify folder | ||
.netlify |
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 @@ | ||
/* /index.html 200 |
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
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,110 @@ | ||
import React, { useState, useEffect } from 'react' | ||
import styled from 'styled-components' | ||
import { InjectedConnector } from '@web3-react/injected-connector' | ||
import { useWeb3React } from '@web3-react/core' | ||
import { INCORRECT_CHAIN_ERROR, VALIDATOR_CONFIRMATION_STATUS } from '../config/constants' | ||
import { useStateProvider } from '../state/StateProvider' | ||
import { signatureToVRS, packSignatures } from '../utils/signatures' | ||
|
||
const StyledButton = styled.button` | ||
color: var(--button-color); | ||
border-color: var(--font-color); | ||
margin-top: 10px; | ||
&:focus { | ||
outline: var(--button-color); | ||
} | ||
` | ||
|
||
interface ManualExecutionButtonParams { | ||
messageData: string | ||
setExecutionData: Function | ||
requiredSignatures: number | ||
} | ||
|
||
export const ManualExecutionButton = ({ | ||
messageData, | ||
setExecutionData, | ||
requiredSignatures | ||
}: ManualExecutionButtonParams) => { | ||
const { home, foreign, setError } = useStateProvider() | ||
const { library, activate, account, active } = useWeb3React() | ||
const [manualExecution, setManualExecution] = useState(false) | ||
const disabled = | ||
home.confirmations.filter(({ signature }) => signature && signature.startsWith('0x')).length < requiredSignatures | ||
|
||
useEffect( | ||
() => { | ||
if (!manualExecution || !foreign.chainId) return | ||
|
||
if (!active) { | ||
activate(new InjectedConnector({ supportedChainIds: [foreign.chainId] }), e => { | ||
if (e.message.includes('Unsupported chain id')) { | ||
setError(INCORRECT_CHAIN_ERROR) | ||
const { ethereum } = window as any | ||
|
||
// remove the error message after chain is correctly changed to the foreign one | ||
const listener = (chainId: string) => { | ||
if (parseInt(chainId.slice(2), 16) === foreign.chainId) { | ||
ethereum.removeListener('chainChanged', listener) | ||
setError((error: string) => (error === INCORRECT_CHAIN_ERROR ? '' : error)) | ||
} | ||
} | ||
ethereum.on('chainChanged', listener) | ||
} else { | ||
setError(e.message) | ||
} | ||
setManualExecution(false) | ||
}) | ||
return | ||
} | ||
|
||
if (!library || !foreign.bridgeContract || !home.confirmations) return | ||
|
||
const collectedSignatures = home.confirmations | ||
.map(confirmation => confirmation.signature!) | ||
.filter(signature => signature && signature.startsWith('0x')) | ||
const signatures = packSignatures(collectedSignatures.map(signatureToVRS)) | ||
const data = foreign.bridgeContract.methods.executeSignatures(messageData, signatures).encodeABI() | ||
setManualExecution(false) | ||
|
||
library.eth | ||
.sendTransaction({ | ||
from: account, | ||
to: foreign.bridgeAddress, | ||
data | ||
}) | ||
.on('transactionHash', (txHash: string) => | ||
setExecutionData({ | ||
status: VALIDATOR_CONFIRMATION_STATUS.PENDING, | ||
validator: account, | ||
txHash, | ||
timestamp: Math.floor(new Date().getTime() / 1000.0), | ||
executionResult: false | ||
}) | ||
) | ||
.on('error', (e: Error) => setError(e.message)) | ||
}, | ||
[ | ||
manualExecution, | ||
library, | ||
activate, | ||
active, | ||
account, | ||
foreign.chainId, | ||
foreign.bridgeAddress, | ||
foreign.bridgeContract, | ||
setError, | ||
messageData, | ||
home.confirmations, | ||
setExecutionData | ||
] | ||
) | ||
|
||
return ( | ||
<div className="is-center"> | ||
<StyledButton disabled={disabled} className="button outline" onClick={() => setManualExecution(true)}> | ||
Execute | ||
</StyledButton> | ||
</div> | ||
) | ||
} |
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,31 @@ | ||
import React from 'react' | ||
import styled from 'styled-components' | ||
import { InfoIcon } from './InfoIcon' | ||
import { CloseIcon } from './CloseIcon' | ||
|
||
const StyledErrorAlert = styled.div` | ||
border: 1px solid var(--failed-color); | ||
border-radius: 4px; | ||
margin-bottom: 20px; | ||
padding-top: 10px; | ||
` | ||
|
||
const CloseIconContainer = styled.div` | ||
cursor: pointer; | ||
` | ||
|
||
const TextContainer = styled.div` | ||
flex-direction: column; | ||
` | ||
|
||
export const ErrorAlert = ({ onClick, error }: { onClick: () => void; error: string }) => ( | ||
<div className="row is-center"> | ||
<StyledErrorAlert className="col-10 is-vertical-align row"> | ||
<InfoIcon color="var(--failed-color)" /> | ||
<TextContainer className="col-10">{error}</TextContainer> | ||
<CloseIconContainer className="col-1 is-vertical-align is-center" onClick={onClick}> | ||
<CloseIcon color="var(--failed-color)" /> | ||
</CloseIconContainer> | ||
</StyledErrorAlert> | ||
</div> | ||
) |
Oops, something went wrong.