-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #271 from lidofinance/feat/csm-motion
feat: add `CSMSettleElStealingPenalty` motion
- Loading branch information
Showing
15 changed files
with
2,399 additions
and
0 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
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 @@ | ||
[ | ||
{ | ||
"inputs": [ | ||
{ | ||
"internalType": "address", | ||
"name": "_trustedCaller", | ||
"type": "address" | ||
}, | ||
{ "internalType": "address", "name": "_csm", "type": "address" } | ||
], | ||
"stateMutability": "nonpayable", | ||
"type": "constructor" | ||
}, | ||
{ | ||
"inputs": [ | ||
{ "internalType": "address", "name": "_creator", "type": "address" }, | ||
{ "internalType": "bytes", "name": "_evmScriptCallData", "type": "bytes" } | ||
], | ||
"name": "createEVMScript", | ||
"outputs": [{ "internalType": "bytes", "name": "", "type": "bytes" }], | ||
"stateMutability": "view", | ||
"type": "function" | ||
}, | ||
{ | ||
"inputs": [], | ||
"name": "csm", | ||
"outputs": [ | ||
{ "internalType": "contract ICSModule", "name": "", "type": "address" } | ||
], | ||
"stateMutability": "view", | ||
"type": "function" | ||
}, | ||
{ | ||
"inputs": [ | ||
{ "internalType": "bytes", "name": "_evmScriptCallData", "type": "bytes" } | ||
], | ||
"name": "decodeEVMScriptCallData", | ||
"outputs": [ | ||
{ "internalType": "uint256[]", "name": "", "type": "uint256[]" } | ||
], | ||
"stateMutability": "pure", | ||
"type": "function" | ||
}, | ||
{ | ||
"inputs": [], | ||
"name": "trustedCaller", | ||
"outputs": [{ "internalType": "address", "name": "", "type": "address" }], | ||
"stateMutability": "view", | ||
"type": "function" | ||
} | ||
] |
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,21 @@ | ||
import { useLidoSWR } from '@lido-sdk/react' | ||
import { ContractCSMRegistry } from 'modules/blockChain/contracts' | ||
import { useWeb3 } from 'modules/blockChain/hooks/useWeb3' | ||
|
||
export const useCSMNodeOperatorsCount = () => { | ||
const { chainId } = useWeb3() | ||
const registry = ContractCSMRegistry.useRpc() | ||
|
||
return useLidoSWR( | ||
[`swr:useCSMNodeOperatorsCount`, chainId], | ||
async () => { | ||
const count = (await registry.getNodeOperatorsCount()).toNumber() | ||
|
||
return count | ||
}, | ||
{ | ||
revalidateOnFocus: false, | ||
revalidateOnReconnect: false, | ||
}, | ||
) | ||
} |
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
20 changes: 20 additions & 0 deletions
20
modules/motions/ui/MotionDescription/DescCSMSettleElStealingPenalty.tsx
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,20 @@ | ||
import { CSMSettleElStealingPenaltyAbi } from 'generated' | ||
import { pluralize } from 'modules/shared/utils/pluralize' | ||
import { NestProps } from './types' | ||
|
||
// CSMSettleElStealingPenalty | ||
export function DescCSMSettleElStealingPenalty({ | ||
callData, | ||
}: NestProps<CSMSettleElStealingPenaltyAbi['decodeEVMScriptCallData']>) { | ||
return ( | ||
<> | ||
Settle (confirm) EL Rewards Stealing penalty for the following CSM{' '} | ||
{pluralize(callData.length, 'operator')}: | ||
{callData.map((item, index) => { | ||
const nodeOperatorId = item.toNumber() | ||
|
||
return <div key={index}>NO #{nodeOperatorId}</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
170 changes: 170 additions & 0 deletions
170
modules/motions/ui/MotionFormStartNew/Parts/StartNewCSMSettleElStealingPenalty.tsx
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,170 @@ | ||
import { utils } from 'ethers' | ||
|
||
import { Fragment } from 'react' | ||
import { useFieldArray, useFormContext } from 'react-hook-form' | ||
import { Plus, ButtonIcon } from '@lidofinance/lido-ui' | ||
import { useWeb3 } from 'modules/blockChain/hooks/useWeb3' | ||
|
||
import { PageLoader } from 'modules/shared/ui/Common/PageLoader' | ||
import { | ||
Fieldset, | ||
MessageBox, | ||
RemoveItemButton, | ||
FieldsWrapper, | ||
FieldsHeader, | ||
FieldsHeaderDesc, | ||
} from '../CreateMotionFormStyle' | ||
|
||
import { ContractCSMSettleElStealingPenalty } from 'modules/blockChain/contracts' | ||
import { MotionType } from 'modules/motions/types' | ||
import { createMotionFormPart } from './createMotionFormPart' | ||
import { estimateGasFallback } from 'modules/motions/utils' | ||
import { useCSMNodeOperatorsCount } from 'modules/motions/hooks/useCSMNodeOperatorsCount' | ||
import { InputNumberControl } from 'modules/shared/ui/Controls/InputNumber' | ||
import { validateUintValue } from 'modules/motions/utils/validateUintValue' | ||
|
||
type NodeOperator = { | ||
id: string | ||
} | ||
|
||
export const formParts = createMotionFormPart({ | ||
motionType: MotionType.CSMSettleElStealingPenalty, | ||
populateTx: async ({ evmScriptFactory, formData, contract }) => { | ||
const sortedNodeOperators = formData.nodeOperators | ||
.map(({ id }) => Number(id)) | ||
.sort((a, b) => a - b) | ||
|
||
const encodedCallData = new utils.AbiCoder().encode( | ||
['uint256[]'], | ||
[sortedNodeOperators], | ||
) | ||
const gasLimit = await estimateGasFallback( | ||
contract.estimateGas.createMotion(evmScriptFactory, encodedCallData), | ||
) | ||
const tx = await contract.populateTransaction.createMotion( | ||
evmScriptFactory, | ||
encodedCallData, | ||
{ gasLimit }, | ||
) | ||
return tx | ||
}, | ||
getDefaultFormData: () => ({ | ||
nodeOperators: [ | ||
{ | ||
id: '', | ||
}, | ||
] as NodeOperator[], | ||
}), | ||
Component: ({ fieldNames, submitAction }) => { | ||
const { walletAddress } = useWeb3() | ||
|
||
const { | ||
data: nodeOperatorsCount, | ||
initialLoading: isNodeOperatorsCountLoading, | ||
} = useCSMNodeOperatorsCount() | ||
|
||
const trustedCaller = ContractCSMSettleElStealingPenalty.useSwrWeb3( | ||
'trustedCaller', | ||
[], | ||
) | ||
|
||
const fieldsArr = useFieldArray({ name: fieldNames.nodeOperators }) | ||
const { watch } = useFormContext() | ||
const selectedNodeOperators: NodeOperator[] = watch( | ||
fieldNames.nodeOperators, | ||
) | ||
|
||
const handleAddSettle = () => | ||
fieldsArr.append({ | ||
id: '', | ||
} as NodeOperator) | ||
|
||
if (trustedCaller.initialLoading || isNodeOperatorsCountLoading) { | ||
return <PageLoader /> | ||
} | ||
|
||
if (trustedCaller.data !== walletAddress) { | ||
return <MessageBox>You should be connected as trusted caller</MessageBox> | ||
} | ||
|
||
if (!nodeOperatorsCount) { | ||
return <MessageBox>There are no node operators</MessageBox> | ||
} | ||
|
||
return ( | ||
<> | ||
{fieldsArr.fields.map((item, fieldIndex) => { | ||
return ( | ||
<Fragment key={item.id}> | ||
<FieldsWrapper> | ||
<FieldsHeader> | ||
{fieldsArr.fields.length > 1 && ( | ||
<FieldsHeaderDesc> | ||
Settle #{fieldIndex + 1} | ||
</FieldsHeaderDesc> | ||
)} | ||
{fieldsArr.fields.length > 1 && ( | ||
<RemoveItemButton | ||
onClick={() => fieldsArr.remove(fieldIndex)} | ||
> | ||
Remove settle {fieldIndex + 1} | ||
</RemoveItemButton> | ||
)} | ||
</FieldsHeader> | ||
|
||
<Fieldset> | ||
<InputNumberControl | ||
name={`${fieldNames.nodeOperators}.${fieldIndex}.id`} | ||
label="Node operator ID" | ||
rules={{ | ||
required: 'Field is required', | ||
validate: value => { | ||
const uintError = validateUintValue(value) | ||
if (uintError) { | ||
return uintError | ||
} | ||
const valueNum = Number(value) | ||
|
||
if (valueNum >= nodeOperatorsCount) { | ||
return 'Invalid node operator ID' | ||
} | ||
|
||
const isAlreadyInInput = selectedNodeOperators.some( | ||
({ id }, index) => | ||
id === value && index !== fieldIndex, | ||
) | ||
|
||
if (isAlreadyInInput) { | ||
return 'This ID is already in the list' | ||
} | ||
|
||
return true | ||
}, | ||
}} | ||
/> | ||
</Fieldset> | ||
</FieldsWrapper> | ||
</Fragment> | ||
) | ||
})} | ||
|
||
{selectedNodeOperators.length < nodeOperatorsCount && ( | ||
<Fieldset> | ||
<ButtonIcon | ||
type="button" | ||
variant="ghost" | ||
size="sm" | ||
onClick={handleAddSettle} | ||
icon={<Plus />} | ||
color="secondary" | ||
> | ||
One more settle | ||
</ButtonIcon> | ||
</Fieldset> | ||
)} | ||
|
||
{submitAction} | ||
</> | ||
) | ||
}, | ||
}) |
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,2 @@ | ||
export const pluralize = (count: number, word: string) => | ||
count === 1 ? word : `${word}s` |